-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[R127] svg loader implement custom create shapes() (#53)
* add createShapes method to SVGLoader * add SVGLoader test
- Loading branch information
1 parent
d843346
commit 8e14e7f
Showing
4 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import * as THREE from 'three'; | ||
|
||
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'; | ||
import { SVGLoader } from 'three/examples/jsm/loaders/SVGLoader'; | ||
|
||
let renderer: THREE.WebGLRenderer; | ||
let scene: THREE.Scene; | ||
let camera: THREE.PerspectiveCamera; | ||
let guiData: { | ||
currentURL: string; | ||
drawFillShapes: boolean; | ||
drawStrokes: boolean; | ||
fillShapesWireframe: boolean; | ||
strokesWireframe: boolean; | ||
}; | ||
|
||
init(); | ||
animate(); | ||
|
||
// | ||
|
||
function init() { | ||
const container = document.getElementById('container') as HTMLElement; | ||
|
||
// | ||
|
||
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.screenSpacePanning = true; | ||
|
||
// | ||
|
||
window.addEventListener('resize', onWindowResize); | ||
|
||
guiData = { | ||
currentURL: 'models/svg/tiger.svg', | ||
drawFillShapes: true, | ||
drawStrokes: true, | ||
fillShapesWireframe: false, | ||
strokesWireframe: false, | ||
}; | ||
|
||
loadSVG(guiData.currentURL); | ||
} | ||
|
||
function loadSVG(url: string) { | ||
// | ||
|
||
scene = new THREE.Scene(); | ||
scene.background = new THREE.Color(0xb0b0b0); | ||
|
||
// | ||
|
||
const helper = new THREE.GridHelper(160, 10); | ||
helper.rotation.x = Math.PI / 2; | ||
scene.add(helper); | ||
|
||
// | ||
|
||
const loader = new SVGLoader(); | ||
|
||
loader.load(url, 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; | ||
|
||
paths.forEach(path => { | ||
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: path.userData!.style.fillOpacity < 1, | ||
side: THREE.DoubleSide, | ||
depthWrite: false, | ||
wireframe: guiData.fillShapesWireframe, | ||
}); | ||
|
||
const shapes = SVGLoader.createShapes(path); | ||
|
||
shapes.forEach(shape => { | ||
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: path.userData!.style.strokeOpacity < 1, | ||
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); | ||
}); | ||
} | ||
|
||
function onWindowResize() { | ||
camera.aspect = window.innerWidth / window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
|
||
renderer.setSize(window.innerWidth, window.innerHeight); | ||
} | ||
|
||
function animate() { | ||
requestAnimationFrame(animate); | ||
|
||
render(); | ||
} | ||
|
||
function render() { | ||
renderer.render(scene, camera); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters