three.cad/src/app.jsx

193 lines
4.9 KiB
React
Raw Normal View History

2021-03-26 17:25:28 +08:00
2021-04-02 16:04:42 +08:00
import React, { useEffect, useReducer, useRef, useState } from 'react';
2021-04-01 16:20:50 +08:00
import './app.css'
2021-03-26 17:25:28 +08:00
2021-03-28 20:00:31 +08:00
import { Provider, useDispatch, useSelector } from 'react-redux'
2021-04-02 16:04:42 +08:00
import { FaCube, FaEdit } from 'react-icons/fa'
import { MdEdit, MdDone, MdVisibilityOff, MdVisibility } from 'react-icons/md'
import { RiShape2Fill } from 'react-icons/ri'
2021-04-03 03:33:09 +08:00
import * as Icon from "./icons";
import { color } from './utils/shared'
2021-03-28 20:00:31 +08:00
2021-03-27 03:18:11 +08:00
export const Root = ({ store }) => (
2021-03-26 17:25:28 +08:00
<Provider store={store}>
2021-03-27 03:18:11 +08:00
<App></App>
2021-03-26 17:25:28 +08:00
</Provider>
);
2021-03-27 03:18:11 +08:00
2021-03-30 13:13:13 +08:00
2021-03-27 03:18:11 +08:00
const App = () => {
2021-03-28 20:00:31 +08:00
const dispatch = useDispatch()
2021-03-29 18:27:34 +08:00
const treeEntries = useSelector(state => state.treeEntries)
2021-04-01 13:35:08 +08:00
const activeSketchNid = useSelector(state => state.activeSketchNid)
2021-03-30 13:13:13 +08:00
// const [state, setState] = useState('x')
// useEffect(()=>{
// console.log('hereeee')
// },[state])
2021-04-02 16:04:42 +08:00
2021-03-30 13:13:13 +08:00
useEffect(() => {
2021-04-01 13:35:08 +08:00
if (!activeSketchNid) {
2021-03-31 07:20:24 +08:00
sc.canvas.addEventListener('pointermove', sc.onHover)
sc.canvas.addEventListener('pointerdown', sc.onPick)
2021-04-01 16:20:50 +08:00
return () => {
sc.canvas.removeEventListener('pointermove', sc.onHover)
sc.canvas.removeEventListener('pointerdown', sc.onPick)
2021-03-31 07:20:24 +08:00
}
2021-03-30 13:13:13 +08:00
}
2021-04-01 13:35:08 +08:00
}, [activeSketchNid])
2021-03-27 03:18:11 +08:00
2021-03-28 20:00:31 +08:00
2021-04-02 08:19:14 +08:00
const btnz = [
activeSketchNid ?
2021-04-05 11:52:17 +08:00
[MdDone, () => {
treeEntries.byNid[activeSketchNid].deactivate()
sc.activeSketch = null
// sc.activeDim = this.activeSketch.obj3d.children[1].children
}, 'Finish'] :
2021-04-02 16:04:42 +08:00
[FaEdit, sc.addSketch, 'Sketch']
2021-04-02 08:19:14 +08:00
,
2021-04-02 16:04:42 +08:00
[FaCube, () => sc.extrude(treeEntries.byNid[activeSketchNid]), 'Extrude'],
2021-04-03 03:33:09 +08:00
[Icon.Union, () => sc.extrude(treeEntries.byNid[activeSketchNid]), 'Union'],
[Icon.Subtract, subtract, 'Subtract'],
[Icon.Intersect, () => sc.extrude(treeEntries.byNid[activeSketchNid]), 'Intersect'],
[Icon.Dimension, () => sc.extrude(treeEntries.byNid[activeSketchNid]), 'Dimension'],
[Icon.Line, () => sc.extrude(treeEntries.byNid[activeSketchNid]), 'Line'],
[Icon.Arc, () => sc.extrude(treeEntries.byNid[activeSketchNid]), 'Arc'],
2021-04-02 08:19:14 +08:00
]
2021-04-01 18:10:00 +08:00
2021-04-05 16:05:53 +08:00
return <div className='absolute left-0 w-40 flex flex-col'>
2021-04-02 08:19:14 +08:00
{
2021-04-02 16:04:42 +08:00
btnz.map(([Icon, fcn, txt], idx) => (
2021-04-05 16:05:53 +08:00
<div className="btn flex items-center justify-end p-1 text-lg" key={idx}
2021-04-02 08:19:14 +08:00
onClick={fcn}
2021-04-01 16:20:50 +08:00
>
2021-04-02 08:19:14 +08:00
<div>{txt}</div>
2021-04-02 16:04:42 +08:00
<Icon className="w-6 h-6 ml-1" />
2021-04-02 08:19:14 +08:00
</div>
))
}
<div className=''>
{treeEntries.allNids.map((entId, idx) => (
2021-04-02 16:04:42 +08:00
<TreeEntry key={idx} entId={entId} />
2021-03-28 20:00:31 +08:00
))}
</div>
2021-04-02 16:04:42 +08:00
2021-04-02 08:19:14 +08:00
</div>
2021-04-02 16:04:42 +08:00
2021-03-28 20:00:31 +08:00
}
2021-04-02 16:04:42 +08:00
const TreeEntry = ({ entId }) => {
const treeEntries = useSelector(state => state.treeEntries.byNid)
const activeSketchNid = useSelector(state => state.activeSketchNid)
2021-04-03 03:33:09 +08:00
let obj3d, entry;
2021-04-05 11:52:17 +08:00
2021-04-03 03:33:09 +08:00
entry = treeEntries[entId]
if (entId[0] == "s") {
obj3d = treeEntries[entId].obj3d
2021-04-02 16:04:42 +08:00
} else {
2021-04-03 03:33:09 +08:00
obj3d = treeEntries[entId]
2021-04-02 16:04:42 +08:00
}
const [_, forceUpdate] = useReducer(x => x + 1, 0);
2021-04-03 03:33:09 +08:00
const vis = obj3d.visible
2021-04-02 16:04:42 +08:00
2021-04-05 16:05:53 +08:00
return <div className='bg-gray-50 flex justify-between w-full'>
2021-04-02 16:04:42 +08:00
<div className='btn'
onClick={() => {
activeSketchNid && treeEntries[activeSketchNid].deactivate()
entry.activate()
2021-04-05 11:52:17 +08:00
sc.activeSketch = entry;
2021-04-02 16:04:42 +08:00
}}
>
<MdEdit />
</div>
{
vis ?
<div className='btn'
onClick={() => {
2021-04-03 03:33:09 +08:00
obj3d.visible = false;
2021-04-02 16:04:42 +08:00
sc.render()
forceUpdate()
}}
>
<MdVisibility />
</div>
:
<div className='btn'
onClick={() => {
2021-04-03 03:33:09 +08:00
obj3d.visible = true;
2021-04-02 16:04:42 +08:00
sc.render()
forceUpdate()
}}
>
<MdVisibilityOff />
</div>
}
<div className="btn"
onPointerEnter={() => {
if (entId[0] == 'm') {
entry.material.color.set(color.hover)
sc.render()
}
}}
onPointerLeave={() => {
const obj = entry
if (entId[0] == 'm' && !sc.selected.includes(obj)) {
2021-04-05 16:05:53 +08:00
obj.material.color.set(color.mesh)
2021-04-02 16:04:42 +08:00
sc.render()
}
}}
onPointerDown={() => {
if (entId[0] == 'm') {
sc.selected.push(
entry
)
sc.render()
}
}}
>
{entId}
</div>
</div>
}
2021-04-03 03:33:09 +08:00
const subtract = () => {
// //Create a bsp tree from each of the meshes
2021-04-05 11:52:17 +08:00
// console.log(sc.selected.length != 2 || !sc.selected.every(e => e.userData.type == 'mesh'), "wtf")
if (sc.selected.length != 2 || !sc.selected.every(e => e.userData.type == 'mesh')) return
// console.log('here')
2021-04-03 03:33:09 +08:00
const [m1, m2] = sc.selected
2021-04-05 11:52:17 +08:00
let bspA = BoolOp.fromMesh(m1)
let bspB = BoolOp.fromMesh(m2)
2021-04-03 03:33:09 +08:00
m1.visible = false
m2.visible = false
// // Subtract one bsp from the other via .subtract... other supported modes are .union and .intersect
let bspResult = bspA.subtract(bspB)
// //Get the resulting mesh from the result bsp, and assign meshA.material to the resulting mesh
2021-04-05 11:52:17 +08:00
let meshResult = BoolOp.toMesh(bspResult, m1.matrix, m1.material)
2021-04-03 03:33:09 +08:00
sc.obj3d.add(meshResult)
sc.render()
}
2021-04-02 16:04:42 +08:00