繁体   English   中英

为什么我的形状不能像这个例子那样改变颜色?

[英]Why won't my shape animate color change like this example?

这是我正在关注的示例: 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 );
}

我觉得这与每次更改后调用render有关。 另外,我不确定为什么形状为蓝色。 我真的是WebGL的新手。 有人有建议的学习材料吗?

在浏览器中以编程方式对事物进行动画处理的推荐方法是使用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); 

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

看到这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM