UNPKG

polygonjs-engine

Version:

node-based webgl 3D engine https://polygonjs.com

88 lines (87 loc) 3.03 kB
import {SVGLoader as SVGLoader2} from "../../modules/three/examples/jsm/loaders/SVGLoader"; import {Color as Color2} from "three/src/math/Color"; import {Group as Group2} from "three/src/objects/Group"; import {MeshBasicMaterial as MeshBasicMaterial2} from "three/src/materials/MeshBasicMaterial"; import {DoubleSide} from "three/src/constants"; import {Mesh as Mesh2} from "three/src/objects/Mesh"; import {ShapeBufferGeometry as ShapeBufferGeometry2} from "three/src/geometries/ShapeBufferGeometry"; export class CoreSVGLoader { constructor(url, scene) { this.url = url; this.scene = scene; } load(options) { return new Promise((resolve, reject) => { const loader = new SVGLoader2(); let url = this.url; if (url[0] != "h") { const assets_root = this.scene.assets.root(); if (assets_root) { url = `${assets_root}${url}`; } } loader.load(url, (data) => { try { const group = this._onLoaded(data, options); resolve(group); } catch (err) { reject([]); } }); }); } _onLoaded(data, options) { const paths = data.paths; const group = new Group2(); for (let i = 0; i < paths.length; i++) { const path = paths[i]; const userData = path.userData; const fillColor = userData.style.fill; if (options.drawFillShapes && fillColor !== void 0 && fillColor !== "none") { this._drawShapes(group, path, options); } const strokeColor = userData.style.stroke; if (options.drawStrokes && strokeColor !== void 0 && strokeColor !== "none") { this._drawStrokes(group, path, options); } } return group; } _drawShapes(group, path, options) { const userData = path.userData; const material = new MeshBasicMaterial2({ color: new Color2().setStyle(userData.style.fill), opacity: userData.style.fillOpacity, transparent: userData.style.fillOpacity < 1, side: DoubleSide, depthWrite: false, wireframe: options.fillShapesWireframe }); const shapes = path.toShapes(true); for (let j = 0; j < shapes.length; j++) { const shape = shapes[j]; const geometry = new ShapeBufferGeometry2(shape); const mesh = new Mesh2(geometry, material); group.add(mesh); } } _drawStrokes(group, path, options) { const userData = path.userData; const material = new MeshBasicMaterial2({ color: new Color2().setStyle(userData.style.stroke), opacity: userData.style.strokeOpacity, transparent: userData.style.strokeOpacity < 1, side: DoubleSide, depthWrite: false, wireframe: options.strokesWireframe }); for (let j = 0, jl = path.subPaths.length; j < jl; j++) { const subPath = path.subPaths[j]; const geometry = SVGLoader2.pointsToStroke(subPath.getPoints(), userData.style); if (geometry) { const mesh = new Mesh2(geometry, material); group.add(mesh); } } } }