webgl_geometry_minecraft_ao.html 20 KB

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