webgl_geometry_minecraft_ao.html 11 KB

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