webgl_loader_svg.html 5.6 KB

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