webgl_geometry_minecraft.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. var container, stats;
  28. var camera, controls, scene, renderer;
  29. var worldWidth = 128, worldDepth = 128;
  30. var worldHalfWidth = worldWidth / 2;
  31. var worldHalfDepth = worldDepth / 2;
  32. var data = generateHeight( worldWidth, worldDepth );
  33. var 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. var matrix = new THREE.Matrix4();
  44. var pxGeometry = new THREE.PlaneBufferGeometry( 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. var nxGeometry = new THREE.PlaneBufferGeometry( 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. var pyGeometry = new THREE.PlaneBufferGeometry( 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. var pzGeometry = new THREE.PlaneBufferGeometry( 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. var nzGeometry = new THREE.PlaneBufferGeometry( 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. var geometries = [];
  70. for ( var z = 0; z < worldDepth; z ++ ) {
  71. for ( var x = 0; x < worldWidth; x ++ ) {
  72. var h = getY( x, z );
  73. matrix.makeTranslation(
  74. x * 100 - worldHalfWidth * 100,
  75. h * 100,
  76. z * 100 - worldHalfDepth * 100
  77. );
  78. var px = getY( x + 1, z );
  79. var nx = getY( x - 1, z );
  80. var pz = getY( x, z + 1 );
  81. var 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. var geometry = BufferGeometryUtils.mergeBufferGeometries( geometries );
  98. geometry.computeBoundingSphere();
  99. var texture = new THREE.TextureLoader().load( 'textures/minecraft/atlas.png' );
  100. texture.magFilter = THREE.NearestFilter;
  101. var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: texture, side: THREE.DoubleSide } ) );
  102. scene.add( mesh );
  103. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  104. scene.add( ambientLight );
  105. var 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, false );
  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. var data = [], perlin = new ImprovedNoise(),
  129. size = width * height, quality = 2, z = Math.random() * 100;
  130. for ( var j = 0; j < 4; j ++ ) {
  131. if ( j === 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  132. for ( var i = 0; i < size; i ++ ) {
  133. var x = i % width, y = ( i / width ) | 0;
  134. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  135. }
  136. quality *= 4;
  137. }
  138. return data;
  139. }
  140. function getY( x, z ) {
  141. return ( data[ x + z * worldWidth ] * 0.2 ) | 0;
  142. }
  143. //
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. render();
  147. stats.update();
  148. }
  149. function render() {
  150. controls.update( clock.getDelta() );
  151. renderer.render( scene, camera );
  152. }
  153. </script>
  154. </body>
  155. </html>