The example in this article describes how WebGL uses FBO to complete the cube map effect. Share it with everyone for your reference, the details are as follows:
This article mainly records some basic points of WebGL, and also learns how to use FBO and environment maps. Let’s take a look at the renderings first (requires support for WebGL, Chrome, Firefox, and IE11).
The main implementation process is as follows. First use FBO to output the current environment in the cube texture, then draw the current cube, and finally draw the ball, and paste the texture associated with the FBO on the sphere.
When starting WebGL, it is best to have some OpenGL basics. When talking about Obj perfection and MD2 earlier, you may have discovered that due to the addition and use of shaders, most of the OpenGL APIs are no longer used. WebGL is similar to this. Most of the functions are shaders completing the main functions. Record the main processes. You can compare them with the previous ones to see if they are similar. In order to familiarize yourself with the basic functions of WebGL, this article does not use a relatively complete framework. A framework (gl-matrix.js) is used to help calculate matrices.
Similar to using OpenGL, we need to initialize the usage environment and extract some global usage. The relevant code is as follows:
Initialization:
var gl;//WebGLRenderingContext var cubeVBO;//Cube buffer ID var sphereVBO;//球体VBO var sphereEBO;//球体EBO var cubeTexID;//立方体纹理ID var fboBuffer;//桢缓存对象 var glCubeProgram;//立方体着色器应用 var glSphereProgram;//球体着色器应用 var fboWidth = 512;//桢缓存绑定纹理宽度 var fboHeight = 512;//桢缓存绑定纹理高度 var targets;//立方体贴图六个方向 var pMatrix = mat4.create();//透视矩阵 var vMatrix = mat4.create();//视图矩阵 var eyePos = vec3.fromValues(0.0, 1.0, 0.0);//眼睛位置 var eyeLookat = vec3.fromValues(0.0, -0.0, 0.0);//眼睛方向 var spherePos = vec3.fromValues(0.0, -0.0, 0.0);//球体位置 var canvanName; function webGLStart(cName) { canvanName = cName; InitWebGL(); InitCubeShader(); InitSphereShader(); InitCubeBuffer(); InitSphereBuffer(); InitFBOCube(); //RenderFBO(); gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.enable(gl.DEPTH_TEST); tick(); } function InitWebGL() { //var canvas = document.getElementById(canvanName); InitGL(canvanName); } function InitGL(canvas) { try { //WebGLRenderingContext gl = canvas.getContext("experimental-webgl"); gl.viewportWidth = canvas.width; gl.viewportHeight = canvas.height; targets = [gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z]; } catch (e) { } if (!gl) { alert("你的浏览器不支持WebGL"); } }
Here, we initialize the upper and lower environments of WebGL in the web page and give a series of initialization processes. The room, that is, the relevant code for the cube is given below.
Cube:
function InitCubeShader() { //WebGLShader var shader_vertex = GetShader("cubeshader-vs"); var shader_fragment = GetShader("cubeshader-fs"); //WebglCubeProgram glCubeProgram = gl.createProgram(); gl.attachShader(glCubeProgram, shader_vertex); gl.attachShader(glCubeProgram, shader_fragment); gl.linkProgram(glCubeProgram); if (!gl.getProgramParameter(glCubeProgram, gl.LINK_STATUS)) { alert("Shader hava error."); } gl.useProgram(glCubeProgram); glCubeProgram.positionAttribute = gl.getAttribLocation(glCubeProgram, "a_position"); glCubeProgram.normalAttribute = gl.getAttribLocation(glCubeProgram, "a_normal"); glCubeProgram.texCoordAttribute = gl.getAttribLocation(glCubeProgram, "a_texCoord"); glCubeProgram.view = gl.getUniformLocation(glCubeProgram, "view"); glCubeProgram.perspective = gl.getUniformLocation(glCubeProgram, "perspective"); } function InitCubeBuffer() { var cubeData = [ -10.0, -10.0, -10.0, 0.0, 0.0, -10.0, 1.0, 0.0, -10.0, 10.0, -10.0, 0.0, 0.0, -10.0, 1.0, 1.0, 10.0, 10.0, -10.0, 0.0, 0.0, -10.0, 0.0, 1.0, 10.0, 10.0, -10.0, 0.0, 0.0, -10.0, 0.0, 1.0, 10.0, -10.0, -10.0, 0.0, 0.0, -10.0, 0.0, 0.0, -10.0, -10.0, -10.0, 0.0, 0.0, -10.0, 1.0, 0.0, -10.0, -10.0, 10.0, 0.0, 0.0, 10.0, 0.0, 0.0, 10.0, -10.0, 10.0, 0.0, 0.0, 10.0, 1.0, 0.0, 10.0, 10.0, 10.0, 0.0, 0.0, 10.0, 1.0, 1.0, 10.0, 10.0, 10.0, 0.0, 0.0, 10.0, 1.0, 1.0, -10.0, 10.0, 10.0, 0.0, 0.0, 10.0, 0.0, 1.0, -10.0, -10.0, 10.0, 0.0, 0.0, 10.0, 0.0, 0.0, -10.0, -10.0, -10.0, 0.0, -10.0, 0.0, 0.0, 0.0, 10.0, -10.0, -10.0, 0.0, -10.0, 0.0, 1.0, 0.0, 10.0, -10.0, 10.0, 0.0, -10.0, 0.0, 1.0, 1.0, 10.0, -10.0, 10.0, 0.0, -10.0, 0.0, 1.0, 1.0, -10.0, -10.0, 10.0, 0.0, -10.0, 0.0, 0.0, 1.0, -10.0, -10.0, -10.0, 0.0, -10.0, 0.0, 0.0, 0.0, 10.0, -10.0, -10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 10.0, 10.0, -10.0, 10.0, 0.0, 0.0, 1.0, 0.0, 10.0, 10.0, 10.0, 10.0, 0.0, 0.0, 1.0, 1.0, 10.0, 10.0, 10.0, 10.0, 0.0, 0.0, 1.0, 1.0, 10.0, -10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 1.0, 10.0, -10.0, -10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 10.0, 10.0, -10.0, 0.0, 10.0, 0.0, 0.0, 0.0, -10.0, 10.0, -10.0, 0.0, 10.0, 0.0, 1.0, 0.0, -10.0, 10.0, 10.0, 0.0, 10.0, 0.0, 1.0, 1.0, -10.0, 10.0, 10.0, 0.0, 10.0, 0.0, 1.0, 1.0, 10.0, 10.0, 10.0, 0.0, 10.0, 0.0, 0.0, 1.0, 10.0, 10.0, -10.0, 0.0, 10.0, 0.0, 0.0, 0.0, -10.0, 10.0, -10.0, -10.0, 0.0, 0.0, 0.0, 0.0, -10.0, -10.0, -10.0, -10.0, 0.0, 0.0, 1.0, 0.0, -10.0, -10.0, 10.0, -10.0, 0.0, 0.0, 1.0, 1.0, -10.0, -10.0, 10.0, -10.0, 0.0, 0.0, 1.0, 1.0, -10.0, 10.0, 10.0, -10.0, 0.0, 0.0, 0.0, 1.0, -10.0, 10.0, -10.0, -10.0, 0.0, 0.0, 0.0, 0.0, ]; cubeVBO = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, cubeVBO); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(cubeData), gl.STATIC_DRAW); } function RenderCube() { gl.useProgram(glCubeProgram); gl.bindBuffer(gl.ARRAY_BUFFER, cubeVBO); gl.vertexAttribPointer(glCubeProgram.positionAttribute, 3, gl.FLOAT, false, 32, 0); gl.enableVertexAttribArray(glCubeProgram.positionAttribute); gl.vertexAttribPointer(glCubeProgram.normalAttribute, 3, gl.FLOAT, false, 32, 12); gl.enableVertexAttribArray(glCubeProgram.normalAttribute); gl.vertexAttribPointer(glCubeProgram.texCoordAttribute, 2, gl.FLOAT, false, 32, 24); gl.enableVertexAttribArray(glCubeProgram.texCoordAttribute); gl.uniformMatrix4fv(glCubeProgram.view, false, vMatrix); gl.uniformMatrix4fv(glCubeProgram.perspective, false, pMatrix); gl.drawArrays(gl.TRIANGLES, 0, 36); }
The above code is mainly divided into initializing the shader object of the cube, initializing the relevant cache, and then drawing the cube. It can be said that in Opengl, if you use a shader to draw, the process is similar. In Opengl, there is no fixed pipeline. Some functions such as InterleavedArrays are used to specify vertices, normals or textures, and vertexAttribPointer is used to transfer data between the application and the shader. The improved version of parameter transfer in the previous MD2 frame animation implementation also has related applications.
The main code corresponding to the cube shader is as follows:
Cube shader implementation:
<script id="cubeshader-fs" type="x-shader/x-fragment"> precision mediump float; varying vec3 normal; varying vec3 tex1; varying vec3 tex2; void main( void ) { float x = tex1.x * 6.28 * 8.0; //2兀 * 8 float y = tex1.y * 6.28 * 8.0; //2兀 * 8 //cos(x)= 8个 (1 -1 1) gl_FragColor = vec4(tex2,1.0) * vec4(sign(cos(x)+cos(y))); // //gl_FragColor = vec4(normal*vec3(0.5)+vec3(0.5), 1); } </script> <script id="cubeshader-vs" type="x-shader/x-vertex"> attribute vec3 a_position; attribute vec3 a_normal; attribute vec2 a_texCoord; uniform mat4 view; uniform mat4 perspective; varying vec3 normal; varying vec3 tex1; varying vec3 tex2; void main( void ) { gl_Position = perspective * view * vec4(a_position,1.0); normal = a_normal; tex1 = vec3(a_texCoord,0.0); tex2 = normalize(a_position)*0.5+0.5; } </script>
In the shader, there is no ftransform() function to call. You have to pass the model, view, and perspective matrix yourself. Here, the model is drawn with the origin as the center, which means the model view matrix is also the view matrix, so The calculation of screen position requires only the view and perspective matrices. In the fragment shader, x and y are passed from the texture coordinates in the vertex shader. The corresponding process is 6.28*8.0, which is equivalent to 8 360 degrees. It is used to control the display of blocks on the cube, and tex2 is in the shader. The value of vertex mapping [0,1] sets different meanings for the six sides of the cube respectively, and then uses the product of two vectors to mix the two color displays, gl_FragColor = vec4(tex2,1.0) * vec4(sign( cos(x)+cos(y))).
Before displaying the sphere, you should first generate the cube drawing of the current environment. Here, use FBO. First generate the frame cache and cube drawing, and associate them. Then, with the origin as the center, draw up, down, left, front, and right respectively, and then use The frame buffer is output to the six faces of the cube respectively. The main code is as follows:
FBO and cube texture:
function InitFBOCube() { // WebGLFramebuffer fboBuffer = gl.createFramebuffer(); gl.bindFramebuffer(gl.FRAMEBUFFER, fboBuffer); fboBuffer.width = 512; fboBuffer.height = 512; cubeTexID = gl.createTexture(); gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeTexID); gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR); for (var i = 0; i < targets.length; i++) { gl.texImage2D(targets[i], 0, gl.RGBA, fboBuffer.width, fboBuffer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); } gl.bindFramebuffer(gl.FRAMEBUFFER, null); } function RenderFBO() { gl.disable(gl.DEPTH_TEST); gl.viewport(0, 0, fboBuffer.width, fboBuffer.height); gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.bindFramebuffer(gl.FRAMEBUFFER, fboBuffer); for (var i = 0; i < targets.length; i++) { gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, targets[i], cubeTexID, null); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); } mat4.perspective(pMatrix, 45, fboBuffer.width / fboBuffer.height, 0.1, 100.0); for (var i = 0; i < targets.length; i++) { gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, targets[i], cubeTexID, null); var lookat = vec3.create(); var up = vec3.create(); up[1] = 1.0; if (i == 0) { lookat[0] = -1.0; } else if (i == 1) { lookat[0] = 1.0; } else if (i == 2) { lookat[1] = -1.0; up[0] = 1.0; } else if (i == 3) { lookat[1] = 1.0; up[0] = 1.0; } else if (i == 4) { lookat[2] == -1.0; } else if (i == 5) { lookat[2] = 1.0; } else { } //vec3.fromValues(0.0, 0.0, 0.0) vMatrix = mat4.create(); mat4.lookAt(vMatrix, vec3.fromValues(0.0, 0.0, 0.0), lookat, up); //mat4.scale(vMatrix, vMatrix, vec3.fromValues(-1.0, -1.0, -1.0)); //mat4.translate(vMatrix, vMatrix, spherePos); RenderCube(); } gl.bindFramebuffer(gl.FRAMEBUFFER, null); gl.enable(gl.DEPTH_TEST); }
I don’t know if there is a problem with the matrix algorithm provided by gl-matrix above, or if it should be like this. The texture map generated when going up and down is wrong, and the upward vector of the camera needs to be deflected. Because the camera position is parallel to the generated Z axis of the target and the set UP axis, the X axis cannot be calculated correctly, and the corresponding UP axis cannot be calculated either, causing an error in the corresponding view matrix.
The last step is to draw the sphere. The code is mainly similar to that of the cube. Pay attention to the vertex algorithm of the sphere.
Sphere:
function InitSphereShader() { //WebGLShader var shader_vertex = GetShader("sphereshader-vs"); var shader_fragment = GetShader("sphereshader-fs"); //WebglCubeProgram glSphereProgram = gl.createProgram(); gl.attachShader(glSphereProgram, shader_vertex); gl.attachShader(glSphereProgram, shader_fragment); gl.linkProgram(glSphereProgram); if (!gl.getProgramParameter(glSphereProgram, gl.LINK_STATUS)) { alert("Shader hava error."); } glSphereProgram.positionAttribute = gl.getAttribLocation(glSphereProgram, "a_position"); glSphereProgram.normalAttribute = gl.getAttribLocation(glSphereProgram, "a_normal"); glSphereProgram.eye = gl.getUniformLocation(glSphereProgram, "eye"); glSphereProgram.mapCube = gl.getUniformLocation(glSphereProgram, "mapCube"); glSphereProgram.model = gl.getUniformLocation(glSphereProgram, "model"); glSphereProgram.view = gl.getUniformLocation(glSphereProgram, "view"); glSphereProgram.perspective = gl.getUniformLocation(glSphereProgram, "perspective"); } function InitSphereBuffer() { var radius = 1; var segments = 16; var rings = 16; var length = segments * rings * 6; var sphereData = new Array(); var sphereIndex = new Array(); for (var y = 0; y < rings; y++) { var phi = (y / (rings - 1)) * Math.PI; for (var x = 0; x < segments; x++) { var theta = (x / (segments - 1)) * 2 * Math.PI; sphereData.push(radius * Math.sin(phi) * Math.cos(theta)); sphereData.push(radius * Math.cos(phi)); sphereData.push(radius * Math.sin(phi) * Math.sin(theta)); sphereData.push(Math.sin(phi) * Math.cos(theta)); sphereData.push(radius * Math.cos(phi)) sphereData.push(Math.sin(phi) * Math.sin(theta)); } } for (var y = 0; y < rings - 1; y++) { for (var x = 0; x < segments - 1; x++) { sphereIndex.push((y + 0) * segments + x); sphereIndex.push((y + 1) * segments + x); sphereIndex.push((y + 1) * segments + x + 1); sphereIndex.push((y + 1) * segments + x + 1); sphereIndex.push((y + 0) * segments + x + 1) sphereIndex.push((y + 0) * segments + x); } } sphereVBO = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, sphereVBO); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(sphereData), gl.STATIC_DRAW); sphereVBO.numItems = segments * rings; sphereEBO = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, sphereEBO); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(sphereIndex), gl.STATIC_DRAW); sphereEBO.numItems = sphereIndex.length; } function RenderSphere() { gl.useProgram(glSphereProgram); gl.bindBuffer(gl.ARRAY_BUFFER, sphereVBO); gl.vertexAttribPointer(glSphereProgram.positionAttribute, 3, gl.FLOAT, false, 24, 0); gl.enableVertexAttribArray(glSphereProgram.positionAttribute); gl.vertexAttribPointer(glSphereProgram.normalAttribute, 3, gl.FLOAT, false, 24, 12); gl.enableVertexAttribArray(glSphereProgram.normalAttribute); var mMatrix = mat4.create(); mat4.translate(mMatrix, mMatrix, spherePos); gl.uniform3f(glSphereProgram.eye, eyePos[0],eyePos[1],eyePos[2]); gl.uniformMatrix4fv(glSphereProgram.model, false, mMatrix); gl.uniformMatrix4fv(glSphereProgram.view, false, vMatrix); gl.uniformMatrix4fv(glSphereProgram.perspective, false, pMatrix); gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeTexID); //gl.uniformMatrix4fv(glSphereProgram.mapCube, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, sphereEBO); gl.drawElements(gl.TRIANGLES, sphereEBO.numItems, gl.UNSIGNED_SHORT, 0); gl.bindTexture(gl.TEXTURE_2D, null); }
As you can see, it is the same three steps as the cube, initialize the shader, initialize the vertices and normals, and draw. The shader code is given below:
Sphere Shader:
<script id="sphereshader-fs" type="x-shader/x-fragment"> precision mediump float; varying vec3 normal; varying vec3 eyevec; uniform samplerCube mapCube; void main( void ) { gl_FragColor = textureCube(mapCube, reflect(normalize(-eyevec), normalize(normal))); } </script> <script id="sphereshader-vs" type="x-shader/x-vertex"> attribute vec3 a_position; attribute vec3 a_normal; uniform mat4 model; uniform mat4 view; uniform mat4 perspective; uniform vec3 eye; varying vec3 normal; varying vec3 eyevec; void main( void ) { gl_Position = perspective * view * model * vec4(a_position,1.0); eyevec = -eye;// a_position.xyz; normal = a_normal; } </script>
A little different from the previous cube is that the sphere has its own model matrix. This is also a normal usage. Then the eye corresponding to the sphere vertex vector and normal are passed in the fragment shader. In the fragment shader, it is useful. To the cube texture generated previously, we obtain the environment color corresponding to the current sphere based on the point where the eye passes through the vertex and reflects the corresponding normal vector to the cube texture. Here, we can directly call textureCube to complete the process mentioned above. , we don’t need to calculate it manually.
For the use of the GetShader function, please refer to the explanation here http://msdn.microsoft.com/zh-TW/library/ie/dn302360(v=vs.85).
It can be said that the main drawing function above has been completed, but ours is active, so we need to simulate how often the client environment is drawn. The main code is as follows:
Animation:
function tick() { Update(); OnDraw(); setTimeout(function () { tick() }, 15); } function OnDraw() { //fbo rander CUBE_MAP RenderFBO(); //element rander gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); mat4.perspective(pMatrix, 45, gl.viewportWidth / gl.viewportHeight, 0.1, 200.0); mat4.lookAt(vMatrix, eyePos, eyeLookat, vec3.fromValues(0.0, 1.0, 0.0)); RenderCube(); RenderSphere(); } var lastTime = new Date().getTime(); function Update() { var timeNow = new Date().getTime(); if (lastTime != 0) { var elapsed = timeNow - lastTime; //3000控制人眼的旋转速度。8控制人眼的远近 eyePos[0] = Math.cos(elapsed / 3000) * 8; eyePos[2] = Math.sin(elapsed / 2000) * 8; spherePos[0] = Math.cos(elapsed / 4000) * 3; spherePos[2] = Math.cos(elapsed / 4000) * 3; } }
在上面,每隔15毫秒呼叫一次Update與Draw函數,其中Update用於更新眼睛與球體位置,Draw繪畫。
完整實例程式碼點擊此處本站下載。
更多關於JS特效相關內容有興趣的讀者可查看本站專題:《jQuery動畫與特效用法總結》及《jQuery常見經典特效匯總》
希望本文所述對大家JavaScript程式設計有所幫助。