diff --git a/apps/jscad-web/main.js b/apps/jscad-web/main.js index 6a8d602..2ae9bea 100644 --- a/apps/jscad-web/main.js +++ b/apps/jscad-web/main.js @@ -13,6 +13,7 @@ import * as remote from './src/remote.js' import { formatStacktrace } from './src/stacktrace.js' import { ViewState } from './src/viewState.js' import * as welcome from './src/welcome.js' +import { runMain } from '../../packages/worker/worker.js' export const byId = id => document.getElementById(id) const appBase = document.baseURI @@ -20,6 +21,7 @@ let currentBase = appBase const toUrl = path => new URL(path, appBase).toString() const viewState = new ViewState() +viewState.onRequireReRender = ()=>paramChangeCallback(lastParams) const gizmo = (window.gizmo = new Gizmo()) byId('overlay').parentNode.appendChild(gizmo) @@ -173,7 +175,7 @@ sendCmdAndSpin('init', { }, }).then(() => { if (loadDefault) { - runScript({ script: defaultCode }) + runScript({ script: defaultCode, smooth: viewState.smoothRender }) } }) @@ -189,18 +191,18 @@ const paramChangeCallback = async params => { working = true let result try{ - result = await sendCmdAndSpin('runMain', { params }) + result = await sendCmdAndSpin('runMain', { params, smooth: viewState.smoothRender }) } finally{ working = false } - handlers.entities(result) + handlers.entities(result, {smooth: viewState.smoothRender}) if(lastParams && lastParams != params) paramChangeCallback(lastParams) } const runScript = async ({ script, url = './index.js', base = currentBase, root }) => { currentBase = base loadDefault = false // don't load default model if something else was loaded - const result = await sendCmdAndSpin('runScript', { script, url, base, root }) + const result = await sendCmdAndSpin('runScript', { script, url, base, root, smooth: viewState.smoothRender }) genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback }) handlers.entities(result) } diff --git a/apps/jscad-web/src/viewState.js b/apps/jscad-web/src/viewState.js index ba54dbe..49b7108 100644 --- a/apps/jscad-web/src/viewState.js +++ b/apps/jscad-web/src/viewState.js @@ -16,6 +16,7 @@ export class ViewState { const darkMode = byId('dark-mode') const showAxis = byId('show-axis') const showGrid = byId('show-grid') + const smoothRender = byId('smooth-render') darkMode.addEventListener('change', () => { this.themeName = darkMode.checked ? 'dark' : 'light' if (darkMode.checked) { @@ -25,6 +26,9 @@ export class ViewState { } this.setTheme(this.themeName) }) + smoothRender.addEventListener('change', () => { + this.setSmoothRender(smoothRender.checked) + }) showAxis.addEventListener('change', () => this.setAxes(showAxis.checked)) showGrid.addEventListener('change', () => this.setGrid(showGrid.checked)) } @@ -41,6 +45,12 @@ export class ViewState { this.saveState() } + setSmoothRender(smoothRender, fireEvent = true) { + this.smoothRender = smoothRender + this.saveState() + if(fireEvent) this.onRequireReRender() + } + setTheme(themeName) { if (!themes[themeName]) throw new Error(`unknown theme ${themeName}`) this.themeName = themeName @@ -86,7 +96,7 @@ export class ViewState { if (grid) items.push({ id: 'grid', items: grid }) if (model) items.push({ id: 'model', items: model }) - this.viewer?.setScene({ items }) + this.viewer?.setScene({ items }, {smooth:this.smoothRender}) } setEngine(viewer) { @@ -108,6 +118,8 @@ export class ViewState { byId('show-axis').checked = this.showAxis this.showGrid = localStorage.getItem('engine.showGrid') !== 'false' byId('show-grid').checked = this.showGrid + this.smoothRender = !!localStorage.getItem('engine.smoothRender') + byId('smooth-render').checked = this.smoothRender const cameraLocation = localStorage.getItem('camera.location') this.camera = cameraLocation ? JSON.parse(cameraLocation) : { position: [180, -180, 220] } } @@ -116,5 +128,8 @@ export class ViewState { localStorage.setItem('engine.theme', this.themeName) localStorage.setItem('engine.showAxis', this.showAxis) localStorage.setItem('engine.showGrid', this.showGrid) + localStorage.setItem('engine.smoothRender', this.smoothRender) } + + onRequireReRender(){} } diff --git a/apps/jscad-web/static/index.html b/apps/jscad-web/static/index.html index 8644dc0..17e8f24 100644 --- a/apps/jscad-web/static/index.html +++ b/apps/jscad-web/static/index.html @@ -82,6 +82,7 @@

Options

  • +
  • Documentation

    diff --git a/packages/format-threejs/index.js b/packages/format-threejs/index.js index 9f1a1cf..8418cf9 100644 --- a/packages/format-threejs/index.js +++ b/packages/format-threejs/index.js @@ -25,7 +25,7 @@ export function CommonToThree({ materials.lines = materials.line materials.instance = materials.mesh // todo support instances for lines - function _CSG2Three(obj, { smooth = true }) { + function _CSG2Three(obj, { smooth = false }) { const { vertices, indices, normals, color, colors, isTransparent = false, opacity } = obj let { transforms } = obj const objType = obj.type || 'mesh' diff --git a/packages/render-threejs/index.js b/packages/render-threejs/index.js index c0466b3..d66513e 100644 --- a/packages/render-threejs/index.js +++ b/packages/render-threejs/index.js @@ -24,6 +24,7 @@ export function RenderThreejs({ let _camera let controls let renderer + let smooth const SHADOW = false const shouldRender = Date.now() const lastRender = true @@ -100,6 +101,10 @@ export function RenderThreejs({ updateView() } + function setSmooth(v){ + smooth = v + } + function setMeshColor(bg = [1, 1, 1]) { meshColor = new Color(...bg) csgConvert.setDefColor(bg) @@ -193,7 +198,8 @@ export function RenderThreejs({ } } - function setScene(scene) { + function setScene(scene,{smooth}={}) { + console.warn('options', {smooth}) groups.forEach(group => { _scene.remove(group) }) @@ -205,7 +211,7 @@ export function RenderThreejs({ const group = new Group() groups.push(group) item.items.forEach(obj => { - const obj3d = csgConvert(obj, scene, meshColor) + const obj3d = csgConvert(obj, { smooth, scene, meshColor}) if (obj3d) { entities.push(obj3d) group.add(obj3d)