webgl不同图像不同纹理_在WebGl中创建键盘敏感的3D扭曲图像

2024-01-07 19:40

本文主要是介绍webgl不同图像不同纹理_在WebGl中创建键盘敏感的3D扭曲图像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

webgl不同图像不同纹理

Creating a Keyboard Sensitive 3D Twisted Images in WebGl
Creating a Keyboard Sensitive 3D Twisted Images in WebGl

Keyboard Sensitive 3D Twisted Images with WebGL. Today we continue HTML5 canvas examples. And today is our second tutorial for WebGL. We will creating animated twisting images. Also we will add handlers to manipulate with mouse and keyboard.

使用WebGL的键盘敏感3D扭曲图像。 今天,我们继续HTML5 canvas示例。 今天是我们的第二本WebGL教程。 我们将创建动画扭曲图像。 另外,我们将添加处理程序以使用鼠标和键盘进行操作。

Here are our demo and downloadable package:

这是我们的演示和可下载的软件包:

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Ok, download the example files and lets start coding !

好的,下载示例文件并开始编码!

步骤1. HTML (Step 1. HTML)

Here are html sources of our demo. As you can see – just empty page.

这是我们的演示的html来源。 如您所见–只是空白页。

index.html (index.html)

<!DOCTYPE html>
<html lang="en" >
<head><link href="css/main.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="js/glMatrix-0.9.5.min.js"></script><script type="text/javascript" src="js/webgl-utils.js"></script><script type="text/javascript" src="js/script.js"></script><title>Creating a Keyboard Sensitive 3D Twisted Images in WebGl | Script Tutorials</title>
</head>
<body onload="initWebGl();"><div class="example"><h3><a href="https://www.script-tutorials.com/twisting-images-webgl/">Creating a Keyboard Sensitive 3D Twisted Images in WebGl | Script Tutorials</a></h3><h3>You can use your mouse + arrow keys + page up / down</h3><canvas id="panel" width="500" height="333"></canvas><div style="clear:both;"></div></div>
</body>
</html>

<!DOCTYPE html>
<html lang="en" >
<head><link href="css/main.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="js/glMatrix-0.9.5.min.js"></script><script type="text/javascript" src="js/webgl-utils.js"></script><script type="text/javascript" src="js/script.js"></script><title>Creating a Keyboard Sensitive 3D Twisted Images in WebGl | Script Tutorials</title>
</head>
<body onload="initWebGl();"><div class="example"><h3><a href="https://www.script-tutorials.com/twisting-images-webgl/">Creating a Keyboard Sensitive 3D Twisted Images in WebGl | Script Tutorials</a></h3><h3>You can use your mouse + arrow keys + page up / down</h3><canvas id="panel" width="500" height="333"></canvas><div style="clear:both;"></div></div>
</body>
</html>

步骤2. CSS (Step 2. CSS)

Here are used CSS styles.

这是使用CSS样式。

css / main.css (css/main.css)

body {background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0
}
.example {background:#fff;width:500px;font-size:80%;border:1px #000 solid;margin:20px auto;padding:15px;position:relative;-moz-border-radius: 3px;-webkit-border-radius:3px
}
h3 {text-align:center;
}

body {background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0
}
.example {background:#fff;width:500px;font-size:80%;border:1px #000 solid;margin:20px auto;padding:15px;position:relative;-moz-border-radius: 3px;-webkit-border-radius:3px
}
h3 {text-align:center;
}

步骤3. JS (Step 3. JS)

js / webgl-utils.js和js / glMatrix-0.9.5.min.js (js/webgl-utils.js and js/glMatrix-0.9.5.min.js)

These files we will use in project for working with WebGL. Both files will in our package.

我们将在项目中使用这些文件来使用WebGL。 这两个文件都将放在我们的程序包中。

js / script.js (js/script.js)

var gl; // global WebGL object
var shaderProgram;
var pics_names=['1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png'];
var pics_num=pics_names.length;
// diffirent initializations
function initGL(canvas) {try {gl = canvas.getContext('experimental-webgl');gl.viewportWidth = canvas.width;gl.viewportHeight = canvas.height;} catch (e) {}if (! gl) {alert('Can`t initialise WebGL, not supported');}
}
function getShader(gl, type) {var str = '';var shader;if (type == 'x-fragment') {str = "#ifdef GL_ES\n"+
"precision highp float;\n"+
"#endif\n"+
"varying vec2 vTextureCoord;\n"+
"uniform sampler2D uSampler;\n"+
"void main(void) {\n"+
"    gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));\n"+
"}\n";shader = gl.createShader(gl.FRAGMENT_SHADER);} else if (type == 'x-vertex') {str = "attribute vec3 aVertexPosition;\n"+
"attribute vec2 aTextureCoord;\n"+
"uniform mat4 uMVMatrix;\n"+
"uniform mat4 uPMatrix;\n"+
"varying vec2 vTextureCoord;\n"+
"void main(void) {\n"+
"    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);\n"+
"    vTextureCoord = aTextureCoord;\n"+
"}\n";shader = gl.createShader(gl.VERTEX_SHADER);} else {return null;}gl.shaderSource(shader, str);gl.compileShader(shader);if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {alert(gl.getShaderInfoLog(shader));return null;}return shader;
}
function initShaders() {var fragmentShader = getShader(gl, 'x-fragment');var vertexShader = getShader(gl, 'x-vertex');shaderProgram = gl.createProgram();gl.attachShader(shaderProgram, vertexShader);gl.attachShader(shaderProgram, fragmentShader);gl.linkProgram(shaderProgram);if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {alert('Can`t initialise shaders');}gl.useProgram(shaderProgram);shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, 'aVertexPosition');gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, 'aTextureCoord');gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, 'uPMatrix');shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, 'uMVMatrix');shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, 'uSampler');
}
var objVertexPositionBuffer=new Array();
var objVertexTextureCoordBuffer=new Array();
var objVertexIndexBuffer=new Array();
function initObjBuffers() {for (var i=0;i<pics_num;i=i+1) {objVertexPositionBuffer[i] = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER, objVertexPositionBuffer[i]);vertices = [Math.cos(i*((2*Math.PI)/pics_num)), -0.5,  Math.sin(i*((2*Math.PI)/pics_num)),Math.cos(i*((2*Math.PI)/pics_num)), 0.5,  Math.sin(i*((2*Math.PI)/pics_num)),Math.cos((i+1)*((2*Math.PI)/pics_num)), 0.5, Math.sin((i+1)*((2*Math.PI)/pics_num)),Math.cos((i+1)*((2*Math.PI)/pics_num)), -0.5,  Math.sin((i+1)*((2*Math.PI)/pics_num)),];gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);objVertexPositionBuffer[i].itemSize = 3;objVertexPositionBuffer[i].numItems = 4;objVertexTextureCoordBuffer[i] = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,  objVertexTextureCoordBuffer[i] );var textureCoords = [0.0, 0.0,0.0, 1.0,1.0, 1.0,1.0, 0.0,];gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);objVertexTextureCoordBuffer[i].itemSize = 2;objVertexTextureCoordBuffer[i].numItems = 4;objVertexIndexBuffer[i] = gl.createBuffer();gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, objVertexIndexBuffer[i]);var objVertexIndices = [0, 1, 2,0, 2, 3,];gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(objVertexIndices), gl.STATIC_DRAW);objVertexIndexBuffer[i].itemSize = 1;objVertexIndexBuffer[i].numItems = 6;}
}
function handleLoadedTexture(texture) {gl.bindTexture(gl.TEXTURE_2D, texture);gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);gl.bindTexture(gl.TEXTURE_2D, null);
}
var crateTextures = Array();
function initTexture(image) {var crateImage = new Image();var texture = gl.createTexture();texture.image = crateImage;crateImage.src = image;crateImage.onload = function () {handleLoadedTexture(texture)}return texture;
}
function initTextures() {for (var i=0; i < pics_num; i++) {crateTextures[i]=initTexture(pics_names[i]);}
}
var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();
function setMatrixUniforms() {gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
function degToRad(degrees) {return degrees * Math.PI / 180;
}
// mouse and keyboard handlers
var xRot = 0;
var xSpeed = 0;
var yRot = 0;
var ySpeed = 10;
var z = -3.0;
var currentlyPressedKeys = {};
function handleKeyDown(event) {currentlyPressedKeys[event.keyCode] = true;
}
function handleKeyUp(event) {currentlyPressedKeys[event.keyCode] = false;
}
function handleKeys() {if (currentlyPressedKeys[33]) { // Page Upz -= 0.05;}if (currentlyPressedKeys[34]) { // Page Downz += 0.05;}if (currentlyPressedKeys[37]) { // Left cursor keyySpeed -= 1;}if (currentlyPressedKeys[39]) { // Right cursor keyySpeed += 1;}if (currentlyPressedKeys[38]) { // Up cursor keyxSpeed -= 1;}if (currentlyPressedKeys[40]) { // Down cursor keyxSpeed += 1;}
}
var mouseDown = false;
var lastMouseX = null;
var lastMouseY = null;
var RotationMatrix = mat4.create();
mat4.identity(RotationMatrix);
function handleMouseDown(event) {mouseDown = true;lastMouseX = event.clientX;lastMouseY = event.clientY;
}
function handleMouseUp(event) {mouseDown = false;
}
function handleMouseMove(event) {if (!mouseDown) {return;}var newX = event.clientX;var newY = event.clientY;var deltaX = newX - lastMouseX;var newRotationMatrix = mat4.create();mat4.identity(newRotationMatrix);mat4.rotate(newRotationMatrix, degToRad(deltaX / 5), [0, 1, 0]);var deltaY = newY - lastMouseY;mat4.rotate(newRotationMatrix, degToRad(deltaY / 5), [1, 0, 0]);mat4.multiply(newRotationMatrix, RotationMatrix, RotationMatrix);lastMouseX = newXlastMouseY = newY;
}
// Draw scene and initialization
var MoveMatrix = mat4.create();
mat4.identity(MoveMatrix);
function drawScene() {gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);mat4.identity(mvMatrix);mat4.translate(mvMatrix, [0.0, 0.0, z]);mat4.rotate(mvMatrix, degToRad(xRot), [1, 0, 0]);mat4.rotate(mvMatrix, degToRad(yRot), [0, 1, 0]);mat4.multiply(mvMatrix, MoveMatrix);mat4.multiply(mvMatrix, RotationMatrix);for (var i=0;i<pics_num;i=i+1) {gl.bindBuffer(gl.ARRAY_BUFFER, objVertexPositionBuffer[i]);gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, objVertexPositionBuffer[i].itemSize, gl.FLOAT, false, 0, 0);gl.bindBuffer(gl.ARRAY_BUFFER, objVertexTextureCoordBuffer[i]);gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, objVertexTextureCoordBuffer[i].itemSize, gl.FLOAT, false, 0, 0);gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D, crateTextures[i]);gl.uniform1i(shaderProgram.samplerUniform, 0);gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, objVertexIndexBuffer[i]);setMatrixUniforms();gl.drawElements(gl.TRIANGLES, objVertexIndexBuffer[i].numItems, gl.UNSIGNED_SHORT, 0);}
}
var lastTime = 0;
function animate() {var timeNow = new Date().getTime();if (lastTime != 0) {var elapsed = timeNow - lastTime;xRot += (xSpeed * elapsed) / 1000.0;yRot += (ySpeed * elapsed) / 1000.0;}lastTime = timeNow;
}
function drawFrame() {requestAnimFrame(drawFrame);handleKeys();drawScene();animate();
}
function initWebGl() {var canvas = document.getElementById('panel');initGL(canvas);initShaders();initObjBuffers();initTextures();gl.clearColor(1.0, 1.0, 1.0, 1.0);gl.enable(gl.DEPTH_TEST);document.onkeydown = handleKeyDown;document.onkeyup = handleKeyUp;canvas.onmousedown = handleMouseDown;document.onmouseup = handleMouseUp;document.onmousemove = handleMouseMove;drawFrame();
}

var gl; // global WebGL object
var shaderProgram;
var pics_names=['1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png'];
var pics_num=pics_names.length;
// diffirent initializations
function initGL(canvas) {try {gl = canvas.getContext('experimental-webgl');gl.viewportWidth = canvas.width;gl.viewportHeight = canvas.height;} catch (e) {}if (! gl) {alert('Can`t initialise WebGL, not supported');}
}
function getShader(gl, type) {var str = '';var shader;if (type == 'x-fragment') {str = "#ifdef GL_ES\n"+
"precision highp float;\n"+
"#endif\n"+
"varying vec2 vTextureCoord;\n"+
"uniform sampler2D uSampler;\n"+
"void main(void) {\n"+
"    gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));\n"+
"}\n";shader = gl.createShader(gl.FRAGMENT_SHADER);} else if (type == 'x-vertex') {str = "attribute vec3 aVertexPosition;\n"+
"attribute vec2 aTextureCoord;\n"+
"uniform mat4 uMVMatrix;\n"+
"uniform mat4 uPMatrix;\n"+
"varying vec2 vTextureCoord;\n"+
"void main(void) {\n"+
"    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);\n"+
"    vTextureCoord = aTextureCoord;\n"+
"}\n";shader = gl.createShader(gl.VERTEX_SHADER);} else {return null;}gl.shaderSource(shader, str);gl.compileShader(shader);if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {alert(gl.getShaderInfoLog(shader));return null;}return shader;
}
function initShaders() {var fragmentShader = getShader(gl, 'x-fragment');var vertexShader = getShader(gl, 'x-vertex');shaderProgram = gl.createProgram();gl.attachShader(shaderProgram, vertexShader);gl.attachShader(shaderProgram, fragmentShader);gl.linkProgram(shaderProgram);if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {alert('Can`t initialise shaders');}gl.useProgram(shaderProgram);shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, 'aVertexPosition');gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, 'aTextureCoord');gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, 'uPMatrix');shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, 'uMVMatrix');shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, 'uSampler');
}
var objVertexPositionBuffer=new Array();
var objVertexTextureCoordBuffer=new Array();
var objVertexIndexBuffer=new Array();
function initObjBuffers() {for (var i=0;i<pics_num;i=i+1) {objVertexPositionBuffer[i] = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER, objVertexPositionBuffer[i]);vertices = [Math.cos(i*((2*Math.PI)/pics_num)), -0.5,  Math.sin(i*((2*Math.PI)/pics_num)),Math.cos(i*((2*Math.PI)/pics_num)), 0.5,  Math.sin(i*((2*Math.PI)/pics_num)),Math.cos((i+1)*((2*Math.PI)/pics_num)), 0.5, Math.sin((i+1)*((2*Math.PI)/pics_num)),Math.cos((i+1)*((2*Math.PI)/pics_num)), -0.5,  Math.sin((i+1)*((2*Math.PI)/pics_num)),];gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);objVertexPositionBuffer[i].itemSize = 3;objVertexPositionBuffer[i].numItems = 4;objVertexTextureCoordBuffer[i] = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,  objVertexTextureCoordBuffer[i] );var textureCoords = [0.0, 0.0,0.0, 1.0,1.0, 1.0,1.0, 0.0,];gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);objVertexTextureCoordBuffer[i].itemSize = 2;objVertexTextureCoordBuffer[i].numItems = 4;objVertexIndexBuffer[i] = gl.createBuffer();gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, objVertexIndexBuffer[i]);var objVertexIndices = [0, 1, 2,0, 2, 3,];gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(objVertexIndices), gl.STATIC_DRAW);objVertexIndexBuffer[i].itemSize = 1;objVertexIndexBuffer[i].numItems = 6;}
}
function handleLoadedTexture(texture) {gl.bindTexture(gl.TEXTURE_2D, texture);gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);gl.bindTexture(gl.TEXTURE_2D, null);
}
var crateTextures = Array();
function initTexture(image) {var crateImage = new Image();var texture = gl.createTexture();texture.image = crateImage;crateImage.src = image;crateImage.onload = function () {handleLoadedTexture(texture)}return texture;
}
function initTextures() {for (var i=0; i < pics_num; i++) {crateTextures[i]=initTexture(pics_names[i]);}
}
var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();
function setMatrixUniforms() {gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
function degToRad(degrees) {return degrees * Math.PI / 180;
}
// mouse and keyboard handlers
var xRot = 0;
var xSpeed = 0;
var yRot = 0;
var ySpeed = 10;
var z = -3.0;
var currentlyPressedKeys = {};
function handleKeyDown(event) {currentlyPressedKeys[event.keyCode] = true;
}
function handleKeyUp(event) {currentlyPressedKeys[event.keyCode] = false;
}
function handleKeys() {if (currentlyPressedKeys[33]) { // Page Upz -= 0.05;}if (currentlyPressedKeys[34]) { // Page Downz += 0.05;}if (currentlyPressedKeys[37]) { // Left cursor keyySpeed -= 1;}if (currentlyPressedKeys[39]) { // Right cursor keyySpeed += 1;}if (currentlyPressedKeys[38]) { // Up cursor keyxSpeed -= 1;}if (currentlyPressedKeys[40]) { // Down cursor keyxSpeed += 1;}
}
var mouseDown = false;
var lastMouseX = null;
var lastMouseY = null;
var RotationMatrix = mat4.create();
mat4.identity(RotationMatrix);
function handleMouseDown(event) {mouseDown = true;lastMouseX = event.clientX;lastMouseY = event.clientY;
}
function handleMouseUp(event) {mouseDown = false;
}
function handleMouseMove(event) {if (!mouseDown) {return;}var newX = event.clientX;var newY = event.clientY;var deltaX = newX - lastMouseX;var newRotationMatrix = mat4.create();mat4.identity(newRotationMatrix);mat4.rotate(newRotationMatrix, degToRad(deltaX / 5), [0, 1, 0]);var deltaY = newY - lastMouseY;mat4.rotate(newRotationMatrix, degToRad(deltaY / 5), [1, 0, 0]);mat4.multiply(newRotationMatrix, RotationMatrix, RotationMatrix);lastMouseX = newXlastMouseY = newY;
}
// Draw scene and initialization
var MoveMatrix = mat4.create();
mat4.identity(MoveMatrix);
function drawScene() {gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);mat4.identity(mvMatrix);mat4.translate(mvMatrix, [0.0, 0.0, z]);mat4.rotate(mvMatrix, degToRad(xRot), [1, 0, 0]);mat4.rotate(mvMatrix, degToRad(yRot), [0, 1, 0]);mat4.multiply(mvMatrix, MoveMatrix);mat4.multiply(mvMatrix, RotationMatrix);for (var i=0;i<pics_num;i=i+1) {gl.bindBuffer(gl.ARRAY_BUFFER, objVertexPositionBuffer[i]);gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, objVertexPositionBuffer[i].itemSize, gl.FLOAT, false, 0, 0);gl.bindBuffer(gl.ARRAY_BUFFER, objVertexTextureCoordBuffer[i]);gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, objVertexTextureCoordBuffer[i].itemSize, gl.FLOAT, false, 0, 0);gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D, crateTextures[i]);gl.uniform1i(shaderProgram.samplerUniform, 0);gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, objVertexIndexBuffer[i]);setMatrixUniforms();gl.drawElements(gl.TRIANGLES, objVertexIndexBuffer[i].numItems, gl.UNSIGNED_SHORT, 0);}
}
var lastTime = 0;
function animate() {var timeNow = new Date().getTime();if (lastTime != 0) {var elapsed = timeNow - lastTime;xRot += (xSpeed * elapsed) / 1000.0;yRot += (ySpeed * elapsed) / 1000.0;}lastTime = timeNow;
}
function drawFrame() {requestAnimFrame(drawFrame);handleKeys();drawScene();animate();
}
function initWebGl() {var canvas = document.getElementById('panel');initGL(canvas);initShaders();initObjBuffers();initTextures();gl.clearColor(1.0, 1.0, 1.0, 1.0);gl.enable(gl.DEPTH_TEST);document.onkeydown = handleKeyDown;document.onkeyup = handleKeyUp;canvas.onmousedown = handleMouseDown;document.onmouseup = handleMouseUp;document.onmousemove = handleMouseMove;drawFrame();
}

And again – long code, but most important. I separated all code to 3 sides: initializations, handlers and drawing of scene. Hope that you already read our previos WebGL lesson. In this case it will more easy to understand today`s code. Just make attention that instead color buffer we will using texture buffer (objVertexTextureCoordBuffer). Also, this demo able to work with any amouht of used images (better – more than 3).

再说一遍-长代码,但最重要。 我将所有代码分为3部分:初始化,处理程序和场景绘制。 希望您已经阅读了我们的previos WebGL课程 。 在这种情况下,更容易理解今天的代码。 请注意,我们将使用纹理缓冲区(objVertexTextureCoordBuffer)代替颜色缓冲区。 而且,该演示程序可以处理大量二手图像(更好–超过3个)。

步骤4.图片 (Step 4. Images)

All these images we will using for twisting:

我们将用于扭曲的所有这些图像:

1
1个
2
2
3
3
4
4
5
5
6
6
7
7
现场演示

结论 (Conclusion)

I hope you enjoyed today`s result. If you have any suggestions or ideas – share it :-) Welcome back our friends!

希望您喜欢今天的成绩。 如果您有任何建议或想法,请分享:-)欢迎回到我们的朋友身边!

翻译自: https://www.script-tutorials.com/twisting-images-webgl/

webgl不同图像不同纹理

这篇关于webgl不同图像不同纹理_在WebGl中创建键盘敏感的3D扭曲图像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/581037

相关文章

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

Python Flask实现定时任务的不同方法详解

《PythonFlask实现定时任务的不同方法详解》在Flask中实现定时任务,最常用的方法是使用APScheduler库,本文将提供一个完整的解决方案,有需要的小伙伴可以跟随小编一起学习一下... 目录完js整实现方案代码解释1. 依赖安装2. 核心组件3. 任务类型4. 任务管理5. 持久化存储生产环境

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注

MySQL的触发器全解析(创建、查看触发器)

《MySQL的触发器全解析(创建、查看触发器)》MySQL触发器是与表关联的存储程序,当INSERT/UPDATE/DELETE事件发生时自动执行,用于维护数据一致性、日志记录和校验,优点包括自动执行... 目录触发器的概念:创建触www.chinasem.cn发器:查看触发器:查看当前数据库的所有触发器的定

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自