geometry_minecraft_ao.html 23 KB

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