UNPKG

@babylonjs/viewer

Version:

The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.

89 lines (87 loc) 4.2 kB
import { i as TU, k as getOrCreateSampler, S as SS } from './index-By0tcgYN.esm.js'; import { _ as _installDepthGrab } from './pbr-transmission-ext-BTqVnS2g.esm.js'; import './generate-mipmaps-BL7R_dXN.esm.js'; const DEPTH_GRAB_SHADER = `@group(0)@binding(0)var t:texture_depth_2d;struct V{@builtin(position)p:vec4f};@vertex fn vs(@builtin(vertex_index)i:u32)->V{let p=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i];return V(vec4f(p,0,1));}@fragment fn fs(@builtin(position)fc:vec4f)->@location(0)f32{return textureLoad(t,vec2<i32>(fc.xy),0);}`; const DEPTH_GRAB_MSAA_SHADER = `@group(0)@binding(0)var t:texture_depth_multisampled_2d;struct V{@builtin(position)p:vec4f};@vertex fn vs(@builtin(vertex_index)i:u32)->V{let p=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i];return V(vec4f(p,0,1));}@fragment fn fs(@builtin(position)fc:vec4f)->@location(0)f32{return textureLoad(t,vec2<i32>(fc.xy),0);}`; let depthGrabPipelines = null; let depthGrabShader = null; let depthGrabMsaaShader = null; let depthGrabBgl = null; let depthGrabMsaaBgl = null; let depthGrabDevice = null; function getDepthGrabPipeline(engine, multisampled) { const device = engine._device; if (device !== depthGrabDevice) { depthGrabPipelines?.clear(); depthGrabPipelines = null; depthGrabShader = null; depthGrabMsaaShader = null; depthGrabBgl = null; depthGrabMsaaBgl = null; depthGrabDevice = device; } if (multisampled) { depthGrabMsaaShader ??= device.createShaderModule({ code: DEPTH_GRAB_MSAA_SHADER }); depthGrabMsaaBgl ??= device.createBindGroupLayout({ entries: [{ binding: 0, visibility: SS.FRAGMENT, texture: { sampleType: "depth", multisampled: true } }] }); } else { depthGrabShader ??= device.createShaderModule({ code: DEPTH_GRAB_SHADER }); depthGrabBgl ??= device.createBindGroupLayout({ entries: [{ binding: 0, visibility: SS.FRAGMENT, texture: { sampleType: "depth" } }] }); } depthGrabPipelines ??= /* @__PURE__ */ new Map(); const key = multisampled ? "msaa" : "1x"; let pipeline = depthGrabPipelines.get(key); if (!pipeline) { const shader = multisampled ? depthGrabMsaaShader : depthGrabShader; pipeline = device.createRenderPipeline({ label: "transmission-depth-grab", layout: device.createPipelineLayout({ bindGroupLayouts: [multisampled ? depthGrabMsaaBgl : depthGrabBgl] }), vertex: { module: shader, entryPoint: "vs" }, fragment: { module: shader, entryPoint: "fs", targets: [{ format: "r32float" }] }, primitive: { topology: "triangle-list" } }); depthGrabPipelines.set(key, pipeline); } return pipeline; } function createDepthGrab(engine, source, width, height, multisampled) { const device = engine._device; const texture = device.createTexture({ label: "transmission-depth-grab", size: { width, height }, format: "r32float", usage: TU.RENDER_ATTACHMENT | TU.TEXTURE_BINDING }); const view = texture.createView(); const tex = { texture, view, sampler: getOrCreateSampler(engine, { magFilter: "nearest", minFilter: "nearest", addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge" }), width, height, invertY: false, _sampleType: "float" // r32float samples as unfilterable float (read with textureLoad) }; const pipeline = getDepthGrabPipeline(engine, multisampled); const bindGroup = device.createBindGroup({ layout: multisampled ? depthGrabMsaaBgl : depthGrabBgl, entries: [{ binding: 0, resource: source.createView({ aspect: "depth-only" }) }] }); return { texture: tex, _view: view, _blit: { _pipeline: pipeline, _bindGroup: bindGroup } }; } function recordDepthGrab(engine, depth) { const pass = engine._currentEncoder.beginRenderPass({ label: "transmission-depth-grab", colorAttachments: [{ view: depth._view, loadOp: "clear", storeOp: "store", clearValue: { r: 0, g: 0, b: 0, a: 0 } }] }); pass.setPipeline(depth._blit._pipeline); pass.setBindGroup(0, depth._blit._bindGroup); pass.draw(3); pass.end(); } _installDepthGrab({ create: createDepthGrab, record: recordDepthGrab }); //# sourceMappingURL=transmission-depth-grab-D7CMQ5g6.esm.js.map