format project
This commit is contained in:
@@ -244,7 +244,6 @@ const makeScriptWorker = ({callback, convertToSolids})=>{
|
||||
let workerBaseURI, onInit
|
||||
|
||||
function runMain(params={}){
|
||||
console.log('runMain');
|
||||
let time = Date.now()
|
||||
let solids
|
||||
try{
|
||||
|
||||
@@ -25,10 +25,10 @@ function Asset({ geometry: incomingGeo }) {
|
||||
}, [incomingGeo])
|
||||
if (!incomingGeo) return null
|
||||
|
||||
let groupData = incomingGeo.children ? incomingGeo : null
|
||||
const groupData = incomingGeo.children ? incomingGeo : null
|
||||
if (lastGroup && lastGroup != groupData) {
|
||||
state.scene.remove(lastGroup)
|
||||
lastGroup.children.forEach(c=>c?.geometry?.dispose())
|
||||
lastGroup.children.forEach((c) => c?.geometry?.dispose())
|
||||
// returning <primitive object={groupData} /> does not add the new group to the scene
|
||||
// there is probably some useRef magic that would make this work, but I don't have time to reseach it
|
||||
/// FIXME - do this properly with useRef or other react magic
|
||||
|
||||
@@ -3,7 +3,6 @@ import Svg from 'src/components/Svg/Svg'
|
||||
import { Popover } from '@headlessui/react'
|
||||
import type { CadPackage } from 'src/helpers/hooks/useIdeState'
|
||||
|
||||
|
||||
const menuOptions: {
|
||||
name: string
|
||||
sub: string
|
||||
|
||||
@@ -7,7 +7,8 @@ const setPoints = (points, p, i)=>{
|
||||
function CSG2Vertices(csg) {
|
||||
let idx = 0
|
||||
|
||||
let vLen = 0, iLen = 0
|
||||
let vLen = 0,
|
||||
iLen = 0
|
||||
for (let poly of csg.polygons) {
|
||||
let len = poly.vertices.length
|
||||
vLen += len * 3
|
||||
@@ -43,14 +44,12 @@ function CSG2Vertices(csg){
|
||||
return { vertices, indices, type: 'mesh' }
|
||||
}
|
||||
|
||||
|
||||
function CSG2LineVertices(csg) {
|
||||
let vLen = csg.points.length * 3
|
||||
if (csg.isClosed) vLen += 3
|
||||
|
||||
var vertices = new Float32Array(vLen)
|
||||
|
||||
|
||||
csg.points.forEach((p, idx) => setPoints(vertices, p, idx * 3))
|
||||
|
||||
if (csg.isClosed) {
|
||||
@@ -69,7 +68,6 @@ function CSG2LineSegmentsVertices(csg){
|
||||
setPoints(vertices, side[1], i + 3)
|
||||
})
|
||||
return { vertices, type: 'lines' }
|
||||
|
||||
}
|
||||
|
||||
function CSGCached(func, data, cacheKey, transferable) {
|
||||
@@ -94,13 +92,18 @@ function CSGCached(func, data, cacheKey, transferable){
|
||||
function CSGToBuffers(csg, transferable) {
|
||||
let obj
|
||||
|
||||
if(csg.polygons) obj = CSGCached(CSG2Vertices,csg,csg.polygons, transferable)
|
||||
if(csg.sides && !csg.points) obj = CSGCached(CSG2LineSegmentsVertices,csg,csg.sides, transferable)
|
||||
if(csg.points) obj = CSGCached(CSG2LineVertices,csg,csg.points, transferable)
|
||||
if (csg.polygons)
|
||||
obj = CSGCached(CSG2Vertices, csg, csg.polygons, transferable)
|
||||
if (csg.sides && !csg.points)
|
||||
obj = CSGCached(CSG2LineSegmentsVertices, csg, csg.sides, transferable)
|
||||
if (csg.points)
|
||||
obj = CSGCached(CSG2LineVertices, csg, csg.points, transferable)
|
||||
|
||||
return obj
|
||||
}
|
||||
CSGToBuffers.clearCache = ()=>{CSGToBuffers.cache = new WeakMap()}
|
||||
CSGToBuffers.clearCache = () => {
|
||||
CSGToBuffers.cache = new WeakMap()
|
||||
}
|
||||
CSGToBuffers.clearCache()
|
||||
|
||||
export { CSGToBuffers }
|
||||
@@ -1,6 +1,20 @@
|
||||
import { RenderArgs, DefaultKernelExport, createUnhealthyResponse, createHealthyResponse } from './common'
|
||||
import { MeshPhongMaterial, LineBasicMaterial, BufferGeometry , BufferAttribute, Line, LineSegments, Color, Mesh, Group} from 'three/build/three.module'
|
||||
|
||||
import {
|
||||
RenderArgs,
|
||||
DefaultKernelExport,
|
||||
createUnhealthyResponse,
|
||||
createHealthyResponse,
|
||||
} from './common'
|
||||
import {
|
||||
MeshPhongMaterial,
|
||||
LineBasicMaterial,
|
||||
BufferGeometry,
|
||||
BufferAttribute,
|
||||
Line,
|
||||
LineSegments,
|
||||
Color,
|
||||
Mesh,
|
||||
Group,
|
||||
} from 'three/build/three.module'
|
||||
|
||||
const materials = {
|
||||
mesh: {
|
||||
@@ -9,16 +23,17 @@ const materials = {
|
||||
},
|
||||
line: {
|
||||
def: new LineBasicMaterial({ color: 0x0000ff }),
|
||||
material: ({color,opacity,transparent})=>new LineBasicMaterial({color,opacity,transparent}),
|
||||
material: ({ color, opacity, transparent }) =>
|
||||
new LineBasicMaterial({ color, opacity, transparent }),
|
||||
},
|
||||
lines:null
|
||||
lines: null,
|
||||
}
|
||||
materials.lines = materials.line
|
||||
|
||||
function CSG2Object3D(obj) {
|
||||
const { vertices, indices, color, transforms } = obj
|
||||
|
||||
let materialDef = materials[obj.type]
|
||||
const materialDef = materials[obj.type]
|
||||
if (!materialDef) {
|
||||
console.error('Can not hangle object type: ' + obj.type, obj)
|
||||
return null
|
||||
@@ -26,18 +41,30 @@ function CSG2Object3D(obj){
|
||||
|
||||
let material = materialDef.def
|
||||
if (color) {
|
||||
let c = color
|
||||
material = new materialDef.material({ color: new Color(c[0],c[1],c[2]), flatShading: true, opacity: c[3] === void 0 ? 1:c[3], transparent: c[3] != 1 && c[3] !== void 0})
|
||||
const c = color
|
||||
material = new materialDef.material({
|
||||
color: new Color(c[0], c[1], c[2]),
|
||||
flatShading: true,
|
||||
opacity: c[3] === void 0 ? 1 : c[3],
|
||||
transparent: c[3] != 1 && c[3] !== void 0,
|
||||
})
|
||||
}
|
||||
|
||||
var geo = new BufferGeometry()
|
||||
const geo = new BufferGeometry()
|
||||
geo.setAttribute('position', new BufferAttribute(vertices, 3))
|
||||
|
||||
var mesh;
|
||||
let mesh
|
||||
switch (obj.type) {
|
||||
case 'mesh': geo.setIndex(new BufferAttribute(indices,1)); mesh = new THREE.Mesh(geo, material); break;
|
||||
case 'line': mesh = new Line(geo, material); break;
|
||||
case 'lines': mesh = new LineSegments(geo, material); break;
|
||||
case 'mesh':
|
||||
geo.setIndex(new BufferAttribute(indices, 1))
|
||||
mesh = new THREE.Mesh(geo, material)
|
||||
break
|
||||
case 'line':
|
||||
mesh = new Line(geo, material)
|
||||
break
|
||||
case 'lines':
|
||||
mesh = new LineSegments(geo, material)
|
||||
break
|
||||
}
|
||||
if (transforms) mesh.applyMatrix4({ elements: transforms })
|
||||
return mesh
|
||||
@@ -57,7 +84,6 @@ export const render: DefaultKernelExport['render'] = async ({
|
||||
code,
|
||||
settings,
|
||||
}: RenderArgs) => {
|
||||
|
||||
if (!scriptWorker) {
|
||||
const baseURI = document.baseURI.toString()
|
||||
const script = `let baseURI = '${baseURI}'
|
||||
@@ -70,17 +96,20 @@ let worker = jscadWorker({
|
||||
})
|
||||
self.addEventListener('message', (e)=>worker.postMessage(e.data))
|
||||
`
|
||||
let blob = new Blob([script],{type: 'text/javascript'})
|
||||
const blob = new Blob([script], { type: 'text/javascript' })
|
||||
scriptWorker = new Worker(window.URL.createObjectURL(blob))
|
||||
scriptWorker.addEventListener('message', (e) => {
|
||||
console.log('message from worker', e.data)
|
||||
let data = e.data
|
||||
const data = e.data
|
||||
if (data.action == 'entities') {
|
||||
if (data.error) {
|
||||
response = createUnhealthyResponse(new Date(), data.error)
|
||||
} else {
|
||||
let group = new Group()
|
||||
data.entities.map(CSG2Object3D).filter(o=>o).forEach(o=>group.add(o))
|
||||
const group = new Group()
|
||||
data.entities
|
||||
.map(CSG2Object3D)
|
||||
.filter((o) => o)
|
||||
.forEach((o) => group.add(o))
|
||||
response = createHealthyResponse({
|
||||
type: 'geometry',
|
||||
data: group,
|
||||
@@ -96,9 +125,14 @@ self.addEventListener('message', (e)=>worker.postMessage(e.data))
|
||||
response = null
|
||||
scriptWorker.postMessage({ action: 'init', baseURI, alias: [] })
|
||||
}
|
||||
scriptWorker.postMessage({action:'runScript', worker:'script', script:code, url:'jscad_script' })
|
||||
scriptWorker.postMessage({
|
||||
action: 'runScript',
|
||||
worker: 'script',
|
||||
script: code,
|
||||
url: 'jscad_script',
|
||||
})
|
||||
|
||||
let waitResult = new Promise(resolve=>{
|
||||
const waitResult = new Promise((resolve) => {
|
||||
resolveReference = resolve
|
||||
})
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ const main = ({scale=1}) => {
|
||||
return [transpCube, star2D, line2D, ...logo]
|
||||
}
|
||||
module.exports = {main}
|
||||
`
|
||||
`,
|
||||
}
|
||||
|
||||
const codeStorageKey = 'Last-editor-code'
|
||||
|
||||
Reference in New Issue
Block a user