webgl_geometry_minecraft_ao.html 10 KB

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