webgl_geometry_minecraft_ao.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - geometry - minecraft - ao - webgl</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="../src/extras/GeometryUtils.js"></script>
  44. <script type="text/javascript" src="../src/extras/primitives/Cube.js"></script>
  45. <script type="text/javascript" src="../src/extras/Detector.js"></script>
  46. <script type="text/javascript" src="js/ImprovedNoise.js"></script>
  47. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  48. <script type="text/javascript" src="js/Stats.js"></script>
  49. <script type="text/javascript">
  50. if ( ! THREE.Detector.webgl ) {
  51. THREE.Detector.addGetWebGLMessage();
  52. document.getElementById( 'container' ).innerHTML = "";
  53. }
  54. var fogExp2 = true;
  55. var container, stats;
  56. var camera, scene, renderer;
  57. var mesh;
  58. var worldWidth = 128, worldDepth = 128,
  59. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2,
  60. data = generateHeight( worldWidth, worldDepth );
  61. var mouseX = 0, mouseY = 0,
  62. lat = 0, lon = 0, phy = 0, theta = 0;
  63. var direction = new THREE.Vector3(),
  64. moveForward = false, moveBackward = false, moveLeft = false, moveRight = false;
  65. var windowHalfX = window.innerWidth / 2;
  66. var windowHalfY = window.innerHeight / 2;
  67. init();
  68. animate();
  69. function init() {
  70. container = document.getElementById( 'container' );
  71. if( fogExp2 )
  72. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  73. else
  74. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 7500 );
  75. camera.target.position.z = - 100;
  76. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  77. camera.target.position.y = camera.position.y;
  78. scene = new THREE.Scene();
  79. if( fogExp2 )
  80. scene.fog = new THREE.FogExp2( 0xffffff, 0.00025 );
  81. else
  82. scene.fog = new THREE.Fog( 0xffffff, - 1000, 7500 );
  83. var debug_texture = false,
  84. debug_numbers = false,
  85. debug_corner_colors = false,
  86. strength = 2,
  87. textures = { side: 'textures/minecraft/grass_dirt.png',
  88. top: 'textures/minecraft/grass.png',
  89. bottom: 'textures/minecraft/dirt.png'
  90. },
  91. m_aot = generateMegamaterialAO( textures, strength, debug_texture, debug_numbers, debug_corner_colors ),
  92. m_ao = generateMegamaterialAO( textures, strength, true, debug_numbers, debug_corner_colors ),
  93. m_t = generateMegamaterialPlain( textures ),
  94. //m_d = generateMegamaterialDebug(),
  95. mat = generateMegamaterialAO( textures, strength, debug_texture, debug_numbers, debug_corner_colors ),
  96. materials = [ mat, mat, mat, mat, mat, mat ];
  97. var i, j, x, z, h, h2, uv,
  98. px, nx, pz, nz, sides,
  99. right, left, bottom, top,
  100. nright, nleft, nback, nfront,
  101. nleftup, nrightup, nbackup, nfrontup,
  102. nrb, nrf, nlb, nlf,
  103. nrbup, nrfup,
  104. face_px, face_nx, face_py, face_ny, face_pz, face_nz,
  105. ti, ri, li, bi, fi, ci, mi, mm, column, row,
  106. cube,
  107. unit = 1/16 * 0.95, padding = 1/16 * 0.025, p, s, t, hash, N = -1,
  108. // map of UV indices for faces of partially defined cubes
  109. uv_index_map = {
  110. 0: { px: N, nx: N, py: 0, ny: N, pz: N, nz: N },
  111. 1: { px: N, nx: N, py: 0, ny: N, pz: N, nz: 1 },
  112. 2: { px: N, nx: N, py: 0, ny: N, pz: 1, nz: N },
  113. 3: { px: N, nx: N, py: 0, ny: N, pz: 1, nz: 2 },
  114. 4: { px: N, nx: 0, py: 1, ny: N, pz: N, nz: N },
  115. 5: { px: N, nx: 0, py: 1, ny: N, pz: N, nz: 2 },
  116. 6: { px: N, nx: 0, py: 1, ny: N, pz: 2, nz: N },
  117. 7: { px: N, nx: 0, py: 1, ny: N, pz: 2, nz: 3 },
  118. 8: { px: 0, nx: N, py: 1, ny: N, pz: N, nz: N },
  119. 9: { px: 0, nx: N, py: 1, ny: N, pz: N, nz: 2 },
  120. 10: { px: 0, nx: N, py: 1, ny: N, pz: 2, nz: N },
  121. 11: { px: 0, nx: N, py: 1, ny: N, pz: 2, nz: 3 },
  122. 12: { px: 0, nx: 1, py: 2, ny: N, pz: N, nz: N },
  123. 13: { px: 0, nx: 1, py: 2, ny: N, pz: N, nz: 3 },
  124. 14: { px: 0, nx: 1, py: 2, ny: N, pz: 3, nz: N },
  125. 15: { px: 0, nx: 1, py: 2, ny: N, pz: 3, nz: 4 }
  126. },
  127. // all possible combinations of corners and sides
  128. // mapped to mixed tiles
  129. // (including corners overlapping sides)
  130. // (excluding corner alone and sides alone)
  131. // looks ugly, but allows to squeeze all
  132. // combinations for one texture into just 3 rows
  133. // instead of 16
  134. mixmap = {
  135. "1_1": 0,
  136. "1_3": 0,
  137. "1_9": 0,
  138. "1_11": 0,
  139. "1_4": 1,
  140. "1_6": 1,
  141. "1_12": 1,
  142. "1_14": 1,
  143. "2_2": 2,
  144. "2_3": 2,
  145. "2_6": 2,
  146. "2_7": 2,
  147. "2_8": 3,
  148. "2_9": 3,
  149. "2_12": 3,
  150. "2_13": 3,
  151. "4_1": 4,
  152. "4_5": 4,
  153. "4_9": 4,
  154. "4_13": 4,
  155. "4_2": 5,
  156. "4_6": 5,
  157. "4_10": 5,
  158. "4_14": 5,
  159. "8_4": 6,
  160. "8_5": 6,
  161. "8_6": 6,
  162. "8_7": 6,
  163. "8_8": 7,
  164. "8_9": 7,
  165. "8_10": 7,
  166. "8_11": 7,
  167. "1_5": 8,
  168. "1_7": 8,
  169. "1_13": 8,
  170. "1_15": 8,
  171. "2_10": 9,
  172. "2_11": 9,
  173. "2_14": 9,
  174. "2_15": 9,
  175. "4_3": 10,
  176. "4_7": 10,
  177. "4_11": 10,
  178. "4_15": 10,
  179. "8_12": 11,
  180. "8_13": 11,
  181. "8_14": 11,
  182. "8_15": 11,
  183. "5_1": 12,
  184. "5_3": 12,
  185. "5_7": 12,
  186. "5_9": 12,
  187. "5_11": 12,
  188. "5_13": 12,
  189. "5_15": 12,
  190. "6_2": 13,
  191. "6_3": 13,
  192. "6_6": 13,
  193. "6_7": 13,
  194. "6_10": 13,
  195. "6_11": 13,
  196. "6_14": 13,
  197. "6_15": 13,
  198. "9_4": 14,
  199. "9_5": 14,
  200. "9_6": 14,
  201. "9_7": 14,
  202. "9_12": 14,
  203. "9_13": 14,
  204. "9_14": 14,
  205. "9_15": 14,
  206. "10_8": 15,
  207. "10_9": 15,
  208. "10_10": 15,
  209. "10_11": 15,
  210. "10_12": 15,
  211. "10_13": 15,
  212. "10_14": 15,
  213. "10_15": 15
  214. },
  215. tilemap = {},
  216. top_row_corners = 0,
  217. top_row_mixed = 1,
  218. top_row_sides = 2,
  219. sides_row = 3,
  220. bottom_row = 4,
  221. geometry = new THREE.Geometry();
  222. // mapping from 256 possible corners + sides combinations
  223. // into 3 x 16 tiles
  224. for ( i = 0; i < 16; i++ ) {
  225. for ( j = 0; j < 16; j++ ) {
  226. mm = i + "_" + j;
  227. if ( i == 0 )
  228. row = top_row_corners;
  229. else if ( mixmap[ mm ] != undefined )
  230. row = top_row_mixed;
  231. else
  232. row = top_row_sides;
  233. tilemap[ mm ] = row;
  234. }
  235. }
  236. function setUVTile( face, s, t ) {
  237. var j, uv = cube.uvs[ face ];
  238. for ( j = 0; j < uv.length; j++ ) {
  239. uv[ j ].u += s * (unit+2*padding);
  240. uv[ j ].v += t * (unit+2*padding);
  241. }
  242. }
  243. for ( z = 0; z < worldDepth; z ++ ) {
  244. for ( x = 0; x < worldWidth; x ++ ) {
  245. h = getY( x, z );
  246. // direct neighbors
  247. h2 = getY( x - 1, z );
  248. nleft = h2 == h || h2 == h + 1;
  249. h2 = getY( x + 1, z );
  250. nright = h2 == h || h2 == h + 1;
  251. h2 = getY( x, z + 1 );
  252. nback = h2 == h || h2 == h + 1;
  253. h2 = getY( x, z - 1 );
  254. nfront = h2 == h || h2 == h + 1;
  255. // corner neighbors
  256. nrb = getY( x - 1, z + 1 ) == h && x > 0 && z < worldDepth - 1 ? 1 : 0;
  257. nrf = getY( x - 1, z - 1 ) == h && x > 0 && z > 0 ? 1 : 0;
  258. nlb = getY( x + 1, z + 1 ) == h && x < worldWidth - 1 && z < worldDepth - 1 ? 1 : 0;
  259. nlf = getY( x + 1, z - 1 ) == h && x < worldWidth - 1 && z > 0 ? 1 : 0;
  260. // up neighbors
  261. nleftup = getY( x - 1, z ) > h && x > 0 ? 1 : 0;
  262. nrightup = getY( x + 1, z ) > h && x < worldWidth - 1 ? 1 : 0;
  263. nbackup = getY( x, z + 1 ) > h && z < worldDepth - 1 ? 1 : 0;
  264. nfrontup = getY( x, z - 1 ) > h && z > 0 ? 1 : 0;
  265. // up corner neighbors
  266. nrbup = getY( x - 1, z + 1 ) > h && x > 0 && z < worldDepth - 1 ? 1 : 0;
  267. nrfup = getY( x - 1, z - 1 ) > h && x > 0 && z > 0 ? 1 : 0;
  268. nlbup = getY( x + 1, z + 1 ) > h && x < worldWidth - 1 && z < worldDepth - 1 ? 1 : 0;
  269. nlfup = getY( x + 1, z - 1 ) > h && x < worldWidth - 1 && z > 0 ? 1 : 0;
  270. // textures
  271. ti = nleftup * 8 + nrightup * 4 + nfrontup * 2 + nbackup * 1;
  272. ri = nrf * 8 + nrb * 4 + 1;
  273. li = nlb * 8 + nlf * 4 + 1;
  274. bi = nrb * 8 + nlb * 4 + 1;
  275. fi = nlf * 8 + nrf * 4 + 1;
  276. ci = nlbup * 8 + nlfup * 4 + nrbup * 2 + nrfup * 1;
  277. // cube sides
  278. px = nx = pz = nz = 0;
  279. px = !nleft || x == 0 ? 1 : 0;
  280. nx = !nright || x == worldWidth - 1 ? 1 : 0;
  281. pz = !nback || z == worldDepth - 1 ? 1 : 0;
  282. nz = !nfront || z == 0 ? 1 : 0;
  283. sides = { px: px, nx: nx, py: true, ny: false, pz: pz, nz: nz };
  284. cube = new Cube( 100, 100, 100, 1, 1, materials, false, sides );
  285. // set UV tiles
  286. for ( i = 0; i < cube.uvs.length; i++ ) {
  287. uv = cube.uvs[ 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. GeometryUtils.merge( geometry, mesh );
  321. }
  322. }
  323. geometry.sortFacesByMaterial();
  324. mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
  325. scene.addObject( mesh );
  326. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  327. scene.addLight( ambientLight );
  328. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.5 );
  329. directionalLight.position.x = 1;
  330. directionalLight.position.y = 1;
  331. directionalLight.position.z = 0.5;
  332. directionalLight.position.normalize();
  333. scene.addLight( directionalLight );
  334. renderer = new THREE.WebGLRenderer();
  335. renderer.setSize( window.innerWidth, window.innerHeight );
  336. container.innerHTML = "";
  337. container.appendChild( renderer.domElement );
  338. stats = new Stats();
  339. stats.domElement.style.position = 'absolute';
  340. stats.domElement.style.top = '0px';
  341. container.appendChild( stats.domElement );
  342. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  343. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  344. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  345. document.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  346. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  347. document.addEventListener( 'keyup', onDocumentKeyUp, false );
  348. document.getElementById( "bao" ).addEventListener( "click", function() { mat.map = m_ao.map; }, false );
  349. document.getElementById( "baot" ).addEventListener( "click", function() { mat.map = m_aot.map; }, false );
  350. document.getElementById( "bt" ).addEventListener( "click", function() { mat.map = m_t.map; }, false );
  351. }
  352. function generateMegamaterialAO( textures, strength, debug_texture, debug_numbers, debug_corner_colors ) {
  353. var count = 0,
  354. tex_side = loadTexture( textures.side, function() { count++; generateTexture() } ),
  355. tex_top = loadTexture( textures.top, function() { count++; generateTexture() } ),
  356. tex_bottom = loadTexture( textures.bottom, function() { count++; generateTexture() } ),
  357. canvas = document.createElement( 'canvas' ),
  358. ctx = canvas.getContext( '2d' ),
  359. size = 256, tile = 16;
  360. canvas.width = canvas.height = size;
  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. canvas.loaded = true;
  371. }
  372. }
  373. return new THREE.MeshLambertMaterial( { map: new THREE.Texture( canvas, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter ) } );
  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. function generateTexture() {
  385. if( count == 3 ) {
  386. var i, sx;
  387. for( i = 0; i < 16; i++ ) {
  388. sx = i * tile;
  389. drawBase( ctx, tex_top, sx, 0 * tile, tile, false );
  390. drawBase( ctx, tex_top, sx, 1 * tile, tile, false );
  391. drawBase( ctx, tex_top, sx, 2 * tile, tile, false );
  392. drawBase( ctx, tex_side, sx, 3 * tile, tile, false );
  393. drawBase( ctx, tex_bottom, sx, 4 * tile, tile, false );
  394. }
  395. canvas.loaded = true;
  396. }
  397. }
  398. return new THREE.MeshLambertMaterial( { map: new THREE.Texture( canvas, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter ) } );
  399. }
  400. function generateMegamaterialDebug() {
  401. var canvas = document.createElement( 'canvas' ),
  402. ctx = canvas.getContext( "2d" ),
  403. size = 256, tile = 16,
  404. i, j, h, s;
  405. canvas.width = size;
  406. canvas.height = size;
  407. ctx.textBaseline = "top";
  408. ctx.font = "8pt arial";
  409. for ( i = 0; i < tile; i++ ) {
  410. for ( j = 0; j < tile; j++ ) {
  411. h = i * tile + j;
  412. ctx.fillStyle = "hsl(" + h + ",90%, 50%)";
  413. ctx.fillRect( i * tile, j * tile, tile, tile );
  414. drawHex( ctx, h, i * tile + 2, j * tile + 2 );
  415. }
  416. }
  417. canvas.loaded = true;
  418. return new THREE.MeshLambertMaterial( { map: new THREE.Texture( canvas, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter ) } );
  419. }
  420. function drawHex( ctx, n, x, y ) {
  421. ctx.fillStyle = "black";
  422. ctx.font = "8pt arial";
  423. ctx.textBaseline = "top";
  424. var s = n.toString( 16 );
  425. s = n < 16 ? "0" + s : s;
  426. ctx.fillText( s, x, y );
  427. }
  428. function drawBase( ctx, image, sx, sy, tile, debug_texture ) {
  429. if ( debug_texture ) {
  430. ctx.fillStyle = "#888";
  431. ctx.fillRect( sx, sy, tile, tile );
  432. } else {
  433. ctx.drawImage( image, sx, sy, tile, tile );
  434. }
  435. }
  436. function drawCorner( ctx, sx, sy, sa, ea, color, step, n ) {
  437. for( var i = 0; i < n; i++ ) {
  438. ctx.strokeStyle = color + step * ( n - i ) + ")";
  439. ctx.beginPath();
  440. ctx.arc( sx, sy, i, sa, ea, 0 ) ;
  441. ctx.stroke();
  442. }
  443. }
  444. function drawSide( ctx, sx, sy, a, b, n, width, height, color, step ) {
  445. for( var i = 0; i < n; i++ ) {
  446. ctx.fillStyle = color + step * ( n - i ) + ")";
  447. ctx.fillRect( sx + a * i, sy + b * i, width, height );
  448. }
  449. }
  450. function drawAOSides( ctx, image, row, column, sides, tile, strength, debug_texture, debug_numbers ) {
  451. var sx = column * tile, sy = row * tile;
  452. drawBase( ctx, image, sx, sy, tile, debug_texture );
  453. drawAOSidesImp( ctx, image, row, column, sides, tile, strength );
  454. if ( debug_numbers ) drawHex( ctx, row * tile + sides, sx + 2, sy + 2 );
  455. }
  456. function drawAOCorners( ctx, image, row, column, corners, tile, strength, debug_texture, debug_numbers, debug_corner_colors ) {
  457. var sx = column * tile, sy = row * tile;
  458. drawBase( ctx, image, sx, sy, tile, debug_texture );
  459. drawAOCornersImp( ctx, image, row, column, corners, tile, strength, debug_corner_colors );
  460. if ( debug_numbers ) drawHex( ctx, row * tile + corners, sx + 2, sy + 2 );
  461. }
  462. function drawAOMixed( ctx, image, row, column, elements, tile, strength, debug_texture, debug_numbers, debug_corner_colors ) {
  463. var sx = column * tile, sy = row * tile,
  464. mmap = {
  465. 0: [ 1, 1 ],
  466. 1: [ 1, 4 ],
  467. 2: [ 2, 2 ],
  468. 3: [ 2, 8 ],
  469. 4: [ 4, 1 ],
  470. 5: [ 4, 2 ],
  471. 6: [ 8, 4 ],
  472. 7: [ 8, 8 ],
  473. 8: [ 1, 5 ],
  474. 9: [ 2, 10 ],
  475. 10: [ 4, 3 ],
  476. 11: [ 8, 12 ],
  477. 12: [ 5, 1 ],
  478. 13: [ 6, 2 ],
  479. 14: [ 9, 4 ],
  480. 15: [ 10, 8 ]
  481. };
  482. drawBase( ctx, image, sx, sy, tile, debug_texture );
  483. drawAOCornersImp( ctx, image, row, column, mmap[ elements ][1], tile, strength, debug_corner_colors );
  484. drawAOSidesImp( ctx, image, row, column, mmap[ elements ][0], tile, strength );
  485. if ( debug_numbers ) drawHex( ctx, row * tile + elements, sx + 2, sy + 2 );
  486. }
  487. function drawAOSidesImp( ctx, image, row, column, sides, tile, strength ) {
  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. left = (sides & 8) == 8,
  492. right = (sides & 4) == 4,
  493. bottom = (sides & 2) == 2,
  494. top = (sides & 1) == 1;
  495. if ( bottom ) drawSide( ctx, sx, sy, 0, 1, half, tile, 1, color, step );
  496. if ( top ) drawSide( ctx, sx, sy + full - 1, 0, -1, half, tile, 1, color, step );
  497. if ( left ) drawSide( ctx, sx, sy, 1, 0, half, 1, tile, color, step );
  498. if ( right ) drawSide( ctx, sx + full - 1, sy, -1, 0, half, 1, tile, color, step );
  499. }
  500. function drawAOCornersImp( ctx, image, row, column, corners, tile, strength, debug_corner_colors ) {
  501. var sx = column * tile, sy = row * tile,
  502. full = tile, step = 1 / full, half = full / 2 + strength,
  503. color = "rgba(0, 0, 0, ",
  504. bottomright = (corners & 8) == 8,
  505. topright = (corners & 4) == 4,
  506. bottomleft = (corners & 2) == 2,
  507. topleft = (corners & 1) == 1;
  508. if ( topleft ) {
  509. if ( debug_corner_colors ) color = "rgba(200, 0, 0, ";
  510. drawCorner( ctx, sx, sy, 0, 1.57, color, step, half );
  511. }
  512. if ( bottomleft ) {
  513. if ( debug_corner_colors ) color = "rgba(0, 200, 0, ";
  514. drawCorner( ctx, sx, sy + full, 4.71, 6.28, color, step, half );
  515. }
  516. if ( bottomright ) {
  517. if ( debug_corner_colors ) color = "rgba(0, 0, 200, ";
  518. drawCorner( ctx, sx + full, sy + full, 3.14, 4.71, color, step, half );
  519. }
  520. if ( topright ) {
  521. if ( debug_corner_colors ) color = "rgba(200, 0, 200, ";
  522. drawCorner( ctx, sx + full, sy, 1.57, 3.14, color, step, half );
  523. }
  524. }
  525. function loadTexture( path, callback ) {
  526. var image = new Image();
  527. image.onload = function () { this.loaded = true; callback(); };
  528. image.src = path;
  529. return image;
  530. }
  531. function generateHeight( width, height ) {
  532. var data = [], perlin = new ImprovedNoise(),
  533. size = width * height, quality = 2, z = Math.random() * 100;
  534. for ( var j = 0; j < 4; j ++ ) {
  535. if ( j == 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  536. for ( var i = 0; i < size; i ++ ) {
  537. var x = i % width, y = ~~ ( i / width );
  538. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  539. }
  540. quality *= 4
  541. }
  542. return data;
  543. }
  544. function getY( x, z ) {
  545. return ~~( data[ x + z * worldWidth ] * 0.2 );
  546. }
  547. function onDocumentMouseDown( event ) {
  548. event.preventDefault();
  549. event.stopPropagation();
  550. switch ( event.button ) {
  551. case 0: moveForward = true; break;
  552. case 2: moveBackward = true; break;
  553. }
  554. }
  555. function onDocumentMouseUp( event ) {
  556. event.preventDefault();
  557. event.stopPropagation();
  558. switch ( event.button ) {
  559. case 0: moveForward = false; break;
  560. case 2: moveBackward = false; break;
  561. }
  562. }
  563. function onDocumentMouseMove(event) {
  564. mouseX = event.clientX - windowHalfX;
  565. mouseY = event.clientY - windowHalfY;
  566. }
  567. function onDocumentKeyDown( event ) {
  568. switch( event.keyCode ) {
  569. case 38: /*up*/
  570. case 87: /*W*/ moveForward = true; break;
  571. case 37: /*left*/
  572. case 65: /*A*/ moveLeft = true; break;
  573. case 40: /*down*/
  574. case 83: /*S*/ moveBackward = true; break;
  575. case 39: /*right*/
  576. case 68: /*D*/ moveRight = true; break;
  577. }
  578. }
  579. function onDocumentKeyUp( event ) {
  580. switch( event.keyCode ) {
  581. case 38: /*up*/
  582. case 87: /*W*/ moveForward = false; break;
  583. case 37: /*left*/
  584. case 65: /*A*/ moveLeft = false; break;
  585. case 40: /*down*/
  586. case 83: /*S*/ moveBackward = false; break;
  587. case 39: /*right*/
  588. case 68: /*D*/ moveRight = false; break;
  589. }
  590. }
  591. //
  592. function animate() {
  593. requestAnimationFrame( animate );
  594. render();
  595. stats.update();
  596. }
  597. function render() {
  598. if ( moveForward ) camera.translateZ( - 15 );
  599. if ( moveBackward ) camera.translateZ( 15 );
  600. if ( moveLeft ) camera.translateX( - 15 );
  601. if ( moveRight ) camera.translateX( 15 );
  602. lon += mouseX * 0.005;
  603. lat -= mouseY * 0.005;
  604. lat = Math.max( - 85, Math.min( 85, lat ) );
  605. phi = ( 90 - lat ) * Math.PI / 180;
  606. theta = lon * Math.PI / 180;
  607. camera.target.position.x = 100 * Math.sin( phi ) * Math.cos( theta ) + camera.position.x;
  608. camera.target.position.y = 100 * Math.cos( phi ) + camera.position.y;
  609. camera.target.position.z = 100 * Math.sin( phi ) * Math.sin( theta ) + camera.position.z;
  610. renderer.render(scene, camera);
  611. }
  612. </script>
  613. </body>
  614. </html>