Spaces:
Running
Running
File size: 8,219 Bytes
ebb3bda 3b7903d ebb3bda 95af024 ebb3bda 95af024 e7b5357 9c554be ebb3bda b95cc56 62c50cd ebb3bda 6b6e352 abee439 ebb3bda 3b7903d ebb3bda b95cc56 bdef08e ab77dc3 bdef08e ebb3bda 9c554be ebb3bda 62c50cd ebb3bda 62c50cd ebb3bda e7b5357 ebb3bda e7b5357 ebb3bda 9c554be ebb3bda bdef08e b95cc56 bdef08e ab77dc3 ebb3bda 62c50cd b95cc56 62c50cd b95cc56 62c50cd b95cc56 62c50cd b95cc56 62c50cd b95cc56 62c50cd b95cc56 62c50cd ebb3bda 3b7903d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
import type { IViewer } from "./IViewer";
import * as BABYLON from "@babylonjs/core";
import "@babylonjs/loaders/glTF";
import "@babylonjs/loaders/OBJ";
export class BabylonViewer implements IViewer {
canvas: HTMLCanvasElement;
engine: BABYLON.Engine;
scene: BABYLON.Scene;
camera: BABYLON.ArcRotateCamera;
vertexCount: number = 0;
topoOnly: boolean = false;
private _originalMaterials: Map<BABYLON.AbstractMesh, BABYLON.Material> = new Map();
private _originalVertexColors: Map<BABYLON.AbstractMesh, BABYLON.Nullable<BABYLON.FloatArray>> = new Map();
private _wireframes: Array<BABYLON.Mesh> = [];
private _solidColor = new BABYLON.Color4(1, 1, 1, 1);
private _wireframeMaterial: BABYLON.StandardMaterial;
private _solidMaterial: BABYLON.StandardMaterial;
constructor(canvas: HTMLCanvasElement) {
this.canvas = canvas;
this.engine = new BABYLON.Engine(canvas, true);
this.scene = new BABYLON.Scene(this.engine);
this.scene.clearColor = BABYLON.Color4.FromHexString("#000000FF");
this.scene.imageProcessingConfiguration.exposure = 1.3;
this.camera = new BABYLON.ArcRotateCamera(
"camera",
Math.PI / 3,
Math.PI / 3,
30,
BABYLON.Vector3.Zero(),
this.scene
);
this.camera.angularSensibilityY = 1000;
this.camera.panningSensibility = 500;
this.camera.wheelPrecision = 5;
this.camera.inertia = 0.9;
this.camera.panningInertia = 0.9;
this.camera.lowerRadiusLimit = 3;
this.camera.upperRadiusLimit = 100;
this.camera.setTarget(BABYLON.Vector3.Zero());
this.camera.attachControl(this.canvas, true);
this.camera.onAfterCheckInputsObservable.add(() => {
this.camera.wheelPrecision = 150 / this.camera.radius;
this.camera.panningSensibility = 10000 / this.camera.radius;
});
this._wireframeMaterial = new BABYLON.StandardMaterial("wireframe", this.scene);
this._wireframeMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
this._wireframeMaterial.emissiveColor = new BABYLON.Color3(0.7, 0.7, 0.7);
this._wireframeMaterial.disableLighting = true;
this._wireframeMaterial.wireframe = true;
this._solidMaterial = new BABYLON.StandardMaterial("solid", this.scene);
this._solidMaterial.diffuseColor = new BABYLON.Color3(0.8, 0.8, 0.8);
this._solidMaterial.alphaMode = 0;
this.handleResize = this.handleResize.bind(this);
window.addEventListener("resize", this.handleResize);
this.canvas.addEventListener("wheel", (e) => e.preventDefault());
}
handleResize() {
this.engine.resize();
}
async loadScene(url: string, loadingBarCallback?: (progress: number) => void, topoOnly?: boolean) {
this.topoOnly = topoOnly ?? false;
// Load scene
await BABYLON.SceneLoader.AppendAsync("", url, this.scene, (event) => {
const progress = event.loaded / event.total;
loadingBarCallback?.(progress);
});
// Dispose of all cameras and lights
this.scene.cameras.forEach((camera) => {
if (camera !== this.camera) {
camera.dispose();
}
});
this.scene.lights.forEach((light) => {
light.dispose();
});
// Add lights
const hemi = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), this.scene);
hemi.intensity = 0.5;
hemi.diffuse = new BABYLON.Color3(1, 1, 1);
hemi.groundColor = new BABYLON.Color3(1, 1, 1);
const sun = new BABYLON.DirectionalLight("sun", new BABYLON.Vector3(-0.5, -1, -0.5), this.scene);
sun.intensity = 1;
sun.diffuse = new BABYLON.Color3(1, 1, 1);
// Center and scale model
const parentNode = new BABYLON.TransformNode("parent", this.scene);
const standardSize = 10;
let scaleFactor = 1;
let center = BABYLON.Vector3.Zero();
if (this.scene.meshes.length > 0) {
let bounds = this.scene.meshes[0].getBoundingInfo().boundingBox;
let min = bounds.minimumWorld;
let max = bounds.maximumWorld;
for (let i = 1; i < this.scene.meshes.length; i++) {
bounds = this.scene.meshes[i].getBoundingInfo().boundingBox;
min = BABYLON.Vector3.Minimize(min, bounds.minimumWorld);
max = BABYLON.Vector3.Maximize(max, bounds.maximumWorld);
}
const extent = max.subtract(min).scale(0.5);
const size = extent.length();
center = BABYLON.Vector3.Center(min, max);
scaleFactor = standardSize / size;
}
this.vertexCount = 0;
this.scene.meshes.forEach((mesh) => {
mesh.setParent(parentNode);
if (mesh.getTotalVertices() > 0) {
this.vertexCount += mesh.getTotalVertices();
}
});
parentNode.position = center.scale(-1 * scaleFactor);
parentNode.scaling.scaleInPlace(scaleFactor);
if (this.topoOnly) {
this.setRenderMode("wireframe");
}
// Run render loop
this.engine.runRenderLoop(() => {
this.scene.render();
});
}
dispose() {
if (this.scene) {
this.scene.dispose();
}
if (this.engine) {
this.engine.dispose();
}
this._originalMaterials.clear();
window.removeEventListener("resize", this.handleResize);
this.canvas.removeEventListener("wheel", (e) => e.preventDefault());
}
async capture(): Promise<string | null> {
if (!this.engine || !this.camera) return null;
const cachedColor = this.scene.clearColor;
this.scene.clearColor = BABYLON.Color4.FromHexString("#00000000");
let data = await new Promise<string>((resolve) => {
BABYLON.Tools.CreateScreenshotUsingRenderTarget(this.engine, this.camera, 512, (result) => {
resolve(result);
});
});
this.scene.clearColor = cachedColor;
return data;
}
setRenderMode(mode: string) {
if (mode === "wireframe") {
this.scene.meshes.forEach((mesh) => {
const vertexData = mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
if (vertexData) {
this._originalVertexColors.set(mesh, vertexData);
const newVertexData = new Array<number>(vertexData.length);
for (let i = 0; i < vertexData.length; i += 4) {
newVertexData[i] = this._solidColor.r;
newVertexData[i + 1] = this._solidColor.g;
newVertexData[i + 2] = this._solidColor.b;
newVertexData[i + 3] = this._solidColor.a;
}
mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, newVertexData);
}
const material = mesh.material as BABYLON.StandardMaterial;
if (material) {
this._originalMaterials.set(mesh, material);
mesh.material = this._solidMaterial;
}
const wireframeMesh = mesh.clone(mesh.name + "_wireframe", null) as BABYLON.Mesh;
wireframeMesh.material = this._wireframeMaterial;
this._wireframes.push(wireframeMesh);
});
} else {
this._wireframes.forEach((mesh) => {
mesh.dispose();
});
this._wireframes = [];
this.scene.meshes.forEach((mesh) => {
const vertexData = this._originalVertexColors.get(mesh);
if (vertexData) {
mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, vertexData);
}
const material = this._originalMaterials.get(mesh);
if (material) {
mesh.material = material;
}
});
}
}
}
|