webgl_geometry_minecraft.html 6.2 KB

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