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 2.19 kB
import { BoxGeometry, CylinderGeometry, HemisphereLight, Mesh, Scene, SphereGeometry, Vector3 } from 'three'; import { snapObject } from './snapObject'; export class MaterialPreviewGenerator { constructor() { this._lights = []; this.shapes = { sphere: new Mesh(new SphereGeometry(1)), cube: new Mesh(new BoxGeometry(1, 1, 1)), cylinder: new Mesh(new CylinderGeometry(0.5, 0.5, 1)), }; const scene = new Scene(); this._channel = 7; const hemisphericLight = new HemisphereLight(0xffffff, 0x444444, 1); hemisphericLight.position.set(0, 10, 0); hemisphericLight.layers.set(this._channel); scene.add(hemisphericLight); this._lights.push(hemisphericLight); this._scene = scene; } dispose() { [...this._lights].forEach(light => light.dispose()); Object.values(this.shapes).forEach(shape => { if (shape.geometry) shape.geometry.dispose(); }); } // todo: show an overlay when this is happening generate(material, renderer, environment, shape = 'sphere') { const object = this.shapes[shape] || new Mesh(new SphereGeometry(1)); object.material = material; if (!object.geometry.attributes.tangent) object.geometry.computeTangents(); // for anisotropy this._scene.add(object); this._scene.environment = environment ?? null; const envIntensity = material.envMapIntensity; // clamp since we have no tonemapping if (typeof envIntensity === 'number') { material.envMapIntensity = Math.max(envIntensity, 2); } const snap = snapObject(renderer, object, this._scene, this._channel, new Vector3(0, 0, 1.5)); // const snap = snapObject(this.viewer, (material.userData.__appliedMeshes as Set<Mesh>).values().next().value, undefined, this._channel) if (typeof envIntensity === 'number') material.envMapIntensity = envIntensity; this._scene.remove(object); object.material = undefined; return snap; } } //# sourceMappingURL=MaterialPreviewGenerator.js.map