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) 18.1 kB
import{G as e,b as t,T as s,C as i,O as r,H as n,J as a,m as h,h as o,t as c,u as _,v as d,R as f,i as u,L as l,k as p,P as m,V as T,_ as g}from"./index-Bw2N6w1y.esm.min.js";import{R as b}from"./rawTexture-CPAqHIA7.esm.min.js";class P{constructor(t){this.name=e.NAME_PROCEDURALTEXTURE,this.scene=t}register(){this.scene._beforeClearStage.registerStep(e.STEP_BEFORECLEAR_PROCEDURALTEXTURE,this,this._beforeClear)}rebuild(){}dispose(){}_beforeClear(){if(this.scene.proceduralTexturesEnabled){t.StartPerformanceCounter("Procedural textures",this.scene.proceduralTextures.length>0);for(let e=0;e<this.scene.proceduralTextures.length;e++){const t=this.scene.proceduralTextures[e];t._shouldRender()&&t.render()}t.EndPerformanceCounter("Procedural textures",this.scene.proceduralTextures.length>0)}}}class x extends s{get shaderLanguage(){return this._shaderLanguage}constructor(t,o,c,_,d=null,f=!0,u=!1,l=i.TEXTURETYPE_UNSIGNED_BYTE){super(null,_,!f),this.isEnabled=!0,this.autoClear=!0,this.onGeneratedObservable=new r,this.onBeforeGenerationObservable=new r,this.nodeMaterialSource=null,this.defines="",this._textures={},this._currentRefreshId=-1,this._frameId=-1,this._refreshRate=1,this._vertexBuffers={},this._uniforms=new Array,this._samplers=new Array,this._floats={},this._ints={},this._floatsArrays={},this._colors3={},this._colors4={},this._vectors2={},this._vectors3={},this._vectors4={},this._matrices={},this._fallbackTextureUsed=!1,this._cachedDefines=null,this._contentUpdateId=-1,this._rtWrapper=null,null===d||d instanceof s?(this._options={},this._fallbackTexture=d):(this._options=d,this._fallbackTexture=d.fallbackTexture??null),this._shaderLanguage=this._options.shaderLanguage??0;let p=(_=this.getScene()||n.LastCreatedScene)._getComponent(e.NAME_PROCEDURALTEXTURE);p||(p=new P(_),_._addComponent(p)),_.proceduralTextures.push(this),this._fullEngine=_.getEngine(),this.name=t,this.isRenderTarget=!0,this._size=o,this._textureType=l,this._generateMipMaps=f,this._drawWrapper=new a(this._fullEngine),this.setFragment(c);const m=this._createRtWrapper(u,o,f,l);this._texture=m.texture;const T=[];T.push(1,1),T.push(-1,1),T.push(-1,-1),T.push(1,-1),this._vertexBuffers[h.PositionKind]=new h(this._fullEngine,T,h.PositionKind,!1,!1,2),this._createIndexBuffer()}_createRtWrapper(e,t,s,i){return e?(this._rtWrapper=this._fullEngine.createRenderTargetCubeTexture(t,{generateMipMaps:s,generateDepthBuffer:!1,generateStencilBuffer:!1,type:i,...this._options}),this.setFloat("face",0)):(this._rtWrapper=this._fullEngine.createRenderTargetTexture(t,{generateMipMaps:s,generateDepthBuffer:!1,generateStencilBuffer:!1,type:i,...this._options}),this._rtWrapper.is3D&&(this.setFloat("layer",0),this.setInt("layerNum",0))),this._rtWrapper}getEffect(){return this._drawWrapper.effect}_setEffect(e){this._drawWrapper.effect=e}getContent(){return this._contentData&&this._frameId===this._contentUpdateId||(this._contentData?this._contentData.then((e=>{this._contentData=this.readPixels(0,0,e),this._contentUpdateId=this._frameId})):(this._contentData=this.readPixels(0,0),this._contentUpdateId=this._frameId)),this._contentData}_createIndexBuffer(){const e=this._fullEngine,t=[];t.push(0),t.push(1),t.push(2),t.push(0),t.push(2),t.push(3),this._indexBuffer=e.createIndexBuffer(t)}_rebuild(){const e=this._vertexBuffers[h.PositionKind];e&&e._rebuild(),this._createIndexBuffer(),this.refreshRate===o.REFRESHRATE_RENDER_ONCE&&(this.refreshRate=o.REFRESHRATE_RENDER_ONCE)}reset(){this._drawWrapper.effect?.dispose(),this._drawWrapper.effect=null,this._cachedDefines=null}_getDefines(){return this.defines}executeWhenReady(e){if(this.isReady())return void e(this);const t=this.getEffect();t&&t.executeWhenCompiled((()=>{e(this)}))}isReady(){const e=this._fullEngine;if(this.nodeMaterialSource)return this._drawWrapper.effect.isReady();if(!this._fragment)return!1;if(this._fallbackTextureUsed)return!0;if(!this._texture)return!1;const t=this._getDefines();if(this._drawWrapper.effect&&t===this._cachedDefines&&this._drawWrapper.effect.isReady())return!0;const s={vertex:"procedural",fragmentElement:this._fragment.fragmentElement,fragmentSource:this._fragment.fragmentSource,fragment:"string"==typeof this._fragment?this._fragment:void 0};return this._cachedDefines!==t&&(this._cachedDefines=t,this._drawWrapper.effect=e.createEffect(s,[h.PositionKind],this._uniforms,this._samplers,t,void 0,void 0,(()=>{this._rtWrapper?.dispose(),this._rtWrapper=this._texture=null,this._fallbackTexture&&(this._texture=this._fallbackTexture._texture,this._texture&&this._texture.incrementReferences()),this._fallbackTextureUsed=!0}),void 0,this._shaderLanguage,(async()=>{this._options.extraInitializationsAsync?1===this.shaderLanguage?await Promise.all([import("./procedural.vertex-W1pyhZo_.esm.min.js"),this._options.extraInitializationsAsync()]):await Promise.all([import("./procedural.vertex-BqzG9Bi7.esm.min.js"),this._options.extraInitializationsAsync()]):1===this.shaderLanguage?await import("./procedural.vertex-W1pyhZo_.esm.min.js"):await import("./procedural.vertex-BqzG9Bi7.esm.min.js")}))),this._drawWrapper.effect.isReady()}resetRefreshCounter(){this._currentRefreshId=-1}setFragment(e){this._fragment=e}get refreshRate(){return this._refreshRate}set refreshRate(e){this._refreshRate=e,this.resetRefreshCounter()}_shouldRender(){return this.isEnabled&&this.isReady()&&this._texture?!this._fallbackTextureUsed&&(-1===this._currentRefreshId||this.refreshRate===this._currentRefreshId?(this._currentRefreshId=1,this._frameId++,!0):(this._currentRefreshId++,!1)):(this._texture&&(this._texture.isReady=!1),!1)}getRenderSize(){return this._size}resize(e,t){if(this._fallbackTextureUsed||!this._rtWrapper||!this._texture)return;const s=this._texture.isCube;this._rtWrapper.dispose();const i=this._createRtWrapper(s,e,t,this._textureType);this._texture=i.texture,this._size=e,this._generateMipMaps=t}_checkUniform(e){-1===this._uniforms.indexOf(e)&&this._uniforms.push(e)}setTexture(e,t){return-1===this._samplers.indexOf(e)&&this._samplers.push(e),this._textures[e]=t,this}setFloat(e,t){return this._checkUniform(e),this._floats[e]=t,this}setInt(e,t){return this._checkUniform(e),this._ints[e]=t,this}setFloats(e,t){return this._checkUniform(e),this._floatsArrays[e]=t,this}setColor3(e,t){return this._checkUniform(e),this._colors3[e]=t,this}setColor4(e,t){return this._checkUniform(e),this._colors4[e]=t,this}setVector2(e,t){return this._checkUniform(e),this._vectors2[e]=t,this}setVector3(e,t){return this._checkUniform(e),this._vectors3[e]=t,this}setVector4(e,t){return this._checkUniform(e),this._vectors4[e]=t,this}setMatrix(e,t){return this._checkUniform(e),this._matrices[e]=t,this}render(e){const t=this.getScene();if(!t)return;const s=this._fullEngine;if(s.enableEffect(this._drawWrapper),this.onBeforeGenerationObservable.notifyObservers(this),s.setState(!1),!this.nodeMaterialSource){for(const e in this._textures)this._drawWrapper.effect.setTexture(e,this._textures[e]);for(const e in this._ints)this._drawWrapper.effect.setInt(e,this._ints[e]);for(const e in this._floats)this._drawWrapper.effect.setFloat(e,this._floats[e]);for(const e in this._floatsArrays)this._drawWrapper.effect.setArray(e,this._floatsArrays[e]);for(const e in this._colors3)this._drawWrapper.effect.setColor3(e,this._colors3[e]);for(const e in this._colors4){const t=this._colors4[e];this._drawWrapper.effect.setFloat4(e,t.r,t.g,t.b,t.a)}for(const e in this._vectors2)this._drawWrapper.effect.setVector2(e,this._vectors2[e]);for(const e in this._vectors3)this._drawWrapper.effect.setVector3(e,this._vectors3[e]);for(const e in this._vectors4)this._drawWrapper.effect.setVector4(e,this._vectors4[e]);for(const e in this._matrices)this._drawWrapper.effect.setMatrix(e,this._matrices[e])}if(!this._texture||!this._rtWrapper)return;s._debugPushGroup?.(`procedural texture generation for ${this.name}`,1);const i=s.currentViewport;if(this.isCube)for(let e=0;e<6;e++)s.bindFramebuffer(this._rtWrapper,e,void 0,void 0,!0),s.bindBuffers(this._vertexBuffers,this._indexBuffer,this._drawWrapper.effect),this._drawWrapper.effect.setFloat("face",e),this.autoClear&&s.clear(t.clearColor,!0,!1,!1),s.drawElementsType(c.TriangleFillMode,0,6),s.unBindFramebuffer(this._rtWrapper,!0);else{let e=1;this._rtWrapper.is3D?e=this._rtWrapper.depth:this._rtWrapper.is2DArray&&(e=this._rtWrapper.layers);for(let i=0;i<e;i++){if(s.bindFramebuffer(this._rtWrapper,0,void 0,void 0,!0,0,i),s.bindBuffers(this._vertexBuffers,this._indexBuffer,this._drawWrapper.effect),this._rtWrapper.is3D||this._rtWrapper.is2DArray){this._drawWrapper.effect?.setFloat("layer",1!==e?i/(e-1):0),this._drawWrapper.effect?.setInt("layerNum",i);for(const e in this._textures)this._drawWrapper.effect.setTexture(e,this._textures[e])}this.autoClear&&s.clear(t.clearColor,!0,!1,!1),s.drawElementsType(c.TriangleFillMode,0,6),s.unBindFramebuffer(this._rtWrapper,!this._generateMipMaps)}}i&&s.setViewport(i),this.isCube&&s.generateMipMapsForCubemap(this._texture,!0),s._debugPopGroup?.(1),this.onGenerated&&this.onGenerated(),this.onGeneratedObservable.notifyObservers(this)}clone(){const e=this.getSize(),t=new x(this.name,e.width,this._fragment,this.getScene(),this._fallbackTexture,this._generateMipMaps);return t.hasAlpha=this.hasAlpha,t.level=this.level,t.coordinatesMode=this.coordinatesMode,t}dispose(){const e=this.getScene();if(!e)return;const t=e.proceduralTextures.indexOf(this);t>=0&&e.proceduralTextures.splice(t,1);const s=this._vertexBuffers[h.PositionKind];s&&(s.dispose(),this._vertexBuffers[h.PositionKind]=null),this._indexBuffer&&this._fullEngine._releaseBuffer(this._indexBuffer)&&(this._indexBuffer=null),this.onGeneratedObservable.clear(),this.onBeforeGenerationObservable.clear(),super.dispose()}}_([d()],x.prototype,"isEnabled",void 0),_([d()],x.prototype,"autoClear",void 0),_([d()],x.prototype,"_generateMipMaps",void 0),_([d()],x.prototype,"_size",void 0),_([d()],x.prototype,"refreshRate",null),f("BABYLON.ProceduralTexture",x);class E{get isSupported(){const e=n.LastCreatedEngine;return!!e&&e.getCaps().texelFetch}get iblSource(){return this._iblSource}set iblSource(e){this._iblSource!==e&&(this._disposeTextures(),this._iblSource=e,e&&(e.isCube,e.isReadyOrNotBlocking()?this._recreateAssetsFromNewIbl():e.onLoadObservable.addOnce(this._recreateAssetsFromNewIbl.bind(this,e))))}_recreateAssetsFromNewIbl(){this._debugPass&&this._debugPass.dispose(),this._createTextures(),this._debugPass&&this._createDebugPass()}getIcdfTexture(){return this._icdfPT?this._icdfPT:this._dummyTexture}setDebugDisplayParams(e,t,s,i){this._debugSizeParams.set(e,t,s,i)}get debugPassName(){return this._debugPassName}getDebugPassPP(){return this._debugPass||this._createDebugPass(),this._debugPass}constructor(e){if(this._cachedDominantDirection=null,this.debugEnabled=!1,this._debugSizeParams=new u(0,0,1,1),this._debugPassName="CDF Debug",this.onGeneratedObservable=new r,e?E._IsScene(e)?this._scene=e:this._engine=e:this._scene=n.LastCreatedScene,this._scene&&(this._engine=this._scene.getEngine()),!this.isSupported)return void l.Warn("CDF renderer is not supported by the current engine.");const t=new Uint16Array([0,0,0,255]);this._dummyTexture=new b(t,1,1,p.TEXTUREFORMAT_RGBA,e,!1,!1,void 0,i.TEXTURETYPE_HALF_FLOAT),this._scene&&E._SceneComponentInitialization(this._scene)}_createTextures(){const e=this._iblSource?{width:this._iblSource.getSize().width,height:this._iblSource.getSize().height}:{width:1,height:1};this._iblSource||(this._iblSource=b.CreateRTexture(new Uint8Array([255]),1,1,this._engine,!1,!1,i.TEXTURE_NEAREST_SAMPLINGMODE,i.TEXTURETYPE_UNSIGNED_BYTE),this._iblSource.name="Placeholder IBL Source"),this._iblSource.isCube&&(e.width*=4,e.height*=2,e.width=1<<Math.floor(Math.log2(e.width)),e.height=1<<Math.floor(Math.log2(e.height)));const t=this._engine.isWebGPU,s={generateDepthBuffer:!1,generateMipMaps:!1,format:i.TEXTUREFORMAT_R,type:i.TEXTURETYPE_FLOAT,samplingMode:i.TEXTURE_NEAREST_SAMPLINGMODE,shaderLanguage:t?1:0,gammaSpace:!1,extraInitializationsAsync:async()=>{t?await Promise.all([import("./iblCdfx.fragment-D2c_sDIF.esm.min.js"),import("./iblCdfy.fragment-D3bS-Adp.esm.min.js"),import("./iblScaledLuminance.fragment-XmQvqi2u.esm.min.js")]):await Promise.all([import("./iblCdfx.fragment-BWbySqUJ.esm.min.js"),import("./iblCdfy.fragment-CIerIK0A.esm.min.js"),import("./iblScaledLuminance.fragment-DkmhVDoR.esm.min.js")])}},r={generateDepthBuffer:!1,generateMipMaps:!1,format:i.TEXTUREFORMAT_RGBA,type:i.TEXTURETYPE_HALF_FLOAT,samplingMode:i.TEXTURE_NEAREST_SAMPLINGMODE,shaderLanguage:t?1:0,gammaSpace:!1,extraInitializationsAsync:async()=>{t?await Promise.all([import("./iblIcdf.fragment-CIKZywVe.esm.min.js"),import("./iblDominantDirection.fragment-BZMWT7Nq.esm.min.js")]):await Promise.all([import("./iblIcdf.fragment-DvT0GFrY.esm.min.js"),import("./iblDominantDirection.fragment-BRNMPQPS.esm.min.js")])}};this._cdfyPT=new x("cdfyTexture",{width:e.width,height:e.height+1},"iblCdfy",this._scene,s,!1,!1),this._cdfyPT.autoClear=!1,this._cdfyPT.setTexture("iblSource",this._iblSource),this._cdfyPT.setInt("iblHeight",e.height),this._cdfyPT.wrapV=i.TEXTURE_CLAMP_ADDRESSMODE,this._cdfyPT.refreshRate=0,this._iblSource.isCube&&(this._cdfyPT.defines="#define IBL_USE_CUBE_MAP\n"),this._cdfxPT=new x("cdfxTexture",{width:e.width+1,height:1},"iblCdfx",this._scene,s,!1,!1),this._cdfxPT.autoClear=!1,this._cdfxPT.setTexture("cdfy",this._cdfyPT),this._cdfxPT.refreshRate=0,this._cdfxPT.wrapU=i.TEXTURE_CLAMP_ADDRESSMODE,this._scaledLuminancePT=new x("iblScaledLuminance",{width:e.width,height:e.height},"iblScaledLuminance",this._scene,{...s,samplingMode:i.TEXTURE_TRILINEAR_SAMPLINGMODE,generateMipMaps:!0},!0,!1),this._scaledLuminancePT.autoClear=!1,this._scaledLuminancePT.setTexture("iblSource",this._iblSource),this._scaledLuminancePT.setInt("iblHeight",e.height),this._scaledLuminancePT.setInt("iblWidth",e.width),this._scaledLuminancePT.refreshRate=0,this._iblSource.isCube&&(this._scaledLuminancePT.defines="#define IBL_USE_CUBE_MAP\n"),this._icdfPT=new x("icdfTexture",{width:e.width,height:e.height},"iblIcdf",this._scene,r,!1,!1),this._icdfPT.autoClear=!1,this._icdfPT.setTexture("cdfy",this._cdfyPT),this._icdfPT.setTexture("cdfx",this._cdfxPT),this._icdfPT.setTexture("iblSource",this._iblSource),this._icdfPT.setTexture("scaledLuminanceSampler",this._scaledLuminancePT),this._icdfPT.refreshRate=0,this._icdfPT.wrapV=i.TEXTURE_CLAMP_ADDRESSMODE,this._icdfPT.wrapU=i.TEXTURE_CLAMP_ADDRESSMODE,this._iblSource.isCube&&(this._icdfPT.defines="#define IBL_USE_CUBE_MAP\n"),this._icdfPT.onGeneratedObservable.addOnce((()=>{this.onGeneratedObservable.notifyObservers()})),this._dominantDirectionPT=new x("iblDominantDirection",{width:1,height:1},"iblDominantDirection",this._scene,r,!1,!1),this._dominantDirectionPT.autoClear=!1,this._dominantDirectionPT.setTexture("icdfSampler",this._icdfPT),this._dominantDirectionPT.refreshRate=0,this._dominantDirectionPT.defines="#define NUM_SAMPLES 32u\n"}_disposeTextures(){this._cdfyPT?.dispose(),this._cdfxPT?.dispose(),this._icdfPT?.dispose(),this._scaledLuminancePT?.dispose(),this._dominantDirectionPT?.dispose()}_createDebugPass(){this._debugPass&&this._debugPass.dispose();const e=this._engine.isWebGPU,t={width:this._engine.getRenderWidth(),height:this._engine.getRenderHeight(),samplingMode:s.BILINEAR_SAMPLINGMODE,engine:this._engine,textureType:i.TEXTURETYPE_UNSIGNED_BYTE,uniforms:["sizeParams"],samplers:["cdfy","icdf","cdfx","iblSource"],defines:this._iblSource?.isCube?"#define IBL_USE_CUBE_MAP\n":"",shaderLanguage:e?1:0,extraInitializations:(e,t)=>{e?t.push(import("./iblCdfDebug.fragment-CGdGxb06.esm.min.js")):t.push(import("./iblCdfDebug.fragment-bEV9p1kp.esm.min.js"))}};this._debugPass=new m(this._debugPassName,"iblCdfDebug",t);const r=this._debugPass.getEffect();r&&(r.defines=this._iblSource?.isCube?"#define IBL_USE_CUBE_MAP\n":""),this._iblSource?.isCube&&this._debugPass.updateEffect("#define IBL_USE_CUBE_MAP\n"),this._debugPass.onApplyObservable.add((e=>{e.setTexture("cdfy",this._cdfyPT),e.setTexture("icdf",this._icdfPT),e.setTexture("cdfx",this._cdfxPT),e.setTexture("iblSource",this._iblSource),e.setFloat4("sizeParams",this._debugSizeParams.x,this._debugSizeParams.y,this._debugSizeParams.z,this._debugSizeParams.w)}))}isReady(){return this._iblSource&&"Placeholder IBL Source"!==this._iblSource.name&&this._iblSource.isReady()&&this._cdfyPT&&this._cdfyPT.isReady()&&this._icdfPT&&this._icdfPT.isReady()&&this._cdfxPT&&this._cdfxPT.isReady()&&this._scaledLuminancePT&&this._scaledLuminancePT.isReady()}renderWhenReady(){this._cachedDominantDirection=null,this._icdfPT.onGeneratedObservable.addOnce((()=>{this.onGeneratedObservable.notifyObservers()}));const e=[],t=[this._cdfyPT,this._cdfxPT,this._scaledLuminancePT,this._icdfPT];for(const s of t)e.push(new Promise((e=>{s.isReady()?e():s.getEffect().executeWhenCompiled((()=>{e()}))})));return Promise.all(e).then((()=>{for(const e of t)e.render()}))}findDominantDirection(){return this._cachedDominantDirection?Promise.resolve(this._cachedDominantDirection):new Promise((e=>{this._dominantDirectionPT.onGeneratedObservable.addOnce((()=>{const t=new Float32Array(4);this._dominantDirectionPT.readPixels(0,0,t,!0).then((()=>{const s=new T(t[0],t[1],t[2]);this._cachedDominantDirection=s,e(s)}))})),this.isReady()?this._dominantDirectionPT.isReady()?this._dominantDirectionPT.render():this._dominantDirectionPT.getEffect().executeWhenCompiled((()=>{this._dominantDirectionPT.render()})):this.onGeneratedObservable.addOnce((()=>{this._dominantDirectionPT.isReady()?this._dominantDirectionPT.render():this._dominantDirectionPT.getEffect().executeWhenCompiled((()=>{this._dominantDirectionPT.render()}))}))}))}dispose(){this._disposeTextures(),this._dummyTexture.dispose(),this._debugPass&&this._debugPass.dispose(),this.onGeneratedObservable.clear()}static _IsScene(e){return"Scene"===e.getClassName()}}E._SceneComponentInitialization=e=>{throw g("IblCdfGeneratorSceneComponentSceneComponent")};export{E as I,x as P}; //# sourceMappingURL=iblCdfGenerator-D9XroGsE.esm.min.js.map