webgl_geometry_terrain_fog.html 5.3 KB

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