webgl_geometry_minecraft_ao.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - minecraft - ao</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. background-color: #ffffff;
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. a { color: #a06851; }
  19. #info {
  20. position: absolute;
  21. top: 0px; width: 100%;
  22. padding: 5px;
  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">
  35. <a href="http://threejs.org" target="_blank">three.js</a> - <a href="http://www.minecraft.net/" target="_blank">minecraft</a> demo [ambient occlusion]. featuring <a href="http://painterlypack.net/" target="_blank">painterly pack</a><br />(left click: forward, right click: backward)
  36. </div>
  37. <script src="../build/three.min.js"></script>
  38. <script src="js/controls/FirstPersonControls.js"></script>
  39. <script src="js/ImprovedNoise.js"></script>
  40. <script src="js/Detector.js"></script>
  41. <script src="js/libs/stats.min.js"></script>
  42. <script>
  43. if ( ! Detector.webgl ) {
  44. Detector.addGetWebGLMessage();
  45. document.getElementById( 'container' ).innerHTML = "";
  46. }
  47. var fogExp2 = true;
  48. var container, stats;
  49. var camera, controls, scene, renderer;
  50. var mesh, mat;
  51. var worldWidth = 200, worldDepth = 200,
  52. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2,
  53. data = generateHeight( worldWidth, worldDepth );
  54. var clock = new THREE.Clock();
  55. init();
  56. animate();
  57. function init() {
  58. container = document.getElementById( 'container' );
  59. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 20000 );
  60. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  61. controls = new THREE.FirstPersonControls( camera );
  62. controls.movementSpeed = 1000;
  63. controls.lookSpeed = 0.125;
  64. controls.lookVertical = true;
  65. controls.constrainVertical = true;
  66. controls.verticalMin = 1.1;
  67. controls.verticalMax = 2.2;
  68. scene = new THREE.Scene();
  69. scene.fog = new THREE.FogExp2( 0xffffff, 0.00015 );
  70. // sides
  71. var light = new THREE.Color( 0xffffff );
  72. var shadow = new THREE.Color( 0x505050 );
  73. var matrix = new THREE.Matrix4();
  74. var pxGeometry = new THREE.PlaneGeometry( 100, 100 );
  75. pxGeometry.faces[ 0 ].vertexColors = [ light, shadow, shadow, light ];
  76. pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  77. pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 3 ].y = 0.5;
  78. pxGeometry.applyMatrix( matrix.makeRotationY( Math.PI / 2 ) );
  79. pxGeometry.applyMatrix( matrix.makeTranslation( 50, 0, 0 ) );
  80. var nxGeometry = new THREE.PlaneGeometry( 100, 100 );
  81. nxGeometry.faces[ 0 ].vertexColors = [ light, shadow, shadow, light ];
  82. nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  83. nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 3 ].y = 0.5;
  84. nxGeometry.applyMatrix( matrix.makeRotationY( - Math.PI / 2 ) );
  85. nxGeometry.applyMatrix( matrix.makeTranslation( - 50, 0, 0 ) );
  86. var pyGeometry = new THREE.PlaneGeometry( 100, 100 );
  87. pyGeometry.faces[ 0 ].vertexColors = [ light, light, light, light ];
  88. pyGeometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5;
  89. pyGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
  90. pyGeometry.applyMatrix( matrix.makeRotationX( - Math.PI / 2 ) );
  91. pyGeometry.applyMatrix( matrix.makeTranslation( 0, 50, 0 ) );
  92. var py2Geometry = new THREE.PlaneGeometry( 100, 100 );
  93. py2Geometry.faces[ 0 ].vertexColors = [ light, light, light, light ];
  94. py2Geometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5;
  95. py2Geometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
  96. py2Geometry.applyMatrix( matrix.makeRotationX( - Math.PI / 2 ) );
  97. py2Geometry.applyMatrix( matrix.makeRotationY( Math.PI / 2 ) );
  98. py2Geometry.applyMatrix( matrix.makeTranslation( 0, 50, 0 ) );
  99. var pzGeometry = new THREE.PlaneGeometry( 100, 100 );
  100. pzGeometry.faces[ 0 ].vertexColors = [ light, shadow, shadow, light ];
  101. pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  102. pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 3 ].y = 0.5;
  103. pzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, 50 ) );
  104. var nzGeometry = new THREE.PlaneGeometry( 100, 100 );
  105. nzGeometry.faces[ 0 ].vertexColors = [ light, shadow, shadow, light ];
  106. nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  107. nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 3 ].y = 0.5;
  108. nzGeometry.applyMatrix( matrix.makeRotationY( Math.PI ) );
  109. nzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, -50 ) );
  110. //
  111. var geometry = new THREE.Geometry();
  112. var dummy = new THREE.Mesh();
  113. for ( var z = 0; z < worldDepth; z ++ ) {
  114. for ( var x = 0; x < worldWidth; x ++ ) {
  115. var h = getY( x, z );
  116. dummy.position.x = x * 100 - worldHalfWidth * 100;
  117. dummy.position.y = h * 100;
  118. dummy.position.z = z * 100 - worldHalfDepth * 100;
  119. var px = getY( x + 1, z );
  120. var nx = getY( x - 1, z );
  121. var pz = getY( x, z + 1 );
  122. var nz = getY( x, z - 1 );
  123. var pxpz = getY( x + 1, z + 1 );
  124. var nxpz = getY( x - 1, z + 1 );
  125. var pxnz = getY( x + 1, z - 1 );
  126. var nxnz = getY( x - 1, z - 1 );
  127. var a = nx > h || nz > h || nxnz > h ? 0 : 1;
  128. var b = nx > h || pz > h || nxpz > h ? 0 : 1;
  129. var c = px > h || pz > h || pxpz > h ? 0 : 1;
  130. var d = px > h || nz > h || pxnz > h ? 0 : 1;
  131. if ( a + c > b + d ) {
  132. dummy.geometry = py2Geometry;
  133. var colors = dummy.geometry.faces[ 0 ].vertexColors;
  134. colors[ 3 ] = a === 0 ? shadow : light;
  135. colors[ 0 ] = b === 0 ? shadow : light;
  136. colors[ 1 ] = c === 0 ? shadow : light;
  137. colors[ 2 ] = d === 0 ? shadow : light;
  138. } else {
  139. dummy.geometry = pyGeometry;
  140. var colors = dummy.geometry.faces[ 0 ].vertexColors;
  141. colors[ 0 ] = a === 0 ? shadow : light;
  142. colors[ 1 ] = b === 0 ? shadow : light;
  143. colors[ 2 ] = c === 0 ? shadow : light;
  144. colors[ 3 ] = d === 0 ? shadow : light;
  145. }
  146. THREE.GeometryUtils.merge( geometry, dummy );
  147. if ( ( px != h && px != h + 1 ) || x == 0 ) {
  148. dummy.geometry = pxGeometry;
  149. var colors = dummy.geometry.faces[ 0 ].vertexColors;
  150. colors[ 0 ] = pxpz > px && x > 0 ? shadow : light;
  151. colors[ 3 ] = pxnz > px && x > 0 ? shadow : light;
  152. THREE.GeometryUtils.merge( geometry, dummy );
  153. }
  154. if ( ( nx != h && nx != h + 1 ) || x == worldWidth - 1 ) {
  155. dummy.geometry = nxGeometry;
  156. var colors = dummy.geometry.faces[ 0 ].vertexColors;
  157. colors[ 0 ] = nxnz > nx && x < worldWidth - 1 ? shadow : light;
  158. colors[ 3 ] = nxpz > nx && x < worldWidth - 1 ? shadow : light;
  159. THREE.GeometryUtils.merge( geometry, dummy );
  160. }
  161. if ( ( pz != h && pz != h + 1 ) || z == worldDepth - 1 ) {
  162. dummy.geometry = pzGeometry;
  163. var colors = dummy.geometry.faces[ 0 ].vertexColors;
  164. colors[ 0 ] = nxpz > pz && z < worldDepth - 1 ? shadow : light;
  165. colors[ 3 ] = pxpz > pz && z < worldDepth - 1 ? shadow : light;
  166. THREE.GeometryUtils.merge( geometry, dummy );
  167. }
  168. if ( ( nz != h && nz != h + 1 ) || z == 0 ) {
  169. dummy.geometry = nzGeometry;
  170. var colors = dummy.geometry.faces[ 0 ].vertexColors;
  171. colors[ 0 ] = pxnz > nz && z > 0 ? shadow : light;
  172. colors[ 3 ] = nxnz > nz && z > 0 ? shadow : light;
  173. THREE.GeometryUtils.merge( geometry, dummy );
  174. }
  175. }
  176. }
  177. var texture = THREE.ImageUtils.loadTexture( 'textures/minecraft/atlas.png' );
  178. texture.magFilter = THREE.NearestFilter;
  179. texture.minFilter = THREE.LinearMipMapLinearFilter;
  180. var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { map: texture, ambient: 0xbbbbbb, vertexColors: THREE.VertexColors } ) );
  181. scene.add( mesh );
  182. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  183. scene.add( ambientLight );
  184. var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  185. directionalLight.position.set( 1, 1, 0.5 ).normalize();
  186. scene.add( directionalLight );
  187. renderer = new THREE.WebGLRenderer( { clearColor: 0xffffff } );
  188. renderer.setSize( window.innerWidth, window.innerHeight );
  189. container.innerHTML = "";
  190. container.appendChild( renderer.domElement );
  191. stats = new Stats();
  192. stats.domElement.style.position = 'absolute';
  193. stats.domElement.style.top = '0px';
  194. container.appendChild( stats.domElement );
  195. //
  196. window.addEventListener( 'resize', onWindowResize, false );
  197. }
  198. function onWindowResize() {
  199. camera.aspect = window.innerWidth / window.innerHeight;
  200. camera.updateProjectionMatrix();
  201. renderer.setSize( window.innerWidth, window.innerHeight );
  202. controls.handleResize();
  203. }
  204. function loadTexture( path, callback ) {
  205. var image = new Image();
  206. image.onload = function () { callback(); };
  207. image.src = path;
  208. return image;
  209. }
  210. function generateHeight( width, height ) {
  211. var data = [], perlin = new ImprovedNoise(),
  212. size = width * height, quality = 2, z = Math.random() * 100;
  213. for ( var j = 0; j < 4; j ++ ) {
  214. if ( j == 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  215. for ( var i = 0; i < size; i ++ ) {
  216. var x = i % width, y = ( i / width ) | 0;
  217. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  218. }
  219. quality *= 4
  220. }
  221. return data;
  222. }
  223. function getY( x, z ) {
  224. return ( data[ x + z * worldWidth ] * 0.2 ) | 0;
  225. }
  226. //
  227. function animate() {
  228. requestAnimationFrame( animate );
  229. render();
  230. stats.update();
  231. }
  232. function render() {
  233. controls.update( clock.getDelta() );
  234. renderer.render( scene, camera );
  235. }
  236. </script>
  237. </body>
  238. </html>