webgl_geometry_minecraft.html 8.1 KB

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