three.cad/src/extrude.js

102 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-03-31 07:20:24 +08:00
import * as THREE from 'three/src/Three';
import { color, ptObj } from './utils/static'
2021-03-29 18:27:34 +08:00
export function extrude(sketch) {
2021-03-26 12:30:35 +08:00
2021-03-29 18:27:34 +08:00
let constraints = sketch.constraints;
let linkedObjs = sketch.linkedObjs;
let children = sketch.children;
2021-03-30 06:12:46 +08:00
let objIdx = sketch.objIdx;
2021-03-26 12:30:35 +08:00
let visited = new Set()
let v2s = []
2021-03-31 07:20:24 +08:00
let offSetPts = []
2021-03-26 12:30:35 +08:00
function findPair(node) {
visited.add(node)
2021-03-31 16:07:34 +08:00
let linkedObj = linkedObjs.get(node.userData.l_id)
2021-03-26 12:30:35 +08:00
let arr;
if (linkedObj[0] == 'line') {
2021-03-30 06:12:46 +08:00
arr = children[objIdx.get(linkedObj[1][2])].geometry.attributes.position.array
2021-03-26 12:30:35 +08:00
} else if (linkedObj[0] == 'arc') {
2021-03-30 06:12:46 +08:00
arr = children[objIdx.get(linkedObj[1][3])].geometry.attributes.position.array
2021-03-26 12:30:35 +08:00
}
for (let i = 0; i < arr.length; i += 3) {
v2s.push(new THREE.Vector2(arr[i], arr[i + 1]))
}
2021-03-31 07:20:24 +08:00
offSetPts.push(arr[0], arr[1])
offSetPts.push(arr[arr.length - 3], arr[arr.length - 2])
2021-03-26 12:30:35 +08:00
for (let i = 0; i < 2; i++) {
2021-04-01 06:03:35 +08:00
let d = children[
objIdx.get(
linkedObj[1][i]
)
]
2021-03-26 12:30:35 +08:00
if (d == -1 || d == node) continue;
2021-03-28 20:00:31 +08:00
if (d == children[1]) {
2021-03-26 12:30:35 +08:00
console.log('pair found')
};
findTouching(d)
}
}
function findTouching(node) {
2021-03-31 16:07:34 +08:00
for (let t of node.userData.constraints) {
2021-03-26 12:30:35 +08:00
if (constraints.get(t)[0] != 'coincident') continue
2021-03-30 06:12:46 +08:00
for (let c of constraints.get(t)[2]) {
if (c == -1) continue;
const d = children[objIdx.get(c)]
if (d == node) continue;
2021-03-26 12:30:35 +08:00
if (d == children[1]) {
2021-03-26 14:00:34 +08:00
console.log('loop found')
2021-03-26 12:30:35 +08:00
} else {
if (!visited.has(d)) {
findPair(d)
}
};
}
}
}
2021-03-28 20:00:31 +08:00
findPair(children[1])
2021-03-26 12:30:35 +08:00
const shape = new THREE.Shape(v2s);
2021-03-28 20:00:31 +08:00
const extrudeSettings = { depth: 8, bevelEnabled: false };
2021-03-26 12:30:35 +08:00
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
2021-03-31 07:20:24 +08:00
const phong = new THREE.MeshPhongMaterial({
2021-04-01 06:03:35 +08:00
color: color.m,
2021-03-31 07:20:24 +08:00
emissive: color.emissive,
flatShading: true
});
2021-03-29 18:27:34 +08:00
const mesh = new THREE.Mesh(geometry, phong)
2021-04-01 06:03:35 +08:00
// mesh.name = "Extrude"
mesh.name = 'm' + nid++
2021-03-31 07:20:24 +08:00
for (let i = 0; i < offSetPts.length; i += 2) {
if (
offSetPts[i] == offSetPts[i - 2] &&
offSetPts[i + 1] == offSetPts[i - 1]
) continue;
mesh.add(
ptObj([offSetPts[i], offSetPts[i + 1], 8])
)
}
2021-03-28 20:00:31 +08:00
2021-03-29 18:27:34 +08:00
mesh.matrixAutoUpdate = false;
mesh.matrix.multiply(sketch.matrix)
2021-03-31 07:20:24 +08:00
this.add(mesh)
2021-03-28 20:00:31 +08:00
2021-03-29 18:27:34 +08:00
this.render()
2021-03-28 20:00:31 +08:00
2021-03-30 09:46:55 +08:00
// sketch.visible = false
this.store.dispatch({ type: 'rx-extrusion', mesh, sketch })
2021-03-26 12:30:35 +08:00
}