format project
This commit is contained in:
@@ -1,46 +1,73 @@
|
||||
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: {
|
||||
def: new MeshPhongMaterial( { color: 0x0084d1, flatShading: true } ),
|
||||
material: (params)=>new MeshPhongMaterial(params),
|
||||
},
|
||||
line: {
|
||||
def: new LineBasicMaterial( { color: 0x0000ff } ),
|
||||
material: ({color,opacity,transparent})=>new LineBasicMaterial({color,opacity,transparent}),
|
||||
},
|
||||
lines:null
|
||||
mesh: {
|
||||
def: new MeshPhongMaterial({ color: 0x0084d1, flatShading: true }),
|
||||
material: (params) => new MeshPhongMaterial(params),
|
||||
},
|
||||
line: {
|
||||
def: new LineBasicMaterial({ color: 0x0000ff }),
|
||||
material: ({ color, opacity, transparent }) =>
|
||||
new LineBasicMaterial({ color, opacity, transparent }),
|
||||
},
|
||||
lines: null,
|
||||
}
|
||||
materials.lines = materials.line
|
||||
|
||||
function CSG2Object3D(obj){
|
||||
const {vertices, indices, color, transforms} = obj
|
||||
function CSG2Object3D(obj) {
|
||||
const { vertices, indices, color, transforms } = obj
|
||||
|
||||
let materialDef = materials[obj.type]
|
||||
if(!materialDef){
|
||||
console.error('Can not hangle object type: '+obj.type, obj)
|
||||
return null
|
||||
const materialDef = materials[obj.type]
|
||||
if (!materialDef) {
|
||||
console.error('Can not hangle object type: ' + obj.type, obj)
|
||||
return null
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
let material = materialDef.def
|
||||
if (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,
|
||||
})
|
||||
}
|
||||
|
||||
var geo = new BufferGeometry()
|
||||
geo.setAttribute('position', new BufferAttribute(vertices,3))
|
||||
const geo = new BufferGeometry()
|
||||
geo.setAttribute('position', new BufferAttribute(vertices, 3))
|
||||
|
||||
var 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;
|
||||
}
|
||||
if(transforms) mesh.applyMatrix4({elements:transforms})
|
||||
return 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
|
||||
}
|
||||
if (transforms) mesh.applyMatrix4({ elements: transforms })
|
||||
return mesh
|
||||
}
|
||||
|
||||
let scriptWorker
|
||||
@@ -48,8 +75,8 @@ const scriptUrl = '/demo-worker.js'
|
||||
let resolveReference = null
|
||||
let response = null
|
||||
|
||||
const callResolve = ()=>{
|
||||
if(resolveReference) resolveReference()
|
||||
const callResolve = () => {
|
||||
if (resolveReference) resolveReference()
|
||||
resolveReference
|
||||
}
|
||||
|
||||
@@ -57,10 +84,9 @@ export const render: DefaultKernelExport['render'] = async ({
|
||||
code,
|
||||
settings,
|
||||
}: RenderArgs) => {
|
||||
|
||||
if(!scriptWorker){
|
||||
if (!scriptWorker) {
|
||||
const baseURI = document.baseURI.toString()
|
||||
const script =`let baseURI = '${baseURI}'
|
||||
const script = `let baseURI = '${baseURI}'
|
||||
importScripts(new URL('${scriptUrl}',baseURI))
|
||||
let worker = jscadWorker({
|
||||
baseURI: baseURI,
|
||||
@@ -70,18 +96,21 @@ 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)=>{
|
||||
scriptWorker.addEventListener('message', (e) => {
|
||||
console.log('message from worker', e.data)
|
||||
let 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))
|
||||
response = createHealthyResponse( {
|
||||
const data = e.data
|
||||
if (data.action == 'entities') {
|
||||
if (data.error) {
|
||||
response = createUnhealthyResponse(new Date(), data.error)
|
||||
} else {
|
||||
const group = new Group()
|
||||
data.entities
|
||||
.map(CSG2Object3D)
|
||||
.filter((o) => o)
|
||||
.forEach((o) => group.add(o))
|
||||
response = createHealthyResponse({
|
||||
type: 'geometry',
|
||||
data: group,
|
||||
consoleMessage: data.scriptStats,
|
||||
@@ -94,11 +123,16 @@ self.addEventListener('message', (e)=>worker.postMessage(e.data))
|
||||
|
||||
callResolve()
|
||||
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
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user