geometry_minecraft_ao.html 23 KB

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