webgl_geometry_terrain.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - terrain + fog</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: #efd1b5;
  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 + fog demo <br />(left click: forward, right click: backward)</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 { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.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. const clock = new THREE.Clock();
  39. init();
  40. function init() {
  41. container = document.getElementById( 'container' );
  42. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0xefd1b5 );
  45. scene.fog = new THREE.FogExp2( 0xefd1b5, 0.0025 );
  46. const data = generateHeight( worldWidth, worldDepth );
  47. camera.position.set( 100, 800, - 800 );
  48. camera.lookAt( - 100, 810, - 800 );
  49. const geometry = new THREE.PlaneGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
  50. geometry.rotateX( - Math.PI / 2 );
  51. const vertices = geometry.attributes.position.array;
  52. for ( let i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
  53. vertices[ j + 1 ] = data[ i ] * 10;
  54. }
  55. texture = new THREE.CanvasTexture( generateTexture( data, worldWidth, worldDepth ) );
  56. texture.wrapS = THREE.ClampToEdgeWrapping;
  57. texture.wrapT = THREE.ClampToEdgeWrapping;
  58. texture.colorSpace = THREE.SRGBColorSpace;
  59. mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
  60. scene.add( mesh );
  61. renderer = new THREE.WebGLRenderer();
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.setAnimationLoop( animate );
  65. container.appendChild( renderer.domElement );
  66. controls = new FirstPersonControls( camera, renderer.domElement );
  67. controls.movementSpeed = 150;
  68. controls.lookSpeed = 0.1;
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. //
  72. window.addEventListener( 'resize', onWindowResize );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. controls.handleResize();
  79. }
  80. function generateHeight( width, height ) {
  81. let seed = Math.PI / 4;
  82. window.Math.random = function () {
  83. const x = Math.sin( seed ++ ) * 10000;
  84. return x - Math.floor( x );
  85. };
  86. const size = width * height, data = new Uint8Array( size );
  87. const 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. let context, image, imageData, shade;
  100. const vector3 = new THREE.Vector3( 0, 0, 0 );
  101. const sun = new THREE.Vector3( 1, 1, 1 );
  102. sun.normalize();
  103. const 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 ( let 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. const 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 ( let i = 0, l = imageData.length; i < l; i += 4 ) {
  132. const 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. render();
  143. stats.update();
  144. }
  145. function render() {
  146. controls.update( clock.getDelta() );
  147. renderer.render( scene, camera );
  148. }
  149. </script>
  150. </body>
  151. </html>