webgl_geometry_terrain.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import Stats from './jsm/libs/stats.module.js';
  34. import { FirstPersonControls } from './jsm/controls/FirstPersonControls.js';
  35. import { ImprovedNoise } from './jsm/math/ImprovedNoise.js';
  36. let container, stats;
  37. let camera, controls, scene, renderer;
  38. let mesh, texture;
  39. const worldWidth = 256, worldDepth = 256;
  40. const clock = new THREE.Clock();
  41. init();
  42. animate();
  43. function init() {
  44. container = document.getElementById( 'container' );
  45. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0xefd1b5 );
  48. scene.fog = new THREE.FogExp2( 0xefd1b5, 0.0025 );
  49. const data = generateHeight( worldWidth, worldDepth );
  50. camera.position.set( 100, 800, - 800 );
  51. camera.lookAt( - 100, 810, - 800 );
  52. const geometry = new THREE.PlaneGeometry( 7500, 7500, worldWidth - 1, worldDepth - 1 );
  53. geometry.rotateX( - Math.PI / 2 );
  54. const vertices = geometry.attributes.position.array;
  55. for ( let i = 0, j = 0, l = vertices.length; i < l; i ++, j += 3 ) {
  56. vertices[ j + 1 ] = data[ i ] * 10;
  57. }
  58. texture = new THREE.CanvasTexture( generateTexture( data, worldWidth, worldDepth ) );
  59. texture.wrapS = THREE.ClampToEdgeWrapping;
  60. texture.wrapT = THREE.ClampToEdgeWrapping;
  61. mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
  62. scene.add( mesh );
  63. renderer = new THREE.WebGLRenderer();
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. container.appendChild( renderer.domElement );
  67. controls = new FirstPersonControls( camera, renderer.domElement );
  68. controls.movementSpeed = 150;
  69. controls.lookSpeed = 0.1;
  70. stats = new Stats();
  71. container.appendChild( stats.dom );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. controls.handleResize();
  80. }
  81. function generateHeight( width, height ) {
  82. let seed = Math.PI / 4;
  83. window.Math.random = function () {
  84. const x = Math.sin( seed ++ ) * 10000;
  85. return x - Math.floor( x );
  86. };
  87. const size = width * height, data = new Uint8Array( size );
  88. const perlin = new ImprovedNoise(), z = Math.random() * 100;
  89. let quality = 1;
  90. for ( let j = 0; j < 4; j ++ ) {
  91. for ( let i = 0; i < size; i ++ ) {
  92. const x = i % width, y = ~ ~ ( i / width );
  93. data[ i ] += Math.abs( perlin.noise( x / quality, y / quality, z ) * quality * 1.75 );
  94. }
  95. quality *= 5;
  96. }
  97. return data;
  98. }
  99. function generateTexture( data, width, height ) {
  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. controls.update( clock.getDelta() );
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>