fixed WebGL warning, add distance support

This commit is contained in:
gbrochar 2020-11-24 13:10:47 +01:00
parent 55cb25f291
commit 31ec733bda
5 changed files with 64 additions and 33 deletions

View File

@ -15,7 +15,7 @@ export function changeFragmentShader(gl: any,
fragmentShader: any, fragmentShader: any,
fsSource: string, fsSource: string,
vsSource: string) { vsSource: string) {
shaderProgram = unlinkShaderProgram(gl, [shaderProgram, fragmentShader] = unlinkShaderProgram(gl,
fragmentShader, fragmentShader,
shaderProgram); shaderProgram);
[shaderProgram, fragmentShader] = initShaderProgram(gl, [shaderProgram, fragmentShader] = initShaderProgram(gl,
@ -40,5 +40,5 @@ export function changeFragmentShader(gl: any,
shaderProgram, 'umodelMatrix'), shaderProgram, 'umodelMatrix'),
}, },
}; };
return programInfo; return [programInfo, fragmentShader];
} }

View File

@ -85,7 +85,11 @@ function main() {
0.), 10.); 0.), 10.);
vec3 tmp = extremize(mod(vPosition.xyz + vec3(100.), vec3(3.)), 3.); vec3 tmp = extremize(mod(vPosition.xyz + vec3(100.), vec3(3.)), 3.);
float texture = (tmp.x + tmp.y + tmp.z) / 9.; float texture = (tmp.x + tmp.y + tmp.z) / 9.;
gl_FragColor = vec4((texture * diffuse * 0.9) + (texture * 0.1) + (specular * vec3(1.)), 1.0); if (abs(vNormal.x) + abs(vNormal.y) + abs(vNormal.z) > 0.01) {
gl_FragColor = vec4((texture * diffuse * 0.9) + (texture * vec3(0.1)) + (specular * vec3(1.)), 1.0);
} else {
gl_FragColor = vec4((texture * vec3(1.)), 1.0);
}
}`; }`;
const fsSource2 = ` const fsSource2 = `
@ -121,7 +125,11 @@ function main() {
0.), 10.); 0.), 10.);
vec3 tmp = extremize(mod(vPosition.xyz + vec3(100.), vec3(3.)), 3.); vec3 tmp = extremize(mod(vPosition.xyz + vec3(100.), vec3(3.)), 3.);
vec3 texture = vec3(tmp.x / 3., tmp.y / 3., tmp.z / 3.); vec3 texture = vec3(tmp.x / 3., tmp.y / 3., tmp.z / 3.);
if (abs(vNormal.x) + abs(vNormal.y) + abs(vNormal.z) > 0.01) {
gl_FragColor = vec4((texture * diffuse * 0.9) + (texture * vec3(0.1)) + (specular * vec3(1.)), 1.0); gl_FragColor = vec4((texture * diffuse * 0.9) + (texture * vec3(0.1)) + (specular * vec3(1.)), 1.0);
} else {
gl_FragColor = vec4((texture * vec3(1.)), 1.0);
}
}`; }`;
/* eslint-enable */ /* eslint-enable */
@ -129,27 +137,16 @@ function main() {
fetch('/static/objs/teapot_normals.obj').then((response) => { fetch('/static/objs/teapot_normals.obj').then((response) => {
return response.text(); return response.text();
}).then((data: any) => { }).then((data: any) => {
let distance: any = $('#input1').val();
distance = parseFloat(distance);
const [ const [
convertedPositions, positions,
convertedNormals, normals,
uvs, uvs,
indices, indices,
] = convert(data); ] = convert(data);
console.log(uvs); console.log(uvs);
console.log(squareRotation); let [shaderProgram, fragmentShader]: any = initShaderProgram(gl,
const normals: any = [];
const positions: any = [];
for (let i = 0; i < convertedNormals.length; i++) {
if (i % 4 != 0) {
normals.push(convertedNormals[i]);
}
}
for (let i = 0; i < convertedPositions.length; i++) {
if (i % 4 != 0) {
positions.push(convertedPositions[i]);
}
}
const [shaderProgram, fragmentShader]: any = initShaderProgram(gl,
vsSource, vsSource,
fsSource); fsSource);
@ -188,19 +185,34 @@ function main() {
changed = true; changed = true;
} }
then = now; then = now;
drawScene(gl, programInfo, buffers, deltaTime, indices.length); drawScene(gl,
programInfo,
buffers,
deltaTime,
indices.length,
distance);
requestAnimationFrame(render); requestAnimationFrame(render);
} }
$(function() { $(function() {
$('#input1').on('keypress', function(event: any) {
if (event.which === 13) {
event.preventDefault();
$('#button3').click();
}
});
$('#button1').on('click', function() { $('#button1').on('click', function() {
programInfo = changeFragmentShader(gl, [programInfo, fragmentShader] = changeFragmentShader(gl,
shaderProgram, fragmentShader, fsSource, vsSource); shaderProgram, fragmentShader, fsSource, vsSource);
}); });
$('#button2').on('click', function() { $('#button2').on('click', function() {
programInfo = changeFragmentShader(gl, [programInfo, fragmentShader] = changeFragmentShader(gl,
shaderProgram, fragmentShader, fsSource2, vsSource); shaderProgram, fragmentShader, fsSource2, vsSource);
}); });
$('#button3').on('click', function() {
distance = $('#input1').val();
distance = parseFloat(distance);
});
}); });
requestAnimationFrame(render); requestAnimationFrame(render);
}); });
@ -214,12 +226,14 @@ function main() {
* @param {any} buffers the buffers to draw * @param {any} buffers the buffers to draw
* @param {number} deltaTime the difference in time since last call * @param {number} deltaTime the difference in time since last call
* @param {number} length the index buffer length * @param {number} length the index buffer length
* @param {number} distance distance of camera
*/ */
function drawScene(gl: any, function drawScene(gl: any,
programInfo: any, programInfo: any,
buffers: any, buffers: any,
deltaTime: number, deltaTime: number,
length: number) { length: number,
distance: number) {
gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0); gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST); gl.enable(gl.DEPTH_TEST);
@ -236,7 +250,7 @@ function drawScene(gl: any,
const fieldOfView = 45 * Math.PI / 180; const fieldOfView = 45 * Math.PI / 180;
const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight; const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
const zNear = 0.1; const zNear = 0.1;
const zFar = 70.0; const zFar = 150.0;
const projectionMatrix = mat4.create(); const projectionMatrix = mat4.create();
// note: glmatrix.js always has the first argument // note: glmatrix.js always has the first argument
@ -273,7 +287,7 @@ function drawScene(gl: any,
mat4.translate( mat4.translate(
viewMatrix, viewMatrix,
viewMatrix, viewMatrix,
[0.0, 0.0, -50.0]); [0.0, 0.0, -distance]);
// Tell WebGL how to pull out the positions from the position // Tell WebGL how to pull out the positions from the position
// buffer into the vertexPosition attribute. // buffer into the vertexPosition attribute.

View File

@ -14,7 +14,14 @@ export default function convert (objText) {
const line = lines[i]; const line = lines[i];
if (line != '') if (line != '')
{ {
const chunks = line.split(" ").map(x => x.trim()); const rawchunks = line.split(" ").map(x => x.trim());
const chunks = [];
for (let i = 0; i < rawchunks.length; i++) {
if (rawchunks[i] != '') {
chunks.push(rawchunks[i]);
}
}
switch (chunks[0]) { switch (chunks[0]) {
case "v": { case "v": {
@ -101,8 +108,12 @@ export default function convert (objText) {
const d = (vertices[i].startsWith("s") ? vertices[i].substr(1) : vertices[i]).split("/"); const d = (vertices[i].startsWith("s") ? vertices[i].substr(1) : vertices[i]).split("/");
outPositions.push(...positions[d[0] - 1]); outPositions.push(...positions[d[0] - 1]);
if (normals.length) {
outNormals.push(...normals[d[2] - 1]); outNormals.push(...normals[d[2] - 1]);
}
if (uvs.length) {
outUVs.push(...uvs[d[1] - 1]); outUVs.push(...uvs[d[1] - 1]);
} }
}
return [outPositions, outNormals, outUVs, indices]; return [outPositions, outNormals, outUVs, indices];
}; };

View File

@ -7,9 +7,8 @@
*/ */
export function unlinkShaderProgram(gl: any, shader: any, shaderProgram: any) { export function unlinkShaderProgram(gl: any, shader: any, shaderProgram: any) {
// Create the shader program // Create the shader program
gl.detachShader(shaderProgram, shader);
gl.deleteShader(shader); gl.deleteShader(shader);
return shaderProgram; return [shaderProgram, shader];
} }
/** /**

View File

@ -14,9 +14,16 @@
<div id="root"> <div id="root">
<canvas id='glCanvas' width='640' height='640'></canvas> <canvas id='glCanvas' width='640' height='640'></canvas>
<div id='ui'> <div id='ui'>
<div class='ui-block'>
<button id='button1'>Change Shader to black and white</button> <button id='button1'>Change Shader to black and white</button>
<button id='button2'>Change Shader to colored</button> <button id='button2'>Change Shader to colored</button>
</div> </div>
<div class='ui-block'>
<input id='input1' value='50'></input>
<button id='button3'>Change camera distance</button>
<div style='display: inline'>Max distance is 150</div>
</div>
</div>
</div> </div>
<script src="js/bundle.js"></script> <script src="js/bundle.js"></script>
</body> </body>