为什么我的形状动画颜色不会像这个例子那样变化?

时间:2017-09-11 23:03:02

标签: javascript webgl

这是我关注的示例:https://thebookofshaders.com/03/

这是我的HTML:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <!--Include A/S WebGL support libraries-->
    <script type="text/javascript" src="../Common/webgl-utils.js"></script>
    <script type="text/javascript" src="../Common/initShaders.js"></script>
    <script type="text/javascript" src="../Common/MV.js"></script>
    <script type="text/javascript" src="../Common/webgl-debug.js"></script>
    <script type="text/javascript" src="assignment1.js"></script>
    <script id="vertex-shader" type="x-shader/x-vertex">
        // GLSL vertex shader code
        attribute vec4 vPosition;
        void main()
        {
            gl_Position = vPosition;
        }
    </script>
    <script id="fragment-shader" type="x-shader/x-fragment">
        // GLSL fragment shader code
        precision mediump float;
        uniform float u_time;
        void main()
        {

            gl_FragColor = vec4( abs(sin(u_time)), 1.0, 1.0, 1.0 );
        }
    </script>
<canvas id="gl-canvas" width="512" height=" 512">>
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
</body>
</html>

这是我的JavaScript:

// square.js -- a graphics "Hello World"
var gl;
var points;

window.onload = function init(){
    var canvas = document.getElementById( "gl-canvas" );

    //    gl = WebGLUtils.setupWebGL( canvas );  // More efficient
    gl = WebGLDebugUtils.makeDebugContext( canvas.getContext("webgl") ); // For debugging
    if ( !gl ) { alert( "WebGL isn't available" );
               }

    // Four 2D Vertices using Angel/Shreiner utility class vac2
    var vertices = [           
        vec2( -0.5, 0.5 ),
        vec2(  0.5,  0.5 ),
        vec2(  0.5, -0.5 ),
        vec2( -0.5, -0.5)
    ];


    //  Configure WebGL

    gl.viewport( canvas.width/2, 0, canvas.width/2, canvas.height/2 );
    gl.clearColor( 0.0, 0.0, 0.0, 1.0 );

    //  Load shaders and initialize attribute buffers using A/S utility initShaders

    var program = initShaders( gl, "vertex-shader", "fragment-shader" ); 
    gl.useProgram( program );

    // Load the data into the GPU using A/S flatten function

    var bufferId = gl.createBuffer();
    gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
    gl.bufferData( gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW ); 


    // Associate our shader variables with our data buffer

    var vPosition = gl.getAttribLocation( program, "vPosition" );
    gl.vertexAttribPointer(
        vPosition, // Specifies the index of the generic vertex attribute to be modified.
        2,         // Specifies the number of components per generic vertex attribute. 
                   // Must be 1, 2, 3, or 4. 
        gl.FLOAT,  // Specifies the data type of each component in the array. 
            // GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, or GL_FLOAT. 
        false,     // Specifies whether fixed-point data values should be normalized (GL_TRUE) 
            // or converted directly as fixed-point values (GL_FALSE) when they are accessed.
        0,         // Specifies the byte offset between consecutive generic vertex attributes. 
            // If stride is 0, the generic vertex attributes are understood 
            // to be tightly packed in the array.
        0          // Specifies a pointer to the first component 
            // of the first generic vertex attribute in the array.
                          );
    gl.enableVertexAttribArray( vPosition );    

    render();
};

function render() {
    gl.clear( gl.COLOR_BUFFER_BIT );
    gl.drawArrays( gl.TRIANGLE_STRIP, 0, 4 );
}

我觉得它与每次更改后调用渲染有关。另外,我不确定为什么形状是蓝色的。我是WebGL的新手。有没有人有任何建议的材料来学习它?

1 个答案:

答案 0 :(得分:1)

在浏览器中以编程方式设置动画内容的推荐方法是使用requestAnimationFrame

示例:

// animate the background color
function render(timeSincePageLoadedInMilliseconds) {
   const timeInSeconds = timeSincePageLoadedInMilliseconds * 0.001;
   document.body.style.backgroundColor = `rgb(${timeInSeconds % 1 * 256 | 0}, 0, 0)`;
   requestAnimationFrame(render);
}
requestAnimationFrame(render);

该示例根据时间更新页面正文的背景颜色。在您自己的程序中,您将更新自己的变量,这些变量代表位置,方向,比例,颜色,您想要制作动画的任何内容,然后重新绘制所有内容。

请参阅this