Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you update a uniform In Three.js?

I had to re-write my question because I was asking the wrong thing before.

I meant to ask, how do you update a uniform in three.js?

like image 378
Kyle Paulsen Avatar asked Apr 28 '15 20:04

Kyle Paulsen


2 Answers

This seems to work:

yourMesh.material.uniforms.yourUniform.value = whatever;
like image 154
Kyle Paulsen Avatar answered Nov 15 '22 18:11

Kyle Paulsen


There are 2 Ways:

  • You can update uniform in Shader Material itself

    OR

  • you can use mesh.material to access the ShaderMaterialand then update the uniform

    Example of both cases:

       var  delta = 0
       var customUniforms = {
        delta: { value: 0 },
        u_time: { value: Date.now() }
       };
    
        // shader material with custom Uniform
        shaderMaterial = new THREE.ShaderMaterial({
          uniforms: customUniforms,
          vertexShader: document.getElementById("vertexShader2").textContent,
          fragmentShader: document.getElementById("fragmentShader2").textContent
        });
    
        // test Mesh object for shader
        var geometry = new THREE.BoxBufferGeometry(10, 10, 10, 10, 10, 10);
        shaderMesh = new THREE.Mesh(geometry, shaderMaterial);
        this.scene.add(shaderMesh);
    

    In animate loop

      animate = () => {
        delta += 0.1; 
    
              // Update uniform in Shader Material
              shaderMaterial.uniforms.delta.value = 0.5 + Math.sin(delta) * 0.0005;
    
              // Update uniform from Mesh itself
         shaderMesh.material.uniforms.u_time.value = delta;
    
    }
    

https://codesandbox.io/s/autumn-http-e9wk5

enter image description here

Complete Example

<body>
    <div id="container"></div>
    <script src="js/three.min.js"></script>
    <script id="vertexShader" type="x-shader/x-vertex">
        void main() {
            gl_Position = vec4( position, 1.0 );
        }
    </script>
    <script id="fragmentShader" type="x-shader/x-fragment">
        uniform vec2 u_resolution;
        uniform float u_time;

        void main() {
            vec2 st = gl_FragCoord.xy/u_resolution.xy;
            gl_FragColor=vec4(st.x,st.y,0.0,1.0);
        }
    </script>
    <script>
        var container;
        var camera, scene, renderer;
        var uniforms;

        init();
        animate();

        function init() {
            container = document.getElementById( 'container' );

            camera = new THREE.Camera();
            camera.position.z = 1;

            scene = new THREE.Scene();

            var geometry = new THREE.PlaneBufferGeometry( 2, 2 );

            uniforms = {
                u_time: { type: "f", value: 1.0 },
                u_resolution: { type: "v2", value: new THREE.Vector2() },
                u_mouse: { type: "v2", value: new THREE.Vector2() }
            };

            var material = new THREE.ShaderMaterial( {
                uniforms: uniforms,
                vertexShader: document.getElementById( 'vertexShader' ).textContent,
                fragmentShader: document.getElementById( 'fragmentShader' ).textContent
            } );

            var mesh = new THREE.Mesh( geometry, material );
            scene.add( mesh );

            renderer = new THREE.WebGLRenderer();
            renderer.setPixelRatio( window.devicePixelRatio );

            container.appendChild( renderer.domElement );

            onWindowResize();
            window.addEventListener( 'resize', onWindowResize, false );

            document.onmousemove = function(e){
              uniforms.u_mouse.value.x = e.pageX
              uniforms.u_mouse.value.y = e.pageY
            }
        }

        function onWindowResize( event ) {
            renderer.setSize( window.innerWidth, window.innerHeight );
            uniforms.u_resolution.value.x = renderer.domElement.width;
            uniforms.u_resolution.value.y = renderer.domElement.height;
        }

        function animate() {
            requestAnimationFrame( animate );
            render();
        }

        function render() {
            uniforms.u_time.value += 0.05;
            renderer.render( scene, camera );
        }
    </script>
</body>
like image 40
Hitesh Sahu Avatar answered Nov 15 '22 19:11

Hitesh Sahu