webgl_geometry_minecraft_ao.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - minecraft - ao</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. color: #61443e;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. /* background-color: #bfd1e5; */
  13. background-color: #ffffff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. a { color: #a06851; }
  18. #info {
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. #oldie {
  24. background:rgb(100,0,0) !important;
  25. color:#fff !important;
  26. margin-top:10em !important;
  27. }
  28. #oldie a { color:#fff }
  29. button { border:0; background:rgba(0,0,0,0.5); color:orange; cursor:pointer }
  30. button:hover { color:gold }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="container"><br /><br /><br /><br /><br />Generating world...</div>
  35. <div id="info">
  36. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - <a href="http://www.minecraft.net/" target="_blank">minecraft</a> demo [ambient occlusion]. featuring <a href="http://painterlypack.net/" target="_blank">painterly pack</a><br />(left click: forward, right click: backward)
  37. <br/><br/>
  38. <button id="bt">texture</button>
  39. <button id="bao">ao</button>
  40. <button id="baot">texture + ao</button>
  41. </div>
  42. <script type="text/javascript" src="../build/Three.js"></script>
  43. <script type="text/javascript" src="js/ImprovedNoise.js"></script>
  44. <script type="text/javascript" src="js/Detector.js"></script>
  45. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  46. <script type="text/javascript" src="js/Stats.js"></script>
  47. <script type="text/javascript">
  48. if ( ! Detector.webgl ) {
  49. Detector.addGetWebGLMessage();
  50. document.getElementById( 'container' ).innerHTML = "";
  51. }
  52. var fogExp2 = true;
  53. var container, stats;
  54. var camera, scene, renderer;
  55. var mesh;
  56. var mat;
  57. var worldWidth = 128, worldDepth = 128,
  58. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2,
  59. data = generateHeight( worldWidth, worldDepth );
  60. init();
  61. animate();
  62. function init() {
  63. container = document.getElementById( 'container' );
  64. camera = new THREE.QuakeCamera( { fov: 60, aspect: window.innerWidth / window.innerHeight, near: 1, far: 20000,
  65. movementSpeed: 15, lookSpeed: 0.005, noFly: false, lookVertical: true } );
  66. camera.target.position.z = - 100;
  67. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  68. camera.target.position.y = camera.position.y;
  69. scene = new THREE.Scene();
  70. scene.fog = new THREE.FogExp2( 0xffffff, 0.00025 );
  71. var debug_texture = false,
  72. debug_numbers = false,
  73. debug_corner_colors = false,
  74. strength = 2,
  75. textures = { side: 'textures/minecraft/grass_dirt.png',
  76. top: 'textures/minecraft/grass.png',
  77. bottom: 'textures/minecraft/dirt.png'
  78. },
  79. m_aot = generateMegamaterialAO( textures, strength, debug_texture, debug_numbers, debug_corner_colors ),
  80. m_ao = generateMegamaterialAO( textures, strength, true, debug_numbers, debug_corner_colors ),
  81. m_t = generateMegamaterialPlain( textures ),
  82. //m_d = generateMegamaterialDebug(),
  83. mat = generateMegamaterialAO( textures, strength, debug_texture, debug_numbers, debug_corner_colors ),
  84. materials = [ mat, mat, mat, mat, mat, mat ];
  85. var i, j, x, z, h, h2, uv,
  86. px, nx, pz, nz, sides,
  87. right, left, bottom, top,
  88. nright, nleft, nback, nfront,
  89. nleftup, nrightup, nbackup, nfrontup,
  90. nrb, nrf, nlb, nlf,
  91. nrbup, nrfup,
  92. face_px, face_nx, face_py, face_ny, face_pz, face_nz,
  93. ti, ri, li, bi, fi, ci, mi, mm, column, row,
  94. cube,
  95. unit = 1/16 * 0.95, padding = 1/16 * 0.025, p, s, t, hash, N = -1,
  96. // map of UV indices for faces of partially defined cubes
  97. uv_index_map = {
  98. 0: { px: N, nx: N, py: 0, ny: N, pz: N, nz: N },
  99. 1: { px: N, nx: N, py: 0, ny: N, pz: N, nz: 1 },
  100. 2: { px: N, nx: N, py: 0, ny: N, pz: 1, nz: N },
  101. 3: { px: N, nx: N, py: 0, ny: N, pz: 1, nz: 2 },
  102. 4: { px: N, nx: 0, py: 1, ny: N, pz: N, nz: N },
  103. 5: { px: N, nx: 0, py: 1, ny: N, pz: N, nz: 2 },
  104. 6: { px: N, nx: 0, py: 1, ny: N, pz: 2, nz: N },
  105. 7: { px: N, nx: 0, py: 1, ny: N, pz: 2, nz: 3 },
  106. 8: { px: 0, nx: N, py: 1, ny: N, pz: N, nz: N },
  107. 9: { px: 0, nx: N, py: 1, ny: N, pz: N, nz: 2 },
  108. 10: { px: 0, nx: N, py: 1, ny: N, pz: 2, nz: N },
  109. 11: { px: 0, nx: N, py: 1, ny: N, pz: 2, nz: 3 },
  110. 12: { px: 0, nx: 1, py: 2, ny: N, pz: N, nz: N },
  111. 13: { px: 0, nx: 1, py: 2, ny: N, pz: N, nz: 3 },
  112. 14: { px: 0, nx: 1, py: 2, ny: N, pz: 3, nz: N },
  113. 15: { px: 0, nx: 1, py: 2, ny: N, pz: 3, nz: 4 }
  114. },
  115. // all possible combinations of corners and sides
  116. // mapped to mixed tiles
  117. // (including corners overlapping sides)
  118. // (excluding corner alone and sides alone)
  119. // looks ugly, but allows to squeeze all
  120. // combinations for one texture into just 3 rows
  121. // instead of 16
  122. mixmap = {
  123. "1_1": 0,
  124. "1_3": 0,
  125. "1_9": 0,
  126. "1_11": 0,
  127. "1_4": 1,
  128. "1_6": 1,
  129. "1_12": 1,
  130. "1_14": 1,
  131. "2_2": 2,
  132. "2_3": 2,
  133. "2_6": 2,
  134. "2_7": 2,
  135. "2_8": 3,
  136. "2_9": 3,
  137. "2_12": 3,
  138. "2_13": 3,
  139. "4_1": 4,
  140. "4_5": 4,
  141. "4_9": 4,
  142. "4_13": 4,
  143. "4_2": 5,
  144. "4_6": 5,
  145. "4_10": 5,
  146. "4_14": 5,
  147. "8_4": 6,
  148. "8_5": 6,
  149. "8_6": 6,
  150. "8_7": 6,
  151. "8_8": 7,
  152. "8_9": 7,
  153. "8_10": 7,
  154. "8_11": 7,
  155. "1_5": 8,
  156. "1_7": 8,
  157. "1_13": 8,
  158. "1_15": 8,
  159. "2_10": 9,
  160. "2_11": 9,
  161. "2_14": 9,
  162. "2_15": 9,
  163. "4_3": 10,
  164. "4_7": 10,
  165. "4_11": 10,
  166. "4_15": 10,
  167. "8_12": 11,
  168. "8_13": 11,
  169. "8_14": 11,
  170. "8_15": 11,
  171. "5_1": 12,
  172. "5_3": 12,
  173. "5_7": 12,
  174. "5_9": 12,
  175. "5_11": 12,
  176. "5_13": 12,
  177. "5_15": 12,
  178. "6_2": 13,
  179. "6_3": 13,
  180. "6_6": 13,
  181. "6_7": 13,
  182. "6_10": 13,
  183. "6_11": 13,
  184. "6_14": 13,
  185. "6_15": 13,
  186. "9_4": 14,
  187. "9_5": 14,
  188. "9_6": 14,
  189. "9_7": 14,
  190. "9_12": 14,
  191. "9_13": 14,
  192. "9_14": 14,
  193. "9_15": 14,
  194. "10_8": 15,
  195. "10_9": 15,
  196. "10_10": 15,
  197. "10_11": 15,
  198. "10_12": 15,
  199. "10_13": 15,
  200. "10_14": 15,
  201. "10_15": 15
  202. },
  203. tilemap = {},
  204. top_row_corners = 0,
  205. top_row_mixed = 1,
  206. top_row_sides = 2,
  207. sides_row = 3,
  208. bottom_row = 4,
  209. geometry = new THREE.Geometry();
  210. // mapping from 256 possible corners + sides combinations
  211. // into 3 x 16 tiles
  212. for ( i = 0; i < 16; i++ ) {
  213. for ( j = 0; j < 16; j++ ) {
  214. mm = i + "_" + j;
  215. if ( i == 0 )
  216. row = top_row_corners;
  217. else if ( mixmap[ mm ] != undefined )
  218. row = top_row_mixed;
  219. else
  220. row = top_row_sides;
  221. tilemap[ mm ] = row;
  222. }
  223. }
  224. function setUVTile( face, s, t ) {
  225. var j, uv = cube.faceVertexUvs[ 0 ][ face ];
  226. for ( j = 0; j < uv.length; j++ ) {
  227. uv[ j ].u += s * (unit+2*padding);
  228. uv[ j ].v += t * (unit+2*padding);
  229. }
  230. }
  231. for ( z = 0; z < worldDepth; z ++ ) {
  232. for ( x = 0; x < worldWidth; x ++ ) {
  233. h = getY( x, z );
  234. // direct neighbors
  235. h2 = getY( x - 1, z );
  236. nleft = h2 == h || h2 == h + 1;
  237. h2 = getY( x + 1, z );
  238. nright = h2 == h || h2 == h + 1;
  239. h2 = getY( x, z + 1 );
  240. nback = h2 == h || h2 == h + 1;
  241. h2 = getY( x, z - 1 );
  242. nfront = h2 == h || h2 == h + 1;
  243. // corner neighbors
  244. nrb = getY( x - 1, z + 1 ) == h && x > 0 && z < worldDepth - 1 ? 1 : 0;
  245. nrf = getY( x - 1, z - 1 ) == h && x > 0 && z > 0 ? 1 : 0;
  246. nlb = getY( x + 1, z + 1 ) == h && x < worldWidth - 1 && z < worldDepth - 1 ? 1 : 0;
  247. nlf = getY( x + 1, z - 1 ) == h && x < worldWidth - 1 && z > 0 ? 1 : 0;
  248. // up neighbors
  249. nleftup = getY( x - 1, z ) > h && x > 0 ? 1 : 0;
  250. nrightup = getY( x + 1, z ) > h && x < worldWidth - 1 ? 1 : 0;
  251. nbackup = getY( x, z + 1 ) > h && z < worldDepth - 1 ? 1 : 0;
  252. nfrontup = getY( x, z - 1 ) > h && z > 0 ? 1 : 0;
  253. // up corner neighbors
  254. nrbup = getY( x - 1, z + 1 ) > h && x > 0 && z < worldDepth - 1 ? 1 : 0;
  255. nrfup = getY( x - 1, z - 1 ) > h && x > 0 && z > 0 ? 1 : 0;
  256. nlbup = getY( x + 1, z + 1 ) > h && x < worldWidth - 1 && z < worldDepth - 1 ? 1 : 0;
  257. nlfup = getY( x + 1, z - 1 ) > h && x < worldWidth - 1 && z > 0 ? 1 : 0;
  258. // textures
  259. ti = nleftup * 8 + nrightup * 4 + nfrontup * 2 + nbackup * 1;
  260. ri = nrf * 8 + nrb * 4 + 1;
  261. li = nlb * 8 + nlf * 4 + 1;
  262. bi = nrb * 8 + nlb * 4 + 1;
  263. fi = nlf * 8 + nrf * 4 + 1;
  264. ci = nlbup * 8 + nlfup * 4 + nrbup * 2 + nrfup * 1;
  265. // cube sides
  266. px = nx = pz = nz = 0;
  267. px = !nleft || x == 0 ? 1 : 0;
  268. nx = !nright || x == worldWidth - 1 ? 1 : 0;
  269. pz = !nback || z == worldDepth - 1 ? 1 : 0;
  270. nz = !nfront || z == 0 ? 1 : 0;
  271. sides = { px: px, nx: nx, py: true, ny: false, pz: pz, nz: nz };
  272. cube = new Cube( 100, 100, 100, 1, 1, 1, materials, false, sides );
  273. // set UV tiles
  274. for ( i = 0; i < cube.faceVertexUvs[ 0 ].length; i++ ) {
  275. uv = cube.faceVertexUvs[ 0 ][ i ];
  276. for ( j = 0; j < uv.length; j++ ) {
  277. p = uv[j].u == 0 ? padding : -padding;
  278. uv[j].u = uv[j].u * unit + p;
  279. p = uv[j].v == 0 ? padding : -padding;
  280. uv[j].v = uv[j].v * unit + p;
  281. }
  282. }
  283. hash = px * 8 + nx * 4 + pz * 2 + nz;
  284. face_px = uv_index_map[ hash ].px;
  285. face_nx = uv_index_map[ hash ].nx;
  286. face_py = uv_index_map[ hash ].py;
  287. face_ny = uv_index_map[ hash ].ny;
  288. face_pz = uv_index_map[ hash ].pz;
  289. face_nz = uv_index_map[ hash ].nz;
  290. if( face_px != N ) setUVTile( face_px, ri, sides_row );
  291. if( face_nx != N ) setUVTile( face_nx, li, sides_row );
  292. if( face_py != N ) {
  293. mm = ti + "_" + ci;
  294. switch ( tilemap[ mm ] ) {
  295. case top_row_sides: column = ti; break;
  296. case top_row_corners: column = ci; break;
  297. case top_row_mixed: column = mixmap[ mm ]; break;
  298. }
  299. setUVTile( face_py, column, tilemap[ mm ] );
  300. }
  301. if( face_ny != N ) setUVTile( face_ny, 0, bottom_row );
  302. if( face_pz != N ) setUVTile( face_pz, bi, sides_row );
  303. if( face_nz != N ) setUVTile( face_nz, fi, sides_row );
  304. mesh = new THREE.Mesh( cube );
  305. mesh.position.x = x * 100 - worldHalfWidth * 100;
  306. mesh.position.y = h * 100;
  307. mesh.position.z = z * 100 - worldHalfDepth * 100;
  308. GeometryUtils.merge( geometry, mesh );
  309. }
  310. }
  311. mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
  312. scene.addObject( mesh );
  313. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  314. scene.addLight( ambientLight );
  315. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.5 );
  316. directionalLight.position.x = 1;
  317. directionalLight.position.y = 1;
  318. directionalLight.position.z = 0.5;
  319. directionalLight.position.normalize();
  320. scene.addLight( directionalLight );
  321. renderer = new THREE.WebGLRenderer();
  322. renderer.setSize( window.innerWidth, window.innerHeight );
  323. container.innerHTML = "";
  324. container.appendChild( renderer.domElement );
  325. stats = new Stats();
  326. stats.domElement.style.position = 'absolute';
  327. stats.domElement.style.top = '0px';
  328. container.appendChild( stats.domElement );
  329. document.getElementById( "bao" ).addEventListener( "click", function() { mat.map = m_ao.map; }, false );
  330. document.getElementById( "baot" ).addEventListener( "click", function() { mat.map = m_aot.map; }, false );
  331. document.getElementById( "bt" ).addEventListener( "click", function() { mat.map = m_t.map; }, false );
  332. }
  333. function generateMegamaterialAO( textures, strength, debug_texture, debug_numbers, debug_corner_colors ) {
  334. var count = 0,
  335. tex_side = loadTexture( textures.side, function() { count++; generateTexture() } ),
  336. tex_top = loadTexture( textures.top, function() { count++; generateTexture() } ),
  337. tex_bottom = loadTexture( textures.bottom, function() { count++; generateTexture() } ),
  338. canvas = document.createElement( 'canvas' ),
  339. ctx = canvas.getContext( '2d' ),
  340. size = 256, tile = 16;
  341. canvas.width = canvas.height = size;
  342. var texture = new THREE.Texture( canvas, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter );
  343. function generateTexture() {
  344. if( count == 3 ) {
  345. for( var i = 0; i < 16; i++ ) {
  346. drawAOCorners( ctx, tex_top, 0, i, i, tile, strength, debug_texture, debug_numbers, debug_corner_colors );
  347. drawAOMixed ( ctx, tex_top, 1, i, i, tile, strength, debug_texture, debug_numbers, debug_corner_colors );
  348. drawAOSides ( ctx, tex_top, 2, i, i, tile, strength, debug_texture, debug_numbers );
  349. drawAOSides ( ctx, tex_side, 3, i, i, tile, strength, debug_texture, debug_numbers );
  350. drawAOSides ( ctx, tex_bottom, 4, i, i, tile, strength, debug_texture, debug_numbers );
  351. }
  352. texture.needsUpdate = true;
  353. }
  354. }
  355. return new THREE.MeshLambertMaterial( { map: texture } );
  356. }
  357. function generateMegamaterialPlain( textures ) {
  358. var count = 0,
  359. tex_side = loadTexture( textures.side, function() { count++; generateTexture() } ),
  360. tex_top = loadTexture( textures.top, function() { count++; generateTexture() } ),
  361. tex_bottom = loadTexture( textures.bottom, function() { count++; generateTexture() } ),
  362. canvas = document.createElement( 'canvas' ),
  363. ctx = canvas.getContext( '2d' ),
  364. size = 256, tile = 16;
  365. canvas.width = canvas.height = size;
  366. var texture = new THREE.Texture( canvas, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter );
  367. function generateTexture() {
  368. if( count == 3 ) {
  369. var i, sx;
  370. for( i = 0; i < 16; i++ ) {
  371. sx = i * tile;
  372. drawBase( ctx, tex_top, sx, 0 * tile, tile, false );
  373. drawBase( ctx, tex_top, sx, 1 * tile, tile, false );
  374. drawBase( ctx, tex_top, sx, 2 * tile, tile, false );
  375. drawBase( ctx, tex_side, sx, 3 * tile, tile, false );
  376. drawBase( ctx, tex_bottom, sx, 4 * tile, tile, false );
  377. }
  378. texture.needsUpdate = true;
  379. }
  380. }
  381. return new THREE.MeshLambertMaterial( { map: texture } );
  382. }
  383. function generateMegamaterialDebug() {
  384. var canvas = document.createElement( 'canvas' ),
  385. ctx = canvas.getContext( "2d" ),
  386. size = 256, tile = 16,
  387. i, j, h, s;
  388. canvas.width = size;
  389. canvas.height = size;
  390. ctx.textBaseline = "top";
  391. ctx.font = "8pt arial";
  392. for ( i = 0; i < tile; i++ ) {
  393. for ( j = 0; j < tile; j++ ) {
  394. h = i * tile + j;
  395. ctx.fillStyle = "hsl(" + h + ",90%, 50%)";
  396. ctx.fillRect( i * tile, j * tile, tile, tile );
  397. drawHex( ctx, h, i * tile + 2, j * tile + 2 );
  398. }
  399. }
  400. var texture = new THREE.Texture( canvas, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter );
  401. texture.needsUpdate = true;
  402. return new THREE.MeshLambertMaterial( { map: texture } );
  403. }
  404. function drawHex( ctx, n, x, y ) {
  405. ctx.fillStyle = "black";
  406. ctx.font = "8pt arial";
  407. ctx.textBaseline = "top";
  408. var s = n.toString( 16 );
  409. s = n < 16 ? "0" + s : s;
  410. ctx.fillText( s, x, y );
  411. }
  412. function drawBase( ctx, image, sx, sy, tile, debug_texture ) {
  413. if ( debug_texture ) {
  414. ctx.fillStyle = "#888";
  415. ctx.fillRect( sx, sy, tile, tile );
  416. } else {
  417. ctx.drawImage( image, sx, sy, tile, tile );
  418. }
  419. }
  420. function drawCorner( ctx, sx, sy, sa, ea, color, step, n ) {
  421. for( var i = 0; i < n; i++ ) {
  422. ctx.strokeStyle = color + step * ( n - i ) + ")";
  423. ctx.beginPath();
  424. ctx.arc( sx, sy, i, sa, ea, 0 ) ;
  425. ctx.stroke();
  426. }
  427. }
  428. function drawSide( ctx, sx, sy, a, b, n, width, height, color, step ) {
  429. for( var i = 0; i < n; i++ ) {
  430. ctx.fillStyle = color + step * ( n - i ) + ")";
  431. ctx.fillRect( sx + a * i, sy + b * i, width, height );
  432. }
  433. }
  434. function drawAOSides( ctx, image, row, column, sides, tile, strength, debug_texture, debug_numbers ) {
  435. var sx = column * tile, sy = row * tile;
  436. drawBase( ctx, image, sx, sy, tile, debug_texture );
  437. drawAOSidesImp( ctx, image, row, column, sides, tile, strength );
  438. if ( debug_numbers ) drawHex( ctx, row * tile + sides, sx + 2, sy + 2 );
  439. }
  440. function drawAOCorners( ctx, image, row, column, corners, tile, strength, debug_texture, debug_numbers, debug_corner_colors ) {
  441. var sx = column * tile, sy = row * tile;
  442. drawBase( ctx, image, sx, sy, tile, debug_texture );
  443. drawAOCornersImp( ctx, image, row, column, corners, tile, strength, debug_corner_colors );
  444. if ( debug_numbers ) drawHex( ctx, row * tile + corners, sx + 2, sy + 2 );
  445. }
  446. function drawAOMixed( ctx, image, row, column, elements, tile, strength, debug_texture, debug_numbers, debug_corner_colors ) {
  447. var sx = column * tile, sy = row * tile,
  448. mmap = {
  449. 0: [ 1, 1 ],
  450. 1: [ 1, 4 ],
  451. 2: [ 2, 2 ],
  452. 3: [ 2, 8 ],
  453. 4: [ 4, 1 ],
  454. 5: [ 4, 2 ],
  455. 6: [ 8, 4 ],
  456. 7: [ 8, 8 ],
  457. 8: [ 1, 5 ],
  458. 9: [ 2, 10 ],
  459. 10: [ 4, 3 ],
  460. 11: [ 8, 12 ],
  461. 12: [ 5, 1 ],
  462. 13: [ 6, 2 ],
  463. 14: [ 9, 4 ],
  464. 15: [ 10, 8 ]
  465. };
  466. drawBase( ctx, image, sx, sy, tile, debug_texture );
  467. drawAOCornersImp( ctx, image, row, column, mmap[ elements ][1], tile, strength, debug_corner_colors );
  468. drawAOSidesImp( ctx, image, row, column, mmap[ elements ][0], tile, strength );
  469. if ( debug_numbers ) drawHex( ctx, row * tile + elements, sx + 2, sy + 2 );
  470. }
  471. function drawAOSidesImp( ctx, image, row, column, sides, tile, strength ) {
  472. var sx = column * tile, sy = row * tile,
  473. full = tile, step = 1 / full, half = full / 2 + strength,
  474. color = "rgba(0, 0, 0, ",
  475. left = (sides & 8) == 8,
  476. right = (sides & 4) == 4,
  477. bottom = (sides & 2) == 2,
  478. top = (sides & 1) == 1;
  479. if ( bottom ) drawSide( ctx, sx, sy, 0, 1, half, tile, 1, color, step );
  480. if ( top ) drawSide( ctx, sx, sy + full - 1, 0, -1, half, tile, 1, color, step );
  481. if ( left ) drawSide( ctx, sx, sy, 1, 0, half, 1, tile, color, step );
  482. if ( right ) drawSide( ctx, sx + full - 1, sy, -1, 0, half, 1, tile, color, step );
  483. }
  484. function drawAOCornersImp( ctx, image, row, column, corners, tile, strength, debug_corner_colors ) {
  485. var sx = column * tile, sy = row * tile,
  486. full = tile, step = 1 / full, half = full / 2 + strength,
  487. color = "rgba(0, 0, 0, ",
  488. bottomright = (corners & 8) == 8,
  489. topright = (corners & 4) == 4,
  490. bottomleft = (corners & 2) == 2,
  491. topleft = (corners & 1) == 1;
  492. if ( topleft ) {
  493. if ( debug_corner_colors ) color = "rgba(200, 0, 0, ";
  494. drawCorner( ctx, sx, sy, 0, 1.57, color, step, half );
  495. }
  496. if ( bottomleft ) {
  497. if ( debug_corner_colors ) color = "rgba(0, 200, 0, ";
  498. drawCorner( ctx, sx, sy + full, 4.71, 6.28, color, step, half );
  499. }
  500. if ( bottomright ) {
  501. if ( debug_corner_colors ) color = "rgba(0, 0, 200, ";
  502. drawCorner( ctx, sx + full, sy + full, 3.14, 4.71, color, step, half );
  503. }
  504. if ( topright ) {
  505. if ( debug_corner_colors ) color = "rgba(200, 0, 200, ";
  506. drawCorner( ctx, sx + full, sy, 1.57, 3.14, color, step, half );
  507. }
  508. }
  509. function loadTexture( path, callback ) {
  510. var image = new Image();
  511. image.onload = function () { callback(); };
  512. image.src = path;
  513. return image;
  514. }
  515. function generateHeight( width, height ) {
  516. var data = [], perlin = new ImprovedNoise(),
  517. size = width * height, quality = 2, z = Math.random() * 100;
  518. for ( var j = 0; j < 4; j ++ ) {
  519. if ( j == 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  520. for ( var i = 0; i < size; i ++ ) {
  521. var x = i % width, y = ~~ ( i / width );
  522. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  523. }
  524. quality *= 4
  525. }
  526. return data;
  527. }
  528. function getY( x, z ) {
  529. return ~~( data[ x + z * worldWidth ] * 0.2 );
  530. }
  531. //
  532. function animate() {
  533. requestAnimationFrame( animate );
  534. render();
  535. stats.update();
  536. }
  537. function render() {
  538. renderer.render( scene, camera );
  539. }
  540. </script>
  541. </body>
  542. </html>