123

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

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

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="canvas"></canvas>
            
          
!
            
                  html {
    	overflow: hidden;
    	touch-action: none;
    	content-zooming: none;
    }
    
    body {
    	position: absolute;
    	margin: 0;
    	padding: 0;
    	width: 100%;
    	height: 100%;
    	background: #000;
    }
    
    #canvas {
    	position: absolute;
    	width: 100%;
    	height: 100%;
    	background: #000;
    	image-rendering: auto;
    }
            
          
!
            
              /**
 * Soft Shadow Mapping tutorial,
 * original CoffeeScript and shaders code
 * by Florian Boesch - @pyalot
 * http://codeflow.org/entries/2013/feb/15/soft-shadow-mapping/
 * ---------------------------------------------
 * Adapted to ES6 by Gerard Ferrandez - @ge1doot
 */


/**
 * setup canvas & webgl framework
 */
const canvas = new Canvas("canvas");
const gl = new WebGL(canvas)
	.depthTest();
const pointer = new Pointer(canvas);

gl.getExtension('OES_texture_half_float');
gl.getExtension('OES_texture_half_float_linear');
gl.getExtension('OES_standard_derivatives');

canvas.enableFullscreen({
	position: 'absolute',
	right: '7px',
	bottom: '7px',
	cursor: 'pointer',
	background: '#1e1e1e',
	fontFamily: 'Lato, Lucida Grande, Lucida Sans Unicode, Tahoma, Sans-Serif',
	fontSize: '0.8rem',
	padding: '2px 7px',
	borderRadius: '3px',
	border: '3px solid transparent',
	color: 'white',
	whiteSpace: 'nowrap',
	textAlign: 'center'
});

/**
 * VSM Shader programs
 */

const displayShader = gl.shader({

	vertex: `
		varying vec3 vWorldNormal;
		varying vec4 vWorldPosition;
		uniform mat4 camProj, camView;
		uniform mat4 model;
		attribute vec3 position, normal;

		void main(){
			vWorldNormal = normal;
			vWorldPosition = model * vec4(position, 1.0);
			gl_Position = camProj * camView * vWorldPosition;
		}
	`,

	fragment: `
		varying vec3 vWorldNormal;
		varying vec4 vWorldPosition;
		uniform vec3 globalColor, modelColor;
		uniform mat4 camView;
		uniform mat4 lightProj, lightView;
		uniform mat3 lightRot;
		uniform sampler2D sLightDepth;

		float linstep(float low, float high, float v){
			return clamp((v-low)/(high-low), 0.0, 1.0);
		}

		float VSM(sampler2D depths, vec2 uv, float compare){
			vec2 moments = texture2D(depths, uv).xy;
			float p = smoothstep(compare-0.02, compare, moments.x);
			float variance = max(moments.y - moments.x*moments.x, -0.001);
			float d = compare - moments.x;
			float p_max = linstep(0.4, 1.0, variance / (variance + d*d));
			return clamp(max(p, p_max), 0.0, 1.0);
		}

		float influence(vec3 normal, float coneAngle){
			float minConeAngle = ((360.0-coneAngle-1.0)/360.0)*PI;
			float maxConeAngle = ((360.0-coneAngle)/360.0)*PI;
			return smoothstep(minConeAngle, maxConeAngle, acos(normal.z));
		}

		float lambert(vec3 surfaceNormal, vec3 lightDirNormal){
			return max(0.0, dot(surfaceNormal, lightDirNormal));
		}

		void main(){
			vec3 worldNormal = normalize(vWorldNormal);
			vec3 camPos = (camView * vWorldPosition).xyz;
			vec3 lightPos = (lightView * vWorldPosition).xyz;
			vec3 lightPosNormal = normalize(lightPos);
			vec3 lightSurfaceNormal = lightRot * worldNormal;
			vec4 lightDevice = lightProj * vec4(lightPos, 1.0);
			vec2 lightDeviceNormal = lightDevice.xy/lightDevice.w;
			vec2 lightUV = lightDeviceNormal*0.5+0.5;

			// shadow calculation
			float lightDepth2 = clamp(length(lightPos)/40.0, 0.0, 1.0);
			float illuminated = VSM(sLightDepth, lightUV, lightDepth2);

			vec3 excident = (
				globalColor * modelColor *
				lambert(lightSurfaceNormal, -lightPosNormal) *
				influence(lightPosNormal, 50.0) *
				illuminated
			);

			gl_FragColor = vec4(excident, 1.0);
		}
`
});

const lightShader = gl.shader({

	vertex: `
		varying vec3 vWorldNormal;
		varying vec4 vWorldPosition;
		uniform mat4 lightProj, lightView;
		uniform mat4 model;
		attribute vec3 position, normal;

		void main(){
			vWorldNormal = normal;
			vWorldPosition = model * vec4(position, 1.0);
			gl_Position = lightProj * lightView * vWorldPosition;
		}
	`,

	fragment: `
		#extension GL_OES_standard_derivatives : enable
		varying vec3 vWorldNormal;
		varying vec4 vWorldPosition;
		uniform mat4 lightView;

		void main(){
			vec3 worldNormal = normalize(vWorldNormal);
			vec3 lightPos = (lightView * vWorldPosition).xyz;
			float depth = clamp(length(lightPos)/40.0, 0.0, 1.0);
			float dx = dFdx(depth);
			float dy = dFdy(depth);
			gl_FragColor = vec4(depth, pow(depth, 2.0) + 0.25*(dx*dx + dy*dy), 0.0, 1.0);
		}
	`
});

const lightDepthTexture = gl.texture({
		type: 0x8D61, // HALF_FLOAT_OES
		channels: 'rgba'
	})
	.bind()
	.setSize(2048, 2048)
	.linear()
	.clampToEdge();

const lightFramebuffer = gl.framebuffer()
	.bind()
	.color(lightDepthTexture)
	.depth()
	.unbind();

const boxFilter = gl.filter(1024);

/**
 * matrix setup
 */
const lightProj = gl.mat4()
	.perspective({
		fov: 60
	}, 1, {
		near: 0.01,
		far: 40
	});

const camProj = gl.mat4();
const camView = gl.mat4();
const lightView = gl.mat4();
const lightRot = gl.mat3();
const model = gl.mat4();


/**
 * state variables
 */
let cx = 0;
let cy = 0;
let camDist = 15;
let camRot = 140;
let camPitch = 41;
let lightRoty = camRot + 30;


/**
 * scene geometry
 */
const cubeGeom = gl.drawable(gl.cube());
const planeGeom = gl.drawable(gl.plane(60));
const sphereGeom = gl.drawable(gl.sphere(1, 36));
const cylinderGeom = gl.drawable(gl.cylinder(1, 36));

const scene = [

	// ground
	[planeGeom, gl.mat4()
				.trans(0, 0, 0), 0.35, 0.25, 0.25],

	// wall & window
	[cubeGeom, gl.mat4()
				.trans(0, 1, 5)
				.scale(6, 1, 0.3), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.trans(0, 5, 5)
				.scale(6, 1, 0.3), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.trans(4, 3, 5)
				.scale(2, 1, 0.3), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.trans(-4, 3, 5)
				.scale(2, 1, 0.3), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.trans(0, 3, 5)
				.scale(0.06, 1, 0.06), 0.5, 0.5, 0.5],
	[cubeGeom, gl.mat4()
				.trans(0, 3, 5)
				.scale(2, 0.06, 0.06), 0.5, 0.5, 0.5],

	// table
	[cubeGeom, gl.mat4()
				.trans(0, 2, 1.5)
				.scale(3, 0.1, 2), 0.25, 0.25, 0.25],
	[cubeGeom, gl.mat4()
				.trans(2.3, 1, 0)
				.scale(0.15, 1, 0.15), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.trans(-2.3, 1, 0)
				.scale(0.15, 1, 0.15), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.trans(2.3, 1, 2.9)
				.scale(0.15, 1, 0.15), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.trans(-2.3, 1, 2.9)
				.scale(0.15, 1, 0.15), 1, 1, 1],

	// bench
	[cubeGeom, gl.mat4()
				.rotatey(-130)
				.trans(0, 1, 3.5)
				.scale(2.5, 0.12, 0.5), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.rotatey(-130)
				.trans(-2.3, 0.5, 3.5)
				.scale(0.1, 0.5, 0.1), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.rotatey(-130)
				.trans(2.3, 0.5, 3.5)
				.scale(0.1, 0.5, 0.1), 1, 1, 1],

	// chair
	[cubeGeom, gl.mat4()
				.rotatey(-20)
				.trans(-0, 1.25, -1.5)
				.scale(0.6, 0.1, 0.6), 1, 0, 0],
	[cubeGeom, gl.mat4()
				.rotatey(-20)
				.trans(-0, 1.5, -2)
				.scale(0.15, 0.5, 0.03), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.rotatey(-20)
				.trans(-0, 2.5, -2)
				.scale(0.5, 0.5, 0.08), 1, 0, 0],
	[cubeGeom, gl.mat4()
				.rotatey(-20)
				.trans(0.4, 0.625, -1.0)
				.scale(0.06, 0.625, 0.06), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.rotatey(-20)
				.trans(-0.4, 0.625, -1.0)
				.scale(0.06, 0.625, 0.06), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.rotatey(-20)
				.trans(0.4, 0.625, -2)
				.scale(0.06, 0.625, 0.06), 1, 1, 1],
	[cubeGeom, gl.mat4()
				.rotatey(-20)
				.trans(-0.4, 0.625, -2)
				.scale(0.06, 0.625, 0.06), 1, 1, 1],

	// structure
	[cylinderGeom, gl.mat4()
				.trans(4, 3, -4)
				.scale(0.25, 4, 0.25), 0.25, 0.25, 0.25],
	[cylinderGeom, gl.mat4()
				.trans(-4, 3, -4)
				.scale(0.25, 4, 0.25), 0.25, 0.25, 0.25],
	[cubeGeom, gl.mat4()
				.trans(0, 5.5, -4)
				.scale(5, 0.12, 0.06), 1, 1, 1],
	[sphereGeom, gl.mat4()
				.trans(0, 6, 1)
				.scale(0.25, 0.25, 0.25), 1, 1, 1]

];

// roof
for (let i = -4; i <= 4; i += 0.5) {
	scene.push(
	    [cylinderGeom, gl.mat4()
					.trans(i, 5.5, 0.5)
					.scale(0.025, 0.025, 4.5), 0.25, 0.25, 0.25]
	);
}

/**
 * drawing functions
 */
let k = 0

function drawScene(shader) {

	for (let m of scene) {
		shader.vec3('modelColor', m[2], m[3], m[4])
			.mat4('model', m[1])
			.draw(m[0]);
	}

	/**
	 * fan
	 */
	shader.vec3('modelColor', 1, 0, 0)
		.mat4('model', gl.mat4()
		.trans(0, 6, 1)
		.rotatey(k + 90)
		.scale(3, 0.1, 0.3)
	)
	.draw(sphereGeom);

	shader.vec3('modelColor', 1, 0, 0)
		.mat4('model', gl.mat4()
		.trans(0, 6, 1)
		.rotatey(k++)
		.scale(3, 0.1, 0.3)
	)
	.draw(sphereGeom);

	/**
	 * sphere
	 */
	shader.vec3('modelColor', 1, 0, 0)
		.mat4('model', gl.mat4()
		.trans(0, 3.8 + 0.75 * Math.cos(k * 0.01), 1)
		.scale(0.7, 0.66, 0.7)
	)
	.draw(sphereGeom);

}

function drawLight() {

	lightFramebuffer.bind();

	gl.viewport(0, 0, lightDepthTexture.width, lightDepthTexture.height)
		.clearColor(1, 1, 1, 1)
		.clearDepth(1)
		.cullFace();

	lightShader.use()
		.mat4('lightView', lightView)
		.mat4('lightProj', lightProj)
		.mat3('lightRot', lightRot);

	drawScene(lightShader);

	lightFramebuffer.unbind();

	boxFilter.apply(lightDepthTexture);

}

function drawCamera() {

	gl
		.adjustSize()
		.viewport()
		.cullFace()
		.clearColor(0, 0, 0, 0)
		.clearDepth(1);

	camProj.perspective({
		fov: 60,
		aspect: gl.aspect,
		near: 0.01,
		far: 100
	});

	camView
		.ident()
		.trans(0, -1, -camDist)
		.rotatex(camPitch)
		.rotatey(camRot);

	displayShader
		.use()
		.vec3('globalColor', 2.0, 1.9, 1.4)
		.mat4('camProj', camProj)
		.mat4('camView', camView)
		.mat4('lightView', lightView)
		.mat4('lightProj', lightProj)
		.mat3('lightRot', lightRot)
		.sampler('sLightDepth', boxFilter);

	drawScene(displayShader);

}


/**
 * mainloop
 */

function draw() {

	requestAnimationFrame(draw);

	if (pointer.isDown) {
		cx += (pointer.x - pointer.xold) / 50;
		cy += (pointer.y - pointer.yold) / 50;
	}

	camRot += cx * 1.5 + 0.02;
	camPitch += cy * 1.25;
	if (camPitch > 85) camPitch = 85;
	else if (camPitch < 1) camPitch = 1;

	camDist += pointer.z * 0.1;
	pointer.z *= 0.9;
	if (camDist < 8) camDist = 8;
	else if (camDist > 50) camDist = 50;

	cx *= 0.95;
	cy *= 0.95;
	pointer.xold = pointer.x;
	pointer.yold = pointer.y;

	lightRoty += (camRot - lightRoty) / 140;
	lightView.ident()
		.trans(0, 0, -15)
		.rotatex(40)
		.rotatey(180 - lightRoty + camRot);
	lightRot.fromMat4Rot(lightView);

	drawLight();
	drawCamera();

}

draw();
            
          
!
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.

Console