webgl_geometry_minecraft.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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://threejs.org" 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.min.js"></script>
  37. <script src="js/controls/FirstPersonControls.js"></script>
  38. <script src="js/ImprovedNoise.js"></script>
  39. <script src="js/Detector.js"></script>
  40. <script src="js/libs/stats.min.js"></script>
  41. <script src="js/wip/TypedGeometry.js"></script>
  42. <script src="js/wip/IndexedTypedGeometry.js"></script>
  43. <script src="js/wip/PlaneTypedGeometry.js"></script>
  44. <script>
  45. if ( ! Detector.webgl ) {
  46. Detector.addGetWebGLMessage();
  47. document.getElementById( 'container' ).innerHTML = "";
  48. }
  49. var container, stats;
  50. var camera, controls, scene, renderer;
  51. var mesh;
  52. var worldWidth = 128, worldDepth = 128,
  53. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2,
  54. data = generateHeight( worldWidth, worldDepth );
  55. var clock = new THREE.Clock();
  56. init();
  57. animate();
  58. function init() {
  59. container = document.getElementById( 'container' );
  60. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  61. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  62. controls = new THREE.FirstPersonControls( camera );
  63. controls.movementSpeed = 1000;
  64. controls.lookSpeed = 0.125;
  65. controls.lookVertical = true;
  66. scene = new THREE.Scene();
  67. // sides
  68. var matrix = new THREE.Matrix4();
  69. var pxGeometry = new THREE.PlaneTypedGeometry( 100, 100 );
  70. pxGeometry.uvs[ 1 ] = 0.5;
  71. pxGeometry.uvs[ 3 ] = 0.5;
  72. pxGeometry.applyMatrix( matrix.makeRotationY( Math.PI / 2 ) );
  73. pxGeometry.applyMatrix( matrix.makeTranslation( 50, 0, 0 ) );
  74. var nxGeometry = new THREE.PlaneTypedGeometry( 100, 100 );
  75. nxGeometry.uvs[ 1 ] = 0.5;
  76. nxGeometry.uvs[ 3 ] = 0.5;
  77. nxGeometry.applyMatrix( matrix.makeRotationY( - Math.PI / 2 ) );
  78. nxGeometry.applyMatrix( matrix.makeTranslation( - 50, 0, 0 ) );
  79. var pyGeometry = new THREE.PlaneTypedGeometry( 100, 100 );
  80. pyGeometry.uvs[ 5 ] = 0.5;
  81. pyGeometry.uvs[ 7 ] = 0.5;
  82. pyGeometry.applyMatrix( matrix.makeRotationX( - Math.PI / 2 ) );
  83. pyGeometry.applyMatrix( matrix.makeTranslation( 0, 50, 0 ) );
  84. var pzGeometry = new THREE.PlaneTypedGeometry( 100, 100 );
  85. pzGeometry.uvs[ 1 ] = 0.5;
  86. pzGeometry.uvs[ 3 ] = 0.5;
  87. pzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, 50 ) );
  88. var nzGeometry = new THREE.PlaneTypedGeometry( 100, 100 );
  89. nzGeometry.uvs[ 1 ] = 0.5;
  90. nzGeometry.uvs[ 3 ] = 0.5;
  91. nzGeometry.applyMatrix( matrix.makeRotationY( Math.PI ) );
  92. nzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, -50 ) );
  93. //
  94. var geometry = new THREE.TypedGeometry( worldWidth * worldDepth * 2 * 5 ); // 2 triangles, 5 possible sides
  95. for ( var z = 0; z < worldDepth; z ++ ) {
  96. for ( var x = 0; x < worldWidth; x ++ ) {
  97. var h = getY( x, z );
  98. matrix.makeTranslation(
  99. x * 100 - worldHalfWidth * 100,
  100. h * 100,
  101. z * 100 - worldHalfDepth * 100
  102. );
  103. var px = getY( x + 1, z );
  104. var nx = getY( x - 1, z );
  105. var pz = getY( x, z + 1 );
  106. var nz = getY( x, z - 1 );
  107. geometry.merge( pyGeometry, matrix );
  108. if ( ( px != h && px != h + 1 ) || x == 0 ) {
  109. geometry.merge( pxGeometry, matrix );
  110. }
  111. if ( ( nx != h && nx != h + 1 ) || x == worldWidth - 1 ) {
  112. geometry.merge( nxGeometry, matrix );
  113. }
  114. if ( ( pz != h && pz != h + 1 ) || z == worldDepth - 1 ) {
  115. geometry.merge( pzGeometry, matrix );
  116. }
  117. if ( ( nz != h && nz != h + 1 ) || z == 0 ) {
  118. geometry.merge( nzGeometry, matrix );
  119. }
  120. }
  121. }
  122. geometry.computeBoundingSphere();
  123. var texture = THREE.ImageUtils.loadTexture( 'textures/minecraft/atlas.png' );
  124. texture.magFilter = THREE.NearestFilter;
  125. texture.minFilter = THREE.LinearMipMapLinearFilter;
  126. var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: texture, ambient: 0xbbbbbb } ) );
  127. scene.add( mesh );
  128. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  129. scene.add( ambientLight );
  130. var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  131. directionalLight.position.set( 1, 1, 0.5 ).normalize();
  132. scene.add( directionalLight );
  133. renderer = new THREE.WebGLRenderer();
  134. renderer.setClearColor( 0xbfd1e5, 1 );
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. container.innerHTML = "";
  137. container.appendChild( renderer.domElement );
  138. stats = new Stats();
  139. stats.domElement.style.position = 'absolute';
  140. stats.domElement.style.top = '0px';
  141. container.appendChild( stats.domElement );
  142. //
  143. window.addEventListener( 'resize', onWindowResize, false );
  144. }
  145. function onWindowResize() {
  146. camera.aspect = window.innerWidth / window.innerHeight;
  147. camera.updateProjectionMatrix();
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. controls.handleResize();
  150. }
  151. function generateHeight( width, height ) {
  152. var data = [], perlin = new ImprovedNoise(),
  153. size = width * height, quality = 2, z = Math.random() * 100;
  154. for ( var j = 0; j < 4; j ++ ) {
  155. if ( j == 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  156. for ( var i = 0; i < size; i ++ ) {
  157. var x = i % width, y = ( i / width ) | 0;
  158. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  159. }
  160. quality *= 4
  161. }
  162. return data;
  163. }
  164. function getY( x, z ) {
  165. return ( data[ x + z * worldWidth ] * 0.2 ) | 0;
  166. }
  167. //
  168. function animate() {
  169. requestAnimationFrame( animate );
  170. render();
  171. stats.update();
  172. }
  173. function render() {
  174. controls.update( clock.getDelta() );
  175. renderer.render( scene, camera );
  176. }
  177. </script>
  178. </body>
  179. </html>