format project

This commit is contained in:
Kurt Hutten
2021-08-01 09:55:07 +10:00
parent 625db5e26b
commit d8998a73b3
9 changed files with 215 additions and 180 deletions

View File

@@ -244,7 +244,6 @@ const makeScriptWorker = ({callback, convertToSolids})=>{
let workerBaseURI, onInit let workerBaseURI, onInit
function runMain(params={}){ function runMain(params={}){
console.log('runMain');
let time = Date.now() let time = Date.now()
let solids let solids
try{ try{

View File

@@ -25,10 +25,10 @@ function Asset({ geometry: incomingGeo }) {
}, [incomingGeo]) }, [incomingGeo])
if (!incomingGeo) return null if (!incomingGeo) return null
let groupData = incomingGeo.children ? incomingGeo : null const groupData = incomingGeo.children ? incomingGeo : null
if (lastGroup && lastGroup != groupData) { if (lastGroup && lastGroup != groupData) {
state.scene.remove(lastGroup) 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 // 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 // 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 /// FIXME - do this properly with useRef or other react magic

View File

@@ -3,7 +3,6 @@ import Svg from 'src/components/Svg/Svg'
import { Popover } from '@headlessui/react' import { Popover } from '@headlessui/react'
import type { CadPackage } from 'src/helpers/hooks/useIdeState' import type { CadPackage } from 'src/helpers/hooks/useIdeState'
const menuOptions: { const menuOptions: {
name: string name: string
sub: string sub: string

View File

@@ -7,7 +7,8 @@ const setPoints = (points, p, i)=>{
function CSG2Vertices(csg) { function CSG2Vertices(csg) {
let idx = 0 let idx = 0
let vLen = 0, iLen = 0 let vLen = 0,
iLen = 0
for (let poly of csg.polygons) { for (let poly of csg.polygons) {
let len = poly.vertices.length let len = poly.vertices.length
vLen += len * 3 vLen += len * 3
@@ -43,14 +44,12 @@ function CSG2Vertices(csg){
return { vertices, indices, type: 'mesh' } return { vertices, indices, type: 'mesh' }
} }
function CSG2LineVertices(csg) { function CSG2LineVertices(csg) {
let vLen = csg.points.length * 3 let vLen = csg.points.length * 3
if (csg.isClosed) vLen += 3 if (csg.isClosed) vLen += 3
var vertices = new Float32Array(vLen) var vertices = new Float32Array(vLen)
csg.points.forEach((p, idx) => setPoints(vertices, p, idx * 3)) csg.points.forEach((p, idx) => setPoints(vertices, p, idx * 3))
if (csg.isClosed) { if (csg.isClosed) {
@@ -69,7 +68,6 @@ function CSG2LineSegmentsVertices(csg){
setPoints(vertices, side[1], i + 3) setPoints(vertices, side[1], i + 3)
}) })
return { vertices, type: 'lines' } return { vertices, type: 'lines' }
} }
function CSGCached(func, data, cacheKey, transferable) { function CSGCached(func, data, cacheKey, transferable) {
@@ -94,13 +92,18 @@ function CSGCached(func, data, cacheKey, transferable){
function CSGToBuffers(csg, transferable) { function CSGToBuffers(csg, transferable) {
let obj let obj
if(csg.polygons) obj = CSGCached(CSG2Vertices,csg,csg.polygons, transferable) if (csg.polygons)
if(csg.sides && !csg.points) obj = CSGCached(CSG2LineSegmentsVertices,csg,csg.sides, transferable) obj = CSGCached(CSG2Vertices, csg, csg.polygons, transferable)
if(csg.points) obj = CSGCached(CSG2LineVertices,csg,csg.points, 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 return obj
} }
CSGToBuffers.clearCache = ()=>{CSGToBuffers.cache = new WeakMap()} CSGToBuffers.clearCache = () => {
CSGToBuffers.cache = new WeakMap()
}
CSGToBuffers.clearCache() CSGToBuffers.clearCache()
export { CSGToBuffers } export { CSGToBuffers }

View File

@@ -1,6 +1,20 @@
import { RenderArgs, DefaultKernelExport, createUnhealthyResponse, createHealthyResponse } from './common' import {
import { MeshPhongMaterial, LineBasicMaterial, BufferGeometry , BufferAttribute, Line, LineSegments, Color, Mesh, Group} from 'three/build/three.module' RenderArgs,
DefaultKernelExport,
createUnhealthyResponse,
createHealthyResponse,
} from './common'
import {
MeshPhongMaterial,
LineBasicMaterial,
BufferGeometry,
BufferAttribute,
Line,
LineSegments,
Color,
Mesh,
Group,
} from 'three/build/three.module'
const materials = { const materials = {
mesh: { mesh: {
@@ -9,16 +23,17 @@ const materials = {
}, },
line: { line: {
def: new LineBasicMaterial({ color: 0x0000ff }), 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 materials.lines = materials.line
function CSG2Object3D(obj) { function CSG2Object3D(obj) {
const { vertices, indices, color, transforms } = obj const { vertices, indices, color, transforms } = obj
let materialDef = materials[obj.type] const materialDef = materials[obj.type]
if (!materialDef) { if (!materialDef) {
console.error('Can not hangle object type: ' + obj.type, obj) console.error('Can not hangle object type: ' + obj.type, obj)
return null return null
@@ -26,18 +41,30 @@ function CSG2Object3D(obj){
let material = materialDef.def let material = materialDef.def
if (color) { if (color) {
let c = color 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}) 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)) geo.setAttribute('position', new BufferAttribute(vertices, 3))
var mesh; let mesh
switch (obj.type) { switch (obj.type) {
case 'mesh': geo.setIndex(new BufferAttribute(indices,1)); mesh = new THREE.Mesh(geo, material); break; case 'mesh':
case 'line': mesh = new Line(geo, material); break; geo.setIndex(new BufferAttribute(indices, 1))
case 'lines': mesh = new LineSegments(geo, material); break; 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 }) if (transforms) mesh.applyMatrix4({ elements: transforms })
return mesh return mesh
@@ -57,7 +84,6 @@ export const render: DefaultKernelExport['render'] = async ({
code, code,
settings, settings,
}: RenderArgs) => { }: RenderArgs) => {
if (!scriptWorker) { if (!scriptWorker) {
const baseURI = document.baseURI.toString() const baseURI = document.baseURI.toString()
const script = `let baseURI = '${baseURI}' const script = `let baseURI = '${baseURI}'
@@ -70,17 +96,20 @@ let worker = jscadWorker({
}) })
self.addEventListener('message', (e)=>worker.postMessage(e.data)) 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 = new Worker(window.URL.createObjectURL(blob))
scriptWorker.addEventListener('message', (e) => { scriptWorker.addEventListener('message', (e) => {
console.log('message from worker', e.data) console.log('message from worker', e.data)
let data = e.data const data = e.data
if (data.action == 'entities') { if (data.action == 'entities') {
if (data.error) { if (data.error) {
response = createUnhealthyResponse(new Date(), data.error) response = createUnhealthyResponse(new Date(), data.error)
} else { } else {
let group = new Group() const group = new Group()
data.entities.map(CSG2Object3D).filter(o=>o).forEach(o=>group.add(o)) data.entities
.map(CSG2Object3D)
.filter((o) => o)
.forEach((o) => group.add(o))
response = createHealthyResponse({ response = createHealthyResponse({
type: 'geometry', type: 'geometry',
data: group, data: group,
@@ -96,9 +125,14 @@ self.addEventListener('message', (e)=>worker.postMessage(e.data))
response = null response = null
scriptWorker.postMessage({ action: 'init', baseURI, alias: [] }) 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 resolveReference = resolve
}) })

View File

@@ -68,7 +68,7 @@ const main = ({scale=1}) => {
return [transpCube, star2D, line2D, ...logo] return [transpCube, star2D, line2D, ...logo]
} }
module.exports = {main} module.exports = {main}
` `,
} }
const codeStorageKey = 'Last-editor-code' const codeStorageKey = 'Last-editor-code'