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) 14.2 kB
import{S as e,bH as t,l as s,C as i,o as a,q as n,s as r,z as l,D as u,F as o,as as h,$ as d,ak as c,aw as g,J as f,H as _}from"./index-BPURObVq.esm.min.js";const m=new RegExp("^([gimus]+)!");class b{constructor(e){this._plugins=[],this._activePlugins=[],this._activePluginsForExtraEvents=[],this._material=e,this._scene=e.getScene(),this._engine=this._scene.getEngine()}_addPlugin(e){for(let t=0;t<this._plugins.length;++t)if(this._plugins[t].name===e.name)return!1;if(this._material._uniformBufferLayoutBuilt&&(this._material.resetDrawCache(),this._material._createUniformBuffer()),!e.isCompatible(this._material.shaderLanguage))throw`The plugin "${e.name}" can't be added to the material "${this._material.name}" because the plugin is not compatible with the shader language of the material.`;const t=e.getClassName();b._MaterialPluginClassToMainDefine[t]||(b._MaterialPluginClassToMainDefine[t]="MATERIALPLUGIN_"+ ++b._MaterialPluginCounter),this._material._callbackPluginEventGeneric=(e,t)=>this._handlePluginEvent(e,t),this._plugins.push(e),this._plugins.sort((e,t)=>e.priority-t.priority),this._codeInjectionPoints={};const s={};s[b._MaterialPluginClassToMainDefine[t]]={type:"boolean",default:!0};for(const e of this._plugins)e.collectDefines(s),this._collectPointNames("vertex",e.getCustomCode("vertex",this._material.shaderLanguage)),this._collectPointNames("fragment",e.getCustomCode("fragment",this._material.shaderLanguage));return this._defineNamesFromPlugins=s,!0}_activatePlugin(e){-1===this._activePlugins.indexOf(e)&&(this._activePlugins.push(e),this._activePlugins.sort((e,t)=>e.priority-t.priority),this._material._callbackPluginEventIsReadyForSubMesh=this._handlePluginEventIsReadyForSubMesh.bind(this),this._material._callbackPluginEventPrepareDefinesBeforeAttributes=this._handlePluginEventPrepareDefinesBeforeAttributes.bind(this),this._material._callbackPluginEventPrepareDefines=this._handlePluginEventPrepareDefines.bind(this),this._material._callbackPluginEventBindForSubMesh=this._handlePluginEventBindForSubMesh.bind(this),e.registerForExtraEvents&&(this._activePluginsForExtraEvents.push(e),this._activePluginsForExtraEvents.sort((e,t)=>e.priority-t.priority),this._material._callbackPluginEventHasRenderTargetTextures=this._handlePluginEventHasRenderTargetTextures.bind(this),this._material._callbackPluginEventFillRenderTargetTextures=this._handlePluginEventFillRenderTargetTextures.bind(this),this._material._callbackPluginEventHardBindForSubMesh=this._handlePluginEventHardBindForSubMesh.bind(this)))}getPlugin(e){for(let t=0;t<this._plugins.length;++t)if(this._plugins[t].name===e)return this._plugins[t];return null}_handlePluginEventIsReadyForSubMesh(e){let t=!0;for(const s of this._activePlugins)t=t&&s.isReadyForSubMesh(e.defines,this._scene,this._engine,e.subMesh);e.isReadyForSubMesh=t}_handlePluginEventPrepareDefinesBeforeAttributes(e){for(const t of this._activePlugins)t.prepareDefinesBeforeAttributes(e.defines,this._scene,e.mesh)}_handlePluginEventPrepareDefines(e){for(const t of this._activePlugins)t.prepareDefines(e.defines,this._scene,e.mesh)}_handlePluginEventHardBindForSubMesh(e){for(const t of this._activePluginsForExtraEvents)t.hardBindForSubMesh(this._material._uniformBuffer,this._scene,this._engine,e.subMesh)}_handlePluginEventBindForSubMesh(e){for(const t of this._activePlugins)t.bindForSubMesh(this._material._uniformBuffer,this._scene,this._engine,e.subMesh)}_handlePluginEventHasRenderTargetTextures(e){let t=!1;for(const e of this._activePluginsForExtraEvents)if(t=e.hasRenderTargetTextures(),t)break;e.hasRenderTargetTextures=t}_handlePluginEventFillRenderTargetTextures(e){for(const t of this._activePluginsForExtraEvents)t.fillRenderTargetTextures(e.renderTargets)}_handlePluginEvent(e,t){switch(e){case 512:{const e=t;for(const t of this._activePlugins)t.getActiveTextures(e.activeTextures);break}case 256:{const e=t;for(const t of this._activePlugins)t.getAnimatables(e.animatables);break}case 1024:{const e=t;let s=!1;for(const t of this._activePlugins)if(s=t.hasTexture(e.texture),s)break;e.hasTexture=s;break}case 2:{const e=t;for(const t of this._plugins)t.dispose(e.forceDisposeTextures);break}case 4:t.defineNames=this._defineNamesFromPlugins;break;case 128:{const e=t;for(const t of this._activePlugins)e.fallbackRank=t.addFallbacks(e.defines,e.fallbacks,e.fallbackRank),t.getAttributes(e.attributes,this._scene,e.mesh);this._uniformList.length>0&&e.uniforms.push(...this._uniformList),this._samplerList.length>0&&e.samplers.push(...this._samplerList),this._uboList.length>0&&e.uniformBuffersNames.push(...this._uboList),e.customCode=this._injectCustomCode(e,e.customCode);break}case 8:{const e=t;this._uboDeclaration="",this._vertexDeclaration="",this._fragmentDeclaration="",this._uniformList=[],this._samplerList=[],this._uboList=[];const s=1===this._material.shaderLanguage;for(const t of this._plugins){const i=t.getUniforms(this._material.shaderLanguage);if(i){if(i.ubo)for(const t of i.ubo){if(t.size&&t.type){const i=t.arraySize??0;if(e.ubo.addUniform(t.name,t.size,i),s){let e;switch(t.type){case"mat4":e="mat4x4f";break;case"float":e="f32";break;default:e=`${t.type}f`}this._uboDeclaration+=i>0?`uniform ${t.name}: array<${e}, ${i}>;\n`:`uniform ${t.name}: ${e};\n`}else this._uboDeclaration+=`${t.type} ${t.name}${i>0?`[${i}]`:""};\n`}this._uniformList.push(t.name)}i.vertex&&(this._vertexDeclaration+=i.vertex+"\n"),i.fragment&&(this._fragmentDeclaration+=i.fragment+"\n"),i.externalUniforms&&this._uniformList.push(...i.externalUniforms)}t.getSamplers(this._samplerList),t.getUniformBuffersNames(this._uboList)}break}}}_collectPointNames(e,t){if(t)for(const s in t)this._codeInjectionPoints[e]||(this._codeInjectionPoints[e]={}),this._codeInjectionPoints[e][s]=!0}_injectCustomCode(s,i){return(a,n)=>{i&&(n=i(a,n)),this._uboDeclaration&&(n=n.replace("#define ADDITIONAL_UBO_DECLARATION",this._uboDeclaration)),this._vertexDeclaration&&(n=n.replace("#define ADDITIONAL_VERTEX_DECLARATION",this._vertexDeclaration)),this._fragmentDeclaration&&(n=n.replace("#define ADDITIONAL_FRAGMENT_DECLARATION",this._fragmentDeclaration));const r=this._codeInjectionPoints?.[a];if(!r)return n;let l=null;for(let i in r){let r="";for(const n of this._activePlugins){const u=this._material.shaderLanguage;let o=n.getCustomCode(a,u)?.[i];o&&(n.resolveIncludes&&(null===l&&(l={defines:[],indexParameters:s.indexParameters,isFragment:!1,shouldUseHighPrecisionShader:this._engine._shouldUseHighPrecisionShader,processor:void 0,supportsUniformBuffers:this._engine.supportsUniformBuffers,shadersRepository:e.GetShadersRepository(u),includesShadersStore:e.GetIncludesShadersStore(u),version:void 0,platformName:this._engine.shaderPlatformName,processingContext:void 0,isNDCHalfZRange:this._engine.isNDCHalfZRange,useReverseDepthBuffer:this._engine.useReverseDepthBuffer,processCodeAfterIncludes:void 0}),l.isFragment="fragment"===a,t(o,l,e=>o=e)),r+=o+"\n")}if(r.length>0)if("!"===i.charAt(0)){i=i.substring(1);let e="g";if("!"===i.charAt(0))e="",i=i.substring(1);else{const t=m.exec(i);t&&t.length>=2&&(e=t[1],i=i.substring(e.length+1))}e.indexOf("g")<0&&(e+="g");const t=new RegExp(i,e);let s=t.exec(n);for(;null!==s;){const{index:e}=s,i=p(r,s);n=n.substring(0,e)+i+n.substring(e+s[0].length),t.lastIndex=e+i.length,s=t.exec(n)}}else{const e="#define "+i;n=n.replace(e,"\n"+r+"\n"+e)}}return n}}}function p(e,t){return e.replace(/\$(\d+)/g,(e,s)=>{const i=Number(s);return i<t.length?t[i]:""})}b._MaterialPluginClassToMainDefine={},b._MaterialPluginCounter=0;let v=(()=>{var e;let t,l,u,o,h=[],d=[],c=[],g=[],f=[],_=[],m=[],p=[];return e=class{isCompatible(e){return 0===e}_enable(e){e&&this._pluginManager._activatePlugin(this)}constructor(e,t,a,n,r=!0,l=!1,u=!1){this.name=s(this,h,void 0),this.priority=(s(this,d),s(this,c,500)),this.resolveIncludes=(s(this,g),s(this,f,!1)),this.registerForExtraEvents=(s(this,_),s(this,m,!1)),this.doNotSerialize=(s(this,p),!1),this._material=e,this.name=t,this.priority=a,this.resolveIncludes=u,e.pluginManager||(e.pluginManager=new b(e),e.onDisposeObservable.add(()=>{e.pluginManager=void 0})),this._pluginDefineNames=n,this._pluginManager=e.pluginManager,r&&this._pluginManager._addPlugin(this),l&&this._enable(!0),this.markAllDefinesAsDirty=e._dirtyCallbacks[i.MATERIAL_AllDirtyFlag]}getClassName(){return"MaterialPluginBase"}isReadyForSubMesh(e,t,s,i){return!0}hardBindForSubMesh(e,t,s,i){}bindForSubMesh(e,t,s,i){}dispose(e){}getCustomCode(e,t=0){return null}collectDefines(e){if(this._pluginDefineNames)for(const t of Object.keys(this._pluginDefineNames)){if("_"===t[0])continue;const s=typeof this._pluginDefineNames[t];e[t]={type:"number"===s?"number":"string"===s?"string":"boolean"===s?"boolean":"object",default:this._pluginDefineNames[t]}}}prepareDefinesBeforeAttributes(e,t,s){}prepareDefines(e,t,s){}hasTexture(e){return!1}hasRenderTargetTextures(){return!1}fillRenderTargetTextures(e){}getActiveTextures(e){}getAnimatables(e){}addFallbacks(e,t,s){return s}getSamplers(e){}getAttributes(e,t,s){}getUniformBuffersNames(e){}getUniforms(e=0){return{}}copyTo(e){a.Clone(()=>e,this)}serialize(){return a.Serialize(this)}parse(e,t,s){a.Parse(()=>this,e,t,s)}},(()=>{const s="function"==typeof Symbol&&Symbol.metadata?Object.create(null):void 0;t=[r()],l=[r()],u=[r()],o=[r()],n(null,null,t,{kind:"field",name:"name",static:!1,private:!1,access:{has:e=>"name"in e,get:e=>e.name,set:(e,t)=>{e.name=t}},metadata:s},h,d),n(null,null,l,{kind:"field",name:"priority",static:!1,private:!1,access:{has:e=>"priority"in e,get:e=>e.priority,set:(e,t)=>{e.priority=t}},metadata:s},c,g),n(null,null,u,{kind:"field",name:"resolveIncludes",static:!1,private:!1,access:{has:e=>"resolveIncludes"in e,get:e=>e.resolveIncludes,set:(e,t)=>{e.resolveIncludes=t}},metadata:s},f,_),n(null,null,o,{kind:"field",name:"registerForExtraEvents",static:!1,private:!1,access:{has:e=>"registerForExtraEvents"in e,get:e=>e.registerForExtraEvents,set:(e,t)=>{e.registerForExtraEvents=t}},metadata:s},m,p),s&&Object.defineProperty(e,Symbol.metadata,{enumerable:!0,configurable:!0,writable:!0,value:s})})(),e})();class x extends g{constructor(){super(...arguments),this.DETAIL=!1,this.DETAILDIRECTUV=0,this.DETAIL_NORMALBLENDMETHOD=0}}let E=(()=>{var e,t,a,g;let m,b,p,E,D,T,P=v,M=[],A=[],L=[],y=[],B=[],k=[],S=[],F=[],I=[],R=[],C=[],N=[];return e=class extends P{get texture(){return l(this,t,"f")}set texture(e){u(this,t,e,"f")}get normalBlendMethod(){return l(this,a,"f")}set normalBlendMethod(e){u(this,a,e,"f")}get isEnabled(){return l(this,g,"f")}set isEnabled(e){u(this,g,e,"f")}_markAllSubMeshesAsTexturesDirty(){this._enable(this._isEnabled),this._internalMarkAllSubMeshesAsTexturesDirty()}isCompatible(){return!0}constructor(e,n=!0){super(e,"DetailMap",140,new x,n),this._texture=null,t.set(this,s(this,M,void 0)),this.diffuseBlendLevel=(s(this,A),s(this,L,1)),this.roughnessBlendLevel=(s(this,y),s(this,B,1)),this.bumpLevel=(s(this,k),s(this,S,1)),this._normalBlendMethod=(s(this,F),o.MATERIAL_NORMALBLENDMETHOD_WHITEOUT),a.set(this,s(this,I,void 0)),this._isEnabled=(s(this,R),!1),g.set(this,s(this,C,!1)),this._internalMarkAllSubMeshesAsTexturesDirty=s(this,N),this._internalMarkAllSubMeshesAsTexturesDirty=e._dirtyCallbacks[i.MATERIAL_TextureDirtyFlag]}isReadyForSubMesh(e,t,s){return!this._isEnabled||!(e._areTexturesDirty&&t.texturesEnabled&&s.getCaps().standardDerivatives&&this._texture&&h.DetailTextureEnabled&&!this._texture.isReady())}prepareDefines(e,t){if(this._isEnabled){e.DETAIL_NORMALBLENDMETHOD=this._normalBlendMethod;const s=t.getEngine();e._areTexturesDirty&&(s.getCaps().standardDerivatives&&this._texture&&h.DetailTextureEnabled&&this._isEnabled?(d(this._texture,e,"DETAIL"),e.DETAIL_NORMALBLENDMETHOD=this._normalBlendMethod):e.DETAIL=!1)}else e.DETAIL=!1}bindForSubMesh(e,t){if(!this._isEnabled)return;const s=this._material.isFrozen;e.useUbo&&s&&e.isSync||this._texture&&h.DetailTextureEnabled&&(e.updateFloat4("vDetailInfos",this._texture.coordinatesIndex,this.diffuseBlendLevel,this.bumpLevel,this.roughnessBlendLevel),c(this._texture,e,"detail")),t.texturesEnabled&&this._texture&&h.DetailTextureEnabled&&e.setTexture("detailSampler",this._texture)}hasTexture(e){return this._texture===e}getActiveTextures(e){this._texture&&e.push(this._texture)}getAnimatables(e){this._texture&&this._texture.animations&&this._texture.animations.length>0&&e.push(this._texture)}dispose(e){e&&this._texture?.dispose()}getClassName(){return"DetailMapConfiguration"}getSamplers(e){e.push("detailSampler")}getUniforms(){return{ubo:[{name:"vDetailInfos",size:4,type:"vec4"},{name:"detailMatrix",size:16,type:"mat4"}]}}},t=new WeakMap,a=new WeakMap,g=new WeakMap,(()=>{const t="function"==typeof Symbol&&Symbol.metadata?Object.create(P[Symbol.metadata]??null):void 0;m=[f("detailTexture"),_("_markAllSubMeshesAsTexturesDirty")],b=[r()],p=[r()],E=[r()],D=[r(),_("_markAllSubMeshesAsTexturesDirty")],T=[r(),_("_markAllSubMeshesAsTexturesDirty")],n(e,null,m,{kind:"accessor",name:"texture",static:!1,private:!1,access:{has:e=>"texture"in e,get:e=>e.texture,set:(e,t)=>{e.texture=t}},metadata:t},M,A),n(e,null,D,{kind:"accessor",name:"normalBlendMethod",static:!1,private:!1,access:{has:e=>"normalBlendMethod"in e,get:e=>e.normalBlendMethod,set:(e,t)=>{e.normalBlendMethod=t}},metadata:t},I,R),n(e,null,T,{kind:"accessor",name:"isEnabled",static:!1,private:!1,access:{has:e=>"isEnabled"in e,get:e=>e.isEnabled,set:(e,t)=>{e.isEnabled=t}},metadata:t},C,N),n(null,null,b,{kind:"field",name:"diffuseBlendLevel",static:!1,private:!1,access:{has:e=>"diffuseBlendLevel"in e,get:e=>e.diffuseBlendLevel,set:(e,t)=>{e.diffuseBlendLevel=t}},metadata:t},L,y),n(null,null,p,{kind:"field",name:"roughnessBlendLevel",static:!1,private:!1,access:{has:e=>"roughnessBlendLevel"in e,get:e=>e.roughnessBlendLevel,set:(e,t)=>{e.roughnessBlendLevel=t}},metadata:t},B,k),n(null,null,E,{kind:"field",name:"bumpLevel",static:!1,private:!1,access:{has:e=>"bumpLevel"in e,get:e=>e.bumpLevel,set:(e,t)=>{e.bumpLevel=t}},metadata:t},S,F),t&&Object.defineProperty(e,Symbol.metadata,{enumerable:!0,configurable:!0,writable:!0,value:t})})(),e})();export{E as D,v as M}; //# sourceMappingURL=material.detailMapConfiguration-FEC73qwc.esm.min.js.map