webgl_loader_svg.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. var renderer, stats, scene, camera, gui, guiData;
  26. init();
  27. animate();
  28. //
  29. function init() {
  30. var 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. var 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, false );
  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. } ).name( 'SVG File' ).onChange( update );
  75. gui.add( guiData, 'drawStrokes' ).name( 'Draw strokes' ).onChange( update );
  76. gui.add( guiData, 'drawFillShapes' ).name( 'Draw fill shapes' ).onChange( update );
  77. gui.add( guiData, 'strokesWireframe' ).name( 'Wireframe strokes' ).onChange( update );
  78. gui.add( guiData, 'fillShapesWireframe' ).name( 'Wireframe fill shapes' ).onChange( update );
  79. function update() {
  80. loadSVG( guiData.currentURL );
  81. }
  82. }
  83. function loadSVG( url ) {
  84. //
  85. scene = new THREE.Scene();
  86. scene.background = new THREE.Color( 0xb0b0b0 );
  87. //
  88. var helper = new THREE.GridHelper( 160, 10 );
  89. helper.rotation.x = Math.PI / 2;
  90. scene.add( helper );
  91. //
  92. var loader = new SVGLoader();
  93. loader.load( url, function ( data ) {
  94. var paths = data.paths;
  95. var group = new THREE.Group();
  96. group.scale.multiplyScalar( 0.25 );
  97. group.position.x = - 70;
  98. group.position.y = 70;
  99. group.scale.y *= - 1;
  100. for ( var i = 0; i < paths.length; i ++ ) {
  101. var path = paths[ i ];
  102. var fillColor = path.userData.style.fill;
  103. if ( guiData.drawFillShapes && fillColor !== undefined && fillColor !== 'none' ) {
  104. var material = new THREE.MeshBasicMaterial( {
  105. color: new THREE.Color().setStyle( fillColor ),
  106. opacity: path.userData.style.fillOpacity,
  107. transparent: path.userData.style.fillOpacity < 1,
  108. side: THREE.DoubleSide,
  109. depthWrite: false,
  110. wireframe: guiData.fillShapesWireframe
  111. } );
  112. var shapes = path.toShapes( true );
  113. for ( var j = 0; j < shapes.length; j ++ ) {
  114. var shape = shapes[ j ];
  115. var geometry = new THREE.ShapeBufferGeometry( shape );
  116. var mesh = new THREE.Mesh( geometry, material );
  117. group.add( mesh );
  118. }
  119. }
  120. var strokeColor = path.userData.style.stroke;
  121. if ( guiData.drawStrokes && strokeColor !== undefined && strokeColor !== 'none' ) {
  122. var material = new THREE.MeshBasicMaterial( {
  123. color: new THREE.Color().setStyle( strokeColor ),
  124. opacity: path.userData.style.strokeOpacity,
  125. transparent: path.userData.style.strokeOpacity < 1,
  126. side: THREE.DoubleSide,
  127. depthWrite: false,
  128. wireframe: guiData.strokesWireframe
  129. } );
  130. for ( var j = 0, jl = path.subPaths.length; j < jl; j ++ ) {
  131. var subPath = path.subPaths[ j ];
  132. var geometry = SVGLoader.pointsToStroke( subPath.getPoints(), path.userData.style );
  133. if ( geometry ) {
  134. var mesh = new THREE.Mesh( geometry, material );
  135. group.add( mesh );
  136. }
  137. }
  138. }
  139. }
  140. scene.add( group );
  141. } );
  142. }
  143. function onWindowResize() {
  144. camera.aspect = window.innerWidth / window.innerHeight;
  145. camera.updateProjectionMatrix();
  146. renderer.setSize( window.innerWidth, window.innerHeight );
  147. }
  148. function animate() {
  149. requestAnimationFrame( animate );
  150. render();
  151. stats.update();
  152. }
  153. function render() {
  154. renderer.render( scene, camera );
  155. }
  156. </script>
  157. </body>
  158. </html>