1
0

webgl_geometry_minecraft.html 6.2 KB

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