webgl_geometry_minecraft_ao.html 10 KB

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