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.

3 lines (2 loc) 12.3 kB
import{n as e,aD as n,m as t,F as r,ap as i,j as o,p as a,f as s,B as u}from"./index-B8IgtevW.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 m(e){l!==e._device&&(l=e._device,d=null,f=null,p=null,v=null,x=null,g=null)}function h(e){return m(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 m(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 m(n),d??=t(n,"picking-mesh-bgl",e.VERTEX|e.FRAGMENT)}(r),b=v?[n(r),g,a??h(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 P(n,t,r=!1,i=null){m(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 k(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 $(e,n,t,r,i){if(!r&&!i)return n.regularPipeline;m(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;m(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),j=new r(w),B=new i(w),T=new a(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,r,i){if(!t.storage?.length)return null;const o=[];for(let n=0;n<t.storage.length;n++){const a=t.storage[n].data(r);if(!a)return null;const c=s(e,a,u.STORAGE,"pick-discard-storage");i.push(c),o.push({binding:n,resource:{buffer:c}})}return e._device.createBindGroup({layout:n,entries:o})}async function M(e,n){const t=n.some(e=>!!e.mesh.vat)?await import("./vat-picking-pipeline-DkZIEtSp.esm.min.js"):null,r=n.some(e=>!e.mesh.vat&&(e.mesh.morphTargets||e.mesh.skeleton))?await import("./deform-picking-projection-BQbR83dT.esm.min.js"):null;return{draw(i,a,s,u,c,l,d,f,p){let v=s;const x=[];for(const s of n){const n=s.mesh,g=n._gpu,m=n.thinInstances,h=t?.getVatPickingProjection(e,n)??null,b=h??r?.getDeformPickingProjection(e,n)??null,y=P(e,null,c,b),I=u?P(e,u,c,b):null,w=u&&I?.discardBGL?G(e,I.discardBGL,u,n,d):null,M=!I||I.discardBGL&&!w?y:I,W=M===I?u:null,L=g.positionBuffer;f&&n._cpuPositions&&f.set(n,n._cpuPositions),p&&n._cpuNormals&&p.set(n,n._cpuNormals);const V=l?l.copyDetailedWorldMatrix(n.worldMatrix):null;if(j.set(n.worldMatrix,0),B[16]=v,m){if(m.count<=0||!m._gpuBuffer)continue;const[u,c]=D(s.ignore);B[17]=u,B[18]=c;const l=o(e,T,"pick-thin-instance-ubo");d.push(l);const f=g._vbLayout?._p,p=_(e,M,W,f);i.setPipeline(p),i.setBindGroup(0,a),i.setBindGroup(1,e._device.createBindGroup({layout:p.getBindGroupLayout(1),entries:[{binding:0,resource:{buffer:l}},{binding:1,resource:{buffer:m._gpuBuffer}}]})),w&&i.setBindGroup(2,w),i.setVertexBuffer(0,L),h&&t?t.bindVatPickingProjection(e,i,p,n,!0,1):b&&r&&r.bindDeformPickingProjection(e,i,p,n,1,!!w),i.setIndexBuffer(g.indexBuffer,g.indexFormat),i.drawIndexed(g.indexCount,m.count),x.push({base:v,count:m.count,mesh:n,thin:!0,world:V,thinVersion:m._version,worldAdjusted:!!W?.worldAdjustWgsl||!!h}),v+=m.count;continue}const E=o(e,T,"pick-mesh-ubo");d.push(E);const z=W?.vertexData?k(n,W.vertexData):null,F=g._vbLayout?._p,A=$(e,M,W,F,z&&W?.vertexData?{attribute:W.vertexData,interleave:z.interleave}:null);i.setPipeline(A),i.setBindGroup(0,a),i.setBindGroup(1,e._device.createBindGroup({layout:A.getBindGroupLayout(1),entries:[{binding:0,resource:{buffer:E}}]})),w&&i.setBindGroup(2,w),i.setVertexBuffer(0,L),z&&i.setVertexBuffer(1,z.buffer),h&&t?t.bindVatPickingProjection(e,i,A,n,!1,z?2:1):b&&r&&r.bindDeformPickingProjection(e,i,A,n,z?2:1,!!w),i.setIndexBuffer(g.indexBuffer,g.indexFormat),i.drawIndexed(g.indexCount),x.push({base:v,count:1,mesh:n,thin:!1,world:V,thinVersion:0,worldAdjusted:!!W?.worldAdjustWgsl||!!h}),v++}return{nextId:v,ranges:x}}}}export{M as prepareAdvancedDraw}; //# sourceMappingURL=picking-advanced-draw-DTRGKtrD.esm.min.js.map