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) 11.1 kB
import{aZ as e,aY as t,C as s,l as r,O as i,m as a,a as n,b4 as h,a7 as o,aA as f,F as _,q as c,s as u}from"./index-BPURObVq.esm.min.js";class l{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)}}}let p=(()=>{var t;let p,d,m,g,x,b=a,R=[],E=[],v=[],W=[],T=[],w=[],y=[],M=[],C=[];return t=class extends b{get shaderLanguage(){return this._shaderLanguage}constructor(t,f,_,c,u=null,p=!0,d=!1,m=s.TEXTURETYPE_UNSIGNED_BYTE){super(null,c,!p),this.isEnabled=(r(this,R),r(this,E,!0)),this.autoClear=(r(this,v),r(this,W,!0)),this.onGenerated=r(this,T),this.onGeneratedObservable=new i,this.onBeforeGenerationObservable=new i,this.nodeMaterialSource=null,this.defines="",this._generateMipMaps=r(this,w,void 0),this._drawWrapper=r(this,y),this._textures={},this._size=r(this,M,void 0),this._textureType=r(this,C),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===u||u instanceof a?(this._options={},this._fallbackTexture=u):(this._options=u,this._fallbackTexture=u.fallbackTexture??null),this._shaderLanguage=this._options.shaderLanguage??0;let g=(c=this.getScene()||n.LastCreatedScene)._getComponent(e.NAME_PROCEDURALTEXTURE);g||(g=new l(c),c._addComponent(g)),this._options.skipSceneRegistration||c.proceduralTextures.push(this),this._fullEngine=c.getEngine(),this.name=t,this.isRenderTarget=!0,this._size=f,this._textureType=m,this._generateMipMaps=p,this._drawWrapper=new h(this._fullEngine),this.setFragment(_);const x=this._createRtWrapper(d,f,p,m);this._texture=x.texture;const b=[];b.push(1,1),b.push(-1,1),b.push(-1,-1),b.push(1,-1),this._vertexBuffers[o.PositionKind]=new o(this._fullEngine,b,o.PositionKind,!1,!1,2),this._createIndexBuffer()}_createRtWrapper(e,t,s,r){return e?(this._rtWrapper=this._fullEngine.createRenderTargetCubeTexture(t,{generateMipMaps:s,generateDepthBuffer:!1,generateStencilBuffer:!1,type:r,...this._options}),this.setFloat("face",0)):(this._rtWrapper=this._fullEngine.createRenderTargetTexture(t,{generateMipMaps:s,generateDepthBuffer:!1,generateStencilBuffer:!1,type:r,...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[o.PositionKind];e&&e._rebuild(),this._createIndexBuffer(),this.refreshRate===f.REFRESHRATE_RENDER_ONCE&&(this.refreshRate=f.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()??!1;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,[o.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-DRUM6DEg.esm.min.js"),this._options.extraInitializationsAsync()]):await Promise.all([import("./procedural.vertex-D4ABgHJZ.esm.min.js"),this._options.extraInitializationsAsync()]):1===this.shaderLanguage?await import("./procedural.vertex-DRUM6DEg.esm.min.js"):await import("./procedural.vertex-D4ABgHJZ.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 r=this._createRtWrapper(s,e,t,this._textureType);this._texture=r.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._enableGPUDebugMarkers&&s._debugPushGroup?.(`procedural texture generation for ${this.name}`);const r=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(_.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 r=0;r<e;r++){if(s.bindFramebuffer(this._rtWrapper,0,void 0,void 0,!0,0,r),s.bindBuffers(this._vertexBuffers,this._indexBuffer,this._drawWrapper.effect),this._rtWrapper.is3D||this._rtWrapper.is2DArray){this._drawWrapper.effect?.setFloat("layer",1!==e?r/(e-1):0),this._drawWrapper.effect?.setInt("layerNum",r);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(_.TriangleFillMode,0,6),s.unBindFramebuffer(this._rtWrapper,!this._generateMipMaps)}}r&&s.setViewport(r),this.isCube&&s.generateMipMapsForCubemap(this._texture,!0),s._enableGPUDebugMarkers&&s._debugPopGroup?.(),this.onGenerated&&this.onGenerated(),this.onGeneratedObservable.notifyObservers(this)}clone(){const e=this.getSize(),s=new t(this.name,e.width,this._fragment,this.getScene(),this._fallbackTexture,this._generateMipMaps);return s.hasAlpha=this.hasAlpha,s.level=this.level,s.coordinatesMode=this.coordinatesMode,s}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[o.PositionKind];s&&(s.dispose(),this._vertexBuffers[o.PositionKind]=null),this._indexBuffer&&this._fullEngine._releaseBuffer(this._indexBuffer)&&(this._indexBuffer=null),this.onGeneratedObservable.clear(),this.onBeforeGenerationObservable.clear(),super.dispose()}},(()=>{const e="function"==typeof Symbol&&Symbol.metadata?Object.create(b[Symbol.metadata]??null):void 0;p=[u()],d=[u()],m=[u()],g=[u()],x=[u()],c(t,null,x,{kind:"getter",name:"refreshRate",static:!1,private:!1,access:{has:e=>"refreshRate"in e,get:e=>e.refreshRate},metadata:e},null,R),c(null,null,p,{kind:"field",name:"isEnabled",static:!1,private:!1,access:{has:e=>"isEnabled"in e,get:e=>e.isEnabled,set:(e,t)=>{e.isEnabled=t}},metadata:e},E,v),c(null,null,d,{kind:"field",name:"autoClear",static:!1,private:!1,access:{has:e=>"autoClear"in e,get:e=>e.autoClear,set:(e,t)=>{e.autoClear=t}},metadata:e},W,T),c(null,null,m,{kind:"field",name:"_generateMipMaps",static:!1,private:!1,access:{has:e=>"_generateMipMaps"in e,get:e=>e._generateMipMaps,set:(e,t)=>{e._generateMipMaps=t}},metadata:e},w,y),c(null,null,g,{kind:"field",name:"_size",static:!1,private:!1,access:{has:e=>"_size"in e,get:e=>e._size,set:(e,t)=>{e._size=t}},metadata:e},M,C),e&&Object.defineProperty(t,Symbol.metadata,{enumerable:!0,configurable:!0,writable:!0,value:e})})(),t})();export{p as P}; //# sourceMappingURL=proceduralTexture.pure-Cev_FPRk.esm.min.js.map