webgl_geometry_minecraft_ao.html 10 KB

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