three.cad/src/Scene.js

312 lines
7.4 KiB
JavaScript
Raw Normal View History

2021-03-31 07:20:24 +08:00
2021-04-01 06:03:35 +08:00
import * as THREE from '../node_modules/three/src/Three';
2021-04-07 03:46:16 +08:00
import { TrackballControls } from './trackball'
import { Sketch } from './Sketch'
import Stats from './stats.module.js';
2021-03-31 07:20:24 +08:00
import { add3DPoint } from './datums'
import { extrude } from './extrude'
2021-04-07 03:46:16 +08:00
import { onHover, onPick } from './mouseEvents';
import { _vec2, _vec3, color, awaitPts } from './shared'
2021-03-31 07:20:24 +08:00
2021-04-07 03:46:16 +08:00
import { AxesHelper } from './axes'
import { Patch } from './patch'
import CSG from "./three-csg.js"
2021-03-31 16:07:34 +08:00
2021-04-03 03:33:09 +08:00
window.BoolOp = CSG
2021-03-31 07:20:24 +08:00
const eq = (a1, a2) => {
if (a1.length != a2.length) return false
for (let i = 0; i < a1.length; i++) {
if (a1[i] != a2[i]) return false
}
return true
}
2021-04-01 13:35:08 +08:00
window.loader = new THREE.ObjectLoader();
2021-04-07 05:10:07 +08:00
window.id = 0
2021-03-31 07:20:24 +08:00
2021-04-01 08:04:14 +08:00
export class Scene {
2021-03-31 07:20:24 +08:00
constructor(store) {
2021-04-01 06:03:35 +08:00
this.store = store;
2021-03-31 07:20:24 +08:00
this.canvas = document.querySelector('#c');
2021-04-08 06:50:53 +08:00
this.rect = this.canvas.getBoundingClientRect().toJSON()
2021-04-07 02:31:14 +08:00
this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas });
2021-03-31 07:20:24 +08:00
const size = 1;
const near = 0;
const far = 100;
this.camera = new THREE.OrthographicCamera(-size, size, size, -size, near, far);
this.camera.zoom = 0.1;
2021-04-07 02:31:14 +08:00
const cameraDist = 50
const xzAngle = 30 * Math.PI / 180
this.camera.position.set(
cameraDist * Math.sin(xzAngle),
cameraDist * Math.tan(30 * Math.PI / 180),
cameraDist * Math.cos(xzAngle)
);
2021-03-31 07:20:24 +08:00
// const controls = new OrbitControls(camera, view1Elem);
const controls = new TrackballControls(this.camera, this.canvas);
controls.target.set(0, 0, 0);
controls.update();
2021-04-01 13:35:08 +08:00
this.obj3d = new THREE.Scene()
2021-03-31 07:20:24 +08:00
2021-04-07 02:31:14 +08:00
this.obj3d.background = new THREE.Color(color.background);
2021-03-31 07:20:24 +08:00
const helpersGroup = new THREE.Group();
helpersGroup.name = "helpersGroup";
2021-04-01 13:35:08 +08:00
this.obj3d.add(helpersGroup);
2021-03-31 07:20:24 +08:00
2021-04-07 02:31:14 +08:00
// const axesHelper = new AxesHelper(0.4);
// helpersGroup.add(axesHelper);
const patch = new Patch(0.5);
helpersGroup.add(patch);
const planeGeom = new THREE.PlaneGeometry(5, 5)
2021-03-31 07:20:24 +08:00
const pxy = new THREE.Mesh(
2021-04-07 02:31:14 +08:00
planeGeom,
2021-03-31 07:20:24 +08:00
new THREE.MeshBasicMaterial({
2021-04-05 11:52:17 +08:00
color: color.plane,
2021-04-07 02:31:14 +08:00
opacity: 0.05,
2021-03-31 07:20:24 +08:00
side: THREE.DoubleSide,
transparent: true,
depthWrite: false,
toneMapped: false
})
);
2021-04-01 06:03:35 +08:00
2021-04-05 11:52:17 +08:00
pxy.userData.type = 'plane'
2021-04-07 02:31:14 +08:00
pxy.add(
new THREE.LineSegments(
new THREE.EdgesGeometry(planeGeom),
new THREE.LineBasicMaterial({ color: color.planeBorder })
)
)
2021-03-31 07:20:24 +08:00
const pyz = pxy.clone().rotateY(Math.PI / 2);
pyz.material = pyz.material.clone();
const pxz = pxy.clone().rotateX(-Math.PI / 2);
pxz.material = pxz.material.clone();
2021-04-07 02:31:14 +08:00
helpersGroup.add(pxy);
2021-03-31 07:20:24 +08:00
helpersGroup.add(pyz);
helpersGroup.add(pxz);
const intensity = 1;
2021-03-31 16:07:34 +08:00
const light1 = new THREE.DirectionalLight(color.lighting, intensity);
2021-03-31 07:20:24 +08:00
light1.position.set(10, 10, 10);
2021-04-01 13:35:08 +08:00
this.obj3d.add(light1);
2021-03-31 07:20:24 +08:00
2021-03-31 16:07:34 +08:00
const light2 = new THREE.DirectionalLight(color.lighting, intensity);
2021-03-31 07:20:24 +08:00
light2.position.set(-10, -10, -5);
2021-04-01 13:35:08 +08:00
this.obj3d.add(light2);
2021-03-31 16:07:34 +08:00
const ambient = new THREE.AmbientLight(color.lighting, intensity);
2021-04-01 13:35:08 +08:00
this.obj3d.add(ambient);
2021-03-31 07:20:24 +08:00
this.render = render.bind(this);
this.addSketch = addSketch.bind(this);
this.extrude = extrude.bind(this);
this.onHover = onHover.bind(this);
this.onPick = onPick.bind(this);
2021-04-03 03:33:09 +08:00
this.awaitPts = awaitPts.bind(this);
2021-03-31 07:20:24 +08:00
2021-04-01 13:35:08 +08:00
this.obj3d.addEventListener('change', this.render);
2021-03-31 07:20:24 +08:00
controls.addEventListener('change', this.render);
controls.addEventListener('start', this.render);
window.addEventListener('resize', this.render);
this.stats = new Stats();
this.stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
document.getElementById('stats').appendChild(this.stats.dom);
this.hovered = [];
this.selected = [];
2021-04-05 11:52:17 +08:00
this.activeSketch = null;
2021-03-31 07:20:24 +08:00
this.render();
}
2021-04-01 06:03:35 +08:00
resizeCanvas(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
saveState() {
localStorage.setItem(
2021-04-07 05:10:07 +08:00
'sv', JSON.stringify([id, this.store.getState()])
2021-04-01 06:03:35 +08:00
)
}
2021-04-01 13:35:08 +08:00
loadState() { //uglyyy
2021-04-07 05:10:07 +08:00
const [curid, state] = JSON.parse(
2021-04-01 06:03:35 +08:00
localStorage.getItem('sv')
)
2021-04-07 05:10:07 +08:00
window.id = curid
2021-04-01 06:03:35 +08:00
2021-04-07 05:10:07 +08:00
const entries = state.treeEntries.byId
2021-04-01 13:35:08 +08:00
for (let k in entries) {
2021-04-01 06:03:35 +08:00
2021-04-04 12:54:27 +08:00
if (k[0] == 's') {
2021-04-01 13:35:08 +08:00
entries[k].obj3d = loader.parse(entries[k].obj3d)
this.obj3d.add(entries[k].obj3d)
2021-04-08 06:50:53 +08:00
entries[k] = new Sketch(this, state.treeEntries.byId[k])
2021-04-01 13:35:08 +08:00
entries[k].obj3d.addEventListener('change', this.render) // !! took 3 hours to realize
} else if (k[0] == 'm') {
2021-04-01 06:03:35 +08:00
2021-04-07 05:10:07 +08:00
entries[k] = loader.parse(state.treeEntries.byId[k])
console.log(entries[k])
2021-04-01 13:35:08 +08:00
this.obj3d.add(entries[k])
2021-04-01 06:03:35 +08:00
2021-04-01 13:35:08 +08:00
}
}
this.store.dispatch({ type: 'restore-state', state })
2021-04-01 06:03:35 +08:00
}
2021-04-07 02:31:14 +08:00
clearSelection() {
for (let x = 0; x < this.selected.length; x++) {
const obj = this.selected[x]
obj.material.color.set(color[obj.userData.type])
}
for (let x = 0; x < this.hovered.length; x++) {
const obj = this.selected[x]
obj.material.color.set(color[obj.userData.type])
}
this.obj3d.dispatchEvent({ type: 'change' })
this.selected = []
console.log('fireed')
}
2021-03-31 07:20:24 +08:00
}
2021-04-05 11:52:17 +08:00
let idx, x, y, ele, pos, dims, matrix;
2021-03-31 07:20:24 +08:00
function render() {
this.stats.begin();
if (this.resizeCanvas(this.renderer)) {
const canvas = this.renderer.domElement;
this.camera.left = -canvas.clientWidth / canvas.clientHeight;
this.camera.right = canvas.clientWidth / canvas.clientHeight;
this.camera.updateProjectionMatrix();
2021-04-08 06:50:53 +08:00
Object.assign(this.rect, this.canvas.getBoundingClientRect().toJSON())
2021-03-31 07:20:24 +08:00
}
2021-04-01 13:35:08 +08:00
this.renderer.render(this.obj3d, this.camera);
2021-04-05 11:52:17 +08:00
// const sketch = this.store.
if (this.activeSketch) {
dims = this.activeSketch.obj3d.children[1].children
matrix = this.activeSketch.obj3d.matrix
for (idx = 1; idx < dims.length; idx += 2) {
ele = dims[idx]
2021-04-05 16:05:53 +08:00
// if (!ele.label) continue;
2021-04-05 11:52:17 +08:00
pos = _vec3.set(
...ele.geometry.attributes.position.array
).applyMatrix4(matrix).project(this.camera)
x = (pos.x * .5 + .5) * this.canvas.clientWidth + 10;
y = (pos.y * -.5 + .5) * this.canvas.clientHeight;
2021-04-07 02:31:14 +08:00
2021-04-05 11:52:17 +08:00
// console.log(i, ele)
// ele.label.style.transform = `translate(-50%, -50%) translate(${x+20}px,${y}px)`;
ele.label.style.transform = `translate(0%, -50%) translate(${x}px,${y}px)`;
}
}
2021-03-31 07:20:24 +08:00
this.stats.end();
}
2021-04-03 03:33:09 +08:00
2021-03-31 07:20:24 +08:00
async function addSketch() {
2021-04-03 03:33:09 +08:00
2021-04-04 12:54:27 +08:00
let sketch;
2021-03-31 07:20:24 +08:00
2021-04-05 11:52:17 +08:00
const references = await this.awaitPts({ point: 3 }, { plane: 1 });
2021-04-04 17:36:41 +08:00
if (!references) return;
2021-04-05 11:52:17 +08:00
if (references[0].userData.type == 'plane') {
2021-04-08 06:50:53 +08:00
sketch = new Sketch(this)
2021-04-04 17:36:41 +08:00
sketch.obj3d.matrix = references[0].matrix
2021-04-01 13:35:08 +08:00
sketch.plane.applyMatrix4(sketch.obj3d.matrix)
sketch.obj3d.inverse = sketch.obj3d.matrix.clone().invert()
2021-04-04 17:36:41 +08:00
this.obj3d.add(sketch.obj3d)
2021-04-04 12:54:27 +08:00
} else {
2021-04-08 06:50:53 +08:00
sketch = new Sketch(this)
2021-04-04 17:36:41 +08:00
this.obj3d.add(sketch.obj3d)
2021-04-01 13:35:08 +08:00
sketch.align(
2021-03-31 07:20:24 +08:00
...references.map(
2021-04-07 03:46:16 +08:00
el => new THREE.Vector3(...el.geometry.attributes.position.array).applyMatrix4(el.matrixWorld)
2021-03-31 07:20:24 +08:00
)
)
}
2021-04-04 12:54:27 +08:00
2021-04-07 02:31:14 +08:00
this.clearSelection()
2021-04-04 12:54:27 +08:00
2021-04-01 13:35:08 +08:00
sketch.activate()
2021-04-05 11:52:17 +08:00
this.activeSketch = sketch
2021-04-04 12:54:27 +08:00
2021-04-01 13:35:08 +08:00
sketch.obj3d.addEventListener('change', this.render);
2021-03-31 07:20:24 +08:00
this.render()
2021-04-04 12:54:27 +08:00
console.log('render')
2021-04-01 13:35:08 +08:00
this.store.dispatch({ type: 'rx-sketch', obj: sketch })
2021-03-31 07:20:24 +08:00
}
2021-04-02 08:19:14 +08:00
window.sc = new Scene(store)
2021-04-07 05:10:07 +08:00
sc.loadState()
2021-03-31 16:07:34 +08:00
2021-04-01 06:03:35 +08:00
2021-03-31 16:07:34 +08:00
2021-04-01 06:03:35 +08:00
2021-03-31 16:07:34 +08:00
2021-03-31 07:20:24 +08:00