webgl_geometry_terrain.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. <!-- 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 { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.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. const clock = new THREE.Clock();
  42. init();
  43. animate();
  44. function init() {
  45. container = document.getElementById( 'container' );
  46. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  47. scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 0xefd1b5 );
  49. scene.fog = new THREE.FogExp2( 0xefd1b5, 0.0025 );
  50. const data = generateHeight( worldWidth, worldDepth );
  51. camera.position.set( 100, 800, - 800 );
  52. camera.lookAt( - 100, 810, - 800 );
  53. const geometry = new THREE.PlaneGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
  54. geometry.rotateX( - Math.PI / 2 );
  55. const vertices = geometry.attributes.position.array;
  56. for ( let i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
  57. vertices[ j + 1 ] = data[ i ] * 10;
  58. }
  59. texture = new THREE.CanvasTexture( generateTexture( data, worldWidth, worldDepth ) );
  60. texture.wrapS = THREE.ClampToEdgeWrapping;
  61. texture.wrapT = THREE.ClampToEdgeWrapping;
  62. texture.colorSpace = THREE.SRGBColorSpace;
  63. mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
  64. scene.add( mesh );
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. renderer.useLegacyLights = false;
  69. container.appendChild( renderer.domElement );
  70. controls = new FirstPersonControls( camera, renderer.domElement );
  71. controls.movementSpeed = 150;
  72. controls.lookSpeed = 0.1;
  73. stats = new Stats();
  74. container.appendChild( stats.dom );
  75. //
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. controls.handleResize();
  83. }
  84. function generateHeight( width, height ) {
  85. let seed = Math.PI / 4;
  86. window.Math.random = function () {
  87. const x = Math.sin( seed ++ ) * 10000;
  88. return x - Math.floor( x );
  89. };
  90. const size = width * height, data = new Uint8Array( size );
  91. const perlin = new ImprovedNoise(), z = Math.random() * 100;
  92. let quality = 1;
  93. for ( let j = 0; j < 4; j ++ ) {
  94. for ( let i = 0; i < size; i ++ ) {
  95. const x = i % width, y = ~ ~ ( i / width );
  96. data[ i ] += Math.abs( perlin.noise( x / quality, y / quality, z ) * quality * 1.75 );
  97. }
  98. quality *= 5;
  99. }
  100. return data;
  101. }
  102. function generateTexture( data, width, height ) {
  103. let context, image, imageData, shade;
  104. const vector3 = new THREE.Vector3( 0, 0, 0 );
  105. const sun = new THREE.Vector3( 1, 1, 1 );
  106. sun.normalize();
  107. const canvas = document.createElement( 'canvas' );
  108. canvas.width = width;
  109. canvas.height = height;
  110. context = canvas.getContext( '2d' );
  111. context.fillStyle = '#000';
  112. context.fillRect( 0, 0, width, height );
  113. image = context.getImageData( 0, 0, canvas.width, canvas.height );
  114. imageData = image.data;
  115. for ( let i = 0, j = 0, l = imageData.length; i < l; i += 4, j ++ ) {
  116. vector3.x = data[ j - 2 ] - data[ j + 2 ];
  117. vector3.y = 2;
  118. vector3.z = data[ j - width * 2 ] - data[ j + width * 2 ];
  119. vector3.normalize();
  120. shade = vector3.dot( sun );
  121. imageData[ i ] = ( 96 + shade * 128 ) * ( 0.5 + data[ j ] * 0.007 );
  122. imageData[ i + 1 ] = ( 32 + shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  123. imageData[ i + 2 ] = ( shade * 96 ) * ( 0.5 + data[ j ] * 0.007 );
  124. }
  125. context.putImageData( image, 0, 0 );
  126. // Scaled 4x
  127. const canvasScaled = document.createElement( 'canvas' );
  128. canvasScaled.width = width * 4;
  129. canvasScaled.height = height * 4;
  130. context = canvasScaled.getContext( '2d' );
  131. context.scale( 4, 4 );
  132. context.drawImage( canvas, 0, 0 );
  133. image = context.getImageData( 0, 0, canvasScaled.width, canvasScaled.height );
  134. imageData = image.data;
  135. for ( let i = 0, l = imageData.length; i < l; i += 4 ) {
  136. const v = ~ ~ ( Math.random() * 5 );
  137. imageData[ i ] += v;
  138. imageData[ i + 1 ] += v;
  139. imageData[ i + 2 ] += v;
  140. }
  141. context.putImageData( image, 0, 0 );
  142. return canvasScaled;
  143. }
  144. //
  145. function animate() {
  146. requestAnimationFrame( animate );
  147. render();
  148. stats.update();
  149. }
  150. function render() {
  151. controls.update( clock.getDelta() );
  152. renderer.render( scene, camera );
  153. }
  154. </script>
  155. </body>
  156. </html>