webgl_geometry_minecraft.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. <style>
  8. body {
  9. color: #61443e;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #bfd1e5;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #a06851;
  24. }
  25. #oldie {
  26. background:rgb(100,0,0) !important;
  27. color:#fff !important;
  28. margin-top:10em !important;
  29. }
  30. #oldie a { color:#fff }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="container"><br /><br /><br /><br /><br />Generating world...</div>
  35. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - <a href="http://www.minecraft.net/" target="_blank">minecraft</a> demo. featuring <a href="http://painterlypack.net/" target="_blank">painterly pack</a><br />(left click: forward, right click: backward)</div>
  36. <script src="../build/Three.js"></script>
  37. <script src="js/ImprovedNoise.js"></script>
  38. <script src="js/Detector.js"></script>
  39. <script src="js/Stats.js"></script>
  40. <script>
  41. if ( ! Detector.webgl ) {
  42. Detector.addGetWebGLMessage();
  43. document.getElementById( 'container' ).innerHTML = "";
  44. }
  45. var container, stats;
  46. var camera, controls, scene, renderer;
  47. var mesh;
  48. var worldWidth = 128, worldDepth = 128,
  49. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2,
  50. data = generateHeight( worldWidth, worldDepth );
  51. var clock = new THREE.Clock();
  52. init();
  53. animate();
  54. function init() {
  55. container = document.getElementById( 'container' );
  56. scene = new THREE.Scene();
  57. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  58. scene.add( camera );
  59. controls = new THREE.FirstPersonControls( camera );
  60. controls.movementSpeed = 1000;
  61. controls.lookSpeed = 0.125;
  62. controls.lookVertical = true;
  63. var grass_dirt = loadTexture( 'textures/minecraft/grass_dirt.png' ),
  64. grass = loadTexture( 'textures/minecraft/grass.png' ),
  65. dirt = loadTexture( 'textures/minecraft/dirt.png' );
  66. var materials = [
  67. grass_dirt, // right
  68. grass_dirt, // left
  69. grass, // top
  70. dirt, // bottom
  71. grass_dirt, // back
  72. grass_dirt // front
  73. ];
  74. var h, h2, px, nx, pz, nz, cubes = [];
  75. for ( var i = 0; i < 16; i++ ) {
  76. px = ( i & 8 ) == 8;
  77. nx = ( i & 4 ) == 4;
  78. pz = ( i & 2 ) == 2;
  79. nz = ( i & 1 ) == 1;
  80. cubes[ i ] = new THREE.CubeGeometry( 100, 100, 100, 1, 1, 1, materials, { px: px, nx: nx, py: true, ny: false, pz: pz, nz: nz } );
  81. }
  82. var geometry = new THREE.Geometry();
  83. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  84. for ( var z = 0; z < worldDepth; z ++ ) {
  85. for ( var x = 0; x < worldWidth; x ++ ) {
  86. px = nx = pz = nz = 0;
  87. h = getY( x, z );
  88. h2 = getY( x + 1, z );
  89. px = ( h2 != h && h2 != h + 1 ) || x == 0 ? 1 : 0;
  90. h2 = getY( x - 1, z );
  91. nx = ( h2 != h && h2 != h + 1 ) || x == worldWidth - 1 ? 1 : 0;
  92. h2 = getY( x, z + 1 );
  93. pz = ( h2 != h && h2 != h + 1 ) || z == worldDepth - 1 ? 1 : 0;
  94. h2 = getY( x, z - 1 );
  95. nz = ( h2 != h && h2 != h + 1 ) || z == 0 ? 1 : 0;
  96. mesh = new THREE.Mesh( cubes[ px * 8 + nx * 4 + pz * 2 + nz ] );
  97. mesh.position.x = x * 100 - worldHalfWidth * 100;
  98. mesh.position.y = h * 100;
  99. mesh.position.z = z * 100 - worldHalfDepth * 100;
  100. THREE.GeometryUtils.merge( geometry, mesh );
  101. }
  102. }
  103. mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
  104. scene.add( mesh );
  105. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  106. scene.add( ambientLight );
  107. var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  108. directionalLight.position.set( 1, 1, 0.5 ).normalize();
  109. scene.add( directionalLight );
  110. renderer = new THREE.WebGLRenderer();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. container.innerHTML = "";
  113. container.appendChild( renderer.domElement );
  114. stats = new Stats();
  115. stats.domElement.style.position = 'absolute';
  116. stats.domElement.style.top = '0px';
  117. container.appendChild( stats.domElement );
  118. }
  119. function loadTexture( path ) {
  120. var image = new Image();
  121. image.onload = function () { texture.needsUpdate = true; };
  122. image.src = path;
  123. var texture = new THREE.Texture( image, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter );
  124. return new THREE.MeshLambertMaterial( { map: texture, ambient: 0xbbbbbb } );
  125. }
  126. function generateHeight( width, height ) {
  127. var data = [], perlin = new ImprovedNoise(),
  128. size = width * height, quality = 2, z = Math.random() * 100;
  129. for ( var j = 0; j < 4; j ++ ) {
  130. if ( j == 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  131. for ( var i = 0; i < size; i ++ ) {
  132. var x = i % width, y = ~~ ( i / width );
  133. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  134. }
  135. quality *= 4
  136. }
  137. return data;
  138. }
  139. function getY( x, z ) {
  140. return ~~( data[ x + z * worldWidth ] * 0.2 );
  141. }
  142. //
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. render();
  146. stats.update();
  147. }
  148. function render() {
  149. controls.update( clock.getDelta() );
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>