webgl_geometry_minecraft_ao.html 23 KB

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