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>
  </head>
  <body>
      <div> Three <div>
      <div id="div"></div>
  </body>
</html>
              
            
!

CSS

              
                div {
  background: #ccc;
  height: 400px;
  width: 400px;
}
              
            
!

JS

              
                /*
NJ Namju Lee
Email: nj.namju@gmail.com
Website: www.njstudio.co.kr
Linkedin: https://www.linkedin.com/in/nj-namju-lee-926b3252/

Materials(Eng)
youtube(eng): https://www.youtube.com/channel/UCP4q3a4ogJN1-SbJclJR3Ww
Medium: https://medium.com/@nj.namju

Materials(KR)
youtube(kr): https://www.youtube.com/channel/UC3Z42uoe9C7Et39h5cLK1dw?view_as=subscriber
Daum Brunch: https://brunch.co.kr/@njnamju
  Data & Design Computaional Design : https://brunch.co.kr/@njnamju/64
Naver Blog: https://blog.naver.com/designju
*/

const div = document.getElementById('div');
const scene = new THREE.Scene();

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap
renderer.setSize( div.clientWidth, div.clientHeight );
renderer.setClearColor( 0xffffff );
div.appendChild( renderer.domElement );

// Field of view , Aspect ratio , Near clipping pane, Far clipping pane
const camera = new THREE.PerspectiveCamera( 65, div.clientWidth / div.clientHeight, 0.1, 100);
camera.position.set(0,0,5);
camera.lookAt(new THREE.Vector3(0,0,0));
renderer.render(scene, camera);

const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.75);
hemiLight.color.setHSL(0.6, 1, 0.6);
hemiLight.groundColor.setHSL(0.095, 1, 0.75);
hemiLight.position.set(0, 50, 0);
scene.add(hemiLight);

const dirLight = new THREE.DirectionalLight( 0xffffff, 0.97 );
dirLight.color.setHSL( 1, 1, 1 );
dirLight.position.set( - 1, 1.75, 1 );
dirLight.position.multiplyScalar( 30 );
dirLight.castShadow = true;

const val = 10; // for quality of the shawdow in the scene
dirLight.shadow.camera.left = - val;
dirLight.shadow.camera.right = val;
dirLight.shadow.camera.top = val;
dirLight.shadow.camera.bottom = - val;

dirLight.shadow.camera.far = 100;
dirLight.shadow.bias = - 0.00001;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
scene.add( dirLight );

const controls = new THREE.OrbitControls( camera, renderer.domElement );

controls.addEventListener( 'change', () => { 
	renderer.render(scene, camera); 
}); 

const distance = (x, y, z, xx, yy, zz) => {
	return Math.sqrt((xx - x) * (xx - x) + (yy - y) * (yy - y) + (zz - z) * (zz - z));   
}

// https://en.wikipedia.org/wiki/Wavefront_.obj_file
const URL_OBJ = 'https://raw.githubusercontent.com/McNopper/OpenGL/master/Binaries/bunny.obj';
const geometryObj = {
  boundingBox: [],
  vertex: []
}

$.get(URL_OBJ , (obj) => {
  const data = obj.split('\n');
	
	geometryObj.boundingBox = [
		[Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE],
		[Number.MIN_VALUE, Number.MIN_VALUE, Number.MIN_VALUE]
	]
	
  for(let i = 0 ; i < data.length; ++i) {
    const ln = data[i].split(' ');
    if(ln[0] === 'v') {
			// console.log(ln)
      geometryObj.vertex.push([+ln[1], +ln[2], +ln[3], 0]); // the last value used for checking isVisiting or not.
			
			// get min and max vector from the individual vertex by this loop
			geometryObj.boundingBox[0][0]  = geometryObj.boundingBox[0][0] > +ln[1] ? +ln[1] : geometryObj.boundingBox[0][0];
			geometryObj.boundingBox[0][1]  = geometryObj.boundingBox[0][1] > +ln[2] ? +ln[2] : geometryObj.boundingBox[0][1]; 
			geometryObj.boundingBox[0][2]  = geometryObj.boundingBox[0][2] > +ln[3] ? +ln[3] : geometryObj.boundingBox[0][2];
			
			geometryObj.boundingBox[1][0]  = geometryObj.boundingBox[1][0] < +ln[1] ? +ln[1] : geometryObj.boundingBox[1][0];
			geometryObj.boundingBox[1][1]  = geometryObj.boundingBox[1][1] < +ln[2] ? +ln[2] : geometryObj.boundingBox[1][1];
			geometryObj.boundingBox[1][2]  = geometryObj.boundingBox[1][2] < +ln[3] ? +ln[3] : geometryObj.boundingBox[1][2];
    } 
  }
	voxelization(geometryObj);
});

const voxelization = (obj) =>{
	// ................................................................................
	// your code goes here

	const theDis = .07;
	
	const xNum = 15;
	const yNum = 15;
	const zNum = 15;
	
	const xInterval = (obj.boundingBox[1][0] - obj.boundingBox[0][0]) / (xNum - 1);
	const yInterval = (obj.boundingBox[1][1] - obj.boundingBox[0][1]) / (yNum - 1);
	const zInterval = (obj.boundingBox[1][2] - obj.boundingBox[0][2]) / (zNum - 1);
	console.log('xInterval', xInterval, 'yInterval', yInterval, 'zInterval', zInterval)
	
	const listVoxel = [];
	
	for(let z = 0 ; z < zNum; ++z) {
		const theZ = obj.boundingBox[0][2] + z * zInterval;
		for(let y = 0 ; y < yNum; ++y) {
			const theY = obj.boundingBox[0][1] + y * yInterval;
			for(let x = 0 ; x < xNum; ++x) {
				const theX = obj.boundingBox[0][0] + x * xInterval;
				
				let needLoop = true;
				for(let i = 0 ; i < obj.vertex.length; i+=1) {
					const v = obj.vertex[i];
					
					if(v[3] === 0) {
						const d = distance(theX, theY, theZ, v[0], v[1], v[2]);
						if(d < theDis) {
							obj.vertex[i][3] = 1;
							listVoxel.push([theX, theY, theZ]);
							needLoop = false;
							break;
						}
					}
				if(!needLoop) {
					break;
				}
				}	
			}
		}
	}
	// console.log(listVoxel)
	// console.log('done!!');
	
	for(let i = 0 ; i < listVoxel.length; ++i) {
		// console.log(i);
		let geometryBox = new THREE.BoxGeometry( xInterval, yInterval, zInterval );
				// geometryBox.computeBoundingSphere();
		// geometryBox.computeVertexNormals();
		geometryBox.computeFaceNormals();
		geometryBox.computeBoundingBox();
		
		let materialBox = new THREE.MeshStandardMaterial( {color: 0x967bb6} );
		let meshCube = new THREE.Mesh( geometryBox, materialBox );

		meshCube.position.x = listVoxel[i][0];
		meshCube.position.y = listVoxel[i][1];
		meshCube.position.z = listVoxel[i][2];
		meshCube.castShadow = true;
		meshCube.receiveShadow = true;
		scene.add( meshCube );
		
	}
	
	// ................................................................................ 
	renderer.render(scene, camera); 
}
              
            
!
999px

Console