webgl_loader_svg.html 5.7 KB

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