webgl_geometry_extrude_shapes2.html 13 KB

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