webgl_geometry_terrain_raycast.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
  78. scene.add( mesh );
  79. const geometryHelper = new THREE.ConeGeometry( 20, 100, 3 );
  80. geometryHelper.translate( 0, 50, 0 );
  81. geometryHelper.rotateX( Math.PI / 2 );
  82. helper = new THREE.Mesh( geometryHelper, new THREE.MeshNormalMaterial() );
  83. scene.add( helper );
  84. container.addEventListener( 'pointermove', onPointerMove );
  85. stats = new Stats();
  86. container.appendChild( stats.dom );
  87. //
  88. window.addEventListener( 'resize', onWindowResize );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function generateHeight( width, height ) {
  96. const size = width * height, data = new Uint8Array( size ),
  97. perlin = new ImprovedNoise(), z = Math.random() * 100;
  98. let quality = 1;
  99. for ( let j = 0; j < 4; j ++ ) {
  100. for ( let i = 0; i < size; i ++ ) {
  101. const x = i % width, y = ~ ~ ( i / width );
  102. data[ i ] += Math.abs( perlin.noise( x / quality, y / quality, z ) * quality * 1.75 );
  103. }
  104. quality *= 5;
  105. }
  106. return data;
  107. }
  108. function generateTexture( data, width, height ) {
  109. // bake lighting into texture
  110. let context, image, imageData, shade;
  111. const vector3 = new THREE.Vector3( 0, 0, 0 );
  112. const sun = new THREE.Vector3( 1, 1, 1 );
  113. sun.normalize();
  114. const canvas = document.createElement( 'canvas' );
  115. canvas.width = width;
  116. canvas.height = height;
  117. context = canvas.getContext( '2d' );
  118. context.fillStyle = '#000';
  119. context.fillRect( 0, 0, width, height );
  120. image = context.getImageData( 0, 0, canvas.width, canvas.height );
  121. imageData = image.data;
  122. for ( let i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) {
  123. vector3.x = data[ j - 2 ] - data[ j + 2 ];
  124. vector3.y = 2;
  125. vector3.z = data[ j - width * 2 ] - data[ j + width * 2 ];
  126. vector3.normalize();
  127. shade = vector3.dot( sun );
  128. imageData[ i ] = ( 96 + shade * 128 ) * ( 0.5 + data[ j ] * 0.007 );
  129. imageData[ i + 1 ] = ( 32 + shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  130. imageData[ i + 2 ] = ( shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  131. }
  132. context.putImageData( image, 0, 0 );
  133. // Scaled 4x
  134. const canvasScaled = document.createElement( 'canvas' );
  135. canvasScaled.width = width * 4;
  136. canvasScaled.height = height * 4;
  137. context = canvasScaled.getContext( '2d' );
  138. context.scale( 4, 4 );
  139. context.drawImage( canvas, 0, 0 );
  140. image = context.getImageData( 0, 0, canvasScaled.width, canvasScaled.height );
  141. imageData = image.data;
  142. for ( let i = 0, l = imageData.length; i < l; i += 4 ) {
  143. const v = ~ ~ ( Math.random() * 5 );
  144. imageData[ i ] += v;
  145. imageData[ i + 1 ] += v;
  146. imageData[ i + 2 ] += v;
  147. }
  148. context.putImageData( image, 0, 0 );
  149. return canvasScaled;
  150. }
  151. //
  152. function animate() {
  153. requestAnimationFrame( animate );
  154. render();
  155. stats.update();
  156. }
  157. function render() {
  158. renderer.render( scene, camera );
  159. }
  160. function onPointerMove( event ) {
  161. pointer.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
  162. pointer.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
  163. raycaster.setFromCamera( pointer, camera );
  164. // See if the ray from the camera into the world hits one of our meshes
  165. const intersects = raycaster.intersectObject( mesh );
  166. // Toggle rotation bool for meshes that we clicked
  167. if ( intersects.length > 0 ) {
  168. helper.position.set( 0, 0, 0 );
  169. helper.lookAt( intersects[ 0 ].face.normal );
  170. helper.position.copy( intersects[ 0 ].point );
  171. }
  172. }
  173. </script>
  174. </body>
  175. </html>