webgl_geometry_terrain_raycast.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. var container, stats;
  27. var camera, controls, scene, renderer;
  28. var mesh, texture;
  29. var worldWidth = 256, worldDepth = 256,
  30. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
  31. var helper;
  32. var raycaster = new THREE.Raycaster();
  33. var 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. var 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. var geometry = new THREE.PlaneBufferGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
  57. geometry.rotateX( - Math.PI / 2 );
  58. var vertices = geometry.attributes.position.array;
  59. for ( var 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. var geometry = new THREE.ConeBufferGeometry( 20, 100, 3 );
  70. geometry.translate( 0, 50, 0 );
  71. geometry.rotateX( Math.PI / 2 );
  72. helper = new THREE.Mesh( geometry, new THREE.MeshNormalMaterial() );
  73. scene.add( helper );
  74. container.addEventListener( 'mousemove', onMouseMove, false );
  75. stats = new Stats();
  76. container.appendChild( stats.dom );
  77. //
  78. window.addEventListener( 'resize', onWindowResize, false );
  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. var size = width * height, data = new Uint8Array( size ),
  87. perlin = new ImprovedNoise(), quality = 1, z = Math.random() * 100;
  88. for ( var j = 0; j < 4; j ++ ) {
  89. for ( var i = 0; i < size; i ++ ) {
  90. var x = i % width, y = ~ ~ ( i / width );
  91. data[ i ] += Math.abs( perlin.noise( x / quality, y / quality, z ) * quality * 1.75 );
  92. }
  93. quality *= 5;
  94. }
  95. return data;
  96. }
  97. function generateTexture( data, width, height ) {
  98. // bake lighting into texture
  99. var canvas, canvasScaled, context, image, imageData, vector3, sun, shade;
  100. vector3 = new THREE.Vector3( 0, 0, 0 );
  101. sun = new THREE.Vector3( 1, 1, 1 );
  102. sun.normalize();
  103. canvas = document.createElement( 'canvas' );
  104. canvas.width = width;
  105. canvas.height = height;
  106. context = canvas.getContext( '2d' );
  107. context.fillStyle = '#000';
  108. context.fillRect( 0, 0, width, height );
  109. image = context.getImageData( 0, 0, canvas.width, canvas.height );
  110. imageData = image.data;
  111. for ( var i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) {
  112. vector3.x = data[ j - 2 ] - data[ j + 2 ];
  113. vector3.y = 2;
  114. vector3.z = data[ j - width * 2 ] - data[ j + width * 2 ];
  115. vector3.normalize();
  116. shade = vector3.dot( sun );
  117. imageData[ i ] = ( 96 + shade * 128 ) * ( 0.5 + data[ j ] * 0.007 );
  118. imageData[ i + 1 ] = ( 32 + shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  119. imageData[ i + 2 ] = ( shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  120. }
  121. context.putImageData( image, 0, 0 );
  122. // Scaled 4x
  123. canvasScaled = document.createElement( 'canvas' );
  124. canvasScaled.width = width * 4;
  125. canvasScaled.height = height * 4;
  126. context = canvasScaled.getContext( '2d' );
  127. context.scale( 4, 4 );
  128. context.drawImage( canvas, 0, 0 );
  129. image = context.getImageData( 0, 0, canvasScaled.width, canvasScaled.height );
  130. imageData = image.data;
  131. for ( var i = 0, l = imageData.length; i < l; i += 4 ) {
  132. var v = ~ ~ ( Math.random() * 5 );
  133. imageData[ i ] += v;
  134. imageData[ i + 1 ] += v;
  135. imageData[ i + 2 ] += v;
  136. }
  137. context.putImageData( image, 0, 0 );
  138. return canvasScaled;
  139. }
  140. //
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. render();
  144. stats.update();
  145. }
  146. function render() {
  147. renderer.render( scene, camera );
  148. }
  149. function onMouseMove( event ) {
  150. mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
  151. mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
  152. raycaster.setFromCamera( mouse, camera );
  153. // See if the ray from the camera into the world hits one of our meshes
  154. var intersects = raycaster.intersectObject( mesh );
  155. // Toggle rotation bool for meshes that we clicked
  156. if ( intersects.length > 0 ) {
  157. helper.position.set( 0, 0, 0 );
  158. helper.lookAt( intersects[ 0 ].face.normal );
  159. helper.position.copy( intersects[ 0 ].point );
  160. }
  161. }
  162. </script>
  163. </body>
  164. </html>