geometry_minecraft_ao.html 24 KB

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