美文网首页WebGL
示例21:立方体-点光源逐片元

示例21:立方体-点光源逐片元

作者: WebGiser | 来源:发表于2021-07-18 20:53 被阅读0次

    效果

    image.png

    代码

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
        </head>
    
        <body>
            <canvas id="webgl" width="600" height="600">
                Please use a browser that supports "canvas"
            </canvas>
    
            <script src="./lib/webgl-utils.js"></script>
            <script src="./lib/webgl-debug.js"></script>
            <script src="./lib/cuon-utils.js"></script>
            <script src="./lib/cuon-matrix.js"></script>
    
            <script>
                let canvas = document.getElementById("webgl");
                let gl = getWebGLContext(canvas);
    
                //顶点着色器
                let VSHADER_SOURCE =
                    'attribute vec4 a_Position;\n' +
                    'attribute vec4 a_Color;\n' +
                    'attribute vec4 a_Normal;\n' +
                    'uniform mat4 u_NormalMatrix;\n' +
                    'uniform mat4 u_MvpMatrix;\n' +
                    'uniform mat4 u_ModelMatrix;\n' +
                    'varying vec4 v_Color;\n' +
                    'varying vec3 v_Position;\n' +
                    'varying vec3 v_Normal;\n' +
                    'void main(){\n' +
                    '   gl_Position = u_MvpMatrix * a_Position;\n' +
                    '   v_Position = vec3(u_ModelMatrix * a_Position);\n' +
                    '   v_Normal = normalize(vec3(u_NormalMatrix * a_Normal));\n'+
                    '   v_Color = a_Color;\n'+
                    '}\n';
    
                //片元着色器
                let FSHADER_SOURCE =
                    '#ifdef GL_ES\n' +
                    'precision mediump float;\n' +
                    '#endif\n' +
                    'uniform vec3 u_LightColor;\n' +
                    'uniform vec3 u_LightPosition;\n' +
                    'uniform vec3 u_AmbientLight;\n' +
                    'varying vec4 v_Color;\n' +
                    'varying vec3 v_Position;\n' +
                    'varying vec3 v_Normal;\n' +
                    'void main(){\n' +
                    '   vec3 normal = normalize(v_Normal);\n'+
                    // 计算光线方向并归一化
                    '   vec3 lightDirection = normalize(u_LightPosition - v_Position);\n' +
                    // 计算光线方向和法向量的点积
                    '   float nDotL = max(dot(lightDirection, normal), 0.0);\n' +
                    // 计算漫反射光的颜色
                    '   vec3 diffuse = u_LightColor * vec3(v_Color) * nDotL;\n' +
                    // 计算环境光产生的反射光的颜色
                    '   vec3 ambient = u_AmbientLight * v_Color.rgb;\n' +
                    '   gl_FragColor = vec4(diffuse+ambient, v_Color.a);\n' +
                    '}\n';
    
                //设置缓冲区
                function initVertexBuffer(gl) {
                    // Create a cube
                    //    v6----- v5
                    //   /|      /|
                    //  v1------v0|
                    //  | |     | |
                    //  | |v7---|-|v4
                    //  |/      |/
                    //  v2------v3
                    let vertices = new Float32Array([
                        2.0, 2.0, 2.0, -2.0, 2.0, 2.0, -2.0, -2.0, 2.0, 2.0, -2.0, 2.0, // v0-v1-v2-v3 front
                        2.0, 2.0, 2.0, 2.0, -2.0, 2.0, 2.0, -2.0, -2.0, 2.0, 2.0, -2.0, // v0-v3-v4-v5 right
                        2.0, 2.0, 2.0, 2.0, 2.0, -2.0, -2.0, 2.0, -2.0, -2.0, 2.0, 2.0, // v0-v5-v6-v1 up
                        -2.0, 2.0, 2.0, -2.0, 2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, 2.0, // v1-v6-v7-v2 left
                        -2.0, -2.0, -2.0, 2.0, -2.0, -2.0, 2.0, -2.0, 2.0, -2.0, -2.0, 2.0, // v7-v4-v3-v2 down
                        2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, 2.0, -2.0, 2.0, 2.0, -2.0 // v4-v7-v6-v5 back
                    ]);
                    let colors = new Float32Array([ // Colors
                        1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v1-v2-v3 front
                        1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right
                        1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v5-v6-v1 up
                        1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v1-v6-v7-v2 left
                        1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v7-v4-v3-v2 down
                        1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0 // v4-v7-v6-v5 back
                    ]);
                    let normals = new Float32Array([ // Normal
                        0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, // v0-v1-v2-v3 front
                        1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, // v0-v3-v4-v5 right
                        0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, // v0-v5-v6-v1 up
                        -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, // v1-v6-v7-v2 left
                        0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, // v7-v4-v3-v2 down
                        0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0 // v4-v7-v6-v5 back
                    ]);
                    // Indices of the vertices
                    let indices = new Uint8Array([
                        0, 1, 2, 0, 2, 3, // front
                        4, 5, 6, 4, 6, 7, // right
                        8, 9, 10, 8, 10, 11, // up
                        12, 13, 14, 12, 14, 15, // left
                        16, 17, 18, 16, 18, 19, // down
                        20, 21, 22, 20, 22, 23 // back
                    ]);
    
                    //创建缓冲区
                    let vertexBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
                    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
                    let FSIZE = vertices.BYTES_PER_ELEMENT;
                    let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
                    gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 3 * FSIZE, 0);
                    gl.enableVertexAttribArray(a_Position);
    
                    let colorBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
                    gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
                    let a_Color = gl.getAttribLocation(gl.program, 'a_Color');
                    gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 3 * FSIZE, 0 * FSIZE);
                    gl.enableVertexAttribArray(a_Color);
    
                    let normalBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
                    gl.bufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW);
                    let a_Normal = gl.getAttribLocation(gl.program, 'a_Normal');
                    gl.vertexAttribPointer(a_Normal, 3, gl.FLOAT, false, 3 * FSIZE, 0 * FSIZE);
                    gl.enableVertexAttribArray(a_Normal);
    
                    let indexBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
                    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
                    return indices.length;
                }
    
    
                initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE);
    
                let u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
                let modelMatrix = new Matrix4();
                modelMatrix.rotate(90, 0, 1, 0);
                gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);
    
                let u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
                let mvpMatrix = new Matrix4();
                mvpMatrix.setPerspective(30, canvas.width / canvas.height, 1, 100);
                mvpMatrix.lookAt(6, 6, 14, 0, 0, 0, 0, 1, 0);
                mvpMatrix.multiply(modelMatrix);
                gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
    
                let u_NormalMatrix = gl.getUniformLocation(gl.program, 'u_NormalMatrix');
                let normalMatrix = new Matrix4();
                normalMatrix.setInverseOf(modelMatrix);
                normalMatrix.transpose();
                gl.uniformMatrix4fv(u_NormalMatrix, false, normalMatrix.elements);
    
    
                let u_LightColor = gl.getUniformLocation(gl.program, 'u_LightColor');
                gl.uniform3f(u_LightColor, 1.0, 1.0, 1.0);
    
                let u_LightPosition = gl.getUniformLocation(gl.program, 'u_LightPosition');
                gl.uniform3f(u_LightPosition, 2.3, 4.0, 3.5);
    
                let u_AmbientLight = gl.getUniformLocation(gl.program, 'u_AmbientLight');
                gl.uniform3f(u_AmbientLight, 0.2, 0.2, 0.2);
    
                let n = initVertexBuffer(gl);
                gl.clearColor(0.0, 0.0, 0.0, 1.0);
                gl.enable(gl.DEPTH_TEST);
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_BYTE, 0);
            </script>
        </body>
    </html>
    

    相关文章

      网友评论

        本文标题:示例21:立方体-点光源逐片元

        本文链接:https://www.haomeiwen.com/subject/xnrkultx.html