@pmndrs/uikit
Version:
Build performant 3D user interfaces with Three.js and yoga.
60 lines (59 loc) • 2.74 kB
JavaScript
import { Box3, Mesh, Sphere } from 'three';
import { createPanelGeometry, panelGeometry } from './utils.js';
import { instancedPanelDepthMaterial, instancedPanelDistanceMaterial } from './panel-material.js';
import { abortableEffect } from '../utils.js';
import { setupBoundingSphere, makeClippedCast, makePanelRaycast, makePanelSpherecast, } from './interaction-panel-mesh.js';
export function createInteractionPanel(orderInfo, rootContext, parentClippingRect, globalMatrix, flexState) {
const boundingSphere = new Sphere();
const panel = Object.assign(new Mesh(panelGeometry), { boundingSphere });
panel.matrixAutoUpdate = false;
const rootObjectRef = rootContext.objectRef;
panel.raycast = makeClippedCast(panel, makePanelRaycast(panel.raycast.bind(panel), rootObjectRef, boundingSphere, globalMatrix, panel), rootContext.objectRef, parentClippingRect, orderInfo, flexState);
panel.spherecast = makeClippedCast(panel, makePanelSpherecast(rootObjectRef, boundingSphere, globalMatrix, panel), rootContext.objectRef, parentClippingRect, orderInfo, flexState);
panel.visible = false;
return panel;
}
export function setupInteractionPanel(panel, rootContext, globalMatrix, size, abortSignal) {
setupBoundingSphere(panel.boundingSphere, rootContext.pixelSize, globalMatrix, size, abortSignal);
abortableEffect(() => {
if (size.value == null) {
return;
}
const [width, height] = size.value;
const pixelSize = rootContext.pixelSize.value;
panel.scale.set(width * pixelSize, height * pixelSize, 1);
panel.updateMatrix();
}, abortSignal);
}
export class InstancedPanelMesh extends Mesh {
instanceMatrix;
count = 0;
isInstancedMesh = true;
instanceColor = null;
morphTexture = null;
boundingBox = new Box3();
boundingSphere = new Sphere();
constructor(instanceMatrix, instanceData, instanceClipping) {
const panelGeometry = createPanelGeometry();
super(panelGeometry);
this.instanceMatrix = instanceMatrix;
this.frustumCulled = false;
panelGeometry.attributes.aData = instanceData;
panelGeometry.attributes.aClipping = instanceClipping;
this.customDepthMaterial = instancedPanelDepthMaterial;
this.customDistanceMaterial = instancedPanelDistanceMaterial;
}
dispose() {
this.dispatchEvent({ type: 'dispose' });
this.geometry.dispose();
}
copy() {
throw new Error('copy not implemented');
}
//functions not needed because intersection (and morphing) is intenionally disabled
computeBoundingBox() { }
computeBoundingSphere() { }
updateMorphTargets() { }
raycast() { }
spherecast() { }
}