webgl_geometry_terrain_raycast.html 6.7 KB

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