1
0

webgl_geometry_minecraft.html 6.4 KB

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