-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
webgl_loader_svg.html
263 lines (179 loc) · 6.71 KB
/
webgl_loader_svg.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!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>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { SVGLoader } from 'three/addons/loaders/SVGLoader.js';
let renderer, scene, camera, gui, guiData;
init();
//
function init() {
const 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 );
//
const controls = new OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render );
controls.screenSpacePanning = true;
//
window.addEventListener( 'resize', onWindowResize );
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();
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',
'Energy': 'models/svg/energy.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',
'Test 9': 'models/svg/tests/9.svg',
'Units': 'models/svg/tests/units.svg',
'Ordering': 'models/svg/tests/ordering.svg',
'Defs': 'models/svg/tests/testDefs/Svg-defs.svg',
'Defs2': 'models/svg/tests/testDefs/Svg-defs2.svg',
'Defs3': 'models/svg/tests/testDefs/Wave-defs.svg',
'Defs4': 'models/svg/tests/testDefs/defs4.svg',
'Defs5': 'models/svg/tests/testDefs/defs5.svg',
'Style CSS inside defs': 'models/svg/style-css-inside-defs.svg',
'Multiple CSS classes': 'models/svg/multiple-css-classes.svg',
'Zero Radius': 'models/svg/zero-radius.svg',
'Styles in svg tag': 'models/svg/tests/styles.svg',
'Round join': 'models/svg/tests/roundJoinPrecisionIssue.svg',
'Ellipse Transformations': 'models/svg/tests/ellipseTransform.svg',
'singlePointTest': 'models/svg/singlePointTest.svg',
'singlePointTest2': 'models/svg/singlePointTest2.svg',
'singlePointTest3': 'models/svg/singlePointTest3.svg',
'emptyPath': 'models/svg/emptyPath.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 );
//
const helper = new THREE.GridHelper( 160, 10, 0x8d8d8d, 0xc1c1c1 );
helper.rotation.x = Math.PI / 2;
scene.add( helper );
//
const loader = new SVGLoader();
loader.load( url, function ( data ) {
const paths = data.paths;
const group = new THREE.Group();
group.scale.multiplyScalar( 0.25 );
group.position.x = - 70;
group.position.y = 70;
group.scale.y *= - 1;
for ( let i = 0; i < paths.length; i ++ ) {
const path = paths[ i ];
const fillColor = path.userData.style.fill;
if ( guiData.drawFillShapes && fillColor !== undefined && fillColor !== 'none' ) {
const material = new THREE.MeshBasicMaterial( {
color: new THREE.Color().setStyle( fillColor ),
opacity: path.userData.style.fillOpacity,
transparent: true,
side: THREE.DoubleSide,
depthWrite: false,
wireframe: guiData.fillShapesWireframe
} );
const shapes = SVGLoader.createShapes( path );
for ( let j = 0; j < shapes.length; j ++ ) {
const shape = shapes[ j ];
const geometry = new THREE.ShapeGeometry( shape );
const mesh = new THREE.Mesh( geometry, material );
group.add( mesh );
}
}
const strokeColor = path.userData.style.stroke;
if ( guiData.drawStrokes && strokeColor !== undefined && strokeColor !== 'none' ) {
const material = new THREE.MeshBasicMaterial( {
color: new THREE.Color().setStyle( strokeColor ),
opacity: path.userData.style.strokeOpacity,
transparent: true,
side: THREE.DoubleSide,
depthWrite: false,
wireframe: guiData.strokesWireframe
} );
for ( let j = 0, jl = path.subPaths.length; j < jl; j ++ ) {
const subPath = path.subPaths[ j ];
const geometry = SVGLoader.pointsToStroke( subPath.getPoints(), path.userData.style );
if ( geometry ) {
const mesh = new THREE.Mesh( geometry, material );
group.add( mesh );
}
}
}
}
scene.add( group );
render();
} );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>