webgl_geometry_terrain_raycast.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - terrain</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #bfd1e5;
  11. color: #61443e;
  12. }
  13. a {
  14. color: #a06851;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container"></div>
  20. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl terrain raycasting demo</div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import Stats from 'three/addons/libs/stats.module.js';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
  34. let container, stats;
  35. let camera, controls, scene, renderer;
  36. let mesh, texture;
  37. const worldWidth = 256, worldDepth = 256,
  38. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
  39. let helper;
  40. const raycaster = new THREE.Raycaster();
  41. const pointer = new THREE.Vector2();
  42. init();
  43. function init() {
  44. container = document.getElementById( 'container' );
  45. container.innerHTML = '';
  46. renderer = new THREE.WebGLRenderer( { antialias: true } );
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. renderer.setAnimationLoop( animate );
  50. container.appendChild( renderer.domElement );
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0xbfd1e5 );
  53. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 10, 20000 );
  54. controls = new OrbitControls( camera, renderer.domElement );
  55. controls.minDistance = 1000;
  56. controls.maxDistance = 10000;
  57. controls.maxPolarAngle = Math.PI / 2;
  58. //
  59. const data = generateHeight( worldWidth, worldDepth );
  60. controls.target.y = data[ worldHalfWidth + worldHalfDepth * worldWidth ] + 500;
  61. camera.position.y = controls.target.y + 2000;
  62. camera.position.x = 2000;
  63. controls.update();
  64. const geometry = new THREE.PlaneGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
  65. geometry.rotateX( - Math.PI / 2 );
  66. const vertices = geometry.attributes.position.array;
  67. for ( let i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
  68. vertices[ j + 1 ] = data[ i ] * 10;
  69. }
  70. //
  71. texture = new THREE.CanvasTexture( generateTexture( data, worldWidth, worldDepth ) );
  72. texture.wrapS = THREE.ClampToEdgeWrapping;
  73. texture.wrapT = THREE.ClampToEdgeWrapping;
  74. texture.colorSpace = THREE.SRGBColorSpace;
  75. mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
  76. scene.add( mesh );
  77. const geometryHelper = new THREE.ConeGeometry( 20, 100, 3 );
  78. geometryHelper.translate( 0, 50, 0 );
  79. geometryHelper.rotateX( Math.PI / 2 );
  80. helper = new THREE.Mesh( geometryHelper, new THREE.MeshNormalMaterial() );
  81. scene.add( helper );
  82. container.addEventListener( 'pointermove', onPointerMove );
  83. stats = new Stats();
  84. container.appendChild( stats.dom );
  85. //
  86. window.addEventListener( 'resize', onWindowResize );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. function generateHeight( width, height ) {
  94. const size = width * height, data = new Uint8Array( size ),
  95. perlin = new ImprovedNoise(), z = Math.random() * 100;
  96. let quality = 1;
  97. for ( let j = 0; j < 4; j ++ ) {
  98. for ( let i = 0; i < size; i ++ ) {
  99. const x = i % width, y = ~ ~ ( i / width );
  100. data[ i ] += Math.abs( perlin.noise( x / quality, y / quality, z ) * quality * 1.75 );
  101. }
  102. quality *= 5;
  103. }
  104. return data;
  105. }
  106. function generateTexture( data, width, height ) {
  107. // bake lighting into texture
  108. let context, image, imageData, shade;
  109. const vector3 = new THREE.Vector3( 0, 0, 0 );
  110. const sun = new THREE.Vector3( 1, 1, 1 );
  111. sun.normalize();
  112. const canvas = document.createElement( 'canvas' );
  113. canvas.width = width;
  114. canvas.height = height;
  115. context = canvas.getContext( '2d' );
  116. context.fillStyle = '#000';
  117. context.fillRect( 0, 0, width, height );
  118. image = context.getImageData( 0, 0, canvas.width, canvas.height );
  119. imageData = image.data;
  120. for ( let i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) {
  121. vector3.x = data[ j - 2 ] - data[ j + 2 ];
  122. vector3.y = 2;
  123. vector3.z = data[ j - width * 2 ] - data[ j + width * 2 ];
  124. vector3.normalize();
  125. shade = vector3.dot( sun );
  126. imageData[ i ] = ( 96 + shade * 128 ) * ( 0.5 + data[ j ] * 0.007 );
  127. imageData[ i + 1 ] = ( 32 + shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  128. imageData[ i + 2 ] = ( shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  129. }
  130. context.putImageData( image, 0, 0 );
  131. // Scaled 4x
  132. const canvasScaled = document.createElement( 'canvas' );
  133. canvasScaled.width = width * 4;
  134. canvasScaled.height = height * 4;
  135. context = canvasScaled.getContext( '2d' );
  136. context.scale( 4, 4 );
  137. context.drawImage( canvas, 0, 0 );
  138. image = context.getImageData( 0, 0, canvasScaled.width, canvasScaled.height );
  139. imageData = image.data;
  140. for ( let i = 0, l = imageData.length; i < l; i += 4 ) {
  141. const v = ~ ~ ( Math.random() * 5 );
  142. imageData[ i ] += v;
  143. imageData[ i + 1 ] += v;
  144. imageData[ i + 2 ] += v;
  145. }
  146. context.putImageData( image, 0, 0 );
  147. return canvasScaled;
  148. }
  149. //
  150. function animate() {
  151. render();
  152. stats.update();
  153. }
  154. function render() {
  155. renderer.render( scene, camera );
  156. }
  157. function onPointerMove( event ) {
  158. pointer.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
  159. pointer.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
  160. raycaster.setFromCamera( pointer, camera );
  161. // See if the ray from the camera into the world hits one of our meshes
  162. const intersects = raycaster.intersectObject( mesh );
  163. // Toggle rotation bool for meshes that we clicked
  164. if ( intersects.length > 0 ) {
  165. helper.position.set( 0, 0, 0 );
  166. helper.lookAt( intersects[ 0 ].face.normal );
  167. helper.position.copy( intersects[ 0 ].point );
  168. }
  169. }
  170. </script>
  171. </body>
  172. </html>