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) 16.7 kB
import{F as e,B as t,S as r,k as n,b1 as i,i as a,b2 as s,as as o,b3 as c,b4 as u,aa as l,T as p,aD as f}from"./index-DFZFpUG-.esm.min.js";import{recordMipmaps as m}from"./generate-mipmaps-Ciyl8G8r.esm.min.js";function d(n,i,a){let s=null;return{name:n.name??"image-processing",engine:i,scene:a,_passes:[],record(){_(s),s=function(e,n){const i=function(e){const t="function"==typeof e?e():e;if(!t)return null;if("_colorTexture"in t)return t._colorTexture;return t.texture}(n);if(!i)throw new Error("Image processing source has no color texture");const a=e._device,s=(i.sampleCount??1)>1,o=a.createBuffer({size:16,usage:t.UNIFORM|t.COPY_DST}),c=a.createBindGroupLayout({entries:[{binding:0,visibility:r.FRAGMENT,buffer:{type:"uniform"}},{binding:1,visibility:r.FRAGMENT,texture:{sampleType:s?"unfilterable-float":"float",multisampled:s}}]}),u="struct P{e:f32,c:f32,t:f32,p:f32}\n@group(0)@binding(0)var<uniform> p:P;\n@vertex fn vs(@builtin(vertex_index)i:u32)->@builtin(position) vec4f{var a=array<vec2f,3>(vec2f(-1,-3),vec2f(3,1),vec2f(-1,1));return vec4f(a[i],0,1);}\nfn ip(r:vec4f)->vec4f{var c=r.rgb*p.e;\nif(p.t>0.5){c=1.0-exp2(-1.590579*c);}\nc=clamp(pow(max(c,vec3f(0)),vec3f(1/2.2)),vec3f(0),vec3f(1));\nlet h=c*c*(3.0-2.0*c);\nif(p.c<1.0){c=mix(vec3f(0.5),c,p.c);}else{c=mix(c,h,p.c-1.0);}\nreturn vec4f(max(c,vec3f(0)),r.a);}",l=s?"@group(0)@binding(1)var s:texture_multisampled_2d<f32>;":"@group(0)@binding(1)var s:texture_2d<f32>;",p=s?"@fragment fn fs(@builtin(position) q:vec4f)->@location(0) vec4f{let d=textureDimensions(s);let px=clamp(vec2i(q.xy),vec2i(0),vec2i(d)-1);let n=textureNumSamples(s);var c=vec4f(0);for(var i=0u;i<n;i++){c+=ip(textureLoad(s,px,i));}return c/f32(n);}":"@fragment fn fs(@builtin(position) q:vec4f)->@location(0) vec4f{let d=textureDimensions(s);return ip(textureLoad(s,clamp(vec2i(q.xy),vec2i(0),vec2i(d)-1),0));}",f=a.createShaderModule({code:`${u}${l}${p}`}),m=a.createRenderPipeline({layout:a.createPipelineLayout({bindGroupLayouts:[c]}),vertex:{module:f,entryPoint:"vs"},fragment:{module:f,entryPoint:"fs",targets:[{format:e.format}]},primitive:{topology:"triangle-list"}}),d=a.createBindGroup({layout:c,entries:[{binding:0,resource:{buffer:o}},{binding:1,resource:i.createView()}]});return{pipeline:m,bindGroup:d,params:o}}(i,n.source)},execute(){if(!s)return 0;const t=a.imageProcessing,r=new e([t.exposure,t.contrast,!0===t.toneMappingEnabled?1:0,0]);i._device.queue.writeBuffer(s.params,0,r);const n=i._currentEncoder.beginRenderPass({colorAttachments:[{view:i.scRT._colorView,loadOp:"clear",storeOp:"store",clearValue:a.clearColor}]});return n.setPipeline(s.pipeline),n.setBindGroup(0,s.bindGroup),n.draw(3),n.end(),1},dispose(){_(s),s=null,this._passes.length=0}}}function _(e){e?.params.destroy()}const v={magFilter:"linear",minFilter:"linear",mipmapFilter:"linear",addressModeU:"repeat",addressModeV:"repeat",addressModeW:"repeat",maxAnisotropy:4};function h(e){return n(e,v)}let g=null;function x(e){g=e}const b="@group(0)@binding(0)var t:texture_2d<f32>;@group(0)@binding(1)var s:sampler;struct V{@builtin(position)p:vec4f,@location(0)u:vec2f};@vertex fn vs(@builtin(vertex_index)i:u32)->V{var p=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));var u=array<vec2f,3>(vec2f(0,1),vec2f(2,1),vec2f(0,-1));return V(vec4f(p[i],0,1),u[i]);}@fragment fn fs(v:V)->@location(0)vec4f{return textureSample(t,s,v.u);}",y="@group(0)@binding(0)var t:texture_multisampled_2d<f32>;struct V{@builtin(position)p:vec4f,@location(0)u:vec2f};@vertex fn vs(@builtin(vertex_index)i:u32)->V{var p=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));var u=array<vec2f,3>(vec2f(0,1),vec2f(2,1),vec2f(0,-1));return V(vec4f(p[i],0,1),u[i]);}fn l(p:vec2i)->vec4f{let n=textureNumSamples(t);var c=vec4f(0);for(var i=0u;i<n;i++){c+=textureLoad(t,p,i);}return c/f32(n);}@fragment fn fs(v:V)->@location(0)vec4f{let d=vec2i(textureDimensions(t));let q=clamp(v.u*vec2f(d)-.5,vec2f(0),vec2f(d-vec2i(1)));let p=vec2i(floor(q));let f=fract(q);let p1=min(p+vec2i(1),d-vec2i(1));return mix(mix(l(p),l(vec2i(p1.x,p.y)),f.x),mix(l(vec2i(p.x,p1.y)),l(p1),f.x),f.y);}";let T=null,M=null,w=null,V=null,P=null,S=null;function k(e,t){let r=null;for(const n of e._frameGraph._tasks)if("_renderables"in n){const e=n;G(e,t),r=e}r&&!e._frameGraph._tasks.some(e=>"transmission-image-processing"===e.name)&&e._frameGraph._tasks.push(d({name:"transmission-image-processing",source:r._config.rt},t,e))}function G(e,t,n){if(e._config.transmission?.grabDepth){const t=e._preload?.bind(e);e._preload=async()=>{t&&await t(),await import("./transmission-depth-grab-2hhJB7Pj.esm.min.js")}}const l={get texture(){return e._targetSignature._transmissionTexture??null},get depthTexture(){return e._targetSignature._transmissionDepthTexture??null}};if(e._executeWithTransmission)return l;!function(e){const t=e._config,r=t.rt._descriptor,n=e.scene.surface,a=n.msaaSamples,s=!t.depth,o=i({lbl:"transmission-linear",format:"rgba16float",dFormat:s?r.dFormat??"depth24plus-stencil8":void 0,_depthClearValue:r._depthClearValue,_depthCompare:r._depthCompare,samples:a,size:n});t.rt=o,t.rst=void 0;const c=e._targetSignature;c._colorFormat="rgba16float",c._depthStencilFormat=t.depth?._descriptor.dFormat??o._descriptor.dFormat,c._depthCompare=o._descriptor._depthCompare,c._sampleCount=a,e._opaqueBundles.length=0,e._lastVersion=-1}(e);let p=null;const f=e.record.bind(e),m=e.execute?.bind(e),d=e.dispose?.bind(e);return e.record=()=>{F(p),p=function(e,t){const r=e._config.rt,n=1024,i=1024,s="rgba16float",o=function(e,t,r){if(!1===e?.generateMipmaps)return 1;const n=Math.floor(Math.log2(Math.max(t,r)))+1,i=u(t,r,4),a=e?.mipLevelCount;if(void 0===a)return Math.min(n,i);return Math.min(n,Math.max(1,0|a))}(e._config.transmission,n,i),c=o>1,l=t._device.createTexture({label:e.name,size:{width:n,height:i},format:s,mipLevelCount:o,usage:a.RENDER_ATTACHMENT|a.TEXTURE_BINDING|a.COPY_DST}),p={texture:l,view:l.createView(),sampler:h(t),width:n,height:i,invertY:!1};return{texture:p,_baseView:l.createView({baseMipLevel:0,mipLevelCount:1}),_sourceWidth:r._width,_sourceHeight:r._height,_sourceTexture:null,_blit:null,_depth:null,_copyCount:L(e._config.transmission),_generateMipmaps:c,_copies:0}}(e,t),e._targetSignature._transmissionTexture=p.texture,f(),function(e,t,n){const i=t._config.rt;e._sourceWidth=i._width,e._sourceHeight=i._height,e._sourceTexture=i._colorTexture;const a=t._targetSignature._sampleCount,s=t._targetSignature;s._transmissionDepthTexture=null;const c=i._depthTexture;t._config.transmission?.grabDepth&&g&&c&&i._width>0&&i._height>0&&(e._depth=g.create(n,c,i._width,i._height,a>1),s._transmissionDepthTexture=e._depth.texture);if(!e._sourceTexture)return;e._blit=function(e,t){return t>1||e._sourceWidth!==e.texture.width||e._sourceHeight!==e.texture.height}(e,a)?function(e,t,n,i){const a=t._device,s=function(e,t,n){const i=e._device;i!==S&&(T?.clear(),T=null,M=null,w=null,V=null,P=null,S=i);n?(w??=i.createShaderModule({code:y}),P??=i.createBindGroupLayout({entries:[{binding:0,visibility:r.FRAGMENT,texture:{sampleType:"unfilterable-float",multisampled:!0}}]})):(M??=i.createShaderModule({code:b}),V??=i.createBindGroupLayout({entries:[{binding:0,visibility:r.FRAGMENT,texture:{sampleType:"float"}},{binding:1,visibility:r.FRAGMENT,sampler:{}}]}));T??=new Map;const a=`${t}:${n?"msaa":""}`;let s=T.get(a);if(!s){const e=n?P:V;s=i.createRenderPipeline({label:"transmission-copy",layout:i.createPipelineLayout({bindGroupLayouts:[e]}),vertex:{module:n?w:M,entryPoint:"vs"},fragment:{module:n?w:M,entryPoint:"fs",targets:[{format:t}]},primitive:{topology:"triangle-list"}}),T.set(a,s)}return s}(t,e.texture.texture.format,i),c=a.createBindGroup({layout:i?P:V,entries:i?[{binding:0,resource:n.createView()}]:[{binding:0,resource:n.createView()},{binding:1,resource:o(t)}]});return{_pipeline:s,_bindGroup:c}}(e,n,e._sourceTexture,a>1):null}(p,e,t)},m&&(e.execute=()=>function(e,t){const r=e.imageProcessing,n=r.toneMappingEnabled,i=e.clearColor,a=(s=i,o=r.exposure,c=r.contrast,u=!0===n,{r:C(s.r,o,c,u),g:C(s.g,o,c,u),b:C(s.b,o,c,u),a:s.a});var s,o,c,u;r.toneMappingEnabled=-1,e.clearColor=a;try{return t()}finally{e.clearColor=i,r.toneMappingEnabled=n}}(e.scene,m)),e.dispose=()=>{F(p),p=null,d?.()},e._executeWithTransmission=r=>function(e,t,r,n){r._copies=0;const i=e._transparentBindings,a=n>1?e._config.rst?._colorView??null:null;let o=U(e,a,n,!1),u=function(e,t){const r=e.engine,n=e._config.rt,i=e.scene,a=e._opaqueBindings,o=e._opaqueBundles;if(A(e,t),e._lastVersion!==i._renderableVersion||e._lastVis!==c||0===o.length){const t=n._descriptor,u=r._device.createRenderBundleEncoder({colorFormats:t.format?[t.format]:[],depthStencilFormat:t.dFormat,sampleCount:t.samples??1});u.setBindGroup(0,e._sceneBG),s(u,a,r),o[0]=u.finish(),e._lastVersion=i._renderableVersion,e._lastVis=c}let u=a.length;return t.executeBundles(o),t.setBindGroup(0,e._sceneBG),u+=s(t,e._directBindings,r),u}(e,o),l=null,p=null;for(let s=0;s<i.length;s++){const c=i[s];if(!0===c.renderable.mesh?.renderOnTop){(p??=[]).push(c);continue}!0===c.renderable._transmissive&&R(r)&&(o.end(),D(r,t),o=U(e,a,n,!0),A(e,o),l=null);const f=c.renderable.mesh;f&&!1===f.visible||(c.pipeline!==l&&(o.setPipeline(c.pipeline),l=c.pipeline),u+=c.draw(o,t))}p&&(u+=s(o,p,t));return o.end(),u}(e,t,p,r),l}function C(e,t,r,n){let i=B(e);if(r<1)i=r>0?B((i-.5*(1-r))/r):.5;else if(r>1){const e=r-1;let t=0,n=1;for(let r=0;r<16;r++){const r=.5*(t+n);r+(r*r*(3-2*r)-r)*e<i?t=r:n=r}i=.5*(t+n)}return i=i**2.2,n&&(i=-Math.log2(Math.max(1-i,1e-6))/1.5905790328979492),t>0?i/t:i}function B(e){return Math.min(Math.max(e,0),1)}function E(e){for(const t of e.meshes){const e=t.material;e&&(e._linearImageProcessing=!0,e._renderFeatures=void 0)}}function F(e){e?.texture.texture.destroy(),e?._depth?.texture.texture.destroy()}function D(e,t){if(!e._sourceTexture)throw new Error("No transmission source");e._blit?function(e,t){const r=e._blit,n=t._currentEncoder.beginRenderPass({colorAttachments:[{view:e._baseView,loadOp:"clear",storeOp:"store",clearValue:{r:0,g:0,b:0,a:0}}]});n.setPipeline(r._pipeline),n.setBindGroup(0,r._bindGroup),n.draw(3),n.end()}(e,t):t._currentEncoder.copyTextureToTexture({texture:e._sourceTexture},{texture:e.texture.texture},{width:e.texture.width,height:e.texture.height}),e._generateMipmaps&&m(t,e.texture.texture,t._currentEncoder),e._depth&&g?.record(t,e._depth),e._copies++}function R(e){return 0===e._copyCount||e._copies<e._copyCount}function U(e,t,r,n){const i=e._colorAttachment,a=n||!e._config.clr?"load":"clear";n&&(i.loadOp="load");const s=e._renderPassDescriptor.depthStencilAttachment;return s&&(s.depthLoadOp=a,s.stencilLoadOp&&(s.stencilLoadOp=a)),i.resolveTarget=r>1?t??void 0:void 0,e.engine._currentEncoder.beginRenderPass(e._renderPassDescriptor)}function A(e,t){const r=e._config,n=r.rt,i=e.scene,a=r.cam??i.camera,s=a?.viewport;if(s){const e=n._width,r=n._height,i=Math.floor(s.x*e),a=Math.floor((1-s.y-s.height)*r),o=Math.ceil((s.x+s.width)*e)-i,c=Math.ceil((1-s.y)*r)-a;t.setViewport(i,a,o,c,0,1),t.setScissorRect(i,a,o,c)}t.setBindGroup(0,e._sceneBG)}function L(e){const t=e?.copyCount??1;return t===1/0?0:Math.max(0,0|t)}const N=1<<20,O={NI:"if(scene.vImageInfos.w>=0.0){",BC:"}"};function $(e,t,r,n,i,a){return`{\n${e||r?"let ts=max(length(mesh.world[0].xyz),max(length(mesh.world[1].xyz),length(mesh.world[2].xyz)));":""}\n${t?"let refractionMapUV=vec2<f32>(dot(material.refractionMapUVm.xy,input.uv),dot(material.refractionMapUVm.zw,input.uv))+material.refractionMapUVt.xy;\n":""}${r?"let thicknessUV=vec2<f32>(dot(material.thicknessUVm.xy,input.uv),dot(material.thicknessUVm.zw,input.uv))+material.thicknessUVt.xy;\n":""}${t?"let ri=material.refractionParams.x*textureSample(refractionMapTexture,refractionMapSampler,refractionMapUV).r;":"let ri=material.refractionParams.x;"}\n${r?`let ths=textureSample(thicknessTexture_,thicknessSampler_,thicknessUV).${n?"g":"r"};\nlet th=(material.thicknessParams.x+ths*material.thicknessParams.y)*ts;`:e?"let th=material.refractionParams.z*ts;":"let th=material.refractionParams.z;"}\nlet ro=1.0-ri;\nlet ra=mix(alphaG,0.0,clamp(material.refractionParams.w*3.0-2.0,0.0,1.0));\nlet lv=clamp(log2(f32(textureDimensions(refractionTexture).x)*ra)-4.0,0.0,f32(textureNumLevels(refractionTexture)-1));\n${i&&a?a:"let rd=refract(-V,N,material.refractionParams.y);\nlet cp=scene.viewProjection*vec4<f32>(input.worldPos+rd*th,1.0);\nlet ruv=(cp.xy/cp.w)*vec2<f32>(0.5,-0.5)+vec2<f32>(0.5,0.5);\nlet er=textureSampleLevel(refractionTexture,refractionSampler_,ruv,lv).rgb*material.environmentIntensity;"}\n${e?"let ab=exp(material.volumeParams.rgb*th);":""}\n${e?"let fr=er*surfaceAlbedo*(ri*ab)*(vec3<f32>(1.0)-colorSpecularEnvReflectance.rgb);":"let fr=er*surfaceAlbedo*ri*(vec3<f32>(1.0)-colorSpecularEnvReflectance.rgb);"}\ncolor=finalIrradiance*ro*ro+finalRadianceScaled+finalSpecularScaled+directDiffuse*ro*ro+fr+emissive;\n}`}function I(e,t,r,n){const i=t.get(`${r}m`),a=t.get(`${r}t`);if(void 0===i||void 0===a)return;const s=i/4,o=a/4,c=n?.uScale??1,u=n?.vScale??1,l=n?.uAng??0;if(0===l)e[s]=c,e[s+1]=0,e[s+2]=0,e[s+3]=u;else{const t=Math.cos(l),r=Math.sin(l);e[s]=t*c,e[s+1]=r*u,e[s+2]=-r*c,e[s+3]=t*u}e[o]=n?.uOffset??0,e[o+1]=n?.vOffset??0,e[o+2]=0,e[o+3]=0}function q(e){return{id:"refraction",phase:"fragment",detect(e){const t=e,r=t.subsurface,n=r?.refraction,i=t._linearImageProcessing?16384:0;if((t.transmissive?n?.intensity??0:0)<=0)return{f:0,f2:i};let a=0,s=i|l;return n?.texture&&(s|=64),r?.thickness?.texture&&(a|=f),r?.thickness?.useGlTFChannel&&(s|=128),void 0!==r?.tint?.atDistance&&(s|=32,n?.dispersion&&(s|=N)),{f:a,f2:s}},frag(t){const r=!!(16384&t._features2);return t._features2&l?function(e,t,r,n,i,a,s){const o=[{_name:"refractionParams",_type:"vec4<f32>"}];e&&o.push({_name:"volumeParams",_type:"vec4<f32>"}),r&&o.push({_name:"thicknessParams",_type:"vec4<f32>"}),t&&o.push({_name:"refractionMapUVm",_type:"vec4<f32>"},{_name:"refractionMapUVt",_type:"vec4<f32>"}),r&&o.push({_name:"thicknessUVm",_type:"vec4<f32>"},{_name:"thicknessUVt",_type:"vec4<f32>"});const c=[{_name:"refractionTexture",_type:{_kind:"texture",_textureType:"texture_2d<f32>"},_visibility:2},{_name:"refractionSampler_",_type:{_kind:"sampler",_samplerType:"sampler"},_visibility:2}];return t&&c.push({_name:"refractionMapTexture",_type:{_kind:"texture",_textureType:"texture_2d<f32>"},_visibility:2},{_name:"refractionMapSampler",_type:{_kind:"sampler",_samplerType:"sampler"},_visibility:2}),r&&c.push({_name:"thicknessTexture_",_type:{_kind:"texture",_textureType:"texture_2d<f32>"},_visibility:2},{_name:"thicknessSampler_",_type:{_kind:"sampler",_samplerType:"sampler"},_visibility:2}),{_id:"refraction",_dependencies:["ibl"],_uboFields:o,_bindings:c,_fragmentSlots:i?{AI:$(e,t,r,n,a,s),...O}:{AI:$(e,t,r,n,a,s)}}}(!!(32&t._features2),!!(64&t._features2),0!==(t._features&f),!!(128&t._features2),r,0!==(t._features2&N),e):r?{_id:"linear",_fragmentSlots:O}:null},writeUbo(e,t,r){!function(e,t,r){const n=t.subsurface,i=n?.refraction;if(!i)return;const a=r.get("refractionParams");if(void 0===a)return;const s=a/4;e[s]=i.intensity??0;const o=i.indexOfRefraction??1.5,c=n.thickness;e[s+1]=1/(i.useThicknessAsDepth&&c?.max?o:1),e[s+2]=i.useThicknessAsDepth?c?.max??0:0,e[s+3]=1/o;const u=r.get("volumeParams");if(void 0!==u){const t=u/4,r=n.tint?.color??[1,1,1],a=Math.max(n.tint?.atDistance??1,1e-4);e[t]=Math.log(Math.max(r[0],1e-6))/a,e[t+1]=Math.log(Math.max(r[1],1e-6))/a,e[t+2]=Math.log(Math.max(r[2],1e-6))/a,e[t+3]=i.dispersion??0}const l=r.get("thicknessParams");if(void 0!==l){const t=l/4,r=c?.min??0,n=c?.max??1;e[t]=r,e[t+1]=n-r}I(e,r,"refractionMapUV",i.texture),I(e,r,"thicknessUV",c?.texture)}(e,t,r)},bind(e,t,r){if(!(e._features2&l))return r;const n=e._refractionTexture;if(n||p(201),t.push({binding:r++,resource:n.view}),t.push({binding:r++,resource:n.sampler}),64&e._features2){const n=e._material.subsurface?.refraction?.texture;t.push({binding:r++,resource:n.view}),t.push({binding:r++,resource:h(e._engine)})}if(0!==(e._features&f)){const n=e._material.subsurface?.thickness?.texture;t.push({binding:r++,resource:n.view}),t.push({binding:r++,resource:n.sampler})}return r},textures(e,t){const r=e.subsurface?.refraction?.texture;r&&t.push(r);const n=e.subsurface?.thickness?.texture;n&&t.push(n)}}}var z=Object.freeze({__proto__:null,registerPbrTransmission:function(e,t,r,n){e._p?.(function(e,t){const r=e.meshes.flatMap(e=>{const t=e.material;return t?[[t,t._linearImageProcessing,t._renderFeatures]]:[]});return E(e),[()=>k(e,t),()=>{for(const[e,t,n]of r)e._linearImageProcessing=t,e._renderFeatures=n}]}(e,t))||function(e,t){E(e),k(e,t)}(e,t),r(q(n))}});export{x as _,z as p}; //# sourceMappingURL=pbr-transmission-ext-DPZWUwNK.esm.min.js.map