Spaces:
Running
Running
File size: 17,364 Bytes
544434e 0e6816a 544434e 2df87ba c7c8c1d 8fe43ef 0e6816a 8fe43ef c7c8c1d 0e6816a 16f55ef f97559f cc8e895 aa04c11 10668a0 0e6816a b6b57ef c7c8c1d 8fe43ef 0e6816a 32ec743 0e6816a 32ec743 0e6816a 32ec743 0e6816a 8fe43ef b2c2744 7a299ad 8fe43ef 0e6816a 8fe43ef 0e6816a b6b57ef 0e6816a 8fe43ef 0e6816a 8fe43ef 0e6816a 8fe43ef 0e6816a 8fe43ef 0e6816a 8fe43ef 5df6c84 8fe43ef 0e6816a 8fe43ef b6b57ef 0e6816a b6b57ef 46ef425 b6b57ef 0e6816a cf3c219 0e6816a b5f2a77 0e6816a b6b57ef 7266910 b76fbf1 b6b57ef 7ed18fb b6b57ef 0180bb9 b6b57ef 037f244 b6b57ef 8f384cd b6b57ef 8f384cd b6b57ef 8f384cd b6b57ef b1f1e34 ee463db bf574d6 b6b57ef 48647c9 b6b57ef f97559f 48647c9 16f55ef f97559f 3c57cf8 f97559f 4f5b831 f97559f 16f55ef 2961c54 16f55ef 2961c54 16f55ef f97559f 7206143 f97559f 48647c9 cc8e895 73d7f32 95470f7 1b8e875 cc8e895 954c7db cc8e895 48647c9 cc8e895 48647c9 61402fe f97559f 61402fe 48647c9 c654788 98ecf15 cc8e895 98ecf15 c654788 98ecf15 2c68125 0180bb9 b6b57ef 7a299ad 8f384cd 5ca201e b6b57ef 8f384cd b6b57ef 8f384cd b6b57ef 8f384cd 19b342a 540545a 5ce9239 540545a 5ce9239 540545a ee463db 037f244 bcac896 037f244 567e051 ee463db 27ccd5d ee463db 38855ff c0f084e ee463db 46ef425 36ea500 46ef425 a96b42c f930d76 c91eb46 ee463db 798fe68 ee463db 037f244 93428c7 b6b57ef 65f42e6 b6b57ef 8fe43ef 8f384cd b6b57ef 0e6816a 84cc625 b2c2744 db9aecb b2c2744 bb5c317 84cc625 bb5c317 da9ea43 09f748f da9ea43 cc8e895 bb5c317 da9ea43 cc8e895 bb5c317 2f3089c | 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | // viewer.js
// ==============================
let pc;
export let app = null;
let cameraEntity = null;
let modelEntity = null;
let tubeEntity = null;
let bouchonEntity = null;
let viewerInitialized = false;
let resizeObserver = null;
// These will hold references to the material(s) you want to update.
let matTransparent = null;
let matOpaque = null;
let tubeTransparent = null;
let tubeOpaque = null;
let chosenCameraX, chosenCameraY, chosenCameraZ;
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
let glbUrl, glbUrl2, glbUrl3, aoUrl, opacityUrl, thicknessUrl;
// RECURSIVE ENTITY TRAVERSAL (needed for material assignment)
function traverse(entity, callback) {
callback(entity);
if (entity.children) {
entity.children.forEach(child => traverse(child, callback));
}
}
export async function initializeViewer(config, instanceId) {
if (viewerInitialized) return;
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
const isMobile = isIOS || /Android/i.test(navigator.userAgent);
glbUrl = config.glb_url;
glbUrl2 = config.glb_url_2;
glbUrl3 = config.glb_url_3;
aoUrl = config.ao_url;
thicknessUrl = config.thickness_url;
opacityUrl = config.opacity_url;
minZoom = 0.2;
maxZoom = 2;
minAngle = -Infinity;
maxAngle = Infinity;
minAzimuth = -Infinity;
maxAzimuth = Infinity;
minPivotY = 0;
minY = -10;
modelX = 0;
modelY = 0;
modelZ = 0;
modelScale = 1;
modelRotationX = 0;
modelRotationY = 0;
modelRotationZ = 0;
const cameraX = 0;
const cameraY = 0;
const cameraZ = 1;
const cameraXPhone = 0;
const cameraYPhone = 0;
const cameraZPhone = 1;
chosenCameraX = isMobile ? cameraXPhone : cameraX;
chosenCameraY = isMobile ? cameraYPhone : cameraY;
chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
const canvasId = 'canvas-' + instanceId;
const progressDialog = document.getElementById('progress-dialog-' + instanceId);
const progressIndicator = document.getElementById('progress-indicator-' + instanceId);
const viewerContainer = document.getElementById('viewer-container-' + instanceId);
let oldCanvas = document.getElementById(canvasId);
if (oldCanvas) oldCanvas.remove();
const canvas = document.createElement('canvas');
canvas.id = canvasId;
canvas.className = 'ply-canvas';
canvas.style.width = "100%";
canvas.style.height = "100%";
canvas.setAttribute('tabindex', '0');
// Safer insert:
if (progressDialog) {
viewerContainer.insertBefore(canvas, progressDialog);
} else {
viewerContainer.appendChild(canvas);
}
canvas.style.touchAction = "none";
canvas.style.webkitTouchCallout = "none";
canvas.addEventListener('gesturestart', e => e.preventDefault());
canvas.addEventListener('gesturechange', e => e.preventDefault());
canvas.addEventListener('gestureend', e => e.preventDefault());
canvas.addEventListener('dblclick', e => e.preventDefault());
canvas.addEventListener('touchstart', e => { if (e.touches.length > 1) e.preventDefault(); }, { passive: false });
canvas.addEventListener('wheel', (e) => {
e.preventDefault();
}, { passive: false });
if (progressDialog) progressDialog.style.display = 'block';
if (!pc) {
pc = await import("https://esm.run/playcanvas");
window.pc = pc;
}
// Create app first
const device = await pc.createGraphicsDevice(canvas, {
deviceTypes: ["webgl2"],
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
antialias: false
});
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
const opts = new pc.AppOptions();
opts.graphicsDevice = device;
opts.mouse = new pc.Mouse(canvas);
opts.touch = new pc.TouchDevice(canvas);
opts.componentSystems = [
pc.RenderComponentSystem,
pc.CameraComponentSystem,
pc.LightComponentSystem,
pc.ScriptComponentSystem,
pc.GSplatComponentSystem,
pc.CollisionComponentSystem,
pc.RigidbodyComponentSystem
];
opts.resourceHandlers = [
pc.TextureHandler,
pc.ContainerHandler,
pc.ScriptHandler,
pc.GSplatHandler
];
app = new pc.Application(canvas, opts);
app.setCanvasFillMode(pc.FILLMODE_NONE);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
resizeObserver = new ResizeObserver(entries => {
entries.forEach(entry => {
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
});
});
resizeObserver.observe(viewerContainer);
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
app.on('destroy', () => resizeObserver.disconnect());
// --- Assets ---
const assets = {
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
model: new pc.Asset('glb', 'container', { url: glbUrl }),
tube: new pc.Asset('glb', 'container', { url: glbUrl2 }),
bouchon: new pc.Asset('glb', 'container', { url: glbUrl3 }),
ao_map: new pc.Asset('color', 'texture', { url: aoUrl }),
op_map: new pc.Asset('color', 'texture', { url: opacityUrl }),
bg_tex: new pc.Asset('color', 'texture', {url: "https://huggingface.co/datasets/MikaFil/3D_models/resolve/main/EARCARE/banniere_earcare.png"}),
thickness_map: new pc.Asset('color', 'texture', { url: thicknessUrl }),
helipad: new pc.Asset(
'helipad-env-atlas',
'texture',
{ url: "https://huggingface.co/datasets/MikaFil/3D_models/resolve/main/EARCARE/hdr/ciel_nuageux_1k.png" },
{ type: pc.TEXTURETYPE_RGBP, mipmaps: false }
)
};
for (const key in assets) app.assets.add(assets[key]);
// Load HDR *before* loading any models
assets.helipad.ready(() => {
app.scene.envAtlas = assets.helipad.resource;
app.scene.skyboxRotation = new pc.Quat().setFromEulerAngles(30, 20, 0);
app.scene.skyboxIntensity = 4;
app.scene.skyboxMip = 0;
// Now load all GLBs and other assets
const loader = new pc.AssetListLoader([
assets.orbit,
assets.model,
assets.tube,
assets.bouchon,
assets.ao_map,
assets.op_map,
assets.bg_tex,
assets.thickness_map
], app.assets);
loader.load(() => {
app.start();
if (progressDialog) progressDialog.style.display = 'none';
// Reorder depth layer for transmission
const depthLayer = app.scene.layers.getLayerById(pc.LAYERID_DEPTH);
app.scene.layers.remove(depthLayer);
app.scene.layers.insertOpaque(depthLayer, 2);
// 1. Models
modelEntity = assets.model.resource.instantiateRenderEntity();
tubeEntity = assets.tube.resource.instantiateRenderEntity();
bouchonEntity = assets.bouchon.resource.instantiateRenderEntity();
app.root.addChild(modelEntity);
app.root.addChild(tubeEntity);
//app.root.addChild(bouchonEntity);
// 2. Materials for main model & tube
//the 2 materials for the main model :
const ao = assets.ao_map.resource;
const op = assets.op_map.resource;
const thickness = assets.thickness_map.resource;
//mat transparent
matTransparent = new pc.StandardMaterial();
matTransparent.blendType = pc.BLEND_NORMAL;
matTransparent.diffuse = new pc.Color(0.7, 0.05, 0.05);
matTransparent.specular = new pc.Color(0.01, 0.01, 0.01);
matTransparent.shininess = 90;
matTransparent.gloss = 1;
matTransparent.metalness = 0.0;
matTransparent.useMetalness = true;
matTransparent.useDynamicRefraction = true;
matTransparent.depthWrite = true;
matTransparent.refraction= 0.8;
matTransparent.refractionIndex = 1;
matTransparent.thickness = 0.02;
matTransparent.thicknessMap = thickness;
matTransparent.opacityMap = op;
matTransparent.opacityMapChannel = "r";
matTransparent.opacity = 0.97;
matTransparent.emissive = new pc.Color(0.372, 0.03, 0.003);
matTransparent.emissiveMap = ao;
matTransparent.emissiveIntensity = 1;
matTransparent.update();
//mat opaque
matOpaque = new pc.StandardMaterial();
matOpaque.blendType = pc.BLEND_NORMAL;
matOpaque.diffuse = new pc.Color(0.7, 0.05, 0.05);
matOpaque.specular = new pc.Color(0.01, 0.01, 0.01);
matOpaque.shininess = 90;
matOpaque.gloss = 1;
matOpaque.metalness = 0.0;
matOpaque.opacityMap = op;
matOpaque.opacityMapChannel = "r";
matOpaque.opacity = 1;
matOpaque.emissive = new pc.Color(0.372, 0.03, 0.003);
matOpaque.emissiveMap = ao;
matOpaque.emissiveIntensity = 1;
matOpaque.update();
//material for the tube
//transparent
tubeTransparent = new pc.StandardMaterial();
tubeTransparent.diffuse = new pc.Color(1, 1, 1);
tubeTransparent.blendType = pc.BLEND_NORMAL;
tubeTransparent.opacity = 0.1;
tubeTransparent.depthTest = false;
tubeTransparent.depthWrite = false;
tubeTransparent.useMetalness = true;
tubeTransparent.useDynamicRefraction = true;
tubeTransparent.thickness = 4;
tubeTransparent.update();
//opaque
tubeOpaque = new pc.StandardMaterial();
tubeOpaque.diffuse = new pc.Color(1, 1, 1);
tubeOpaque.opacity = 0.9;
tubeOpaque.update();
traverse(modelEntity, node => {
if (node.render && node.render.meshInstances) {
for (let mi of node.render.meshInstances) {
mi.material = matOpaque;
}
}
});
traverse(tubeEntity, node => {
if (node.render && node.render.meshInstances) {
for (let mi of node.render.meshInstances) {
mi.material = tubeOpaque;
}
}
});
// Do NOT override materials for bouchonEntity!
// 3. Position/scale/orientation
modelEntity.setPosition(modelX, modelY, modelZ);
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
modelEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
tubeEntity.setPosition(modelX, modelY, modelZ);
tubeEntity.setLocalScale(modelScale, modelScale, modelScale);
tubeEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
bouchonEntity.setPosition(modelX, modelY, modelZ);
bouchonEntity.setLocalScale(modelScale, modelScale, modelScale);
bouchonEntity.setLocalEulerAngles(modelRotationX, modelRotationY, modelRotationZ);
// 4. Camera & orbit
cameraEntity = new pc.Entity('camera');
cameraEntity.addComponent('camera', {
clearColor: new pc.Color(1, 1, 1, 1),
toneMapping: pc.TONEMAP_NEUTRAL
});
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
cameraEntity.lookAt(modelEntity.getPosition());
cameraEntity.addComponent('script');
// --- **CRITICAL** for KHR_materials_transmission! ---
cameraEntity.camera.requestSceneColorMap(true);
cameraEntity.script.create('orbitCamera', {
attributes: {
focusEntity: modelEntity,
inertiaFactor: 0.2,
distanceMax: maxZoom,
distanceMin: minZoom,
pitchAngleMax: maxAngle,
pitchAngleMin: minAngle,
yawAngleMax: maxAzimuth,
yawAngleMin: minAzimuth,
minY: minY,
frameOnStart: false
}
});
cameraEntity.script.create('orbitCameraInputMouse');
cameraEntity.script.create('orbitCameraInputTouch');
app.root.addChild(cameraEntity);
// Remove the Skybox layer from the camera
const skyboxLayer = app.scene.layers.getLayerByName("Skybox");
const camLayers = cameraEntity.camera.layers.slice();
const idx = camLayers.indexOf(skyboxLayer.id);
if (idx !== -1) {
camLayers.splice(idx, 1);
cameraEntity.camera.layers = camLayers;
}
// --- Add this block for a cube parented to the camera ---
// Create the cube
const bgTex = assets.bg_tex.resource;
const bgPlane = new pc.Entity("Plane");
bgPlane.addComponent("model", { type: "plane" });
// Set the cube's local position relative to the camera
bgPlane.setLocalPosition(0, 0, -10); // 2 units in front of camera
// Optionally, scale or color the cube
bgPlane.setLocalScale(11, 1, 5.5);
bgPlane.setLocalEulerAngles(90, 0, 0);
// Simple material for visibility
const mat = new pc.StandardMaterial();
mat.diffuse = new pc.Color(1, 1, 1);
mat.diffuseMap = bgTex;
mat.emissive = new pc.Color(1, 1, 1);
mat.emissiveMap = bgTex;
mat.emissiveIntensity = 1;
mat.useLighting = false;
mat.update();
bgPlane.model.material = mat;
// Parent to the camera
cameraEntity.addChild(bgPlane);
// 5. Light
const light = new pc.Entity("mainLight");
light.addComponent('light',{
type: "directional",
color: new pc.Color(1, 1, 1),
intensity: 0.9,
});
light.setPosition(0, 1, -1);
light.lookAt(0, 0, 0);
app.root.addChild(light);
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
app.once('update', () => resetViewerCamera());
// Tooltips supported if tooltips_url set
try {
if (config.tooltips_url) {
import('./tooltips.js').then(tooltipsModule => {
tooltipsModule.initializeTooltips({
app,
cameraEntity,
modelEntity,
tooltipsUrl: config.tooltips_url,
defaultVisible: !!config.showTooltipsDefault,
moveDuration: config.tooltipMoveDuration || 0.6
});
}).catch(e => {
// Tooltips optional: fail silently if missing
});
}
} catch (e) {
// Tooltips optional, fail silently
}
viewerInitialized = true;
});
});
app.assets.load(assets.helipad); // Start by loading the HDR
}
export function resetViewerCamera() {
try {
if (!cameraEntity || !modelEntity || !app) return;
const orbitCam = cameraEntity.script.orbitCamera;
if (!orbitCam) return;
const modelPos = modelEntity.getPosition();
const tempEnt = new pc.Entity();
tempEnt.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
tempEnt.lookAt(modelPos);
const dist = new pc.Vec3().sub2(
new pc.Vec3(chosenCameraX, chosenCameraY, chosenCameraZ),
modelPos
).length();
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
cameraEntity.lookAt(modelPos);
orbitCam.pivotPoint = modelPos.clone();
orbitCam._targetDistance = dist;
orbitCam._distance = dist;
const rot = tempEnt.getRotation();
const fwd = new pc.Vec3();
rot.transformVector(pc.Vec3.FORWARD, fwd);
const yaw = Math.atan2(-fwd.x, -fwd.z) * pc.math.RAD_TO_DEG;
const yawQuat = new pc.Quat().setFromEulerAngles(0, -yaw, 0);
const rotNoYaw = new pc.Quat().mul2(yawQuat, rot);
const fNoYaw = new pc.Vec3();
rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
const pitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
orbitCam._targetYaw = yaw;
orbitCam._yaw = yaw;
orbitCam._targetPitch = pitch;
orbitCam._pitch = pitch;
if (orbitCam._updatePosition) orbitCam._updatePosition();
tempEnt.destroy();
} catch (e) {
// Silent fail
}
}
/**
* Change the main model's diffuse color in real time.
* r,g,b are floats [0,1]. This is a reusable function for new colors.
*/
export function changeColor(dr, dg, db, er, eg, eb, op, boolTrans) {
//cameraEntity.camera.clearColor = new pc.Color(colorBgR, colorBgG, colorBgB);
if(boolTrans){
if (!matTransparent) return;
matTransparent.diffuse.set(dr, dg, db);
matTransparent.emissive.set(er, eg, eb);
matTransparent.opacity = op;
matTransparent.update();
tubeTransparent.diffuse.set(dr, dg, db);
tubeTransparent.update();
traverse(modelEntity, node => {
if (node.render && node.render.meshInstances) {
for (let mi of node.render.meshInstances) {
mi.material = matTransparent;
}
}
});
traverse(tubeEntity, node => {
if (node.render && node.render.meshInstances) {
for (let mi of node.render.meshInstances) {
mi.material = tubeTransparent;
}
}
});
}else{
if (!matOpaque) return;
matOpaque.diffuse.set(dr, dg, db);
matOpaque.emissive.set(er, eg, eb);
matOpaque.update();
traverse(modelEntity, node => {
if (node.render && node.render.meshInstances) {
for (let mi of node.render.meshInstances) {
mi.material = matOpaque;
}
}
});
traverse(tubeEntity, node => {
if (node.render && node.render.meshInstances) {
for (let mi of node.render.meshInstances) {
mi.material = tubeOpaque;
}
}
});
}
}
|