3

How can I handle big numbers, such as the one below, in GLSL?

I am supplying a shader with Date.now() as a uniform, which is described as:

The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. — MDN

For example, 1514678400000 being the last day of the year.

Passing this value to my ShaderMaterial results in not much happening, unless I scale the value down by a lot.

Specifically, this is the part where the behavior seems to diverge from my expectation of it mapping the last 2500 ms to a value ranging from 0–1:

JavaScript: ( Date.now() % 2500 ) / 2500

GLSL: mod( time, 2500.0 ) / 2500.0

I would prefer to do these calculations on the GPU instead, but am unsure as to how I should approach this?

Below is a small scene which illustrates the issue:

const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 )
const renderer = new THREE.WebGLRenderer()

renderer.setSize( window.innerWidth, window.innerHeight )
camera.position.z = 0.5

document.body.appendChild( renderer.domElement )

const checkbox = document.getElementById( "toggle" )

const geo = new THREE.PlaneGeometry( 1, 1 )
const mat = new THREE.ShaderMaterial({
  vertexShader: `
    void main() {
      gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
    }
  `,
  fragmentShader: `
    uniform bool check;
    uniform float time;
    
    const float TAU = 6.2831;
    
    void main() {
      float fColor;
      
      check 
        ? fColor = ( sin( ( time * TAU ) ) + 1.0 ) / 2.0
        : fColor = ( sin( ( ( mod( time, 2500.0 ) / 2500.0 ) * TAU ) ) + 1.0 ) / 2.0;
        
      gl_FragColor = vec4( 1.0, fColor, 1.0, 1.0 );
    }
  `,
  uniforms: {
    "check": { value: false },
    "time": { value: 1.0 },
  },
})

const plane = new THREE.Mesh( geo, mat )
scene.add( plane )

const animate = function() {
  requestAnimationFrame( animate )

  if ( checkbox.checked ) {
    plane.material.uniforms.check.value = true
    plane.material.uniforms.time.value = ( Date.now() % 2500 ) / 2500
  } else {
    plane.material.uniforms.check.value = false
    plane.material.uniforms.time.value = Date.now()
  }

  renderer.render( scene, camera )
}

animate()
body {
  margin: 0;
}

canvas {
  width: 100%;
  height: 100%;
}

#config {
  position: absolute;
  color: #fff;
  cursor: pointer;
  user-select: none;
  font: bold 1em/1.5 sans-serif;
  padding: 1em;
}

#config label,
#config input {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.js"></script>
<div id="config">
  <input id="toggle" type="checkbox">
  <label for="toggle">Use JavaScript</label>
</div>
Jason
  • 4,905
  • 1
  • 30
  • 38

1 Answers1

3

I am supplying a shader with Date.now()

It produces unpredictable things in shaders, especially when you use trigonometrical functions, like sin(), cos() and so on. The reason is that you pass milliseconds as a big integer number. That's why you have to divide it by 1000 to make them a float number, i.e. transform milliseconds to seconds.

So, dividing is one appoach. Another one is to use THREE.Clock() and its .getDelta() method. Have a look at the code snippet.

const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 )
const renderer = new THREE.WebGLRenderer()

renderer.setSize( window.innerWidth, window.innerHeight )
camera.position.z = 0.5

document.body.appendChild( renderer.domElement )


const geo = new THREE.PlaneGeometry( 1, 1 )
const mat = new THREE.ShaderMaterial({
  vertexShader: `
    void main() {
      gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
    }
  `,
  fragmentShader: `
    uniform bool check;
    uniform float time;
    
    const float TAU = 6.2831;
    
    void main() {
      float fColor;
      
      fColor = ( sin( ( time * TAU ) ) + 1.0 ) / 2.0;
        
      gl_FragColor = vec4( 1.0, fColor, 1.0, 1.0 );
    }
  `,
  uniforms: {
    "check": { value: false },
    "time": { value: 1.0 },
  },
})

const plane = new THREE.Mesh( geo, mat )
scene.add( plane )

var clock = new THREE.Clock();
var time = 0;

const animate = function() {
  requestAnimationFrame( animate )
  
  time += clock.getDelta();

  plane.material.uniforms.time.value = time * 0.4; // make it 2.5 times slower

  renderer.render( scene, camera )
}

animate()
body {
  margin: 0;
}

canvas {
  width: 100%;
  height: 100%;
}

#config {
  position: absolute;
  color: #fff;
  cursor: pointer;
  user-select: none;
  font: bold 1em/1.5 sans-serif;
  padding: 1em;
}

#config label,
#config input {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.js"></script>
prisoner849
  • 16,894
  • 4
  • 34
  • 68