geometry_minecraft.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - 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. </style>
  25. </head>
  26. <body>
  27. <div id="container"><br /><br /><br /><br /><br />Generating world...</div>
  28. <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>
  29. <script type="text/javascript" src="js/Stats.js"></script>
  30. <script type="text/javascript" src="js/ImprovedNoise.js"></script>
  31. <script type="text/javascript" src="../build/Three.js"></script>
  32. <script type="text/javascript" src="../src/extras/GeometryUtils.js"></script>
  33. <script type="text/javascript" src="../src/extras/primitives/Cube.js"></script>
  34. <script type="text/javascript">
  35. var container, stats;
  36. var camera, scene, renderer;
  37. var mesh;
  38. var worldWidth = 128, worldDepth = 128,
  39. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2,
  40. data = generateHeight( worldWidth, worldDepth );
  41. var mouseX = 0, mouseY = 0,
  42. lat = 0, lon = 0, phy = 0, theta = 0;
  43. var direction = new THREE.Vector3(),
  44. moveForward = false, moveBackward = false;
  45. var windowHalfX = window.innerWidth / 2;
  46. var windowHalfY = window.innerHeight / 2;
  47. init();
  48. setInterval( loop, 1000 / 60 );
  49. function init() {
  50. container = document.getElementById( 'container' );
  51. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  52. camera.target.position.z = - 100;
  53. scene = new THREE.Scene();
  54. var grass_dirt = loadTexture( 'textures/minecraft/grass_dirt.png' ),
  55. grass = loadTexture( 'textures/minecraft/grass.png' ),
  56. dirt = loadTexture( 'textures/minecraft/dirt.png' );
  57. var materials = [
  58. grass_dirt, // right
  59. grass_dirt, // left
  60. grass, // top
  61. dirt, // bottom
  62. grass_dirt, // back
  63. grass_dirt // front
  64. ];
  65. var h, h2, px, nx, pz, nz, cubes = [];
  66. for ( var i = 0; i < 16; i++ ) {
  67. px = (i & 8) == 8;
  68. nx = (i & 4) == 4;
  69. pz = (i & 2) == 2;
  70. nz = (i & 1) == 1;
  71. cubes[ i ] = new Cube( 100, 100, 100, 1, 1, materials, false, { px: px, nx: nx, py: true, ny: false, pz: pz, nz: nz } );
  72. }
  73. var geometry = new THREE.Geometry();
  74. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  75. camera.target.position.y = camera.position.y;
  76. for ( var z = 0; z < worldDepth; z ++ ) {
  77. for ( var x = 0; x < worldWidth; x ++ ) {
  78. px = nx = pz = nz = 0;
  79. h = getY( x, z );
  80. h2 = getY( x - 1, z );
  81. px = ( h2 != h && h2 != h + 1 ) || x == 0 ? 1 : 0;
  82. h2 = getY( x + 1, z );
  83. nx = ( h2 != h && h2 != h + 1 ) || x == worldWidth - 1 ? 1 : 0;
  84. h2 = getY( x, z + 1 );
  85. pz = ( h2 != h && h2 != h + 1 ) || z == worldDepth - 1 ? 1 : 0;
  86. h2 = getY( x, z - 1 );
  87. nz = ( h2 != h && h2 != h + 1 ) || z == 0 ? 1 : 0;
  88. mesh = new THREE.Mesh( cubes[ px * 8 + nx * 4 + pz * 2 + nz ] );
  89. mesh.position.x = x * 100 - worldHalfWidth * 100;
  90. mesh.position.y = h * 100;
  91. mesh.position.z = z * 100 - worldHalfDepth * 100;
  92. GeometryUtils.merge( geometry, mesh );
  93. }
  94. }
  95. geometry.sortFacesByMaterial();
  96. mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
  97. scene.addObject( mesh );
  98. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  99. scene.addLight( ambientLight );
  100. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.5 );
  101. directionalLight.position.x = 1;
  102. directionalLight.position.y = 1;
  103. directionalLight.position.z = 0.5;
  104. directionalLight.position.normalize();
  105. scene.addLight( directionalLight );
  106. renderer = new THREE.WebGLRenderer();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. container.innerHTML = "";
  109. container.appendChild( renderer.domElement );
  110. stats = new Stats();
  111. stats.domElement.style.position = 'absolute';
  112. stats.domElement.style.top = '0px';
  113. container.appendChild( stats.domElement );
  114. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  115. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  116. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  117. document.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  118. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  119. document.addEventListener( 'keyup', onDocumentKeyUp, false );
  120. }
  121. function generateAO( image, sides ) {
  122. var c = document.createElement('canvas');
  123. c.width = image.width;
  124. c.height = image.height;
  125. c.getContext( "2d" ).drawImage( image, 0, 0 );
  126. return c;
  127. }
  128. function loadTexture( path ) {
  129. var image = new Image();
  130. image.onload = function () { this.loaded = true; };
  131. image.src = path;
  132. return new THREE.MeshLambertMaterial( { map: new THREE.Texture( image, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter ) } );
  133. }
  134. function generateHeight( width, height ) {
  135. var data = [], perlin = new ImprovedNoise(),
  136. size = width * height, quality = 2, z = Math.random() * 100;
  137. for ( var j = 0; j < 4; j ++ ) {
  138. if ( j == 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  139. for ( var i = 0; i < size; i ++ ) {
  140. var x = i % width, y = ~~ ( i / width );
  141. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  142. }
  143. quality *= 4
  144. }
  145. return data;
  146. }
  147. function getY( x, z ) {
  148. return ~~( data[ x + z * worldWidth ] * 0.2 );
  149. }
  150. function onDocumentMouseDown( event ) {
  151. event.preventDefault();
  152. event.stopPropagation();
  153. switch ( event.button ) {
  154. case 0: moveForward = true; break;
  155. case 2: moveBackward = true; break;
  156. }
  157. }
  158. function onDocumentMouseUp( event ) {
  159. event.preventDefault();
  160. event.stopPropagation();
  161. switch ( event.button ) {
  162. case 0: moveForward = false; break;
  163. case 2: moveBackward = false; break;
  164. }
  165. }
  166. function onDocumentMouseMove(event) {
  167. mouseX = event.clientX - windowHalfX;
  168. mouseY = event.clientY - windowHalfY;
  169. }
  170. function onDocumentKeyDown( event ) {
  171. switch( event.keyCode ) {
  172. case 38: /*↑*/ moveForward = true; break;
  173. case 40: /*↓*/ moveBackward = true; break;
  174. case 87: /*W*/ moveForward = true; break;
  175. case 83: /*S*/ moveBackward = true; break;
  176. }
  177. }
  178. function onDocumentKeyUp( event ) {
  179. switch( event.keyCode ) {
  180. case 38: /*↑*/ moveForward = false; break;
  181. case 40: /*↓*/ moveBackward = false; break;
  182. case 87: /*W*/ moveForward = false; break;
  183. case 83: /*S*/ moveBackward = false; break;
  184. }
  185. }
  186. function loop() {
  187. if ( moveForward ) camera.translateZ( - 15 );
  188. if ( moveBackward ) camera.translateZ( 15 );
  189. lon += mouseX * 0.005;
  190. lat -= mouseY * 0.005;
  191. lat = Math.max( - 85, Math.min( 85, lat ) );
  192. phi = ( 90 - lat ) * Math.PI / 180;
  193. theta = lon * Math.PI / 180;
  194. camera.target.position.x = 100 * Math.sin( phi ) * Math.cos( theta ) + camera.position.x;
  195. camera.target.position.y = 100 * Math.cos( phi ) + camera.position.y;
  196. camera.target.position.z = 100 * Math.sin( phi ) * Math.sin( theta ) + camera.position.z;
  197. renderer.render(scene, camera);
  198. stats.update();
  199. }
  200. </script>
  201. </body>
  202. </html>