webgl_geometry_minecraft_ao.html 20 KB

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