UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

147 lines (146 loc) 6.33 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { DebugGraphics } from "../../platform/graphics/debug-graphics.js"; import { BlendState } from "../../platform/graphics/blend-state.js"; import { RenderPass } from "../../platform/graphics/render-pass.js"; import { UNIFORMTYPE_MAT4 } from "../../platform/graphics/constants.js"; import { UniformBufferFormat, UniformFormat } from "../../platform/graphics/uniform-buffer-format.js"; import { SHADER_PICK, SHADER_DEPTH_PICK } from "../../scene/constants.js"; const tempMeshInstances = []; const lights = [[], [], []]; class RenderPassPicker extends RenderPass { constructor(device, renderer) { super(device); /** @type {BlendState} */ __publicField(this, "blendState", BlendState.NOBLEND); /** @type {CameraComponent} */ __publicField(this, "camera"); /** @type {Scene} */ __publicField(this, "scene"); /** @type {Layer[]} */ __publicField(this, "layers"); /** @type {Map<number, MeshInstance | GSplatComponent>} */ __publicField(this, "mapping"); /** @type {boolean} */ __publicField(this, "depth"); /** @type {number[]} */ __publicField(this, "_qualifiedLayerIndices", []); /** @type {Map<number, MeshInstance|null>} */ __publicField(this, "_pickMeshInstances", /* @__PURE__ */ new Map()); /** * Minimal view uniform format used by the picker. The pick shaders only need the view * projection (and the view matrix for depth picking); any other view uniform a shader happens * to reference falls back to the per-mesh uniform buffer automatically. This avoids pulling in * the full forward view format (and its lighting / shadow uniforms) which the picker does not * need. * * @type {UniformBufferFormat|null} */ __publicField(this, "_viewUniformFormat", null); this.renderer = renderer; } getViewUniformFormat() { if (!this._viewUniformFormat) { this._viewUniformFormat = new UniformBufferFormat(this.device, [ new UniformFormat("matrix_viewProjection", UNIFORMTYPE_MAT4), new UniformFormat("matrix_view", UNIFORMTYPE_MAT4) ]); } return this._viewUniformFormat; } /** * @param {CameraComponent} camera - The camera component used for picking. * @param {Scene} scene - The scene to pick from. * @param {Layer[]} layers - The layers to pick from. * @param {Map<number, MeshInstance | GSplatComponent>} mapping - Map to store ID to object mappings. * @param {boolean} depth - Whether to render depth information. */ update(camera, scene, layers, mapping, depth) { this.camera = camera; this.scene = scene; this.layers = layers; this.mapping = mapping; this.depth = depth; if (scene.clusteredLightingEnabled) { this.emptyWorldClusters = this.renderer.worldClustersAllocator.empty; } } // Filter qualifying layers and prepare gsplat pick mesh instances for the compute-based // renderer. The execute() loop iterates the pre-built list instead of re-filtering. before() { this._qualifiedLayerIndices.length = 0; this._pickMeshInstances.clear(); const { camera, scene, layers, renderer } = this; const srcLayers = scene.layers.layerList; const gsplatDirector = renderer.gsplatDirector; const pickerWidth = this.renderTarget?.width ?? 1; const pickerHeight = this.renderTarget?.height ?? 1; for (let i = 0; i < srcLayers.length; i++) { const srcLayer = srcLayers[i]; if (layers && layers.indexOf(srcLayer) < 0) continue; if (!scene.layers.isSubLayerRenderedByCamera(i, camera.camera)) continue; this._qualifiedLayerIndices.push(i); renderer.culler.requestMeshInstanceCull(camera.camera, srcLayer); if (gsplatDirector) { const pickMI = gsplatDirector.prepareForPicking(camera.camera, pickerWidth, pickerHeight, srcLayer); if (pickMI) { this._pickMeshInstances.set(i, pickMI); } } } renderer.culler.executeMeshInstanceCull(); } execute() { const device = this.device; const { renderer, camera, scene, mapping, renderTarget } = this; const srcLayers = scene.layers.layerList; const isTransparent = scene.layers.subLayerList; for (const i of this._qualifiedLayerIndices) { const srcLayer = srcLayers[i]; const transparent = isTransparent[i]; DebugGraphics.pushGpuMarker(device, `${srcLayer.name}(${transparent ? "TRANSP" : "OPAQUE"})`); if (srcLayer._clearDepthBuffer) { renderer.clear(camera.camera, false, true, false); } const culledInstances = srcLayer.getCulledInstances(camera.camera); const meshInstances = transparent ? culledInstances.transparent : culledInstances.opaque; for (let j = 0; j < meshInstances.length; j++) { const meshInstance = meshInstances[j]; if (meshInstance.pick) { tempMeshInstances.push(meshInstance); mapping.set(meshInstance.id, meshInstance); } } const pickMI = this._pickMeshInstances.get(i); if (pickMI) { tempMeshInstances.push(pickMI); } if (scene.gsplat.enableIds) { const placements = srcLayer.gsplatPlacements; for (let j = 0; j < placements.length; j++) { const placement = placements[j]; const component = placement.node?.gsplat; if (component) { mapping.set(placement.id, component); } } } if (tempMeshInstances.length > 0) { const shaderPass = this.depth ? SHADER_DEPTH_PICK : SHADER_PICK; renderer.renderForwardLayer(camera.camera, renderTarget, null, void 0, shaderPass, { meshInstances: tempMeshInstances, splitLights: lights, lightClusters: this.emptyWorldClusters, viewUniformFormat: this.getViewUniformFormat(), drawCallback: () => device.setBlendState(this.blendState) }); tempMeshInstances.length = 0; } DebugGraphics.popGpuMarker(device); } } } export { RenderPassPicker };