123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - geometry - terrain</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #bfd1e5;
- color: #61443e;
- }
- a {
- color: #a06851;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl terrain raycasting demo</div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { ImprovedNoise } from './jsm/math/ImprovedNoise.js';
- var container, stats;
- var camera, controls, scene, renderer;
- var mesh, texture;
- var worldWidth = 256, worldDepth = 256,
- worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
- var helper;
- var raycaster = new THREE.Raycaster();
- var mouse = new THREE.Vector2();
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- container.innerHTML = "";
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xbfd1e5 );
- camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 10, 20000 );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 1000;
- controls.maxDistance = 10000;
- controls.maxPolarAngle = Math.PI / 2;
- //
- var data = generateHeight( worldWidth, worldDepth );
- controls.target.y = data[ worldHalfWidth + worldHalfDepth * worldWidth ] + 500;
- camera.position.y = controls.target.y + 2000;
- camera.position.x = 2000;
- controls.update();
- var geometry = new THREE.PlaneBufferGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
- geometry.rotateX( - Math.PI / 2 );
- var vertices = geometry.attributes.position.array;
- for ( var i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
- vertices[ j + 1 ] = data[ i ] * 10;
- }
- geometry.computeFaceNormals(); // needed for helper
- //
- texture = new THREE.CanvasTexture( generateTexture( data, worldWidth, worldDepth ) );
- texture.wrapS = THREE.ClampToEdgeWrapping;
- texture.wrapT = THREE.ClampToEdgeWrapping;
- mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
- scene.add( mesh );
- var geometry = new THREE.ConeBufferGeometry( 20, 100, 3 );
- geometry.translate( 0, 50, 0 );
- geometry.rotateX( Math.PI / 2 );
- helper = new THREE.Mesh( geometry, new THREE.MeshNormalMaterial() );
- scene.add( helper );
- container.addEventListener( 'mousemove', onMouseMove, false );
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function generateHeight( width, height ) {
- var size = width * height, data = new Uint8Array( size ),
- perlin = new ImprovedNoise(), quality = 1, z = Math.random() * 100;
- for ( var j = 0; j < 4; j ++ ) {
- for ( var i = 0; i < size; i ++ ) {
- var x = i % width, y = ~ ~ ( i / width );
- data[ i ] += Math.abs( perlin.noise( x / quality, y / quality, z ) * quality * 1.75 );
- }
- quality *= 5;
- }
- return data;
- }
- function generateTexture( data, width, height ) {
- // bake lighting into texture
- var canvas, canvasScaled, context, image, imageData, vector3, sun, shade;
- vector3 = new THREE.Vector3( 0, 0, 0 );
- sun = new THREE.Vector3( 1, 1, 1 );
- sun.normalize();
- canvas = document.createElement( 'canvas' );
- canvas.width = width;
- canvas.height = height;
- context = canvas.getContext( '2d' );
- context.fillStyle = '#000';
- context.fillRect( 0, 0, width, height );
- image = context.getImageData( 0, 0, canvas.width, canvas.height );
- imageData = image.data;
- for ( var i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) {
- vector3.x = data[ j - 2 ] - data[ j + 2 ];
- vector3.y = 2;
- vector3.z = data[ j - width * 2 ] - data[ j + width * 2 ];
- vector3.normalize();
- shade = vector3.dot( sun );
- imageData[ i ] = ( 96 + shade * 128 ) * ( 0.5 + data[ j ] * 0.007 );
- imageData[ i + 1 ] = ( 32 + shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
- imageData[ i + 2 ] = ( shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
- }
- context.putImageData( image, 0, 0 );
- // Scaled 4x
- canvasScaled = document.createElement( 'canvas' );
- canvasScaled.width = width * 4;
- canvasScaled.height = height * 4;
- context = canvasScaled.getContext( '2d' );
- context.scale( 4, 4 );
- context.drawImage( canvas, 0, 0 );
- image = context.getImageData( 0, 0, canvasScaled.width, canvasScaled.height );
- imageData = image.data;
- for ( var i = 0, l = imageData.length; i < l; i += 4 ) {
- var v = ~ ~ ( Math.random() * 5 );
- imageData[ i ] += v;
- imageData[ i + 1 ] += v;
- imageData[ i + 2 ] += v;
- }
- context.putImageData( image, 0, 0 );
- return canvasScaled;
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- renderer.render( scene, camera );
- }
- function onMouseMove( event ) {
- mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
- mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
- raycaster.setFromCamera( mouse, camera );
- // See if the ray from the camera into the world hits one of our meshes
- var intersects = raycaster.intersectObject( mesh );
- // Toggle rotation bool for meshes that we clicked
- if ( intersects.length > 0 ) {
- helper.position.set( 0, 0, 0 );
- helper.lookAt( intersects[ 0 ].face.normal );
- helper.position.copy( intersects[ 0 ].point );
- }
- }
- </script>
- </body>
- </html>
|