123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - svg loader</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #b0b0b0;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - SVGLoader
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { SVGLoader } from './jsm/loaders/SVGLoader.js';
- var renderer, stats, scene, camera, gui, guiData;
- init();
- animate();
- //
- function init() {
- var container = document.getElementById( 'container' );
- //
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 0, 200 );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.screenSpacePanning = true;
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- guiData = {
- currentURL: 'models/svg/tiger.svg',
- drawFillShapes: true,
- drawStrokes: true,
- fillShapesWireframe: false,
- strokesWireframe: false
- };
- loadSVG( guiData.currentURL );
- createGUI();
- }
- function createGUI() {
- if ( gui ) gui.destroy();
- gui = new GUI( { width: 350 } );
- gui.add( guiData, 'currentURL', {
- "Tiger": 'models/svg/tiger.svg',
- "Three.js": 'models/svg/threejs.svg',
- "Joins and caps": 'models/svg/lineJoinsAndCaps.svg',
- "Hexagon": 'models/svg/hexagon.svg',
- "Test 1": 'models/svg/tests/1.svg',
- "Test 2": 'models/svg/tests/2.svg',
- "Test 3": 'models/svg/tests/3.svg',
- "Test 4": 'models/svg/tests/4.svg',
- "Test 5": 'models/svg/tests/5.svg',
- "Test 6": 'models/svg/tests/6.svg',
- "Test 7": 'models/svg/tests/7.svg',
- "Test 8": 'models/svg/tests/8.svg',
- "Units": 'models/svg/tests/units.svg'
- } ).name( 'SVG File' ).onChange( update );
- gui.add( guiData, 'drawStrokes' ).name( 'Draw strokes' ).onChange( update );
- gui.add( guiData, 'drawFillShapes' ).name( 'Draw fill shapes' ).onChange( update );
- gui.add( guiData, 'strokesWireframe' ).name( 'Wireframe strokes' ).onChange( update );
- gui.add( guiData, 'fillShapesWireframe' ).name( 'Wireframe fill shapes' ).onChange( update );
- function update() {
- loadSVG( guiData.currentURL );
- }
- }
- function loadSVG( url ) {
- //
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xb0b0b0 );
- //
- var helper = new THREE.GridHelper( 160, 10 );
- helper.rotation.x = Math.PI / 2;
- scene.add( helper );
- //
- var loader = new SVGLoader();
- loader.load( url, function ( data ) {
- var paths = data.paths;
- var group = new THREE.Group();
- group.scale.multiplyScalar( 0.25 );
- group.position.x = - 70;
- group.position.y = 70;
- group.scale.y *= - 1;
- for ( var i = 0; i < paths.length; i ++ ) {
- var path = paths[ i ];
- var fillColor = path.userData.style.fill;
- if ( guiData.drawFillShapes && fillColor !== undefined && fillColor !== 'none' ) {
- var material = new THREE.MeshBasicMaterial( {
- color: new THREE.Color().setStyle( fillColor ),
- opacity: path.userData.style.fillOpacity,
- transparent: path.userData.style.fillOpacity < 1,
- side: THREE.DoubleSide,
- depthWrite: false,
- wireframe: guiData.fillShapesWireframe
- } );
- var shapes = path.toShapes( true );
- for ( var j = 0; j < shapes.length; j ++ ) {
- var shape = shapes[ j ];
- var geometry = new THREE.ShapeBufferGeometry( shape );
- var mesh = new THREE.Mesh( geometry, material );
- group.add( mesh );
- }
- }
- var strokeColor = path.userData.style.stroke;
- if ( guiData.drawStrokes && strokeColor !== undefined && strokeColor !== 'none' ) {
- var material = new THREE.MeshBasicMaterial( {
- color: new THREE.Color().setStyle( strokeColor ),
- opacity: path.userData.style.strokeOpacity,
- transparent: path.userData.style.strokeOpacity < 1,
- side: THREE.DoubleSide,
- depthWrite: false,
- wireframe: guiData.strokesWireframe
- } );
- for ( var j = 0, jl = path.subPaths.length; j < jl; j ++ ) {
- var subPath = path.subPaths[ j ];
- var geometry = SVGLoader.pointsToStroke( subPath.getPoints(), path.userData.style );
- if ( geometry ) {
- var mesh = new THREE.Mesh( geometry, material );
- group.add( mesh );
- }
- }
- }
- }
- scene.add( group );
- } );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|