Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script type='x-shader/x-vertex' id='vertexShader'>
//	Simplex 4D Noise 
//	by Ian McEwan, Ashima Arts
//
vec4 permute(vec4 x){return mod(((x*34.0)+1.0)*x, 289.0);}
float permute(float x){return floor(mod(((x*34.0)+1.0)*x, 289.0));}
vec4 taylorInvSqrt(vec4 r){return 1.79284291400159 - 0.85373472095314 * r;}
float taylorInvSqrt(float r){return 1.79284291400159 - 0.85373472095314 * r;}

vec4 grad4(float j, vec4 ip){
  const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
  vec4 p,s;

  p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
  p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
  s = vec4(lessThan(p, vec4(0.0)));
  p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www; 

  return p;
}

float snoise(vec4 v){
  const vec2  C = vec2( 0.138196601125010504,  // (5 - sqrt(5))/20  G4
                        0.309016994374947451); // (sqrt(5) - 1)/4   F4
// First corner
  vec4 i  = floor(v + dot(v, C.yyyy) );
  vec4 x0 = v -   i + dot(i, C.xxxx);

// Other corners

// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
  vec4 i0;

  vec3 isX = step( x0.yzw, x0.xxx );
  vec3 isYZ = step( x0.zww, x0.yyz );
//  i0.x = dot( isX, vec3( 1.0 ) );
  i0.x = isX.x + isX.y + isX.z;
  i0.yzw = 1.0 - isX;

//  i0.y += dot( isYZ.xy, vec2( 1.0 ) );
  i0.y += isYZ.x + isYZ.y;
  i0.zw += 1.0 - isYZ.xy;

  i0.z += isYZ.z;
  i0.w += 1.0 - isYZ.z;

  // i0 now contains the unique values 0,1,2,3 in each channel
  vec4 i3 = clamp( i0, 0.0, 1.0 );
  vec4 i2 = clamp( i0-1.0, 0.0, 1.0 );
  vec4 i1 = clamp( i0-2.0, 0.0, 1.0 );

  //  x0 = x0 - 0.0 + 0.0 * C 
  vec4 x1 = x0 - i1 + 1.0 * C.xxxx;
  vec4 x2 = x0 - i2 + 2.0 * C.xxxx;
  vec4 x3 = x0 - i3 + 3.0 * C.xxxx;
  vec4 x4 = x0 - 1.0 + 4.0 * C.xxxx;

// Permutations
  i = mod(i, 289.0); 
  float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
  vec4 j1 = permute( permute( permute( permute (
             i.w + vec4(i1.w, i2.w, i3.w, 1.0 ))
           + i.z + vec4(i1.z, i2.z, i3.z, 1.0 ))
           + i.y + vec4(i1.y, i2.y, i3.y, 1.0 ))
           + i.x + vec4(i1.x, i2.x, i3.x, 1.0 ));
// Gradients
// ( 7*7*6 points uniformly over a cube, mapped onto a 4-octahedron.)
// 7*7*6 = 294, which is close to the ring size 17*17 = 289.

  vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;

  vec4 p0 = grad4(j0,   ip);
  vec4 p1 = grad4(j1.x, ip);
  vec4 p2 = grad4(j1.y, ip);
  vec4 p3 = grad4(j1.z, ip);
  vec4 p4 = grad4(j1.w, ip);

// Normalise gradients
  vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
  p0 *= norm.x;
  p1 *= norm.y;
  p2 *= norm.z;
  p3 *= norm.w;
  p4 *= taylorInvSqrt(dot(p4,p4));

// Mix contributions from the five corners
  vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
  vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4)            ), 0.0);
  m0 = m0 * m0;
  m1 = m1 * m1;
  return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
               + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;

}
	
float map(float value, float min1, float max1, float min2, float max2) {
  return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}
	
varying float vNoise;
varying vec3 vNormal;
varying vec3 vViewPosition;
varying vec2 vUv;
varying vec3 vertex;
	
void main() {
	vUv = uv;
	vertex = position;
	// vNoise = snoise(vec4(position*0.5, 0.));
	// vNoise = map(vNoise,-1.,1.,0.,1.);
	vec4 modelViewPosition = modelViewMatrix * vec4( position, 1.0 );
	vViewPosition = -modelViewPosition.xyz;
	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
	vNormal = normalMatrix * normal;
}
</script>

<script type='x-shader/x-fragment' id='fragmentShader'>	
  // Pick a coordinate to visualize in a grid
  vec2 coord = vertex.xy;

  // Compute anti-aliased world-space grid lines
  vec2 grid = abs(fract(coord - 0.5) - 0.5) / fwidth(coord);
  float line = min(grid.x, grid.y);
	
	vec4 diffuseColor = vec4(vec3(1.), 1. + uThreshold - min(line, 1.0));
</script>

<script type='x-shader/x-vertex' id='fragmentShaderBeforeMain'>
	#extension GL_OES_standard_derivatives : enable
	uniform vec3 uColor;
	varying vec3 vertex;
	uniform float uThreshold;
</script>
              
            
!

CSS

              
                canvas {
	display:block
}
              
            
!

JS

              
                //main app
class App {
	constructor() {
		this.time = 0;
		this.clock = new THREE.Clock();
		this.init();
		window.addEventListener("resize", this.onWindowResize.bind(this), false);
	}

	init() {
		// renderer
		this.renderer = new THREE.WebGLRenderer({ antialias: true });
		this.renderer.setSize(window.innerWidth, window.innerHeight);
		this.renderer.setPixelRatio = window.devicePixelRatio;
		document.body.appendChild(this.renderer.domElement);

		// scene
		this.scene = new THREE.Scene();

		// camera
		this.camera = new THREE.PerspectiveCamera(
			40,
			window.innerWidth / window.innerHeight,
			1,
			10000
		);
		this.camera.position.set(30, 0, 30);

		// controls
		this.controls = new THREE.OrbitControls(
			this.camera,
			document.querySelector("canvas")
		);
		this.controls.enabled = true;
		this.controls.enablePan = false;

		// ambient light
		this.scene.add(new THREE.AmbientLight(0x222222));

		// directional light
		let lightTop = new THREE.DirectionalLight(0xffffff, 0.7);
		lightTop.position.set(0, 500, 200);
		lightTop.castShadow = true;
		this.scene.add(lightTop);

		let lightBottom = new THREE.DirectionalLight(0xffffff, 0.25);
		lightBottom.position.set(0, -500, 400);
		lightBottom.castShadow = true;
		this.scene.add(lightBottom);

		let ambientLight = new THREE.AmbientLight(0x798296);
		this.scene.add(ambientLight);

		// axes
		// this.scene.add(new THREE.AxesHelper(20));

		// let geometry = new THREE.BoxGeometry(10, 10, 10);
		// let material = new THREE.ShaderMaterial({
		// 	uniforms: {},
		// 	vertexShader: document.querySelector("#vertexShader").textContent,
		// 	fragmentShader: document.querySelector("#fragmentShader").textContent
		// });
		// let cube = new THREE.Mesh(geometry, material);
		// this.scene.add(cube);

		// loadModel({url:'https://rocheclement.fr/public/models/WaltHead.obj'})
		// .then((model)=>{
		// 	this.model = model.media
		// 	this.scene.add(this.model)
		// })
		
		this.addModel()

		this.load().then(assets => {
			this.addPostProcessing(assets);
			this.isPostProcessingEnabled = true;

			this.addGUI();
		});

		//animation loop
		this.renderer.setAnimationLoop(this.render.bind(this));
	}
	
	addModel() {
		let phongShader = THREE.ShaderLib.phong;

		let fragmentShader = phongShader.fragmentShader;
		fragmentShader =
			document.querySelector("#fragmentShaderBeforeMain").textContent +
			fragmentShader.replace(
				"vec4 diffuseColor = vec4( diffuse, opacity );",
				document.querySelector("#fragmentShader").textContent
			);
		let material = new THREE.ShaderMaterial({
			uniforms: THREE.UniformsUtils.merge([
				phongShader.uniforms,
				{
					uThreshold: {
						value: 0
					},
				}
			]),
			vertexShader: document.querySelector("#vertexShader").textContent,
			fragmentShader: fragmentShader,
			lights: true,
			transparent: true,
			side: THREE.FrontSide,
			precison: 'highp',
			// wireframe: true
			depthWrite: false,
			depthTest: false,
			opacity: 1.
		})
		
		return new Promise((resolve, reject) => {
			loadModel({
				id: "head",
				url: "https://rocheclement.fr/public/models/head.obj"
			}).then(model => {
				this.model = model.media;
				this.model.children[0].material = material;
				this.scene.add(this.model);
				
				this.modelBack=this.model.clone()
				this.model.children[0].material=
				this.model.children[0].material.clone()
				this.model.children[0].material.side = THREE.BackSide;
			//	back.position.x+=10
				this.model.add(this.modelBack);
				
				setTimeout(()=>{
					this.appear()
				},1500)
				
				this.gui
				.add(
				this.model.children[0].material.uniforms.uThreshold,
				"value",
				0,
				1,
				0.01
			)
			.name("threshold")
			.listen();
				resolve();
			});
		});
	}
	
	appear() {
		this.model.children[0].material.uniforms.uThreshold.value = this.modelBack.children[0].material.uniforms.uThreshold.value =  0;
		TweenLite.to([this.model.children[0].material.uniforms.uThreshold, this.modelBack.children[0].material.uniforms.uThreshold], 2, {
			value: 1,
			ease: Power4.easeOut
		});
	}

	disappear() {
		this.model.children[0].material.uniforms.uThreshold.value = this.modelBack.children[0].material.uniforms.uThreshold.value =  1;
		TweenLite.to([this.model.children[0].material.uniforms.uThreshold, this.modelBack.children[0].material.uniforms.uThreshold], 2, {
			value: 0,
			ease: Power4.easeOut
		});
	}

	render() {
		// this.clock.update();
		this.time = this.time + this.clock.getDelta();
		
		if(this.model) this.model.rotation.y += 0.01
		

		Boolean(this.isPostProcessingEnabled)
			? this.composer.render(this.clock.getDelta())
			: this.renderer.render(this.scene, this.camera);
	}

	load() {
		const assets = new Map();
		const loadingManager = new THREE.LoadingManager();

		return new Promise((resolve, reject) => {
			loadingManager.onError = reject;
			loadingManager.onProgress = (item, loaded, total) => {
				if (loaded === total) {
					resolve(assets);
				}
			};

			const searchImage = new Image();
			const areaImage = new Image();

			searchImage.addEventListener("load", function() {
				assets.set("smaa-search", this);
				loadingManager.itemEnd("smaa-search");
			});

			areaImage.addEventListener("load", function() {
				assets.set("smaa-area", this);
				loadingManager.itemEnd("smaa-area");
			});

			// Register the new image assets.
			loadingManager.itemStart("smaa-search");
			loadingManager.itemStart("smaa-area");

			// Load the images asynchronously.
			searchImage.src = PP.SMAAEffect.searchImageDataURL;
			areaImage.src = PP.SMAAEffect.areaImageDataURL;
		});
	}

	addPostProcessing(assets) {
		// this.renderer = renderer;
		this.composer = new PP.EffectComposer(this.renderer);

		this.noiseEffect = new PP.NoiseEffect({ premultiply: true });
		this.vignetteEffect = new PP.VignetteEffect();
		this.bloomEffect = new PP.BloomEffect();

		this.SMAAEffect = new PP.SMAAEffect(
			assets.get("smaa-search"),
			assets.get("smaa-area")
		);
		this.SMAAEffect.setOrthogonalSearchSteps(112);
		this.SMAAEffect.setEdgeDetectionThreshold(0.5);
		this.chromaticAberrationEffect = new PP.ChromaticAberrationEffect();
		this.chromaticAberrationEffect.offset.x = this.chromaticAberrationEffect.offset.y = 0.001

		this.renderPass = new PP.RenderPass(this.scene, this.camera);
		this.effectPass = new PP.EffectPass(this.camera, this.SMAAEffect);

		this.effectPass2 = new PP.EffectPass(
			this.chromaticAberrationEffect,
			this.bloomEffect,
			this.chromaticAberrationEffect
		);

		// this.noiseEffect.blendMode.opacity.value = 0.75;
		this.effectPass2.renderToScreen = true;

		this.composer.addPass(this.renderPass);
		this.composer.addPass(this.effectPass);
		this.composer.addPass(this.effectPass2);
	}

	addGUI() {
		this.gui = new dat.GUI();

		this.params = {
			postprocessing: {
				enabled: true,
				bloom: {
					blendFunction: PP.BlendFunction.SCREEN,
					resolutionScale: 0.5,
					kernelSize: PP.KernelSize.LARGE,
					distinction: 1.0,
					dithering: false
				},
				chroma: {
					offset: {
						x: 0,
						y: 0
					}
				},
				SMAA: {
					searchStep: 112,
					edgeDetectionThreshold: 0.5
				}
			}
		};

		let pp = this.gui.addFolder("post-processing");
		pp.add(this, "isPostProcessingEnabled").name("enabled");

// 		//bloom
// 		let bloom = pp.addFolder("bloom");
// 		bloom.open();

// 		bloom
// 			.add(this.params.postprocessing.bloom, "resolutionScale", 0.01, 1)
// 			.name("resolution")
// 			.onChange(value => {
// 				this.bloomEffect.setResolutionScale(value);
// 			});

// 		bloom
// 			.add(this.params.postprocessing.bloom, "kernelSize", PP.KernelSize)
// 			.name("kernel size")
// 			.onChange(value => {
// 				this.bloomEffect.kernelSize = value;
// 			});

// 		let luminance = bloom.addFolder("Luminance");
// 		luminance.open();
// 		luminance
// 			.add(this.params.postprocessing.bloom, "distinction", 1, 10)
// 			.name("distinction")
// 			.onChange(value => {
// 				this.bloomEffect.distinction = value;
// 			});

// 		// bloom
// 		// 	.add(this.params.postprocessing.bloom, "blendFunction", PP.BlendFunction)
// 		// 	.name("blend mode")
// 		// 	.onChange(value => {
// 		// 		this.bloomEffect.blendMode.blendFunction = parseInt(value);
// 		// 	});

// 		bloom
// 			.add(this.params.postprocessing.bloom, "dithering")
// 			.name("dithering")
// 			.onChange(value => {
// 				this.bloomEffect.dithering = value;
// 			});

// 		let chroma = pp.addFolder("chromatic aberration");
// 		chroma.open();

// 		let offset = chroma.addFolder("offset");
// 		offset
// 			.add(this.params.postprocessing.chroma.offset, "x", -0.01, 0.01)
// 			.step(0.001)
// 			.onChange(value => {
// 				this.chromaticAberrationEffect.offset.x = value;
// 			});

// 		offset
// 			.add(this.params.postprocessing.chroma.offset, "y", -0.01, 0.01)
// 			.step(0.001)
// 			.onChange(value => {
// 				this.chromaticAberrationEffect.offset.y = value;
// 			});

// 		let SMAA = pp.addFolder("SMAA");
// 		SMAA.open();

// 		SMAA.add(this.params.postprocessing.SMAA, "searchStep", 0, 112)
// 			.name("search step")
// 			.onChange(value => {
// 				this.SMAAEffect.setOrthogonalSearchSteps(value);
// 			});

// 		SMAA.add(this.params.postprocessing.SMAA, "edgeDetectionThreshold", 0.05, 0.5)
// 			.name("sensitivity")
// 			.step(0.01)
// 			.onChange(value => {
// 				this.SMAAEffect.setEdgeDetectionThreshold(value);
// 			});
		this.gui.add(this, "appear");
		this.gui.add(this, "disappear");
	}

	onWindowResize() {
		this.camera.aspect = window.innerWidth / window.innerHeight;
		this.camera.updateProjectionMatrix();

		this.renderer.setSize(window.innerWidth, window.innerHeight);
	}
}

const PP = POSTPROCESSING;

const simplex = new SimplexNoise();

Number.prototype.map = function(in_min, in_max, out_min, out_max) {
	return (this - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};

function loadModel(model) {
	return new Promise((resolve, reject) => {
		const ext = model.url.split(".").pop();

		switch (ext) {
			case "obj": {
				const loader = new THREE.OBJLoader();

				// load a resource
				loader.load(
					// resource URL
					model.url,
					// Function when resource is loaded
					object => {
						resolve({ id: model.id, media: object, type: "obj" });
					},

					() => {},
					() => {
						reject("An error happened with the model import.");
					}
				);
				break;
			}

			case "gltf": {
				const loader = new THREE.GLTFLoader();

				// load a resource
				loader.load(
					// resource URL
					model.url,
					// Function when resource is loaded
					object => {
						resolve({ id: model.id, media: object, type: "gltf" });
					},

					() => {},
					() => {
						reject("An error happened with the model import.");
					}
				);
				break;
			}

			default: {
				const loader = new THREE.OBJLoader();

				// load a resource
				loader.load(
					// resource URL
					model.url,
					// Function when resource is loaded
					object => {
						resolve({ id: model.id, media: object, type: "obj" });
					},

					() => {},
					() => {
						reject("An error happened with the model import.");
					}
				);
			}
		}
	});
}

//init app
const app = new App();

              
            
!
999px

Console