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

              
                <html>
  <head>
    <script src="index.js"></script>
    <script>
      var main_object;
      document.addEventListener('DOMContentLoaded', function () {

	// Initilalize Renderer -------------------------------------------------------------------------
	const renderer = new THREE.WebGLRenderer({
		canvas: document.querySelector('#myCanvas'),
		antialias: false
	});
	renderer.setClearColor(0xFFFFFF, 1);
	
  renderer.setSize(window.innerWidth, window.innerHeight);
	renderer.setPixelRatio(window.devicePixelRatio);

	// Initialize Scene
	const scene = new THREE.Scene();

	// Careate geometry
	let img = document.querySelector("#item");
	let canvas = createCanvas(img.width, img.height);
	canvas.context.drawImage(img, 0, 0);
	let geometry = new THREE.PixelArtGeometry(canvas.canvas, 1);
	canvas.canvas.remove();
	
	// create texture
	let texture = new THREE.TextureLoader().load(img.src);
	texture.magFilter = THREE.NearestFilter;
	texture.minFilter = THREE.NearestFilter;
	texture.type = THREE.FloatType;
	
	// Creating Emissive Map for animation
	let emissiveMaps = [];
	for(let i=0; i<56; i++){
		let canvas = drawAnimated(i);
		let texture = new THREE.CanvasTexture(canvas);
		texture.magFilter = THREE.NearestFilter;
		texture.minFilter = THREE.NearestFilter;
		texture.type = THREE.FloatType;
		emissiveMaps.push(texture);
	}

	// Create material
	let material = new THREE.MeshLambertMaterial({
		map: texture,
		emissiveMap: emissiveMaps[0],
		emissive: "#ef22ef", /// Emissive Color
		transparent: false,
		side: THREE.DoubleSide,
		alphaTest: 0.99 
	})

	// Create mesh
	let box = new THREE.Mesh(
		geometry, 
		material
	);
	box.scale.set(10, 10, 10);
	main_object = box;

	// add object to scene
	scene.add(main_object);

	// rander
	Render(scene);

	function Render(scene) {

		// Initialize camera
		var viewSize = 300;

		var aspectRatio = window.innerWidth / window.innerHeight;
		const camera = new THREE.OrthographicCamera(
			-aspectRatio * viewSize / 2,
			aspectRatio * viewSize / 2,
			viewSize / 2,
			-viewSize / 2,
			-1000,
			1000
		);

		camera.position.set(viewSize, viewSize * 0.8168, viewSize); // Isometric position
		camera.lookAt(scene.position);

		// Initialize lighting -------------------------------------------------------------------------------------

		let dirLight = new THREE.DirectionalLight(0xFFFFFF);
		dirLight.intensity = 0.435;
		dirLight.position.set(-1, 2.25, 1.24).normalize();
		dirLight.castShadow = true;
		dirLight.shadow.mapSize.width = 2048;
		dirLight.shadow.mapSize.height = 2048;
		let d = 50;
		dirLight.shadow.camera.left = -d;
		dirLight.shadow.camera.right = d;
		dirLight.shadow.camera.top = d;
		dirLight.shadow.camera.bottom = -d;
		dirLight.shadow.camera.far = 3500;
		dirLight.shadow.bias = -0.0001;
		scene.add(dirLight);

		let ambientLight = new THREE.AmbientLight(0xFCFCFF);
		ambientLight.intensity = 0.618;
		scene.add(ambientLight);

		// Begin render -------------------------------------------------------------------------------------

		let pos_cycle = 0;
		let glint_cycle = 0;

		tick();

		//// Tick ////
		function tick() {

			// object positions
			pos_cycle = pos_cycle < 360 ? pos_cycle + 1 : 0;
			var y = Math.sin(deg2rad(pos_cycle * 2)) * 20;
			main_object.position.set(0, -y, 0);
			main_object.rotation.set(0, deg2rad(pos_cycle * 1), 0);
			
			// Switch emissive maps
			main_object.material.emissiveMap = emissiveMaps[Math.floor(glint_cycle)];
			glint_cycle = glint_cycle < emissiveMaps.length ? glint_cycle + 0.1 : 0;

			// re-render
			renderer.render(scene, camera);
			requestAnimationFrame(tick); //loop
		}
	}

})

function drawAnimated(cycle) {
	let cycle2 = (cycle + 32) % 56;

	let width = 64;
	let height = 64;

	/// Create Base Canvas
	let canvas = createCanvas(width, height);
	canvas.context.clearRect(0, 0, width, height);
	canvas.context.fillStyle = "#000000";
	canvas.context.fillRect(0, 0, width, height);

	/// Load Glint Map
	let enchant_glint = createCanvas(width, height);
	enchant_glint.context.drawImage(document.querySelector("#enchant_glint"), 0, 0);

	/// rotated canvas width
	let canvas_width = Math.sqrt(2) * (([height, width]).sort((a, b) => { return a - b })[1]);

	/// Draw emissive map rotated 80°
	{
		let stretchCanvas = createCanvas(canvas_width, canvas_width);
		stretchCanvas.context.imageSmoothingEnabled = true;
		stretchCanvas.context.drawImage(enchant_glint.canvas, cycle, 0, 8, 64, 0, 0, canvas_width, canvas_width);

		let rotatedCanvas = createCanvas(width, height);
		rotatedDraw(rotatedCanvas.context, stretchCanvas.canvas, 0, 0, canvas_width, canvas_width, 0, 0, width, width, 80);
		margeDraw(canvas.canvas, rotatedCanvas.canvas);

		stretchCanvas.canvas.remove();
		rotatedCanvas.canvas.remove();
	}

	/// Draw emissive map rotated -10°
	{
		let stretchCanvas = createCanvas(canvas_width, canvas_width);
		stretchCanvas.context.imageSmoothingEnabled = true;
		stretchCanvas.context.drawImage(enchant_glint.canvas, 56 - cycle2, 0, 8, 64, 0, 0, canvas_width, canvas_width);

		let rotatedCanvas = createCanvas(width, height);
		rotatedDraw(rotatedCanvas.context, stretchCanvas.canvas, 0, 0, canvas_width, canvas_width, 0, 0, width, width, -10);
		margeDraw(canvas.canvas, rotatedCanvas.canvas);

		stretchCanvas.canvas.remove();
		rotatedCanvas.canvas.remove();
	}

	return canvas.canvas;
}

function createCanvas(width, height) {
	let canvas = document.createElement("canvas");
	canvas.setAttribute("width", width);
	canvas.setAttribute("height", height);
	let context = canvas.getContext('2d');
	context.imageSmoothingEnabled = false;
	return { canvas: canvas, context: context }
}

function margeDraw(dest_canvas, src_canvas) {
	let ctx = src_canvas.getContext('2d');
	let ctx2 = dest_canvas.getContext('2d');
	let w = src_canvas.width;
	let h = src_canvas.height;
	let data = ctx.getImageData(0, 0, w, h);
	let data2 = ctx2.getImageData(0, 0, w, h);
	let clamp = (value) => {
		return value < 0 ? 0 : value > 255 ? 255 : value;
	};
	for (let x = 0; x < w; x++) {
		for (let y = 0; y < h; y++) {
			let d = (x + y * data.width) * 4;
			let r = data.data[d], g = data.data[d + 1], b = data.data[d + 2];
			let r2 = data2.data[d], g2 = data2.data[d + 1], b2 = data2.data[d + 2], a2 = data.data[d + 3];
			
			let opacity = 0.5;
			a2 = Math.floor(a2 * opacity);
			data2.data[d + 0] = Math.floor(clamp(r * a2 / 255 + (255 - a2) * r2 / 255));
			data2.data[d + 1] = Math.floor(clamp(r * a2 / 255 + (255 - a2) * g2 / 255));
			data2.data[d + 2] = Math.floor(clamp(b * a2 / 255 + (255 - a2) * b2 / 255));
			data2.data[d + 3] = 255;
		}
	}
	ctx2.putImageData(data2, 0, 0);
}

function rotatedDraw(context, image, srcX, srcY, srcW, srcH, dstX, dstY, dstW, dstH, angle) {
	// create temporary large canvas
	let canvas = document.createElement("canvas");
	canvas.setAttribute("width", srcW * 2);
	canvas.setAttribute("height", srcH * 2);
	let contex = canvas.getContext('2d');

	// rotated draw
	contex.beginPath();
	contex.clearRect(0, 0, srcW, srcH);
	let x = srcW / 2;
	let y = srcH / 2;
	let angleInRadians = deg2rad(angle);
	contex.translate(x, y);
	contex.rotate(angleInRadians);
	contex.drawImage(image, srcX, srcY, srcW, srcH, -x, -y, srcW, srcH);
	contex.rotate(-angleInRadians);
	contex.translate(-x, -y);

	// bitblit to destination canvas
	context.drawImage(canvas, x - dstW / 2, y - dstH / 2, dstW, dstH, dstX, dstY, dstW, dstH);
	canvas.remove();
	return context;
}

function deg2rad(deg) {
	return deg * (Math.PI / 180)
}


    </script>
  </head>
  <body>
  <canvas id="myCanvas"></canvas>
      <img id="item" style="display:none" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAi0lEQVQ4y2NgIAO4uLj8h2GyNMOAhITEf7I079u3j3QDqKb5xo0bNNKMLVRBfJAmomwGKc7OzoYbQpJmZEOmTZsGVgzSBGKT5GeYASAMcg3JAYbuFZI1e5qJ/s/wkwNrhAUq0YaANHemq//XkOMGawBphBlEMPRBGGQzTDMygBlE0M8k+xfdBnINAAA+k7meeyM0+AAAAABJRU5ErkJggg==" alt="" />
  <img id="enchant_glint" style="display:none" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAwBQTFRFGBgYMTExODg4Pz8/QUFBS0tLTk5OT09PUFBQVlZWV1dXWVlZW1tbXV1dXl5eX19fYmJiZ2dnaGhoaWlpampqa2trbGxsbW1tb29vcXFxdHR0eXl5fX19fn5+goKCg4ODhISEhoaGiYmJi4uLj4+PkJCQlJSUlZWVmJiYmpqam5ubnJycoKCgo6OjpqamuLi4vb29xcXFzMzM0dHR2tra3d3d5eXl////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD+Y22gAAAGRJREFUWEftzE0KgCAQBtAc/EUQaRF0/3O1aRUuCiJCSme6R3zvAE+NR7xNcBfnFjkVtn3yT38rDWvX5JMMes9OhWbJCAvXInOVjfNpyGRaFAIECBAgQIAAAQIECBAgQIDg78EHpfzB6tcniosAAAAASUVORK5CYII=" alt="" />
  </body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
THREE.PixelArtGeometry = function (canvas, depth, useVertexColors) {
	THREE.Geometry.call( this );
	this.faceVertexUvs[0] = [];
	let ctx = canvas.getContext('2d'),
		w = canvas.width, 
		h = canvas.height,
		z = depth,
		v = [],
		color = new THREE.Color("#FFFFFF");
	let data = ctx.getImageData(0, 0, w, h);

	if(!useVertexColors){
		// front face
		v = [
			this.vertices.push(new THREE.Vector3(0,  0,  z)) -1,
			this.vertices.push(new THREE.Vector3(w,  0,  z)) -1,
			this.vertices.push(new THREE.Vector3(0, -h,  z)) -1,
			this.vertices.push(new THREE.Vector3(w, -h,  z)) -1,
		];
		this.faces.push(
			new THREE.Face3(v[2], v[1], v[0], THREE.vertexNormals, color, 0),
			new THREE.Face3(v[1], v[2], v[3], THREE.vertexNormals, color, 0)
		);
		this.faceVertexUvs[0].push(
			[ new THREE.Vector2(0, 0), new THREE.Vector2(1, 1), new THREE.Vector2(0, 1) ],
			[ new THREE.Vector2(1, 1), new THREE.Vector2(0, 0), new THREE.Vector2(1, 0) ]
		);

		// back face
		v = [
			this.vertices.push(new THREE.Vector3(0,  0,  0)) -1,
			this.vertices.push(new THREE.Vector3(w,  0,  0)) -1,
			this.vertices.push(new THREE.Vector3(0, -h,  0)) -1,
			this.vertices.push(new THREE.Vector3(w, -h,  0)) -1,
		];
		this.faces.push(
			new THREE.Face3(v[0], v[1], v[2], THREE.vertexNormals, color, 0),
			new THREE.Face3(v[3], v[2], v[1], THREE.vertexNormals, color, 0)
		);
		this.faceVertexUvs[0].push(
			[ new THREE.Vector2(0, 1), new THREE.Vector2(1, 1), new THREE.Vector2(0, 0) ],
			[ new THREE.Vector2(1, 0), new THREE.Vector2(0, 0), new THREE.Vector2(1, 1) ]
		);
	}

	for(let x=0;x<w;x++)
	{
		for(let y=0;y<h;y++)
		{
			//////// vertices / face ////////
			let d = (x + y * data.width) * 4;
			if(data.data[d+3]) {
				if(useVertexColors){
					color = new THREE.Color(data.data[d]<<16 ^ data.data[d+1]<<8 ^ data.data[d+2]);
				}
				color.offsetHSL(0, 0, 1-data.data[d+3]/255);
				let chk = {
					right:  x+1>=w || !data.data[(x+1 + (y  ) * data.width) * 4 + 3],
					left:   x-1<0  || !data.data[(x-1 + (y  ) * data.width) * 4 + 3],
					top:    y+1>=h || !data.data[(x   + (y+1) * data.width) * 4 + 3],
					bottom: y-1<0  || !data.data[(x   + (y-1) * data.width) * 4 + 3],
				};

				if(useVertexColors){
					// front face
					v = [ // vertex indices. (array.push() returns array.length)
						this.vertices.push(new THREE.Vector3(x,   -y,     z)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -y,     z)) -1,
						this.vertices.push(new THREE.Vector3(x,   -(y+1), z)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -(y+1), z)) -1,
					];
					this.faces.push(
						new THREE.Face3(v[2], v[1], v[0], THREE.vertexNormals, color, 0),
						new THREE.Face3(v[1], v[2], v[3], THREE.vertexNormals, color, 0)
					);
					this.faceVertexUvs[0].push(
						[ new THREE.Vector2(x/w, 1-y/h), new THREE.Vector2((x+1)/w, 1-(y+1)/h), new THREE.Vector2(x/w,     1-(y+1)/h) ],
						[ new THREE.Vector2(x/w, 1-y/h), new THREE.Vector2((x+1)/w, 1-y/h    ), new THREE.Vector2((x+1)/w, 1-(y+1)/h) ]
					);

					// back face
					v = [
						this.vertices.push(new THREE.Vector3(x,   -y,     0)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -y,     0)) -1,
						this.vertices.push(new THREE.Vector3(x,   -(y+1), 0)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -(y+1), 0)) -1,
					];
					this.faces.push(
						new THREE.Face3(v[0], v[1], v[2], THREE.vertexNormals, color, 0),
						new THREE.Face3(v[3], v[2], v[1], THREE.vertexNormals, color, 0)
					);
					this.faceVertexUvs[0].push(
						[ new THREE.Vector2((x+1)/w, 1-y/h), new THREE.Vector2(x/w, 1-(y+1)/h), new THREE.Vector2((x+1)/w, 1-(y+1)/h) ],
						[ new THREE.Vector2((x+1)/w, 1-y/h), new THREE.Vector2(x/w, 1-y/h    ), new THREE.Vector2(x/w,     1-(y+1)/h) ]
					);
				}

				if(chk.right){
					v = [
						this.vertices.push(new THREE.Vector3(x+1, -y,     z)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -y,     0)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -(y+1), z)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -(y+1), 0)) -1,
					];
					this.faces.push(
						new THREE.Face3(v[2], v[1], v[0], THREE.vertexNormals, color, 0),
						new THREE.Face3(v[1], v[2], v[3], THREE.vertexNormals, color, 0)
					);
					this.faceVertexUvs[0].push(
						[ new THREE.Vector2(x/w, 1-y/h), new THREE.Vector2((x+1)/w, 1-(y+1)/h), new THREE.Vector2(x/w,     1-(y+1)/h) ],
						[ new THREE.Vector2(x/w, 1-y/h), new THREE.Vector2((x+1)/w, 1-y/h    ), new THREE.Vector2((x+1)/w, 1-(y+1)/h) ]
					);
				}

				if(chk.left){
					v = [
						this.vertices.push(new THREE.Vector3(x, -y,     z)) -1,
						this.vertices.push(new THREE.Vector3(x, -y,     0)) -1,
						this.vertices.push(new THREE.Vector3(x, -(y+1), z)) -1,
						this.vertices.push(new THREE.Vector3(x, -(y+1), 0)) -1,
					];
					this.faces.push(
						new THREE.Face3(v[0], v[1], v[2], THREE.vertexNormals, color, 0),
						new THREE.Face3(v[3], v[2], v[1], THREE.vertexNormals, color, 0)
					);
					this.faceVertexUvs[0].push(
						[ new THREE.Vector2((x+1)/w, 1-y/h), new THREE.Vector2(x/w, 1-(y+1)/h), new THREE.Vector2((x+1)/w, 1-(y+1)/h) ],
						[ new THREE.Vector2((x+1)/w, 1-y/h), new THREE.Vector2(x/w, 1-y/h    ), new THREE.Vector2(x/w,     1-(y+1)/h) ]
					);
				}
				
				if(chk.top){
					v = [
						this.vertices.push(new THREE.Vector3(x,   -(y+1), z)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -(y+1), z)) -1,
						this.vertices.push(new THREE.Vector3(x,   -(y+1), 0)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -(y+1), 0)) -1,
					];
					this.faces.push(
						new THREE.Face3(v[2], v[1], v[0], THREE.vertexNormals, color, 0),
						new THREE.Face3(v[1], v[2], v[3], THREE.vertexNormals, color, 0)
					);
					this.faceVertexUvs[0].push(
						[ new THREE.Vector2(x/w, 1-y/h), new THREE.Vector2((x+1)/w, 1-(y+1)/h), new THREE.Vector2(x/w,     1-(y+1)/h) ],
						[ new THREE.Vector2(x/w, 1-y/h), new THREE.Vector2((x+1)/w, 1-y/h    ), new THREE.Vector2((x+1)/w, 1-(y+1)/h) ]
					);
				}

				if(chk.bottom){
					v = [
						this.vertices.push(new THREE.Vector3(x,   -y, z)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -y, z)) -1,
						this.vertices.push(new THREE.Vector3(x,   -y, 0)) -1,
						this.vertices.push(new THREE.Vector3(x+1, -y, 0)) -1,
					];
					this.faces.push(
						new THREE.Face3(v[0], v[1], v[2], THREE.vertexNormals, color, 0),
						new THREE.Face3(v[3], v[2], v[1], THREE.vertexNormals, color, 0)
					);
					this.faceVertexUvs[0].push(
						[ new THREE.Vector2((x+1)/w, 1-y/h), new THREE.Vector2(x/w, 1-(y+1)/h), new THREE.Vector2((x+1)/w, 1-(y+1)/h) ],
						[ new THREE.Vector2((x+1)/w, 1-y/h), new THREE.Vector2(x/w, 1-y/h    ), new THREE.Vector2(x/w,     1-(y+1)/h) ]
					);
				}
			}
		}
	}
	this.center();
	this.mergeVertices();
	this.computeFaceNormals();
};

THREE.PixelArtGeometry.prototype = Object.create(THREE.Geometry.prototype);
THREE.PixelArtGeometry.prototype.constructor = THREE.PixelArtGeometry;
              
            
!
999px

Console