webgl_geometry_extrude_shapes2.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - extrude shapes from geodata</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #ffffff;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. font-weight: bold;
  14. background-color: #000000;
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. position: absolute;
  20. top: 0px;
  21. width: 100%;
  22. padding: 5px;
  23. }
  24. a {
  25. color: #ffffff;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container"></div>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Shapes Extrusion via Geo Data
  33. </div>
  34. <script type="text/javascript" src="../build/three.js"></script>
  35. <script src="js/controls/OrbitControls.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. // From d3-threeD.js
  39. /* This Source Code Form is subject to the terms of the Mozilla Public
  40. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  41. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  42. function d3threeD(exports) {
  43. const DEGS_TO_RADS = Math.PI / 180, UNIT_SIZE = 100;
  44. const DIGIT_0 = 48, DIGIT_9 = 57, COMMA = 44, SPACE = 32, PERIOD = 46, MINUS = 45;
  45. exports.transformSVGPath =
  46. function transformSVGPath(pathStr) {
  47. var path = new THREE.ShapePath();
  48. var idx = 1, len = pathStr.length, activeCmd,
  49. x = 0, y = 0, nx = 0, ny = 0, firstX = null, firstY = null,
  50. x1 = 0, x2 = 0, y1 = 0, y2 = 0,
  51. rx = 0, ry = 0, xar = 0, laf = 0, sf = 0, cx, cy;
  52. function eatNum() {
  53. var sidx, c, isFloat = false, s;
  54. // eat delims
  55. while (idx < len) {
  56. c = pathStr.charCodeAt(idx);
  57. if (c !== COMMA && c !== SPACE)
  58. break;
  59. idx++;
  60. }
  61. if (c === MINUS)
  62. sidx = idx++;
  63. else
  64. sidx = idx;
  65. // eat number
  66. while (idx < len) {
  67. c = pathStr.charCodeAt(idx);
  68. if (DIGIT_0 <= c && c <= DIGIT_9) {
  69. idx++;
  70. continue;
  71. }
  72. else if (c === PERIOD) {
  73. idx++;
  74. isFloat = true;
  75. continue;
  76. }
  77. s = pathStr.substring(sidx, idx);
  78. return isFloat ? parseFloat(s) : parseInt(s);
  79. }
  80. s = pathStr.substring(sidx);
  81. return isFloat ? parseFloat(s) : parseInt(s);
  82. }
  83. function nextIsNum() {
  84. var c;
  85. // do permanently eat any delims...
  86. while (idx < len) {
  87. c = pathStr.charCodeAt(idx);
  88. if (c !== COMMA && c !== SPACE)
  89. break;
  90. idx++;
  91. }
  92. c = pathStr.charCodeAt(idx);
  93. return (c === MINUS || (DIGIT_0 <= c && c <= DIGIT_9));
  94. }
  95. var canRepeat;
  96. activeCmd = pathStr[0];
  97. while (idx <= len) {
  98. canRepeat = true;
  99. switch (activeCmd) {
  100. // moveto commands, become lineto's if repeated
  101. case 'M':
  102. x = eatNum();
  103. y = eatNum();
  104. path.moveTo(x, y);
  105. activeCmd = 'L';
  106. firstX = x;
  107. firstY = y;
  108. break;
  109. case 'm':
  110. x += eatNum();
  111. y += eatNum();
  112. path.moveTo(x, y);
  113. activeCmd = 'l';
  114. firstX = x;
  115. firstY = y;
  116. break;
  117. case 'Z':
  118. case 'z':
  119. canRepeat = false;
  120. if (x !== firstX || y !== firstY)
  121. path.lineTo(firstX, firstY);
  122. break;
  123. // - lines!
  124. case 'L':
  125. case 'H':
  126. case 'V':
  127. nx = (activeCmd === 'V') ? x : eatNum();
  128. ny = (activeCmd === 'H') ? y : eatNum();
  129. path.lineTo(nx, ny);
  130. x = nx;
  131. y = ny;
  132. break;
  133. case 'l':
  134. case 'h':
  135. case 'v':
  136. nx = (activeCmd === 'v') ? x : (x + eatNum());
  137. ny = (activeCmd === 'h') ? y : (y + eatNum());
  138. path.lineTo(nx, ny);
  139. x = nx;
  140. y = ny;
  141. break;
  142. // - cubic bezier
  143. case 'C':
  144. x1 = eatNum(); y1 = eatNum();
  145. case 'S':
  146. if (activeCmd === 'S') {
  147. x1 = 2 * x - x2; y1 = 2 * y - y2;
  148. }
  149. x2 = eatNum();
  150. y2 = eatNum();
  151. nx = eatNum();
  152. ny = eatNum();
  153. path.bezierCurveTo(x1, y1, x2, y2, nx, ny);
  154. x = nx; y = ny;
  155. break;
  156. case 'c':
  157. x1 = x + eatNum();
  158. y1 = y + eatNum();
  159. case 's':
  160. if (activeCmd === 's') {
  161. x1 = 2 * x - x2;
  162. y1 = 2 * y - y2;
  163. }
  164. x2 = x + eatNum();
  165. y2 = y + eatNum();
  166. nx = x + eatNum();
  167. ny = y + eatNum();
  168. path.bezierCurveTo(x1, y1, x2, y2, nx, ny);
  169. x = nx; y = ny;
  170. break;
  171. // - quadratic bezier
  172. case 'Q':
  173. x1 = eatNum(); y1 = eatNum();
  174. case 'T':
  175. if (activeCmd === 'T') {
  176. x1 = 2 * x - x1;
  177. y1 = 2 * y - y1;
  178. }
  179. nx = eatNum();
  180. ny = eatNum();
  181. path.quadraticCurveTo(x1, y1, nx, ny);
  182. x = nx;
  183. y = ny;
  184. break;
  185. case 'q':
  186. x1 = x + eatNum();
  187. y1 = y + eatNum();
  188. case 't':
  189. if (activeCmd === 't') {
  190. x1 = 2 * x - x1;
  191. y1 = 2 * y - y1;
  192. }
  193. nx = x + eatNum();
  194. ny = y + eatNum();
  195. path.quadraticCurveTo(x1, y1, nx, ny);
  196. x = nx; y = ny;
  197. break;
  198. // - elliptical arc
  199. case 'A':
  200. rx = eatNum();
  201. ry = eatNum();
  202. xar = eatNum() * DEGS_TO_RADS;
  203. laf = eatNum();
  204. sf = eatNum();
  205. nx = eatNum();
  206. ny = eatNum();
  207. if (rx !== ry) {
  208. console.warn("Forcing elliptical arc to be a circular one :(",
  209. rx, ry);
  210. }
  211. // SVG implementation notes does all the math for us! woo!
  212. // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  213. // step1, using x1 as x1'
  214. x1 = Math.cos(xar) * (x - nx) / 2 + Math.sin(xar) * (y - ny) / 2;
  215. y1 = -Math.sin(xar) * (x - nx) / 2 + Math.cos(xar) * (y - ny) / 2;
  216. // step 2, using x2 as cx'
  217. var norm = Math.sqrt(
  218. (rx*rx * ry*ry - rx*rx * y1*y1 - ry*ry * x1*x1) /
  219. (rx*rx * y1*y1 + ry*ry * x1*x1));
  220. if (laf === sf)
  221. norm = -norm;
  222. x2 = norm * rx * y1 / ry;
  223. y2 = norm * -ry * x1 / rx;
  224. // step 3
  225. cx = Math.cos(xar) * x2 - Math.sin(xar) * y2 + (x + nx) / 2;
  226. cy = Math.sin(xar) * x2 + Math.cos(xar) * y2 + (y + ny) / 2;
  227. var u = new THREE.Vector2(1, 0),
  228. v = new THREE.Vector2((x1 - x2) / rx,
  229. (y1 - y2) / ry);
  230. var startAng = Math.acos(u.dot(v) / u.length() / v.length());
  231. if (u.x * v.y - u.y * v.x < 0)
  232. startAng = -startAng;
  233. // we can reuse 'v' from start angle as our 'u' for delta angle
  234. u.x = (-x1 - x2) / rx;
  235. u.y = (-y1 - y2) / ry;
  236. var deltaAng = Math.acos(v.dot(u) / v.length() / u.length());
  237. // This normalization ends up making our curves fail to triangulate...
  238. if (v.x * u.y - v.y * u.x < 0)
  239. deltaAng = -deltaAng;
  240. if (!sf && deltaAng > 0)
  241. deltaAng -= Math.PI * 2;
  242. if (sf && deltaAng < 0)
  243. deltaAng += Math.PI * 2;
  244. path.absarc(cx, cy, rx, startAng, startAng + deltaAng, sf);
  245. x = nx;
  246. y = ny;
  247. break;
  248. default:
  249. throw new Error("weird path command: " + activeCmd);
  250. }
  251. // just reissue the command
  252. if (canRepeat && nextIsNum())
  253. continue;
  254. activeCmd = pathStr[idx++];
  255. }
  256. return path;
  257. }
  258. }
  259. var $d3g = {};
  260. d3threeD( $d3g );
  261. /// Part from g0v/twgeojson
  262. /// Graphic Engine and Geo Data Init Functions
  263. var addGeoObject = function( group, svgObject ) {
  264. var paths = svgObject.paths;
  265. var amounts = svgObject.amounts;
  266. var colors = svgObject.colors;
  267. var center = svgObject.center;
  268. for ( var i = 0; i < paths.length; i ++ ) {
  269. var path = $d3g.transformSVGPath( paths[ i ] );
  270. var color = new THREE.Color( colors[ i ] );
  271. var material = new THREE.MeshLambertMaterial( {
  272. color: color,
  273. emissive: color
  274. } );
  275. var amount = amounts[ i ];
  276. var simpleShapes = path.toShapes( true );
  277. for ( var j = 0; j < simpleShapes.length; j ++ ) {
  278. simpleShape = simpleShapes[ j ];
  279. var shape3d = new THREE.ExtrudeBufferGeometry( simpleShape, {
  280. amount: amount,
  281. bevelEnabled: false
  282. } );
  283. var mesh = new THREE.Mesh( shape3d, material );
  284. mesh.rotation.x = Math.PI;
  285. mesh.translateZ( - amount - 1 );
  286. mesh.translateX( - center.x );
  287. mesh.translateY( - center.y );
  288. group.add( mesh );
  289. }
  290. }
  291. };
  292. var renderer, stats, scene, camera;
  293. init();
  294. animate();
  295. //
  296. function init() {
  297. var container = document.getElementById( 'container' );
  298. //
  299. scene = new THREE.Scene();
  300. scene.background = new THREE.Color( 0xb0b0b0 );
  301. //
  302. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  303. camera.position.set( 0, 0, 200 );
  304. //
  305. var group = new THREE.Group();
  306. scene.add( group );
  307. //
  308. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.6 );
  309. directionalLight.position.set( 0.75, 0.75, 1.0 ).normalize();
  310. scene.add( directionalLight );
  311. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.2 );
  312. scene.add( ambientLight );
  313. //
  314. var helper = new THREE.GridHelper( 160, 10 );
  315. helper.rotation.x = Math.PI / 2;
  316. group.add( helper );
  317. //
  318. var obj = initSVGObject();
  319. addGeoObject( group, obj );
  320. //
  321. renderer = new THREE.WebGLRenderer( { antialias: true } );
  322. renderer.setPixelRatio( window.devicePixelRatio );
  323. renderer.setSize( window.innerWidth, window.innerHeight );
  324. container.appendChild( renderer.domElement );
  325. //
  326. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  327. //
  328. stats = new Stats();
  329. container.appendChild( stats.dom );
  330. //
  331. window.addEventListener( 'resize', onWindowResize, false );
  332. }
  333. function initSVGObject() {
  334. var obj = {};
  335. /// The geo data from Taipei City, Keelung City, Taipei County in SVG form
  336. obj.paths = [
  337. /// Taipei City
  338. "M366.2182,108.9780 L368.0329,110.3682 L367.5922,112.4411 L369.9258,116.0311 L368.9827,117.3543 " +
  339. "L371.5686,119.8491 L370.5599,121.7206 L372.9314,124.8009 L368.8889,126.7603 L369.2695,130.7622 " +
  340. "L366.1499,130.3388 L363.4698,128.1161 L362.9256,125.6018 L360.8153,126.4025 L360.2968,124.3588 " +
  341. "L361.9519,121.1623 L360.4475,118.7162 L358.1163,117.8678 L358.7094,115.7577 L361.6243,112.4576 Z",
  342. /// Keelung City
  343. "M380.2689,113.3850 L383.5604,114.2370 L383.7404,114.2386 L385.4082,115.6247 L384.9725,117.4631 " +
  344. "L381.6681,117.9439 L383.0209,121.0914 L379.4649,122.7061 L373.4987,118.8487 L372.0980,114.7589 " +
  345. "L377.9716,112.0707 Z",
  346. /// Taipei County
  347. "M359.4486,155.6690 L357.0422,152.7420 L355.1688,148.0173 L357.1186,145.8045 L354.1323,141.2242 " +
  348. "L351.1807,141.6609 L348.9387,140.5372 L349.5415,137.8396 L347.5174,136.1694 L347.6299,129.2327 " +
  349. "L351.4192,128.8067 L354.2518,125.3113 L352.5805,121.8038 L349.3190,120.9429 L344.3277,116.7676 " +
  350. "L350.9772,115.1221 L354.5759,112.5371 L354.5667,110.6949 L357.4098,105.7489 L362.3963,101.8443 " +
  351. "L364.4415,101.0819 L364.5314,101.0828 L364.6209,101.1230 L364.7698,101.2029 L368.1221,101.5115 " +
  352. "L371.7216,104.1338 L372.2958,106.7261 L375.5949,109.6971 L377.0415,108.8875 L377.0737,108.6526 " +
  353. "L377.4037,108.6165 L376.8840,109.7091 L376.7323,109.9037 L377.9416,112.0705 L371.7970,114.8736 " +
  354. "L374.0935,119.4031 L380.7848,122.7963 L382.6529,121.9897 L381.5792,117.8256 L385.0339,117.3069 " +
  355. "L385.4082,115.6247 L388.7014,116.3969 L389.8697,116.6024 L390.0206,116.4860 L391.0396,116.6118 " +
  356. "L394.6665,116.9929 L394.4694,119.2255 L394.3172,119.4987 L395.3792,121.8977 L395.2728,124.0526 " +
  357. "L397.2123,125.6350 L401.1709,126.2516 L401.2612,126.2130 L401.4086,126.6060 L400.1992,127.7733 " +
  358. "L399.7769,128.0446 L399.6247,128.3179 L398.1779,129.0521 L394.2418,129.2969 L388.7324,130.9385 " +
  359. "L389.2782,134.0003 L383.7237,137.0111 L381.7445,139.9336 L379.7001,139.9546 L376.1539,143.0580 " +
  360. "L371.3022,144.1094 L368.6009,146.5914 L368.7361,151.1399 L363.6153,154.4980 " +
  361. /// Taipei County hole.
  362. "M363.4600,128.3904 L366.6300,130.3829 L369.3732,129.3913 L369.5603,125.6695 L374.3989,125.1677 " +
  363. "L370.8412,123.6440 L371.0684,118.8252 L369.0431,117.3157 L369.6882,115.7936 L367.8578,112.8749 " +
  364. "L368.1217,110.4867 L366.5152,109.2554 L361.9554,112.3435 L358.1163,117.8678 L361.7218,120.2192 " +
  365. "L360.7261,126.3232 L362.8064,125.5221 Z"];
  366. obj.amounts = [ 19, 20, 21 ];
  367. obj.colors = [ 0xC07000, 0xC08000, 0xC0A000 ];
  368. obj.center = { x: 365, y: 125 };
  369. return obj;
  370. }
  371. function onWindowResize() {
  372. camera.aspect = window.innerWidth / window.innerHeight;
  373. camera.updateProjectionMatrix();
  374. renderer.setSize( window.innerWidth, window.innerHeight );
  375. }
  376. function animate() {
  377. requestAnimationFrame( animate );
  378. render();
  379. stats.update();
  380. }
  381. function render() {
  382. renderer.render( scene, camera );
  383. }
  384. </script>
  385. </body>
  386. </html>