rename nid to id

master
howard 2021-04-06 14:10:07 -07:00
parent 3412f5537c
commit c6a3803b8d
10 changed files with 66 additions and 1269 deletions

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@ const eq = (a1, a2) => {
} }
window.loader = new THREE.ObjectLoader(); window.loader = new THREE.ObjectLoader();
window.nid = 0 window.id = 0
export class Scene { export class Scene {
constructor(store) { constructor(store) {
@ -158,31 +158,34 @@ export class Scene {
saveState() { saveState() {
localStorage.setItem( localStorage.setItem(
'sv', JSON.stringify([nid, this.store.getState()]) 'sv', JSON.stringify([id, this.store.getState()])
) )
} }
loadState() { //uglyyy loadState() { //uglyyy
const [curNid, state] = JSON.parse( const [curid, state] = JSON.parse(
localStorage.getItem('sv') localStorage.getItem('sv')
) )
window.nid = curNid window.id = curid
const entries = state.treeEntries.byNid const entries = state.treeEntries.byId
console.log(entries)
for (let k in entries) { for (let k in entries) {
console.log(k)
if (k[0] == 's') { if (k[0] == 's') {
entries[k].obj3d = loader.parse(entries[k].obj3d) entries[k].obj3d = loader.parse(entries[k].obj3d)
this.obj3d.add(entries[k].obj3d) this.obj3d.add(entries[k].obj3d)
entries[k] = new Sketch(this.camera, this.canvas, this.store, state.treeEntries.byNid[k]) entries[k] = new Sketch(this.camera, this.canvas, this.store, state.treeEntries.byId[k])
entries[k].obj3d.addEventListener('change', this.render) // !! took 3 hours to realize entries[k].obj3d.addEventListener('change', this.render) // !! took 3 hours to realize
} else if (k[0] == 'm') { } else if (k[0] == 'm') {
entries[k] = loader.parse(state.treeEntries.byNid[k]) entries[k] = loader.parse(state.treeEntries.byId[k])
console.log(entries[k])
this.obj3d.add(entries[k]) this.obj3d.add(entries[k])
} }
@ -294,7 +297,7 @@ async function addSketch() {
} }
window.sc = new Scene(store) window.sc = new Scene(store)
// sc.loadState() sc.loadState()

View File

@ -7,7 +7,7 @@ import { onHover, onDrag, onDragDim, onPick, onRelease } from './mouseEvents'
import { addDimension, setCoincident } from './constraintEvents' import { addDimension, setCoincident } from './constraintEvents'
import { get3PtArc } from './drawArc' import { get3PtArc } from './drawArc'
import { _vec2, _vec3, raycaster, awaitPts } from './shared' import { _vec2, _vec3, raycaster, awaitPts } from './shared'
import { replacer, reviver } from './mapJSONReplacer' import { replacer, reviver } from './utils'
import { AxesHelper } from './sketchAxes' import { AxesHelper } from './sketchAxes'
import { drawDimension, _onMoveDimension, setDimLines, updateDim } from './drawDimension'; import { drawDimension, _onMoveDimension, setDimLines, updateDim } from './drawDimension';
@ -36,7 +36,7 @@ class Sketch {
if (preload === undefined) { if (preload === undefined) {
this.obj3d = new THREE.Group() this.obj3d = new THREE.Group()
this.obj3d.name = "s" + nid++ this.obj3d.name = "s" + id++
this.obj3d.userData.type = "sketch" this.obj3d.userData.type = "sketch"
this.obj3d.matrixAutoUpdate = false; this.obj3d.matrixAutoUpdate = false;

View File

@ -32,7 +32,7 @@ export async function drawDimension() {
pointMaterial.clone() pointMaterial.clone()
) )
line.userData.nids = pts.map(e => e.name) line.userData.ids = pts.map(e => e.name)
@ -111,7 +111,7 @@ export async function drawDimension() {
const p1 = new THREE.Vector2() const p1 = new THREE.Vector2()
const p2 = new THREE.Vector2() const p2 = new THREE.Vector2()
const p3 = new THREE.Vector2() const p3 = new THREE.Vector2()
let dir, hyp, proj, perp, p1e, p2e, nids, _p1, _p2; let dir, hyp, proj, perp, p1e, p2e, ids, _p1, _p2;
@ -142,10 +142,10 @@ export function updateDim(c_id) {
export function _onMoveDimension(point, line) { export function _onMoveDimension(point, line) {
nids = line.userData.nids ids = line.userData.ids
_p1 = this.obj3d.children[this.objIdx.get(nids[0])].geometry.attributes.position.array _p1 = this.obj3d.children[this.objIdx.get(ids[0])].geometry.attributes.position.array
_p2 = this.obj3d.children[this.objIdx.get(nids[1])].geometry.attributes.position.array _p2 = this.obj3d.children[this.objIdx.get(ids[1])].geometry.attributes.position.array
p1.set(_p1[0], _p1[1]) p1.set(_p1[0], _p1[1])
p2.set(_p2[0], _p2[1]) p2.set(_p2[0], _p2[1])
@ -191,10 +191,10 @@ export function setDimLines() {
} }
nids = dims[i].userData.nids ids = dims[i].userData.ids
_p1 = this.obj3d.children[this.objIdx.get(nids[0])].geometry.attributes.position.array _p1 = this.obj3d.children[this.objIdx.get(ids[0])].geometry.attributes.position.array
_p2 = this.obj3d.children[this.objIdx.get(nids[1])].geometry.attributes.position.array _p2 = this.obj3d.children[this.objIdx.get(ids[1])].geometry.attributes.position.array
const offset = dims[i + 1].userData.offset const offset = dims[i + 1].userData.offset

View File

@ -68,15 +68,21 @@ export function extrude(sketch) {
const shape = new THREE.Shape(v2s); const shape = new THREE.Shape(v2s);
const extrudeSettings = { depth: 8, bevelEnabled: false }; const extrudeSettings = { depth: 8, bevelEnabled: false };
const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings); const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
const phong = new THREE.MeshPhongMaterial({ const material = new THREE.MeshPhongMaterial({
color: color.mesh, color: color.mesh,
emissive: color.emissive, emissive: color.emissive,
flatShading: true
}); });
const mesh = new THREE.Mesh(geometry, phong) const mesh = new THREE.Mesh(geometry, material)
// mesh.name = "Extrude" mesh.name = 'm' + id++
mesh.name = 'm' + nid++
mesh.userData.type = 'mesh' mesh.userData.type = 'mesh'
for (let i = 0; i < offSetPts.length; i += 2) { for (let i = 0; i < offSetPts.length; i += 2) {
@ -95,6 +101,9 @@ export function extrude(sketch) {
this.obj3d.add(mesh) this.obj3d.add(mesh)
this.render() this.render()
// sketch.visible = false // sketch.visible = false

View File

@ -95,16 +95,10 @@ const preloadedState = {
window.store = createStore(reducer, preloadedState, applyMiddleware(logger)) window.store = createStore(reducer, preloadedState, applyMiddleware(logger))
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render( ReactDOM.render(<Root store={store} />, document.getElementById('react'));
React.createElement(Root, { store: store }, null)
, document.getElementById('react')
);
}); });

View File

@ -19,6 +19,7 @@ export function onHover(e) {
hoverPts = raycaster.intersectObjects([...this.obj3d.children[1].children, ...this.obj3d.children]) hoverPts = raycaster.intersectObjects([...this.obj3d.children[1].children, ...this.obj3d.children])
if (hoverPts.length) { if (hoverPts.length) {
let minDist = Infinity; let minDist = Infinity;
for (let i = 0; i < hoverPts.length; i++) { for (let i = 0; i < hoverPts.length; i++) {
if (!hoverPts[i].distanceToRay) continue; if (!hoverPts[i].distanceToRay) continue;
@ -29,6 +30,8 @@ export function onHover(e) {
idx.push(i) idx.push(i)
} }
} }
// console.log(hoverPts, idx) // console.log(hoverPts, idx)
if (!idx.length) idx.push(0) if (!idx.length) idx.push(0)
} }
@ -39,19 +42,37 @@ export function onHover(e) {
hoverPts = raycaster.intersectObjects(this.obj3d.children, true) hoverPts = raycaster.intersectObjects(this.obj3d.children, true)
if (hoverPts.length) { if (hoverPts.length) {
// for (let i = 0; i < hoverPts.length; i++) {
// const obj = hoverPts[i].object
// if (['point', 'plane'].includes(obj.userData.type)) {
// idx.push(i)
// break;
// }
// }
// console.log(hoverPts)
let minDist = Infinity;
for (let i = 0; i < hoverPts.length; i++) { for (let i = 0; i < hoverPts.length; i++) {
const obj = hoverPts[i].object if (!hoverPts[i].distanceToRay) continue;
if (['point', 'plane'].includes(obj.userData.type)) {
if (hoverPts[i].distanceToRay < minDist - 0.0001) {
minDist = hoverPts[i].distanceToRay
idx = [i]
} else if (Math.abs(hoverPts[i].distanceToRay - minDist) < 0.0001) {
idx.push(i) idx.push(i)
break;
} }
} }
if (!idx.length) { if (!idx.length) {
const obj = hoverPts[0].object const obj = hoverPts[0].object
if (obj.userData.type == "mesh" && obj.visible) { if (obj.userData.type == "mesh" && obj.visible) {
idx.push(0) idx.push(0)
} else { } else if (['point', 'plane'].includes(obj.userData.type)) {
idx.push(0) idx.push(0)
} }
} }

View File

@ -55,7 +55,7 @@ const ptObj = (n) => {
), ),
pointMaterial.clone() pointMaterial.clone()
); );
ret.name = "p" + nid++ ret.name = "p" + id++
ret.userData.type = 'point' ret.userData.type = 'point'
return ret return ret
} }
@ -67,7 +67,7 @@ const lineObj = (n = 1) => {
), ),
lineMaterial.clone() lineMaterial.clone()
); );
ret.name = 'l' + nid++ ret.name = 'l' + id++
ret.userData.type = 'line' ret.userData.type = 'line'
return ret return ret
} }

View File

@ -4,7 +4,7 @@ const tailwindcss = require('tailwindcss')
module.exports = { module.exports = {
entry: { entry: {
index: './src/index.js', index: './src/index.jsx',
scene: './src/Scene.js', scene: './src/Scene.js',
}, },
output: { output: {