As this Stack Overflow post suggests, I tried to use SVGLoader
to load sharp-looking SVGs, and by changing some parameters of the three.js official SVGLoader
example I was able to load SVGs nicely. However, when I try to change it to Sprite so that the SVG is always facing the screen, SVGs no longer show up, with no errors in console. Is there a way to achieve the effect without changing the facing of each SVG in each animation cycle (and without using TextureLoader
)?
Code:
import {
Mesh,
Group,
Color,
MeshBasicMaterial,
ShapeGeometry,
Sprite,
SpriteMaterial
} from 'three'
import { SVGLoader } from 'three/examples/jsm/loaders/SVGLoader'
const loader = new SVGLoader()
/**
* @param {string} url Path to SVG file
* @param {string} c Color
*/
const SVGNode = (url, c) => {
const group = new Group()
loader.load(url, (data) => {
const paths = data.paths
for (let i = 0; i < paths.length; i++) {
const path = paths[i]
const fillColor = path.userData.style.fill
if (fillColor !== undefined && fillColor !== 'none') {
const material = new MeshBasicMaterial({ // Change this to SpriteMaterial, and
color: new Color().setStyle(c).convertSRGBToLinear()
})
const shapes = SVGLoader.createShapes(path)
for (let j = 0; j < shapes.length; j++) {
const shape = shapes[j]
const geometry = new ShapeGeometry(shape)
const mesh = new Mesh(geometry, material) // change this to Sprite, the group is no longer visible
group.add(mesh)
}
}
}
})
return group
}
export default SVGNode