webgl_loader_svg.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - svg loader</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. background-color: #b0b0b0;
  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> - SVGLoader
  18. </div>
  19. <script type="module">
  20. import * as THREE from '../build/three.module.js';
  21. import Stats from './jsm/libs/stats.module.js';
  22. import { GUI } from './jsm/libs/dat.gui.module.js';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. import { SVGLoader } from './jsm/loaders/SVGLoader.js';
  25. let renderer, stats, scene, camera, gui, guiData;
  26. init();
  27. animate();
  28. //
  29. function init() {
  30. const container = document.getElementById( 'container' );
  31. //
  32. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  33. camera.position.set( 0, 0, 200 );
  34. //
  35. renderer = new THREE.WebGLRenderer( { antialias: true } );
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. container.appendChild( renderer.domElement );
  39. //
  40. const controls = new OrbitControls( camera, renderer.domElement );
  41. controls.screenSpacePanning = true;
  42. //
  43. stats = new Stats();
  44. container.appendChild( stats.dom );
  45. //
  46. window.addEventListener( 'resize', onWindowResize );
  47. guiData = {
  48. currentURL: 'models/svg/tiger.svg',
  49. drawFillShapes: true,
  50. drawStrokes: true,
  51. fillShapesWireframe: false,
  52. strokesWireframe: false
  53. };
  54. loadSVG( guiData.currentURL );
  55. createGUI();
  56. }
  57. function createGUI() {
  58. if ( gui ) gui.destroy();
  59. gui = new GUI( { width: 350 } );
  60. gui.add( guiData, 'currentURL', {
  61. "Tiger": 'models/svg/tiger.svg',
  62. "Three.js": 'models/svg/threejs.svg',
  63. "Joins and caps": 'models/svg/lineJoinsAndCaps.svg',
  64. "Hexagon": 'models/svg/hexagon.svg',
  65. "Test 1": 'models/svg/tests/1.svg',
  66. "Test 2": 'models/svg/tests/2.svg',
  67. "Test 3": 'models/svg/tests/3.svg',
  68. "Test 4": 'models/svg/tests/4.svg',
  69. "Test 5": 'models/svg/tests/5.svg',
  70. "Test 6": 'models/svg/tests/6.svg',
  71. "Test 7": 'models/svg/tests/7.svg',
  72. "Test 8": 'models/svg/tests/8.svg',
  73. "Units": 'models/svg/tests/units.svg',
  74. "Defs": 'models/svg/tests/testDefs/Svg-defs.svg',
  75. "Defs2": 'models/svg/tests/testDefs/Svg-defs2.svg',
  76. "Defs3": 'models/svg/tests/testDefs/Wave-defs.svg',
  77. "Defs4": 'models/svg/tests/testDefs/defs4.svg',
  78. "Zero Radius": 'models/svg/zero-radius.svg'
  79. } ).name( 'SVG File' ).onChange( update );
  80. gui.add( guiData, 'drawStrokes' ).name( 'Draw strokes' ).onChange( update );
  81. gui.add( guiData, 'drawFillShapes' ).name( 'Draw fill shapes' ).onChange( update );
  82. gui.add( guiData, 'strokesWireframe' ).name( 'Wireframe strokes' ).onChange( update );
  83. gui.add( guiData, 'fillShapesWireframe' ).name( 'Wireframe fill shapes' ).onChange( update );
  84. function update() {
  85. loadSVG( guiData.currentURL );
  86. }
  87. }
  88. function loadSVG( url ) {
  89. //
  90. scene = new THREE.Scene();
  91. scene.background = new THREE.Color( 0xb0b0b0 );
  92. //
  93. const helper = new THREE.GridHelper( 160, 10 );
  94. helper.rotation.x = Math.PI / 2;
  95. scene.add( helper );
  96. //
  97. const loader = new SVGLoader();
  98. loader.load( url, function ( data ) {
  99. const paths = data.paths;
  100. const group = new THREE.Group();
  101. group.scale.multiplyScalar( 0.25 );
  102. group.position.x = - 70;
  103. group.position.y = 70;
  104. group.scale.y *= - 1;
  105. for ( let i = 0; i < paths.length; i ++ ) {
  106. const path = paths[ i ];
  107. const fillColor = path.userData.style.fill;
  108. if ( guiData.drawFillShapes && fillColor !== undefined && fillColor !== 'none' ) {
  109. const material = new THREE.MeshBasicMaterial( {
  110. color: new THREE.Color().setStyle( fillColor ),
  111. opacity: path.userData.style.fillOpacity,
  112. transparent: path.userData.style.fillOpacity < 1,
  113. side: THREE.DoubleSide,
  114. depthWrite: false,
  115. wireframe: guiData.fillShapesWireframe
  116. } );
  117. const shapes = path.toShapes( true );
  118. for ( let j = 0; j < shapes.length; j ++ ) {
  119. const shape = shapes[ j ];
  120. const geometry = new THREE.ShapeGeometry( shape );
  121. const mesh = new THREE.Mesh( geometry, material );
  122. group.add( mesh );
  123. }
  124. }
  125. const strokeColor = path.userData.style.stroke;
  126. if ( guiData.drawStrokes && strokeColor !== undefined && strokeColor !== 'none' ) {
  127. const material = new THREE.MeshBasicMaterial( {
  128. color: new THREE.Color().setStyle( strokeColor ),
  129. opacity: path.userData.style.strokeOpacity,
  130. transparent: path.userData.style.strokeOpacity < 1,
  131. side: THREE.DoubleSide,
  132. depthWrite: false,
  133. wireframe: guiData.strokesWireframe
  134. } );
  135. for ( let j = 0, jl = path.subPaths.length; j < jl; j ++ ) {
  136. const subPath = path.subPaths[ j ];
  137. const geometry = SVGLoader.pointsToStroke( subPath.getPoints(), path.userData.style );
  138. if ( geometry ) {
  139. const mesh = new THREE.Mesh( geometry, material );
  140. group.add( mesh );
  141. }
  142. }
  143. }
  144. }
  145. scene.add( group );
  146. } );
  147. }
  148. function onWindowResize() {
  149. camera.aspect = window.innerWidth / window.innerHeight;
  150. camera.updateProjectionMatrix();
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. render();
  156. stats.update();
  157. }
  158. function render() {
  159. renderer.render( scene, camera );
  160. }
  161. </script>
  162. </body>
  163. </html>