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

              
                <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
*/

// var canvas = document.getElementById("canvas-005");
// var ctx = canvas.getContext('2d');

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

const renderer = new THREE.WebGLRenderer({ antialias: true });
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.01, 1000);
camera.position.set(15,40,40);
camera.lookAt(new THREE.Vector3(0, 0, 0));
renderer.render(scene, camera);

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

const rgbtoGray = (r: number, g: number, b: number) => {
	return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
controls.addEventListener( 'change', () => { 
	renderer.render(scene, camera); 
}); 


// https://en.wikipedia.org/wiki/Digital_elevation_model

// https://www.food4rhino.com/app/numerical-mapping-utility
// https://www.food4rhino.com/app/numerical-image-utility

const path0 = 'https://upload.wikimedia.org/wikipedia/commons/5/57/Heightmap.png';

const path1 = 'https://lh3.googleusercontent.com/proxy/lv9EK2cYgbmfUbbVefYYAdiPC65ytOaq5skrOOtJwu-PX8T6xHO556cUyWOmT0r8cK2BiwL8bJIEi0AEOkujdjdYsTlsUvoJ03g9aA';

const path2 = 'https://upload.wikimedia.org/wikipedia/commons/1/15/Srtm_ramp2.world.21600x10800.jpg';


const img = new Image(); // Create new img element
img.src = path0; 
img.crossOrigin = "Anonymous";
img.onload = () => { 
	buildMeshByImg(img);
}


const buildMeshByImg = (img) => {
	// ................................................................................
	// your code goes here
	const xNum = 50;
	const yNum = 50;
	
	const width = img.width;
	const height = img.height;
	const zScale = 5.5;
	
	const xInterval = width / (xNum - 1.0);
  const yInterval = height / (yNum - 1.0);
	
	const canvas = document.createElement('canvas');
	canvas.width = width;
	canvas.height = height;
	div.append(canvas);
	const ctx = canvas.getContext('2d');
	ctx.drawImage(img, 0, 0);

	const geometry = new THREE.Geometry();
	geometry.vertices = [];
	 for (let y = 0; y < yNum; ++y) {
			for (let x = 0; x < xNum; ++x) {
				let col = ctx.getImageData(x * xInterval, y * yInterval, 1, 1).data;
				let zVal = rgbtoGray(col[0] /255, col[1] / 255, col[2] / 255);
				let z = -zVal * zScale;
				geometry.vertices.push(new THREE.Vector3( x - (xNum * 0.5) , y - (yNum * 0.5), z));
			}
	 }
	console.log(geometry)
	geometry.faces = [];
	 for (let y = 0; y < yNum - 1 ; ++y) {
			for (let x = 0; x < xNum  - 1; ++x) {
				let index = x + (y * xNum);
				geometry.faces.push(new THREE.Face3(index, index + 1, index + xNum));
				geometry.faces.push(new THREE.Face3(index + 1, index + xNum + 1 , index + xNum));
			}
	 }
	geometry.rotateX(Math.PI * 0.5);
  geometry.computeFaceNormals();
	const  material = new THREE.MeshNormalMaterial();
	material.side = THREE.DoubleSide;
	const mesh = new THREE.Mesh(geometry, material );
  scene.add(mesh);
	renderer.render(scene, camera); 
	// ................................................................................ 
}
              
            
!
999px

Console