美文网首页WebGL
示例18:立方体-平行光下的漫反射

示例18:立方体-平行光下的漫反射

作者: WebGiser | 来源:发表于2021-07-16 20:16 被阅读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_MvpMatrix;\n' +
                    'uniform vec3 u_LightColor;\n' +
                    'uniform vec3 u_LightDirection;\n' +
                    'varying vec4 v_Color;\n' +
                    'void main(){\n' +
                    '   gl_Position = u_MvpMatrix * a_Position;\n' +
                    '   vec3 normal = normalize(vec3(a_Normal));\n' +
                    '   float nDotL = max(dot(u_LightDirection, normal), 0.0);\n' +
                    '   vec3 diffuse = u_LightColor * vec3(a_Color) * nDotL;\n' +
                    '   v_Color = vec4(diffuse, a_Color.a);\n' +
                    '}\n';
    
                //片元着色器
                let FSHADER_SOURCE =
                    '#ifdef GL_ES\n' +
                    'precision mediump float;\n' +
                    '#endif\n' +
                    'varying vec4 v_Color;\n' +
                    'void main(){\n' +
                    '   gl_FragColor = v_Color;\n' +
                    '}\n';
    
                //设置缓冲区
                function initVertexBuffer(gl) {
                    // Create a cube
                    //    v6----- v5
                    //   /|      /|
                    //  v1------v0|
                    //  | |     | |
                    //  | |v7---|-|v4
                    //  |/      |/
                    //  v2------v3
                    let vertices = new Float32Array([
                        1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, // v0-v1-v2-v3 front
                        1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, // v0-v3-v4-v5 right
                        1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, // v0-v5-v6-v1 up
                        -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, // v1-v6-v7-v2 left
                        -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, // v7-v4-v3-v2 down
                        1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0 // v4-v7-v6-v5 back
                    ]);
                    let colors = new Float32Array([ 
                        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([ 
                        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_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
                let mvpMatrix = new Matrix4();
                mvpMatrix.setPerspective(30, canvas.width / canvas.height, 1, 100);
                mvpMatrix.lookAt(3, 3, 7, 0, 0, 0, 0, 1, 0);
                gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
    
                let u_LightColor = gl.getUniformLocation(gl.program, 'u_LightColor');
                gl.uniform3f(u_LightColor, 1.0, 1.0, 1.0);
    
                let u_LightDirection = gl.getUniformLocation(gl.program, 'u_LightDirection');
                let lightDirection = new Vector3([0.5, 3.0, 4.0]);
                lightDirection.normalize();
                gl.uniform3fv(u_LightDirection, lightDirection.elements);
    
                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>
    

    相关文章

      网友评论

        本文标题:示例18:立方体-平行光下的漫反射

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