webgl_geometry_minecraft.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - minecraft</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> - <a href="http://www.minecraft.net/" target="_blank" rel="noopener">minecraft</a> demo. featuring <a href="http://painterlypack.net/" target="_blank" rel="noopener">painterly pack</a><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. import * as BufferGeometryUtils from './jsm/utils/BufferGeometryUtils.js';
  37. let container, stats;
  38. let camera, controls, scene, renderer;
  39. const worldWidth = 128, worldDepth = 128;
  40. const worldHalfWidth = worldWidth / 2;
  41. const worldHalfDepth = worldDepth / 2;
  42. const data = generateHeight( worldWidth, worldDepth );
  43. const clock = new THREE.Clock();
  44. init();
  45. animate();
  46. function init() {
  47. container = document.getElementById( 'container' );
  48. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  49. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0xbfd1e5 );
  52. // sides
  53. const matrix = new THREE.Matrix4();
  54. const pxGeometry = new THREE.PlaneGeometry( 100, 100 );
  55. pxGeometry.attributes.uv.array[ 1 ] = 0.5;
  56. pxGeometry.attributes.uv.array[ 3 ] = 0.5;
  57. pxGeometry.rotateY( Math.PI / 2 );
  58. pxGeometry.translate( 50, 0, 0 );
  59. const nxGeometry = new THREE.PlaneGeometry( 100, 100 );
  60. nxGeometry.attributes.uv.array[ 1 ] = 0.5;
  61. nxGeometry.attributes.uv.array[ 3 ] = 0.5;
  62. nxGeometry.rotateY( - Math.PI / 2 );
  63. nxGeometry.translate( - 50, 0, 0 );
  64. const pyGeometry = new THREE.PlaneGeometry( 100, 100 );
  65. pyGeometry.attributes.uv.array[ 5 ] = 0.5;
  66. pyGeometry.attributes.uv.array[ 7 ] = 0.5;
  67. pyGeometry.rotateX( - Math.PI / 2 );
  68. pyGeometry.translate( 0, 50, 0 );
  69. const pzGeometry = new THREE.PlaneGeometry( 100, 100 );
  70. pzGeometry.attributes.uv.array[ 1 ] = 0.5;
  71. pzGeometry.attributes.uv.array[ 3 ] = 0.5;
  72. pzGeometry.translate( 0, 0, 50 );
  73. const nzGeometry = new THREE.PlaneGeometry( 100, 100 );
  74. nzGeometry.attributes.uv.array[ 1 ] = 0.5;
  75. nzGeometry.attributes.uv.array[ 3 ] = 0.5;
  76. nzGeometry.rotateY( Math.PI );
  77. nzGeometry.translate( 0, 0, - 50 );
  78. //
  79. const geometries = [];
  80. for ( let z = 0; z < worldDepth; z ++ ) {
  81. for ( let x = 0; x < worldWidth; x ++ ) {
  82. const h = getY( x, z );
  83. matrix.makeTranslation(
  84. x * 100 - worldHalfWidth * 100,
  85. h * 100,
  86. z * 100 - worldHalfDepth * 100
  87. );
  88. const px = getY( x + 1, z );
  89. const nx = getY( x - 1, z );
  90. const pz = getY( x, z + 1 );
  91. const nz = getY( x, z - 1 );
  92. geometries.push( pyGeometry.clone().applyMatrix4( matrix ) );
  93. if ( ( px !== h && px !== h + 1 ) || x === 0 ) {
  94. geometries.push( pxGeometry.clone().applyMatrix4( matrix ) );
  95. }
  96. if ( ( nx !== h && nx !== h + 1 ) || x === worldWidth - 1 ) {
  97. geometries.push( nxGeometry.clone().applyMatrix4( matrix ) );
  98. }
  99. if ( ( pz !== h && pz !== h + 1 ) || z === worldDepth - 1 ) {
  100. geometries.push( pzGeometry.clone().applyMatrix4( matrix ) );
  101. }
  102. if ( ( nz !== h && nz !== h + 1 ) || z === 0 ) {
  103. geometries.push( nzGeometry.clone().applyMatrix4( matrix ) );
  104. }
  105. }
  106. }
  107. const geometry = BufferGeometryUtils.mergeBufferGeometries( geometries );
  108. geometry.computeBoundingSphere();
  109. const texture = new THREE.TextureLoader().load( 'textures/minecraft/atlas.png' );
  110. texture.magFilter = THREE.NearestFilter;
  111. const mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: texture, side: THREE.DoubleSide } ) );
  112. scene.add( mesh );
  113. const ambientLight = new THREE.AmbientLight( 0xcccccc );
  114. scene.add( ambientLight );
  115. const directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  116. directionalLight.position.set( 1, 1, 0.5 ).normalize();
  117. scene.add( directionalLight );
  118. renderer = new THREE.WebGLRenderer( { antialias: true } );
  119. renderer.setPixelRatio( window.devicePixelRatio );
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. container.appendChild( renderer.domElement );
  122. controls = new FirstPersonControls( camera, renderer.domElement );
  123. controls.movementSpeed = 1000;
  124. controls.lookSpeed = 0.125;
  125. controls.lookVertical = true;
  126. stats = new Stats();
  127. container.appendChild( stats.dom );
  128. //
  129. window.addEventListener( 'resize', onWindowResize );
  130. }
  131. function onWindowResize() {
  132. camera.aspect = window.innerWidth / window.innerHeight;
  133. camera.updateProjectionMatrix();
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. controls.handleResize();
  136. }
  137. function generateHeight( width, height ) {
  138. const data = [], perlin = new ImprovedNoise(),
  139. size = width * height, z = Math.random() * 100;
  140. let quality = 2;
  141. for ( let j = 0; j < 4; j ++ ) {
  142. if ( j === 0 ) for ( let i = 0; i < size; i ++ ) data[ i ] = 0;
  143. for ( let i = 0; i < size; i ++ ) {
  144. const x = i % width, y = ( i / width ) | 0;
  145. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  146. }
  147. quality *= 4;
  148. }
  149. return data;
  150. }
  151. function getY( x, z ) {
  152. return ( data[ x + z * worldWidth ] * 0.15 ) | 0;
  153. }
  154. //
  155. function animate() {
  156. requestAnimationFrame( animate );
  157. render();
  158. stats.update();
  159. }
  160. function render() {
  161. controls.update( clock.getDelta() );
  162. renderer.render( scene, camera );
  163. }
  164. </script>
  165. </body>
  166. </html>