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="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. 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. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0xffffff );
  43. scene.fog = new THREE.FogExp2( 0xffffff, 0.00015 );
  44. // sides
  45. var light = new THREE.Color( 0xffffff );
  46. var shadow = new THREE.Color( 0x505050 );
  47. var matrix = new THREE.Matrix4();
  48. var pxGeometry = new THREE.PlaneGeometry( 100, 100 );
  49. pxGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ];
  50. pxGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ];
  51. pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  52. pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
  53. pxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
  54. pxGeometry.rotateY( Math.PI / 2 );
  55. pxGeometry.translate( 50, 0, 0 );
  56. var nxGeometry = new THREE.PlaneGeometry( 100, 100 );
  57. nxGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ];
  58. nxGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ];
  59. nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  60. nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
  61. nxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
  62. nxGeometry.rotateY( - Math.PI / 2 );
  63. nxGeometry.translate( - 50, 0, 0 );
  64. var pyGeometry = new THREE.PlaneGeometry( 100, 100 );
  65. pyGeometry.faces[ 0 ].vertexColors = [ light, light, light ];
  66. pyGeometry.faces[ 1 ].vertexColors = [ light, light, light ];
  67. pyGeometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5;
  68. pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 0 ].y = 0.5;
  69. pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 1 ].y = 0.5;
  70. pyGeometry.rotateX( - Math.PI / 2 );
  71. pyGeometry.translate( 0, 50, 0 );
  72. var py2Geometry = new THREE.PlaneGeometry( 100, 100 );
  73. py2Geometry.faces[ 0 ].vertexColors = [ light, light, light ];
  74. py2Geometry.faces[ 1 ].vertexColors = [ light, light, light ];
  75. py2Geometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5;
  76. py2Geometry.faceVertexUvs[ 0 ][ 1 ][ 0 ].y = 0.5;
  77. py2Geometry.faceVertexUvs[ 0 ][ 1 ][ 1 ].y = 0.5;
  78. py2Geometry.rotateX( - Math.PI / 2 );
  79. py2Geometry.rotateY( Math.PI / 2 );
  80. py2Geometry.translate( 0, 50, 0 );
  81. var pzGeometry = new THREE.PlaneGeometry( 100, 100 );
  82. pzGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ];
  83. pzGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ];
  84. pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  85. pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
  86. pzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
  87. pzGeometry.translate( 0, 0, 50 );
  88. var nzGeometry = new THREE.PlaneGeometry( 100, 100 );
  89. nzGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ];
  90. nzGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ];
  91. nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
  92. nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
  93. nzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
  94. nzGeometry.rotateY( Math.PI );
  95. nzGeometry.translate( 0, 0, - 50 );
  96. //
  97. var geometry = new THREE.Geometry();
  98. for ( var z = 0; z < worldDepth; z ++ ) {
  99. for ( var x = 0; x < worldWidth; x ++ ) {
  100. var h = getY( x, z );
  101. matrix.makeTranslation(
  102. x * 100 - worldHalfWidth * 100,
  103. h * 100,
  104. z * 100 - worldHalfDepth * 100
  105. );
  106. var px = getY( x + 1, z );
  107. var nx = getY( x - 1, z );
  108. var pz = getY( x, z + 1 );
  109. var nz = getY( x, z - 1 );
  110. var pxpz = getY( x + 1, z + 1 );
  111. var nxpz = getY( x - 1, z + 1 );
  112. var pxnz = getY( x + 1, z - 1 );
  113. var nxnz = getY( x - 1, z - 1 );
  114. var a = nx > h || nz > h || nxnz > h ? 0 : 1;
  115. var b = nx > h || pz > h || nxpz > h ? 0 : 1;
  116. var c = px > h || pz > h || pxpz > h ? 0 : 1;
  117. var d = px > h || nz > h || pxnz > h ? 0 : 1;
  118. if ( a + c > b + d ) {
  119. var colors = py2Geometry.faces[ 0 ].vertexColors;
  120. colors[ 0 ] = b === 0 ? shadow : light;
  121. colors[ 1 ] = c === 0 ? shadow : light;
  122. colors[ 2 ] = a === 0 ? shadow : light;
  123. var colors = py2Geometry.faces[ 1 ].vertexColors;
  124. colors[ 0 ] = c === 0 ? shadow : light;
  125. colors[ 1 ] = d === 0 ? shadow : light;
  126. colors[ 2 ] = a === 0 ? shadow : light;
  127. geometry.merge( py2Geometry, matrix );
  128. } else {
  129. var colors = pyGeometry.faces[ 0 ].vertexColors;
  130. colors[ 0 ] = a === 0 ? shadow : light;
  131. colors[ 1 ] = b === 0 ? shadow : light;
  132. colors[ 2 ] = d === 0 ? shadow : light;
  133. var colors = pyGeometry.faces[ 1 ].vertexColors;
  134. colors[ 0 ] = b === 0 ? shadow : light;
  135. colors[ 1 ] = c === 0 ? shadow : light;
  136. colors[ 2 ] = d === 0 ? shadow : light;
  137. geometry.merge( pyGeometry, matrix );
  138. }
  139. if ( ( px != h && px != h + 1 ) || x == 0 ) {
  140. var colors = pxGeometry.faces[ 0 ].vertexColors;
  141. colors[ 0 ] = pxpz > px && x > 0 ? shadow : light;
  142. colors[ 2 ] = pxnz > px && x > 0 ? shadow : light;
  143. var colors = pxGeometry.faces[ 1 ].vertexColors;
  144. colors[ 2 ] = pxnz > px && x > 0 ? shadow : light;
  145. geometry.merge( pxGeometry, matrix );
  146. }
  147. if ( ( nx != h && nx != h + 1 ) || x == worldWidth - 1 ) {
  148. var colors = nxGeometry.faces[ 0 ].vertexColors;
  149. colors[ 0 ] = nxnz > nx && x < worldWidth - 1 ? shadow : light;
  150. colors[ 2 ] = nxpz > nx && x < worldWidth - 1 ? shadow : light;
  151. var colors = nxGeometry.faces[ 1 ].vertexColors;
  152. colors[ 2 ] = nxpz > nx && x < worldWidth - 1 ? shadow : light;
  153. geometry.merge( nxGeometry, matrix );
  154. }
  155. if ( ( pz != h && pz != h + 1 ) || z == worldDepth - 1 ) {
  156. var colors = pzGeometry.faces[ 0 ].vertexColors;
  157. colors[ 0 ] = nxpz > pz && z < worldDepth - 1 ? shadow : light;
  158. colors[ 2 ] = pxpz > pz && z < worldDepth - 1 ? shadow : light;
  159. var colors = pzGeometry.faces[ 1 ].vertexColors;
  160. colors[ 2 ] = pxpz > pz && z < worldDepth - 1 ? shadow : light;
  161. geometry.merge( pzGeometry, matrix );
  162. }
  163. if ( ( nz != h && nz != h + 1 ) || z == 0 ) {
  164. var colors = nzGeometry.faces[ 0 ].vertexColors;
  165. colors[ 0 ] = pxnz > nz && z > 0 ? shadow : light;
  166. colors[ 2 ] = nxnz > nz && z > 0 ? shadow : light;
  167. var colors = nzGeometry.faces[ 1 ].vertexColors;
  168. colors[ 2 ] = nxnz > nz && z > 0 ? shadow : light;
  169. geometry.merge( nzGeometry, matrix );
  170. }
  171. }
  172. }
  173. geometry = new THREE.BufferGeometry().fromGeometry( geometry );
  174. var texture = new THREE.TextureLoader().load( 'textures/minecraft/atlas.png' );
  175. texture.magFilter = THREE.NearestFilter;
  176. texture.minFilter = THREE.LinearMipmapLinearFilter;
  177. var mesh = new THREE.Mesh(
  178. geometry,
  179. new THREE.MeshLambertMaterial( { map: texture, vertexColors: THREE.VertexColors, side: THREE.DoubleSide } )
  180. );
  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();
  188. renderer.setPixelRatio( window.devicePixelRatio );
  189. renderer.setSize( window.innerWidth, window.innerHeight );
  190. container.appendChild( renderer.domElement );
  191. controls = new FirstPersonControls( camera, renderer.domElement );
  192. controls.movementSpeed = 1000;
  193. controls.lookSpeed = 0.125;
  194. controls.lookVertical = true;
  195. controls.constrainVertical = true;
  196. controls.verticalMin = 1.1;
  197. controls.verticalMax = 2.2;
  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>