webgl_geometry_minecraft.html 7.9 KB

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