Pen Settings

HTML

CSS

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

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

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.

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

              
                
  <h1>Minecraft 3D Item OBJ/MTL Generator</h1>
	Item texture (16 x 16): <input id="item_file" type="file" accept="image/png" /><br>
  Texture: <input id="tex_file" type="file" accept="image/png" />
  <button id="execute">Capture</button>
  <button id="mrotate">Rotate Model</button>
  <button id="reset">Reset camera</button><br>
	<canvas id="myCanvas"></canvas><img id="drawed"><br>
<img id="tex" style="display:none">
              
            
!

CSS

              
                
		body {
			background-color: white;
		}
		#myCanvas {
			background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAAAAABX3VL4AAAADklEQVQIHWPYXc9QvxsAB2ICdaBJUyUAAAAASUVORK5CYII=");
			background-size: 12px 12px;
			image-rendering: pixelated;
			-ms-interpolation-mode: nearest-neighbor;
			display: inline-block;
			padding: 0px;
      cursor: all-scroll;
		}
              
            
!

JS

              
                var last_mesh;

$(function () {

	const width = 300;
	const height = 300;

	const zoom = 0.56;

	const night = false;

	// initialize renderer -------------------------------------------------------------------------
	const renderer = new THREE.WebGLRenderer({
		canvas: document.querySelector('#myCanvas'),
		antialias: false,
		alpha: true,
		preserveDrawingBuffer: true
	});
	renderer.setClearColor(0xFF00FF, 0);
	renderer.setPixelRatio(window.devicePixelRatio);
	renderer.setSize(width, height);

	$("#execute").on("click", function () {
		let img = document.querySelector("#drawed");
		img.src = renderer.domElement.toDataURL('image/png');
	});
  
	$("#reset").on("click", function(){
		controls.reset()
    last_mesh.rotation.y = 0
	});
  
	// model rotate
	$("#mrotate").on("click", function(){
		last_mesh.rotation.y += deg2rad(45)
	})

	// initilaize scene
	const scene = new THREE.Scene();
	Render(scene)
	
	$("#item_file").change(function () {
		if(!this.files.length){
			return; //no file selected
		}

		let file = this.files[0];
		let fileRdr = new FileReader();
		fileRdr.onload = function() {
			let imgTmp = new Image();
			imgTmp.onload = function(){
				let canvas = document.createElement("canvas")
				canvas.setAttribute("width", this.width)
				canvas.setAttribute("height", this.height)
				let ctx = canvas.getContext("2d")
				ctx.clearRect(0, 0, this.width, this.height)
				ctx.drawImage(imgTmp, 0, 0)

				if(last_mesh){
					scene.remove(last_mesh)
					delete last_mesh
				}

				let geometry = new THREE.PixelArtGeometry(canvas, 1)
				let texture = new THREE.CanvasTexture(canvas)
				texture.magFilter = THREE.NearestFilter;
				texture.minFilter = THREE.NearestFilter;
				texture.type = THREE.FloatType;

				let material = new THREE.MeshLambertMaterial({map: texture, transparent: false, alphaTest: 0.5, side: THREE.DoubleSide})
				let mesh = new THREE.Mesh(geometry, material)
				mesh.name = file.name.match(/(.*)(?:\.([^.]+$))/)[1]
				material.name = file.name.match(/(.*)(?:\.([^.]+$))/)[1] + "_mtl"

				scene.add(mesh)
				last_mesh = mesh

				var exporter = new THREE.OBJExporter();
				let result = "mtllib " + mesh.name + '_item.mtl' + "\n" + exporter.parse(mesh);

				$("#obj_file,#mtl_file").remove()
				
				var data = "application/x-msdownload;charset=utf-8," + encodeURIComponent(result)
				$('<a id="obj_file" href="data:' + data + '" download="' + mesh.name + '_item.obj">' + mesh.name + '_item.obj</a>').css("margin-right", "8px").appendTo('body');


				let material_setting = ""
				material_setting += "newmtl " + material.name + "\n"
				material_setting += "illum 1\n"
				material_setting += "map_Kd " + file.name + "\n"
				material_setting += "map_d " + file.name + "\n"
				material_setting += "newmtl none\n"
				
				data = "application/x-msdownload;charset=utf-8," + encodeURIComponent(material_setting)
				$('<a id="mtl_file" href="data:' + data + '" download="' + mesh.name + '_item.mtl">' + mesh.name + '_item.mtl</a>').appendTo('body');

			}
			imgTmp.src = fileRdr.result;
		}
		fileRdr.readAsDataURL(file);

	});

	$("#tex_file").change(function () {
		if(!this.files.length){
			return; //no file selected
		}
		let file = this.files[0];
		let fileRdr = new FileReader();
		fileRdr.onload = function() {
			let imgTmp = new Image();
			imgTmp.onload = function(){
			  if(last_mesh){
			    last_mesh.material.map.image = imgTmp
			    last_mesh.material.map.needsUpdate = true
			  }
			}
			imgTmp.src = fileRdr.result;
		}
		fileRdr.readAsDataURL(file);

	});
  
	function Render(scene) {

		// initilalize camera
		var viewSize = 10;

		var aspectRatio = width / height;
		const camera = new THREE.OrthographicCamera(
			-aspectRatio * viewSize / 2,
			aspectRatio * viewSize / 2,
			viewSize / 2,
			-viewSize / 2,
			-1000,
			1000
		);

		camera.position.set(viewSize/2, viewSize * Math.tan(deg2rad(39.23))/2, viewSize); // Isometric position
		camera.lookAt(scene.position);

		camera.zoom = zoom;
		camera.updateProjectionMatrix();

		controls = new THREE.OrbitControls(camera, renderer.domElement);
		camera.position.y -= 0.25

		// Lighting -------------------------------------------------------------------------------------

		const dirLight = new THREE.DirectionalLight(night ? 0x9999cc : 0xFFFFFF);
		//dirLight.intensity = 0.435;
		dirLight.intensity = 0.46;
		dirLight.position.set(-1, 2.25, 1.24).normalize();
		dirLight.castShadow = true;
		dirLight.shadow.mapSize.width = 2048;
		dirLight.shadow.mapSize.height = 2048;
		var 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);
		var ambientLight = new THREE.AmbientLight(night ? 0x333333 : 0xFCFCFF);
		ambientLight.intensity = 0.618;
		scene.add(ambientLight);

		// Run tick -------------------------------------------------------------------------------------

		tick();

		function tick() {

			// Rendering
			renderer.render(scene, camera);
			requestAnimationFrame(tick);
		}
	}
});

// Returns radians from an angle
function deg2rad(deg) {
	return deg * (Math.PI / 180)
}

              
            
!
999px

Console