UNPKG

threepipe

Version:

A 3D viewer framework built on top of three.js in TypeScript with a focus on quality rendering, modularity and extensibility.

49 lines (48 loc) 1.89 kB
import { _testFinish, generateUiFolder, LoadingScreenPlugin, PickingPlugin, SimplifyModifierPlugin, ThreeViewer, } from 'threepipe'; import { TweakpaneUiPlugin } from '@threepipe/plugin-tweakpane'; import { SimplifyModifier } from 'three/examples/jsm/modifiers/SimplifyModifier.js'; import { createSimpleButtons } from '../examples-utils/simple-bottom-buttons.js'; class SimplifyModifierPluginImpl extends SimplifyModifierPlugin { constructor() { super(...arguments); this.uiConfig = generateUiFolder('Simplify Modifier', this); } _simplify(geometry, count) { const res = new SimplifyModifier().modify(geometry, count); res.computeVertexNormals(); return res; } } async function init() { const viewer = new ThreeViewer({ canvas: document.getElementById('mcanvas'), msaa: true, plugins: [PickingPlugin, LoadingScreenPlugin], }); const simplify = viewer.addPluginSync(SimplifyModifierPluginImpl); const ui = viewer.addPluginSync(new TweakpaneUiPlugin(true)); await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr', { setBackground: true, }); const result = await viewer.load('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', { autoCenter: true, autoScale: true, }); const mats = []; result?.traverse((obj) => { obj.materials?.map(m => { if (!m) return; m.wireframe = true; mats.push(m); }); }); createSimpleButtons({ ['Simplify']: async (_) => { await simplify.simplifyAll(result, { factor: 0.5 }); }, }); ui.setupPluginUi(SimplifyModifierPluginImpl); mats.forEach(m => ui.appendChild(m.uiConfig, { expanded: false })); } init().finally(_testFinish);