webgl_geometry_terrain_raycast.html 6.4 KB

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