2022-11-27 14:06:33 +11:00
|
|
|
import { useStore } from '../useStore'
|
2022-11-28 09:37:46 +11:00
|
|
|
import { DoubleSide } from 'three'
|
|
|
|
import { addLine, Program } from '../lang/abstractSyntaxTree'
|
2022-11-27 14:06:33 +11:00
|
|
|
|
|
|
|
export const SketchPlane = () => {
|
2022-11-28 09:37:46 +11:00
|
|
|
const { ast, setGuiMode, guiMode, updateAst } = useStore(
|
|
|
|
({ guiMode, setGuiMode, ast, updateAst }) => ({
|
|
|
|
guiMode,
|
|
|
|
setGuiMode,
|
|
|
|
ast,
|
|
|
|
updateAst,
|
|
|
|
})
|
|
|
|
)
|
2022-11-27 14:06:33 +11:00
|
|
|
if (guiMode.mode !== 'sketch') {
|
|
|
|
return null
|
|
|
|
}
|
2022-12-06 05:40:05 +11:00
|
|
|
if (guiMode.sketchMode !== 'points' && guiMode.sketchMode !== 'sketchEdit' ) {
|
2022-11-27 14:06:33 +11:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:37:46 +11:00
|
|
|
const sketchGridName = 'sketchGrid'
|
|
|
|
|
|
|
|
const ninety = Math.PI / 2
|
|
|
|
const gridRotation: [number, number, number] = [0, 0, 0]
|
|
|
|
const clickDetectPlaneRotation: [number, number, number] = [0, 0, 0]
|
2022-11-28 21:05:56 +11:00
|
|
|
if (guiMode.axis === 'xy') {
|
2022-11-28 09:37:46 +11:00
|
|
|
gridRotation[0] = ninety
|
2022-11-27 14:06:33 +11:00
|
|
|
} else if (guiMode.axis === 'xz') {
|
2022-11-28 21:05:56 +11:00
|
|
|
clickDetectPlaneRotation[0] = ninety
|
|
|
|
} else if (guiMode.axis === 'yz') {
|
2022-11-28 09:37:46 +11:00
|
|
|
gridRotation[2] = ninety
|
|
|
|
clickDetectPlaneRotation[1] = ninety
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<mesh
|
|
|
|
rotation={clickDetectPlaneRotation}
|
|
|
|
name={sketchGridName}
|
|
|
|
onClick={(e) => {
|
2022-12-06 05:40:05 +11:00
|
|
|
if (guiMode.sketchMode !== 'points') {
|
|
|
|
return
|
|
|
|
}
|
2022-11-28 09:37:46 +11:00
|
|
|
const sketchGridIntersection = e.intersections.find(
|
|
|
|
({ object }) => object.name === sketchGridName
|
|
|
|
)
|
|
|
|
const point = roundy(sketchGridIntersection?.point)
|
|
|
|
let _ast: Program = ast
|
|
|
|
? ast
|
|
|
|
: {
|
|
|
|
type: 'Program',
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
body: [],
|
|
|
|
}
|
2022-11-28 21:05:56 +11:00
|
|
|
let addLinePoint: [number, number] = [point.x, point.y]
|
|
|
|
if (guiMode.axis === 'xz') {
|
|
|
|
addLinePoint = [point.x, point.z]
|
|
|
|
} else if (guiMode.axis === 'yz') {
|
|
|
|
addLinePoint = [point.z, point.y]
|
|
|
|
}
|
2022-12-04 15:52:27 +11:00
|
|
|
const { modifiedAst } = addLine(
|
|
|
|
_ast,
|
|
|
|
guiMode.pathToNode,
|
|
|
|
addLinePoint
|
|
|
|
)
|
2022-11-28 09:37:46 +11:00
|
|
|
updateAst(modifiedAst)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<planeGeometry args={[30, 40]} />
|
|
|
|
<meshStandardMaterial
|
|
|
|
color="blue"
|
|
|
|
side={DoubleSide}
|
|
|
|
opacity={0}
|
|
|
|
transparent
|
|
|
|
/>
|
|
|
|
</mesh>
|
|
|
|
<gridHelper args={[30, 40, 'blue', 'hotpink']} rotation={gridRotation} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function roundy({ x, y, z }: any) {
|
|
|
|
const roundOff = (num: number, places: number): number => {
|
|
|
|
const x = Math.pow(10, places)
|
|
|
|
return Math.round(num * x) / x
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
x: roundOff(x, 2),
|
|
|
|
y: roundOff(y, 2),
|
|
|
|
z: roundOff(z, 2),
|
2022-11-27 14:06:33 +11:00
|
|
|
}
|
|
|
|
}
|