@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.
3 lines (2 loc) • 12.3 kB
JavaScript
import{S as e,aw as n,h as t,d as r,B as i,F as o,ai as a,e as s,j as u}from"./index-DFZFpUG-.esm.min.js";function c(e,n={}){const t=n.vertexDataComponents??0,r=n.exposeVertexData??!1,i=n.detailed??!1,o=n._vertexProjection??null,a=`\n${i?"enable primitive_index;":""}\n\nstruct SceneUniforms {\nviewProjection: mat4x4f,\nfragmentCoord: vec2f,\n};\n@group(0) @binding(0) var<uniform> scene: SceneUniforms;\n\n${function(e){return`\nstruct PickDiscardInput {\nworldPos: vec3f,\nfragmentCoord: vec2f,\npickId: u32,\nthinInstanceIndex: u32,\nhasThinInstance: u32,\ninstanceExtras: vec4f,\n${e?"vertexData: vec4f,":""}\n};\nstruct PickWorldInput {\nworldPos: vec3f,\nlocalPos: vec3f,\nbasis0: vec3f,\nbasis1: vec3f,\nbasis2: vec3f,\norigin: vec3f,\ninstanceExtras: vec4f,\nthinInstanceIndex: u32,\nhasThinInstance: u32,\nvertexData: vec4f,\n};\n`}(r)}\n${function(e){return e.storage?.length?e.storage.map((e,n)=>`@group(2) @binding(${n}) var<storage, read> ${e.name}: ${e.type};`).join("\n"):""}(n)}\n${n.discardWgsl??"\nfn shouldDiscardPick(input: PickDiscardInput) -> bool {\nreturn false;\n}\n"}\n${n.worldAdjustWgsl??"\nfn adjustPickWorld(input: PickWorldInput) -> vec3f {\nreturn input.worldPos;\n}\n"}\n${e?o?.thinDeclarations??"":o?.regularDeclarations??""}\n${function(e,n){return`\nstruct VsOut {\n@builtin(position) p: vec4f,\n@location(0) @interpolate(flat) pickId: u32,\n@location(1) worldPos: vec3f,\n@location(2) @interpolate(flat) thinInstanceIndex: u32,\n@location(3) @interpolate(flat) hasThinInstance: u32,\n@location(4) @interpolate(flat) instanceExtras: vec4f,\n${e?"@location(5) @interpolate(flat) vertexData: vec4f,":""}\n@location(6) @interpolate(flat) excluded: u32,\n${n?"@location(7) localPos: vec3f,":""}\n};\nstruct FsOut {\n@location(0) color: vec4f,\n@location(1) depth: f32,\n${n?"@location(2) detail: vec4u,":""}\n};\n@fragment fn fs(input: VsOut${n?", @builtin(primitive_index) primitiveIndex: u32":""}) -> FsOut {\nif (input.excluded != 0u) { discard; }\nif (shouldDiscardPick(PickDiscardInput(input.worldPos, scene.fragmentCoord, input.pickId, input.thinInstanceIndex, input.hasThinInstance, input.instanceExtras${e?", input.vertexData":""}))) { discard; }\nlet id = input.pickId;\nlet r = f32((id >> 16u) & 0xFFu) / 255.0;\nlet g = f32((id >> 8u) & 0xFFu) / 255.0;\nlet b = f32(id & 0xFFu) / 255.0;\nreturn FsOut(vec4f(r, g, b, 1.0), input.p.z${n?", vec4u(primitiveIndex, bitcast<u32>(input.localPos.x), bitcast<u32>(input.localPos.y), bitcast<u32>(input.localPos.z))":""});\n}\n`}(r,i)}\n`;return e?`\n${a}\nstruct TIMeshUniforms {\nworld: mat4x4f,\nbaseMeshPickId: u32,\nexcludedThinInstanceStart: u32,\nexcludedThinInstanceCount: u32,\n};\n@group(1) @binding(0) var<uniform> tiMesh: TIMeshUniforms;\n@group(1) @binding(1) var<storage, read> instances: array<mat4x4f>;\n@vertex fn vs(@location(0) position: vec3f${o?.thinInputs??""}, @builtin(instance_index) instanceIndex: u32) -> VsOut {\nlet packed = instances[instanceIndex];\nlet instanceWorld = mat4x4f(\nvec4f(packed[0].xyz, 0.0),\nvec4f(packed[1].xyz, 0.0),\nvec4f(packed[2].xyz, 0.0),\nvec4f(packed[3].xyz, 1.0),\n);\nlet world = tiMesh.world * instanceWorld;\nlet extras = vec4f(packed[0].w, packed[1].w, packed[2].w, packed[3].w);\nlet baseWorld = (world * vec4f(position, 1.0)).xyz;\n${o?.thinBody??"let projectedTransform = world;\nlet projectedWorld = baseWorld;"}\nlet wp = adjustPickWorld(PickWorldInput(projectedWorld, position, projectedTransform[0].xyz, projectedTransform[1].xyz, projectedTransform[2].xyz, projectedTransform[3].xyz, extras, instanceIndex, 1u, vec4f(0.0)));\nvar out: VsOut;\nout.p = scene.viewProjection * vec4f(wp, 1.0);\nout.pickId = tiMesh.baseMeshPickId + instanceIndex;\nout.worldPos = wp;\nout.thinInstanceIndex = instanceIndex;\nout.hasThinInstance = 1u;\nout.instanceExtras = extras;\nlet excludedOffset = instanceIndex - tiMesh.excludedThinInstanceStart;\nout.excluded = select(0u, 1u, tiMesh.excludedThinInstanceCount > 0u && instanceIndex >= tiMesh.excludedThinInstanceStart && excludedOffset < tiMesh.excludedThinInstanceCount);\n${i?"out.localPos = position;":""}\n${r?"out.vertexData = vec4f(0.0);":""}\nreturn out;\n}\n`:`\n${a}\nstruct MeshUniforms {\nworld: mat4x4f,\npickId: u32,\n};\n@group(1) @binding(0) var<uniform> mesh: MeshUniforms;\n@vertex fn vs(@location(0) position: vec3f${function(e){return 0===e?"":`, @location(5) vertexData: vec${e}f`}(t)}${o?.regularInputs??""}) -> VsOut {\nvar out: VsOut;\nlet vertexPayload = ${function(e){return 2===e?"vec4f(vertexData, 0.0, 0.0)":3===e?"vec4f(vertexData, 0.0)":4===e?"vertexData":"vec4f(0.0)"}(t)};\nlet baseWorld = (mesh.world * vec4f(position, 1.0)).xyz;\n${o?.regularBody??"let projectedTransform = mesh.world;\nlet projectedWorld = baseWorld;"}\nlet wp = adjustPickWorld(PickWorldInput(projectedWorld, position, projectedTransform[0].xyz, projectedTransform[1].xyz, projectedTransform[2].xyz, projectedTransform[3].xyz, vec4f(0.0), 0xffffffffu, 0u, vertexPayload));\nout.p = scene.viewProjection * vec4f(wp, 1.0);\nout.pickId = mesh.pickId;\nout.worldPos = wp;\nout.thinInstanceIndex = 0xffffffffu;\nout.hasThinInstance = 0u;\nout.instanceExtras = vec4f(0.0);\nout.excluded = 0u;\n${i?"out.localPos = position;":""}\n${r?"out.vertexData = vertexPayload;":""}\nreturn out;\n}\n`}let l=null,d=null,f=null,p=null,v=null,x=null,g=null;function h(e){l!==e._device&&(l=e._device,d=null,f=null,p=null,v=null,x=null,g=null)}function m(e){return h(e),p??=e._device.createBindGroupLayout({label:"picking-empty-bgl",entries:[]})}function b(e){if(!e)return"default";const n=e.storage?.map(e=>e.vertex?"v":"f").join("")??"none";return`rule:${e.key}:${e.vertexData??"none"}:${e.worldAdjustWgsl?"adjust":"identity"}:${n}`}function y(e){return{arrayStride:e?._stride??12,attributes:[{shaderLocation:0,offset:e?._offset??0,format:"float32x3"}]}}function I(r,i,o,a,s,u,l,p,v){const x=r._device.createShaderModule({label:`${s}-shader`,code:c(i,{discardWgsl:o?.wgsl,worldAdjustWgsl:o?.worldAdjustWgsl,storage:o?.storage,vertexDataComponents:l,exposeVertexData:!!o?.vertexData,detailed:p,_vertexProjection:v?.shader})}),g=i?function(n){return h(n),f||(f=n._device.createBindGroupLayout({label:"picking-ti-mesh-bgl",entries:[{binding:0,visibility:e.VERTEX|e.FRAGMENT,buffer:{type:"uniform"}},{binding:1,visibility:e.VERTEX,buffer:{type:"read-only-storage"}}]})),f}(r):function(n){return h(n),d??=t(n,"picking-mesh-bgl",e.VERTEX|e.FRAGMENT)}(r),b=v?[n(r),g,a??m(r),i?v.thinBGL:v.regularBGL]:a?[n(r),g,a]:[n(r),g],y=r._device.createPipelineLayout({label:`${s}-pipeline-layout`,bindGroupLayouts:b});return r._device.createRenderPipeline({label:`${s}-pipeline`,layout:y,vertex:{module:x,entryPoint:"vs",buffers:u},fragment:{module:x,entryPoint:"fs",targets:p?[{format:"rgba8unorm"},{format:"r32float"},{format:"rgba32uint"}]:[{format:"rgba8unorm"},{format:"r32float"}]},depthStencil:{format:"depth24plus",depthCompare:"greater",depthWriteEnabled:!0},primitive:{topology:"triangle-list",cullMode:"none"},multisample:{count:1}})}function $(n,t,r=!1,i=null){h(n);const o=r&&n._device.features.has("primitive-index"),a=`${b(t)}:${o?"detailed":"basic"}:${i?.key??"affine"}`,s=v??=new Map,u=s.get(a);if(u)return u;const c=t??null,l=c?function(n,t){return t.storage?.length?n._device.createBindGroupLayout({label:`picking-discard-${t.key}-bgl`,entries:t.storage.map((n,t)=>({binding:t,visibility:e.FRAGMENT|(n.vertex?e.VERTEX:0),buffer:{type:"read-only-storage"}}))}):null}(n,c):null,d={regularPipeline:I(n,!1,c,l,c?`picking-${c.key}-${i?.key??"affine"}`:`picking-${i?.key??"affine"}`,[y(),...i?.vertexBuffers??[]],0,o,i),thinInstancePipeline:I(n,!0,c,l,c?`picking-ti-${c.key}-${i?.key??"affine"}`:`picking-ti-${i?.key??"affine"}`,[y(),...i?.vertexBuffers??[]],0,o,i),discardBGL:l,detailed:o,_vertexProjection:i};return s.set(a,d),d}function P(e,n){const t=e._gpu;switch(n){case"normal":return{buffer:t.normalBuffer,interleave:t._vbLayout?._n};case"uv":return!1===t.hasUv?null:{buffer:t.uvBuffer,interleave:t._vbLayout?._u};case"uv2":return!1!==t.hasUv2&&t.uv2Buffer?{buffer:t.uv2Buffer,interleave:t._vbLayout?._u2}:null;case"tangent":return!1!==t.hasTangent&&t.tangentBuffer?{buffer:t.tangentBuffer,interleave:t._vbLayout?._t}:null;case"color":return!1!==t.hasColor&&t.colorBuffer?{buffer:t.colorBuffer,interleave:t._vbLayout?._c}:null}}function k(e,n,t,r,i){if(!r&&!i)return n.regularPipeline;h(e);const o=i?function(e,n){const t="normal"===e?3:"uv"===e||"uv2"===e?2:4;return{components:t,layout:{arrayStride:n?._stride??4*t,attributes:[{shaderLocation:5,offset:n?._offset??0,format:`float32x${t}`}]}}}(i.attribute,i.interleave):null,a=`${b(t)}:${n.detailed?"detailed":"basic"}:${n._vertexProjection?.key??"affine"}:p${r?._stride??12},${r?._offset??0}:d${i?`${i.attribute},${o.layout.arrayStride},${i.interleave?._offset??0}`:"none"}`,s=x??=new Map,u=s.get(a);if(u)return u;const c=`picking-vb-${a}`,l=I(e,!1,t,n.discardBGL,c,o?[y(r),o.layout,...n._vertexProjection?.vertexBuffers??[]]:[y(r),...n._vertexProjection?.vertexBuffers??[]],o?.components??0,n.detailed,n._vertexProjection);return s.set(a,l),l}function _(e,n,t,r){if(!r)return n.thinInstancePipeline;h(e);const i=`${b(t)}:${n.detailed?"detailed":"basic"}:${n._vertexProjection?.key??"affine"}:p${r._stride},${r._offset}`,o=g??=new Map,a=o.get(i);if(a)return a;const s=I(e,!0,t,n.discardBGL,`picking-ti-vb-${i}`,[y(r),...n._vertexProjection?.vertexBuffers??[]],0,n.detailed,n._vertexProjection);return o.set(i,s),s}const w=new ArrayBuffer(80),B=new o(w),j=new a(w),T=new u(w);function D(e){return e?.thinInstanceRange?[Math.max(0,0|e.thinInstanceRange.start),Math.max(0,0|e.thinInstanceRange.count)]:void 0===e?.thinInstanceIndex?[0,0]:[Math.max(0,0|e.thinInstanceIndex),1]}function G(e,n,t,o,a){if(!t.storage?.length)return null;const s=[];for(let n=0;n<t.storage.length;n++){const u=t.storage[n].data(o);if(!u)return null;const c=r(e,u,i.STORAGE,"pick-discard-storage");a.push(c),s.push({binding:n,resource:{buffer:c}})}return e._device.createBindGroup({layout:n,entries:s})}async function M(e,n){const t=n.some(e=>!!e.mesh.vat)?await import("./vat-picking-pipeline-NESiWpc7.esm.min.js"):null;return{draw(o,a,u,c,l,d,f,p,v,x){let g=u;const h=[];for(const u of n){const n=u.mesh,m=n._gpu,b=n.thinInstances,y=t?.getVatPickingProjection(e,n)??null,I=$(e,null,l,y),w=c?$(e,c,l,y):null,M=c&&w?.discardBGL?G(e,w.discardBGL,c,n,p):null,W=!w||w.discardBGL&&!M?I:w,L=W===w?c:null;let E,V=m.positionBuffer;if(d&&(n.morphTargets||n.skeleton)){const t=d.computeDeformedPositions(n);t&&(V=r(e,t,i.VERTEX,"pick-deformed-position"),p.push(V),E=t)}else l&&(E=n._cpuPositions);v&&E&&v.set(n,E),x&&n._cpuNormals&&x.set(n,n._cpuNormals);const z=f?f.copyDetailedWorldMatrix(n.worldMatrix):null;if(B.set(n.worldMatrix,0),j[16]=g,b){if(b.count<=0||!b._gpuBuffer)continue;const[r,i]=D(u.ignore);j[17]=r,j[18]=i;const c=s(e,T,"pick-thin-instance-ubo");p.push(c);const l=V===m.positionBuffer?m._vbLayout?._p:void 0,d=_(e,W,L,l);o.setPipeline(d),o.setBindGroup(0,a),o.setBindGroup(1,e._device.createBindGroup({layout:d.getBindGroupLayout(1),entries:[{binding:0,resource:{buffer:c}},{binding:1,resource:{buffer:b._gpuBuffer}}]})),M&&o.setBindGroup(2,M),o.setVertexBuffer(0,V),y&&t&&t.bindVatPickingProjection(e,o,d,n,!0,1),o.setIndexBuffer(m.indexBuffer,m.indexFormat),o.drawIndexed(m.indexCount,b.count),h.push({base:g,count:b.count,mesh:n,thin:!0,world:z,thinVersion:b._version,worldAdjusted:!!L?.worldAdjustWgsl||!!y}),g+=b.count;continue}const F=s(e,T,"pick-mesh-ubo");p.push(F);const A=L?.vertexData?P(n,L.vertexData):null,R=V===m.positionBuffer?m._vbLayout?._p:void 0,C=k(e,W,L,R,A&&L?.vertexData?{attribute:L.vertexData,interleave:A.interleave}:null);o.setPipeline(C),o.setBindGroup(0,a),o.setBindGroup(1,e._device.createBindGroup({layout:C.getBindGroupLayout(1),entries:[{binding:0,resource:{buffer:F}}]})),M&&o.setBindGroup(2,M),o.setVertexBuffer(0,V),A&&o.setVertexBuffer(1,A.buffer),y&&t&&t.bindVatPickingProjection(e,o,C,n,!1,A?2:1),o.setIndexBuffer(m.indexBuffer,m.indexFormat),o.drawIndexed(m.indexCount),h.push({base:g,count:1,mesh:n,thin:!1,world:z,thinVersion:0,worldAdjusted:!!L?.worldAdjustWgsl||!!y}),g++}return{nextId:g,ranges:h}}}}export{M as prepareAdvancedDraw};
//# sourceMappingURL=picking-advanced-draw-D_xa9wm1.esm.min.js.map