async function create3dFromSvgV8(svg, type, scaleBy, color=false) {
const loader = new THREE.SVGLoader();
let paths = loader.parse(svg.outerHTML).paths;
let style = paths[0].userData.style;
const shapes = []
let points = [];
paths.forEach((path, i) => {
paths[i].toShapes(true, false).forEach((shape, i) => {
shapes.push(shape);
});
for (let j = 0, jl = path.subPaths.length; j < jl; j++) {
points.push(path.subPaths[j].getPoints());
}
});
const group = new THREE.Group();
const depth = 40
if( ['lines'].includes(type) ){
points.forEach((pointsArr, i) => {
const geometry = THREE.SVGLoader.pointsToStroke( pointsArr, style )
material.color = new THREE.Color().setStyle( color||'pink' )
const mesh = new THREE.Mesh( geometry, material );
group.add( mesh );
});
}
const material = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide });
material.color = new THREE.Color().setStyle( color||'blue' )
let extrudeSettings = { depth: depth, bevelEnabled: false }
// Shapes: Extrtrude Geometry
if( ['geoms'].includes(type) ){
shapes.forEach(shape => {
const geometry = new THREE.ExtrudeBufferGeometry(shape, extrudeSettings);
const mesh = new THREE.Mesh(geometry, material);
group.add(mesh);
});
}
// Points: Extrude Stroke
if (["extrude"].includes(type)) {
let x = 20; // depth
let y = 2; // Topwidth
let z = 2; // bottomWidth
let startPos = depth+x/2+1;
const shapeBufferGeometry = [];
points.forEach((twoVectorArray, i) => {
let threeVectorArray = [];
twoVectorArray.forEach((v, i) => { threeVectorArray.push(new THREE.Vector3(v.x, v.y, startPos)); });
const extrusionPath = new THREE.CurvePath();
for (let i = 0; i < threeVectorArray.length - 1; i++) {
extrusionPath.add( new THREE.LineCurve3( threeVectorArray[i], threeVectorArray[i + 1] ) );
}
const squareShape = new THREE.Shape()
.moveTo(x, 0)
.lineTo(x, y)
.lineTo(y, z)
.lineTo(z, 0);
var geometry = new THREE.ExtrudeBufferGeometry(squareShape, {
extrudePath: extrusionPath,
curveSegments: 1,
steps: 200,
bevelEnabled: false,
});
material.color = new THREE.Color().setHex(Math.random() * 0xffffff);
shapeBufferGeometry.push(geometry);
});
const iconGeometry = THREE.BufferGeometryUtils.mergeBufferGeometries( shapeBufferGeometry, true);
const mesh = new THREE.Mesh(iconGeometry, material);
group.add(mesh);
}
position(group)
return group;
}