webgl_geometry_minecraft_ao.html 21 KB

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