[
  {
    "path": "Fire.js",
    "content": "/**\n * @author mattatz / http://github.com/mattatz\n *\n * Ray tracing based real-time procedural volumetric fire object for three.js\n */\n\nTHREE.Fire = function ( fireTex, color ) {\n\n\tvar fireMaterial = new THREE.ShaderMaterial( {\n        defines         : THREE.FireShader.defines,\n        uniforms        : THREE.UniformsUtils.clone( THREE.FireShader.uniforms ),\n        vertexShader    : THREE.FireShader.vertexShader,\n        fragmentShader  : THREE.FireShader.fragmentShader,\n\t\ttransparent     : true,\n\t\tdepthWrite      : false,\n        depthTest       : false\n\t} );\n\n    // initialize uniforms \n\n    fireTex.magFilter = fireTex.minFilter = THREE.LinearFilter;\n    fireTex.wrapS = fireTex.wrapT = THREE.ClampToEdgeWrapping;\n    \n    fireMaterial.uniforms.fireTex.value = fireTex;\n    fireMaterial.uniforms.color.value = color || new THREE.Color( 0xeeeeee );\n    fireMaterial.uniforms.invModelMatrix.value = new THREE.Matrix4();\n    fireMaterial.uniforms.scale.value = new THREE.Vector3( 1, 1, 1 );\n    fireMaterial.uniforms.seed.value = Math.random() * 19.19;\n\n\tTHREE.Mesh.call( this, new THREE.BoxGeometry( 1.0, 1.0, 1.0 ), fireMaterial );\n};\n\nTHREE.Fire.prototype = Object.create( THREE.Mesh.prototype );\nTHREE.Fire.prototype.constructor = THREE.Fire;\n\nTHREE.Fire.prototype.update = function ( time ) {\n\n    var invModelMatrix = this.material.uniforms.invModelMatrix.value;\n\n    this.updateMatrixWorld();\n    invModelMatrix.getInverse( this.matrixWorld );\n\n    if( time !== undefined ) {\n        this.material.uniforms.time.value = time;\n    }\n\n    this.material.uniforms.invModelMatrix.value = invModelMatrix;\n\n    this.material.uniforms.scale.value = this.scale;\n\n};\n"
  },
  {
    "path": "FireShader.js",
    "content": "/**\n * @author mattatz / http://mattatz.github.io\n *\n * Ray tracing based real-time procedural volumetric fire shader.\n *\n * Based on \n * Alfred et al. Real-Time procedural volumetric fire / http://dl.acm.org/citation.cfm?id=1230131\n * and \n * webgl-noise / https://github.com/ashima/webgl-noise/blob/master/src/noise3D.glsl\n * and\n * primitive: blog | object space raymarching / https://github.com/ashima/webgl-noise/blob/master/src/noise3D.glsl\n */\n\nTHREE.FireShader = {\n\n    defines: {\n        \"ITERATIONS\"    : \"20\",\n        \"OCTIVES\"       : \"3\"\n    },\n\n    uniforms: {\n        \"fireTex\"       : { type : \"t\",     value : null },\n        \"color\"         : { type : \"c\",     value : null },\n        \"time\"          : { type : \"f\",     value : 0.0 },\n        \"seed\"          : { type : \"f\",     value : 0.0 },\n        \"invModelMatrix\": { type : \"m4\",    value : null },\n        \"scale\"         : { type : \"v3\",    value : null },\n\n        \"noiseScale\"    : { type : \"v4\",    value : new THREE.Vector4(1, 2, 1, 0.3) },\n        \"magnitude\"     : { type : \"f\",     value : 1.3 },\n        \"lacunarity\"    : { type : \"f\",     value : 2.0 },\n        \"gain\"          : { type : \"f\",     value : 0.5 }\n    },\n\n    vertexShader: [\n        \"varying vec3 vWorldPos;\",\n        \"void main() {\",\n            \"gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\",\n            \"vWorldPos = (modelMatrix * vec4(position, 1.0)).xyz;\",\n        \"}\"\n    ].join(\"\\n\"),\n\n    fragmentShader: [\n        \"uniform vec3 color;\",\n        \"uniform float time;\",\n        \"uniform float seed;\",\n        \"uniform mat4 invModelMatrix;\",\n        \"uniform vec3 scale;\",\n\n        \"uniform vec4 noiseScale;\",\n        \"uniform float magnitude;\",\n        \"uniform float lacunarity;\",\n        \"uniform float gain;\",\n\n        \"uniform sampler2D fireTex;\",\n\n        \"varying vec3 vWorldPos;\",\n\n        // GLSL simplex noise function by ashima / https://github.com/ashima/webgl-noise/blob/master/src/noise3D.glsl\n        // -------- simplex noise\n        \"vec3 mod289(vec3 x) {\",\n            \"return x - floor(x * (1.0 / 289.0)) * 289.0;\",\n        \"}\",\n\n        \"vec4 mod289(vec4 x) {\",\n            \"return x - floor(x * (1.0 / 289.0)) * 289.0;\",\n        \"}\",\n\n        \"vec4 permute(vec4 x) {\",\n            \"return mod289(((x * 34.0) + 1.0) * x);\",\n        \"}\",\n\n        \"vec4 taylorInvSqrt(vec4 r) {\",\n            \"return 1.79284291400159 - 0.85373472095314 * r;\",\n        \"}\",\n\n        \"float snoise(vec3 v) {\",\n            \"const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);\",\n            \"const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);\",\n\n            // First corner\n            \"vec3 i  = floor(v + dot(v, C.yyy));\",\n            \"vec3 x0 = v - i + dot(i, C.xxx);\",\n\n            // Other corners\n            \"vec3 g = step(x0.yzx, x0.xyz);\",\n            \"vec3 l = 1.0 - g;\",\n            \"vec3 i1 = min(g.xyz, l.zxy);\",\n            \"vec3 i2 = max(g.xyz, l.zxy);\",\n\n            //   x0 = x0 - 0.0 + 0.0 * C.xxx;\n            //   x1 = x0 - i1  + 1.0 * C.xxx;\n            //   x2 = x0 - i2  + 2.0 * C.xxx;\n            //   x3 = x0 - 1.0 + 3.0 * C.xxx;\n            \"vec3 x1 = x0 - i1 + C.xxx;\",\n            \"vec3 x2 = x0 - i2 + C.yyy;\", // 2.0*C.x = 1/3 = C.y\n            \"vec3 x3 = x0 - D.yyy;\",      // -1.0+3.0*C.x = -0.5 = -D.y\n\n            // Permutations\n            \"i = mod289(i); \",\n            \"vec4 p = permute(permute(permute( \",\n                    \"i.z + vec4(0.0, i1.z, i2.z, 1.0))\",\n                    \"+ i.y + vec4(0.0, i1.y, i2.y, 1.0)) \",\n                    \"+ i.x + vec4(0.0, i1.x, i2.x, 1.0));\",\n\n            // Gradients: 7x7 points over a square, mapped onto an octahedron.\n            // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)\n            \"float n_ = 0.142857142857;\", // 1.0/7.0\n            \"vec3  ns = n_ * D.wyz - D.xzx;\",\n\n            \"vec4 j = p - 49.0 * floor(p * ns.z * ns.z);\", //  mod(p,7*7)\n\n            \"vec4 x_ = floor(j * ns.z);\",\n            \"vec4 y_ = floor(j - 7.0 * x_);\", // mod(j,N)\n\n            \"vec4 x = x_ * ns.x + ns.yyyy;\",\n            \"vec4 y = y_ * ns.x + ns.yyyy;\",\n            \"vec4 h = 1.0 - abs(x) - abs(y);\",\n\n            \"vec4 b0 = vec4(x.xy, y.xy);\",\n            \"vec4 b1 = vec4(x.zw, y.zw);\",\n\n            //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;\n            //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;\n            \"vec4 s0 = floor(b0) * 2.0 + 1.0;\",\n            \"vec4 s1 = floor(b1) * 2.0 + 1.0;\",\n            \"vec4 sh = -step(h, vec4(0.0));\",\n\n            \"vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;\",\n            \"vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;\",\n\n            \"vec3 p0 = vec3(a0.xy, h.x);\",\n            \"vec3 p1 = vec3(a0.zw, h.y);\",\n            \"vec3 p2 = vec3(a1.xy, h.z);\",\n            \"vec3 p3 = vec3(a1.zw, h.w);\",\n\n            //Normalise gradients\n            \"vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));\",\n            \"p0 *= norm.x;\",\n            \"p1 *= norm.y;\",\n            \"p2 *= norm.z;\",\n            \"p3 *= norm.w;\",\n\n            // Mix final noise value\n            \"vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);\",\n            \"m = m * m;\",\n            \"return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));\",\n        \"}\",\n        // simplex noise --------\n\n        \"float turbulence(vec3 p) {\",\n            \"float sum = 0.0;\",\n            \"float freq = 1.0;\",\n            \"float amp = 1.0;\",\n            \n            \"for(int i = 0; i < OCTIVES; i++) {\",\n                \"sum += abs(snoise(p * freq)) * amp;\",\n                \"freq *= lacunarity;\",\n                \"amp *= gain;\",\n            \"}\",\n\n            \"return sum;\",\n        \"}\",\n\n        \"vec4 samplerFire (vec3 p, vec4 scale) {\",\n            \"vec2 st = vec2(sqrt(dot(p.xz, p.xz)), p.y);\",\n\n            \"if(st.x <= 0.0 || st.x >= 1.0 || st.y <= 0.0 || st.y >= 1.0) return vec4(0.0);\",\n\n            \"p.y -= (seed + time) * scale.w;\",\n            \"p *= scale.xyz;\",\n\n            \"st.y += sqrt(st.y) * magnitude * turbulence(p);\",\n\n            \"if(st.y <= 0.0 || st.y >= 1.0) return vec4(0.0);\",\n           \n            \"return texture2D(fireTex, st);\",\n        \"}\",\n\n        \"vec3 localize(vec3 p) {\",\n            \"return (invModelMatrix * vec4(p, 1.0)).xyz;\",\n        \"}\",\n\n        \"void main() {\",\n            \"vec3 rayPos = vWorldPos;\",\n            \"vec3 rayDir = normalize(rayPos - cameraPosition);\",\n            \"float rayLen = 0.0288 * length(scale.xyz);\",\n\n            \"vec4 col = vec4(0.0);\",\n\n            \"for(int i = 0; i < ITERATIONS; i++) {\",\n                \"rayPos += rayDir * rayLen;\",\n\n                \"vec3 lp = localize(rayPos);\",\n\n                \"lp.y += 0.5;\",\n                \"lp.xz *= 2.0;\",\n                \"col += samplerFire(lp, noiseScale);\",\n            \"}\",\n\n            \"col.a = col.r;\",\n\n            \"gl_FragColor = col;\",\n        \"}\",\n\n\t].join(\"\\n\")\n\n};\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2015 mattatz\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n"
  },
  {
    "path": "README.md",
    "content": "THREE.Fire\n=====================\n\nRay tracing based [real-time procedural volumetric fire](http://dl.acm.org/citation.cfm?id=1230131) object for three.js.\n\n![fire](https://raw.githubusercontent.com/mattatz/THREE.Fire/master/Captures/fire.gif)\n\n\n## Example\n\n```javascript\n\nvar scene = new THREE.Scene();\n\nvar textureLoader = new THREE.TextureLoader();\nvar tex = textureLoader.load(\"Fire.png\");\nvar fire = new THREE.Fire( tex );\n\nscene.add( fire );\n\nfunction animate() {\n  requestAnimationFrame( animate );\n\n  fire.update(performance.now() / 1000);\n  renderer.render( scene, camera );\n}\nanimate();\n\n```\n\n## Description\n\nTHREE.Fire is extended THREE.Mesh object.\nA geometry of THREE.Fire is THREE.BoxGeometry and fire shapes are generated by noise and a fire texture.\n\n![wireframe](https://raw.githubusercontent.com/mattatz/THREE.Fire/master/Captures/wireframe.gif \"Visualization geometry of THREE.Fire with wireframe\")\n\nYou must pass a fire texture argument to THREE.Fire like this.\n![firetex](https://raw.githubusercontent.com/mattatz/THREE.Fire/master/Fire.png \"Fire texture\")\n\n[for more details.](http://dl.acm.org/citation.cfm?id=1230131)\n\n## Demo\n\n[Demo](https://mattatz.github.io/THREE.Fire)\n\n## Sources\n\n- Real-Time procedural volumetric fire - http://dl.acm.org/citation.cfm?id=1230131\n\n- webgl-noise - https://github.com/ashima/webgl-noise/blob/master/src/noise3D.glsl\n\n- primitive: blog | object space raymarching - http://i-saint.hatenablog.com/entry/2015/08/24/225254\n\n"
  }
]