webgl_geometry_minecraft.html 5.7 KB

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