JAVASCRIPT   15

Fire.js

Guest on 29th July 2021 03:13:50 PM

  1. /**
  2.  * @author mattatz / http://github.com/mattatz
  3.  *
  4.  * Ray tracing based real-time procedural volumetric fire object for three.js
  5.  */
  6.  
  7. THREE.Fire = function ( fireTex, color ) {
  8.  
  9.         var fireMaterial = new THREE.ShaderMaterial( {
  10.         defines         : THREE.FireShader.defines,
  11.         uniforms        : THREE.UniformsUtils.clone( THREE.FireShader.uniforms ),
  12.         vertexShader    : THREE.FireShader.vertexShader,
  13.         fragmentShader  : THREE.FireShader.fragmentShader,
  14.                 transparent     : true,
  15.                 depthWrite      : false,
  16.         depthTest       : false
  17.         } );
  18.  
  19.     // initialize uniforms
  20.  
  21.     fireTex.magFilter = fireTex.minFilter = THREE.LinearFilter;
  22.     fireTex.wrapS = fireTex.wrapT = THREE.ClampToEdgeWrapping;
  23.    
  24.     fireMaterial.uniforms.fireTex.value = fireTex;
  25.     fireMaterial.uniforms.color.value = color || new THREE.Color( 0xeeeeee );
  26.     fireMaterial.uniforms.invModelMatrix.value = new THREE.Matrix4();
  27.     fireMaterial.uniforms.scale.value = new THREE.Vector3( 1, 1, 1 );
  28.     fireMaterial.uniforms.seed.value = Math.random() * 19.19;
  29.  
  30.         THREE.Mesh.call( this, new THREE.BoxGeometry( 1.0, 1.0, 1.0 ), fireMaterial );
  31. };
  32.  
  33. THREE.Fire.prototype = Object.create( THREE.Mesh.prototype );
  34. THREE.Fire.prototype.constructor = THREE.Fire;
  35.  
  36. THREE.Fire.prototype.update = function ( time ) {
  37.  
  38.     var invModelMatrix = this.material.uniforms.invModelMatrix.value;
  39.  
  40.     this.updateMatrixWorld();
  41.     invModelMatrix.getInverse( this.matrixWorld );
  42.  
  43.     if( time !== undefined ) {
  44.         this.material.uniforms.time.value = time;
  45.     }
  46.  
  47.     this.material.uniforms.invModelMatrix.value = invModelMatrix;
  48.  
  49.     this.material.uniforms.scale.value = this.scale;
  50.  
  51. };

Raw Paste


Login or Register to edit or fork this paste. It's free.