cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Stylized Fire / Evaporating Shader

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <!--

An attempt to try out a technique mentioned by @minionsart and @ciaccodavide on Twitter:
https://twitter.com/minionsart/status/964257071423283200
https://twitter.com/ciaccodavide/status/964407412634472448

-->

<script type="x-shader/x-fragment" id="shader-flame-fragment">
uniform vec3 colorStart;
uniform vec3 colorEnd;
uniform sampler2D textureGradient;
uniform sampler2D textureNoise;
uniform float threshold;

varying vec2 vUv;
	
void main() {
	float gradient = texture2D(textureGradient, vUv).r;
	float noise = texture2D(textureNoise, vUv).r;
	float alpha = gradient * noise;
	
	if (alpha < threshold) discard;
	
	float alphaRender = (1.0 - threshold) * alpha;
	
	vec3 color = mix(colorStart, colorEnd, alphaRender);
	gl_FragColor = vec4(color, 1.0);
}
</script>

<script type="x-shader/x-fragment" id="shader-gradient-fragment">
uniform float power;
varying vec2 vUv;
	
void main() {
	float darkness = pow(1.0 - vUv.y, power);
	
	gl_FragColor = vec4(darkness, darkness, darkness, 1.0);
}
</script>

<script type="x-shader/x-fragment" id="shader-noise-fragment">
uniform int octaves;
uniform float persistence;
uniform float scale;
uniform float speed;
uniform float time;

varying vec2 vUv;

const int OCTAVES_MAX = 16;

float noiseOctaves(int octaves, vec2 position, float persistence, vec2 frequency) {
	if (octaves == 0) return 0.0;
	
	float amplitude = 1.0;
	float amplitudeSum = 0.0;
	float noise = 0.0;
	
	for (int i = 0; i < OCTAVES_MAX; i++) {
		if (i >= octaves) break;
		
		float octave = float(i);
		float t = octave / float(octaves);
		
		noise = noise + pnoise(position * frequency, frequency) * amplitude;
		
		amplitudeSum = amplitudeSum + amplitude;
		amplitude = amplitude * persistence;
		frequency = 2.0 * frequency;
	}
	
	return (1.0 + noise / amplitudeSum) * 0.5;
}
	
	
void main() {
	vec2 position = vec2(0.0, -time) * speed + vUv;
	float noise = noiseOctaves(octaves, position, persistence, vec2(scale, scale));
	
	gl_FragColor = vec4(noise, noise, noise, 1.0);
}
</script>

<script type="x-shader/x-vertex" id="shader-passthrough-vertex">
varying vec2 vUv;

void main() {
	vUv = uv.xy;
	
	gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>

<script type="x-shader/x-fragment" id="shader-texture-fragment">
uniform sampler2D texture;
varying vec2 vUv;
	
void main() {
	gl_FragColor = texture2D(texture, vUv);
}
</script>

<script type="x-shader/x-generic" id="shader-noise-2d">
//
// GLSL textureless classic 2D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author:  Stefan Gustavson (stefan.gustavson@liu.se)
// Version: 2011-08-22
//
// Many thanks to Ian McEwan of Ashima Arts for the
// ideas for permutation and gradient selection.
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/stegu/webgl-noise
//

vec4 mod289(vec4 x)
{
  return x - floor(x * (1.0 / 289.0)) * 289.0;
}

vec4 permute(vec4 x)
{
  return mod289(((x*34.0)+1.0)*x);
}

vec4 taylorInvSqrt(vec4 r)
{
  return 1.79284291400159 - 0.85373472095314 * r;
}

vec2 fade(vec2 t) {
  return t*t*t*(t*(t*6.0-15.0)+10.0);
}

// Classic Perlin noise
float cnoise(vec2 P)
{
  vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
  vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
  Pi = mod289(Pi); // To avoid truncation effects in permutation
  vec4 ix = Pi.xzxz;
  vec4 iy = Pi.yyww;
  vec4 fx = Pf.xzxz;
  vec4 fy = Pf.yyww;

  vec4 i = permute(permute(ix) + iy);

  vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
  vec4 gy = abs(gx) - 0.5 ;
  vec4 tx = floor(gx + 0.5);
  gx = gx - tx;

  vec2 g00 = vec2(gx.x,gy.x);
  vec2 g10 = vec2(gx.y,gy.y);
  vec2 g01 = vec2(gx.z,gy.z);
  vec2 g11 = vec2(gx.w,gy.w);

  vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  g00 *= norm.x;  
  g01 *= norm.y;  
  g10 *= norm.z;  
  g11 *= norm.w;  

  float n00 = dot(g00, vec2(fx.x, fy.x));
  float n10 = dot(g10, vec2(fx.y, fy.y));
  float n01 = dot(g01, vec2(fx.z, fy.z));
  float n11 = dot(g11, vec2(fx.w, fy.w));

  vec2 fade_xy = fade(Pf.xy);
  vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
  float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
  return 2.3 * n_xy;
}

// Classic Perlin noise, periodic variant
float pnoise(vec2 P, vec2 rep)
{
  vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
  vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
  Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
  Pi = mod289(Pi);        // To avoid truncation effects in permutation
  vec4 ix = Pi.xzxz;
  vec4 iy = Pi.yyww;
  vec4 fx = Pf.xzxz;
  vec4 fy = Pf.yyww;

  vec4 i = permute(permute(ix) + iy);

  vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
  vec4 gy = abs(gx) - 0.5 ;
  vec4 tx = floor(gx + 0.5);
  gx = gx - tx;

  vec2 g00 = vec2(gx.x,gy.x);
  vec2 g10 = vec2(gx.y,gy.y);
  vec2 g01 = vec2(gx.z,gy.z);
  vec2 g11 = vec2(gx.w,gy.w);

  vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  g00 *= norm.x;  
  g01 *= norm.y;  
  g10 *= norm.z;  
  g11 *= norm.w;  

  float n00 = dot(g00, vec2(fx.x, fy.x));
  float n10 = dot(g10, vec2(fx.y, fy.y));
  float n01 = dot(g01, vec2(fx.z, fy.z));
  float n11 = dot(g11, vec2(fx.w, fy.w));

  vec2 fade_xy = fade(Pf.xy);
  vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
  float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
  return 2.3 * n_xy;
}
</script>
            
          
!
            
              class ShaderChunkLibrary {
	static get flameFragment() {
		return document.getElementById('shader-flame-fragment').textContent;
	}
	
	static get noise2DPeriodic() {
		return document.getElementById('shader-noise-2d').textContent;
	}
	
	static get gradientFragment() {
		return document.getElementById('shader-gradient-fragment').textContent;
	}
	
	static get noiseFragment() {
		return ShaderChunkLibrary.noise2DPeriodic + document.getElementById('shader-noise-fragment').textContent;
	}
	
	static get passThroughVertex() {
		return document.getElementById('shader-passthrough-vertex').textContent;
	}
	
	static get textureFragment() {
		return document.getElementById('shader-texture-fragment').textContent;
	}
}

class BufferRenderer {
	constructor(material) {
		this.camera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5, 1, 3);
		this.camera.position.z = 2;
		
		this.material = material;
		this.plane = BufferRenderer.createPlane(material);

		this.scene = new THREE.Scene();
		this.scene.add(this.plane);
	}
	
	render(renderer, renderTarget) {
		renderer.render(this.scene, this.camera, renderTarget);
	}
	
	static createPlane(material) {
		const geometry = new THREE.PlaneBufferGeometry(1, 1, 1, 1);
		return new THREE.Mesh(geometry, material);
	}
}

class Application {
	static get RENDERER_OPTIONS() {
		return {
			antialias: true,
		};
	}
	
	constructor() {
		this.bufferRenderers = Application.createBufferRenderers();
		
		this.buffers = new Array(3).fill().map(_ => new THREE.WebGLRenderTarget(1024, 1024, {
			wrapS: THREE.RepeatWrapping,
			wrapT: THREE.RepeatWrapping,
		}));
		
		this.cylinder = Application.createCylinder();
		
		this.planes = Application.createPlanes(32, 8);
		
		this.camera = new THREE.PerspectiveCamera(45, 1, 1, 512);
		this.camera.position.z = 256;
		
		this.renderer = new THREE.WebGLRenderer(Application.RENDERER_OPTIONS);
		
		this.scene = new THREE.Scene();
		this.scene.add(this.cylinder, ...Object.values(this.planes));
		
		this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
		this.controls.update();
	}
	
	setSize(width, height) {
		this.camera.aspect = width / height;
		this.camera.updateProjectionMatrix();
		
		this.renderer.setSize(width, height);
	}
	
	render(time) {
		this.controls.update();
		
		this.bufferRenderers.gradient.render(this.renderer, this.buffers[0]);
		
		this.bufferRenderers.noise.material.uniforms.time.value = time;
		this.bufferRenderers.noise.render(this.renderer, this.buffers[1]);
		
		this.planes.gradient.material.uniforms.texture.value = this.buffers[0].texture;
		this.planes.noise.material.uniforms.texture.value = this.buffers[1].texture;
		
		this.cylinder.material.uniforms.textureGradient.value = this.buffers[0].texture;
		this.cylinder.material.uniforms.textureNoise.value = this.buffers[1].texture;
		
		this.renderer.render(this.scene, this.camera);
	}
	
	static createBuffer(size) {
		return THREE.WebGLRenderTarget(size, size, {
			wrapS: THREE.RepeatWrapping,
			wrapT: THREE.RepeatWrapping,
		});
	}
	
	static createBufferRenderers() {
		const materialBasic = new THREE.MeshBasicMaterial();
		
		const materialGradient = new THREE.ShaderMaterial({
			fragmentShader: ShaderChunkLibrary.gradientFragment,
			vertexShader: ShaderChunkLibrary.passThroughVertex,
			uniforms: {
				power: { value: 1 },
			},
		});
		
		const materialNoise = new THREE.ShaderMaterial({
			fragmentShader: ShaderChunkLibrary.noiseFragment,
			vertexShader: ShaderChunkLibrary.passThroughVertex,
			uniforms: {
				octaves: { value: 1 },
				persistence: { value: 1 },
				scale: { value: 1 },
				speed: { value: 1 },
				time: { value: 0 },
			}
		});
		
		return {
			gradient: new BufferRenderer(materialGradient),
			noise: new BufferRenderer(materialNoise),
		};
	}
	
	static createCylinder() {
		const geometry = new THREE.CylinderBufferGeometry(16, 16, 64, 16, 1, true);
		const material = new THREE.ShaderMaterial({
			blending: THREE.AdditiveBlending,
			depthTest: false,
			fragmentShader: ShaderChunkLibrary.flameFragment,
			vertexShader: ShaderChunkLibrary.passThroughVertex,
			side: THREE.DoubleSide,
			transparent: true,
			uniforms: {
				colorStart: { value: null },
				colorEnd: { value: null },
				textureGradient: { value: null },
				textureNoise: { value: null },
				threshold: { value: 0 }
			},
		});
		
		return new THREE.Mesh(geometry, material);
	}
	
	static createPlanes(size, spacing = 0) {
		const createMaterial = () => new THREE.ShaderMaterial({
			fragmentShader: ShaderChunkLibrary.textureFragment,
			vertexShader: ShaderChunkLibrary.passThroughVertex,
			side: THREE.DoubleSide,
			uniforms: {
				texture: { value: null },
			},
		});
		
		const planes = {
			noise: Application.createTexturePlane(size, createMaterial()),
			gradient: Application.createTexturePlane(size, createMaterial()),
		};
		
		Object.values(planes).forEach((p, i, arr) => p.position.set(((i + 0.5) - (arr.length * 0.5)) * (size + spacing), size, 0));
		
		return planes;
	}
	
	static createTexturePlane(size, material) {
		const geometry = new THREE.PlaneBufferGeometry(size, size, 1, 1);
		
		return new THREE.Mesh(geometry, material);
	}
}

const animate = (callback) => {
	const update = (time) => {
		requestAnimationFrame(update);
		callback(time / 1000);
	};
	
	update(performance.now());
};

const createUpdateSettingsCallback = (options) => () => {
	app.cylinder.material.uniforms.colorStart.value = new THREE.Color(options.composition.colorStart);
	app.cylinder.material.uniforms.colorEnd.value = new THREE.Color(options.composition.colorEnd);
	app.cylinder.material.uniforms.threshold.value = options.composition.threshold;
	
	app.bufferRenderers.gradient.material.uniforms.power.value = options.gradient.power;
	
	app.bufferRenderers.noise.material.uniforms.octaves.value = options.noise.octaves;
	app.bufferRenderers.noise.material.uniforms.persistence.value = options.noise.persistence;
	app.bufferRenderers.noise.material.uniforms.scale.value = options.noise.scale;
	app.bufferRenderers.noise.material.uniforms.speed.value = options.noise.speed;
};

const app = new Application();
app.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(app.renderer.domElement);

const gui = new dat.GUI();
const guiOptions = {
	composition: {
		colorStart: '#F00',
		colorEnd: '#FF0',
		threshold: 0.25,
	},
	gradient: {
		power: 1.5,
	},
	noise: {
		octaves: 8,
		persistence: 0.5,
		scale: 16,
		speed: 0.75,
	},
};

const updateSettings = createUpdateSettingsCallback(guiOptions);

const guiComposition = gui.addFolder('Composition');
guiComposition.addColor(guiOptions.composition, 'colorStart').onChange(updateSettings);
guiComposition.addColor(guiOptions.composition, 'colorEnd').onChange(updateSettings);
guiComposition.add(guiOptions.composition, 'threshold', 0, 1, 0.05).onChange(updateSettings);

const guiGradient = gui.addFolder('Gradient');
guiGradient.add(guiOptions.gradient, 'power', 0, 3, 0.25).onChange(updateSettings);

const guiNoise = gui.addFolder('Noise');
guiNoise.add(guiOptions.noise, 'octaves', 1, 16, 1).onChange(updateSettings);
guiNoise.add(guiOptions.noise, 'persistence', 0, 1, 0.05).onChange(updateSettings);
guiNoise.add(guiOptions.noise, 'scale', 1, 16, 1).onChange(updateSettings);
guiNoise.add(guiOptions.noise, 'speed', -2, 2, 0.1).onChange(updateSettings);

guiComposition.open();
guiGradient.open();
guiNoise.open();

window.addEventListener('resize', () => app.setSize(window.innerWidth, window.innerHeight));

updateSettings();
animate(time => app.render(time));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console