webgl_geometry_terrain.html 5.3 KB

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