webgl_geometry_minecraft_ao.html 23 KB

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