@hology/core 0.0.156 → 0.0.157
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/rendering.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var e;import{__decorate as t,__metadata as s}from"tslib";import*as i from"three";import{Color as r,Material as n,Matrix4 as a,Mesh as o,PerspectiveCamera as l,ShaderChunk as h,ShaderMaterial as c,WebGLRenderTarget as d}from"three";import{CopyShader as u,EffectComposer as m,FXAAShader as p,GammaCorrectionShader as g,LUTPass as f,RenderPass as v,ShaderPass as P,VRButton as T}from"three-stdlib";import{CSMShader as M,CSMUtil as b}from"./csm.js";import{bool as x,colorToNormal as y,float as w,NodeShaderMaterial as R,normalize as S,rgb as C,textureSampler2d as F,varyingAttributes as E,varyingTransformed as I,vec4 as A,BooleanExpression as B,select as W,ifDefApply as U}from"three-shader-graph";import{Reflector as D}from"three-stdlib";import{BokehPass as j,OutputPass as H}from"three/examples/jsm/Addons.js";import{CSM as L}from"three/examples/jsm/csm/CSM.js";import O from"three/examples/jsm/libs/stats.module.js";import{GTAOPass as V}from"three/examples/jsm/postprocessing/GTAOPass.js";import{Service as G}from"typedi";import{depthUniformName as k,farUniformName as N,nearUniformName as z,resolutionUniformName as _,sceneNormalUniformName as q,supportsDepthTextureExtension as X}from"./shader-nodes/depth.js";import{elapsedTimeUniformName as Y}from"./shader-nodes/time.js";import{sceneMapUniformName as Q}from"./shader-nodes/scene-sample.js";import{DepthPass as K}from"./utils/three/depth-pass.js";import{GPUStatsPanel as J}from"./utils/three/gpu-stats-panel.js";import{OutlinePass as Z}from"./utils/three/outline-pass.js";import{findFirstVisibleObject as $,traverseVisibleStop as ee}from"./utils/three/traverse.js";import{clamp as te}from"./utils/math.js";import{ColorPass as se}from"./rendering/color-pass.js";import{SSRPass as ie}from"./rendering/ssr/SSRPass.js";import{SSRShader as re}from"./rendering/ssr/SSRShader.js";import{VolumetricFogPass as ne}from"./rendering/fog/volumetric-fog-pass.js";import{OutlineEffect as ae}from"./rendering/outline-effect.js";import{UnrealBloomPass as oe}from"./rendering/bloom/UnrealBloomPass.js";b.patchSetupMaterial();const le=document.createElement("div");le.style.position="absolute",le.style.left="50%",le.style.top="50%",le.style.color="black",le.style.zIndex="999";(new i.Layers).set(9);const he=new i.MeshBasicMaterial({color:"black"}),ce=new i.MeshDepthMaterial;ce.depthPacking=i.RGBADepthPacking,ce.blending=i.NoBlending,ce.side=i.DoubleSide;const de=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);let ue=e=class{setPaused(e){this.paused=e}resizeRender(){this.previousClientWith===this.container.clientWidth&&this.previousClientHeight===this.container.clientHeight||0!==this.container.clientWidth&&0!==this.container.clientHeight&&(this.previousClientWith=this.container.clientWidth,this.previousClientHeight=this.container.clientHeight,this.camera instanceof l&&(this.camera.aspect=this.container.clientWidth/this.container.clientHeight,this.camera.updateProjectionMatrix()),this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(this.container.clientWidth,this.container.clientHeight),this.composer.setSize(this.container.clientWidth*this.resolutionScale,this.container.clientHeight*this.resolutionScale),this.dofPass.setSize(this.container.clientWidth*this.resolutionScale,this.container.clientHeight*this.resolutionScale),this.aoPass.setGBuffer(this.gRenderTarget.depthTexture,this.gRenderTarget.textures[0]),this.sceneColorRenderTarget.dispose(),this.sceneColorRenderTarget=e.createSceneColorRenderTarget(this.renderer,this.container))}addPostProcessVolume(e){if(0===this.postProcessVolumes.length)this.postProcessVolumes.push(e);else{let t=!1;for(let s=0;s<this.postProcessVolumes.length;s++)if(e.priority<this.postProcessVolumes[s].priority){this.postProcessVolumes.splice(s,0,e),t=!0;break}t||this.postProcessVolumes.push(e)}}removePostProcessVolume(e){const t=this.postProcessVolumes.indexOf(e);t>-1&&this.postProcessVolumes.splice(t,1)}constructor(t,s={}){this.container=t,this.options=s,this.windowVisible=!0,this.running=!0,this.paused=!1,this.fpsCap=null,this.postProcessVolumes=[],this.postProcessSettings={},this.resolutionScale=1,this.maxPixelRatio=de?1:window.devicePixelRatio,this.onResize=()=>{if(this.resizeRender(),!this.paused)try{this.render()}catch(e){}},this.onVisiblityChane=()=>{this.windowVisible=!document.hidden},this.isDepthTextureExtensionSupported=!0,this.onLoopCallbacks=[],this.stats=new O,this._showStats=!0,this.gbufferMaterialCache=new Map,this.pmremGeneratorResults=new WeakMap,this.insetHeight=200,this.insetWidth=this.insetHeight*(16/9),this.insetOffsetY=250,this.insetMargin=10,this.maxInsetCameras=4,this.overlayCameras=new Set,this.prevClearColor=new r,this.hadBloom=!1,this.bloomStoredMaterials={},this.bloomHidden=[],null!=s.maxPixelRatio&&(this.maxPixelRatio=s.maxPixelRatio),window.renderer=this.renderer=new i.WebGLRenderer({antialias:!1,powerPreference:"high-performance"});new i.MeshStandardMaterial({color:"#ccc"});this.scene=new i.Scene,this.scene.matrixWorldAutoUpdate=!0,this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(t.clientWidth,t.clientHeight),this.renderer.xr.enabled=this.options.enableXR??!1,!0===this.options.enableXR&&document.body.appendChild(T.createButton(this.renderer));const n=new ae(this.renderer,{defaultThickness:.005,defaultColor:[0,0,0],defaultAlpha:1,defaultKeepAlive:!0});this.outlineEffect=n,this.gRenderTarget=new d(t.clientWidth,t.clientHeight,{count:2,minFilter:i.NearestFilter,magFilter:i.NearestFilter,type:i.HalfFloatType,format:i.RGBAFormat}),this.gRenderTarget.depthTexture=new i.DepthTexture(t.clientWidth,t.clientHeight),this.gRenderTarget.depthTexture.type=i.UnsignedShortType,this.gRenderTarget.depthTexture.minFilter=i.NearestFilter,this.gRenderTarget.depthTexture.magFilter=i.NearestFilter,this.gRenderTarget.texture.generateMipmaps=!1,this.gRenderTarget.stencilBuffer=!1,this.composer=new m(this.renderer);var a=(t.clientWidth||1)/(t.clientHeight||1);const o=new i.PerspectiveCamera(45,a,.5,800);o.layers.enable(19),this.setCamera(o),this.renderer.shadowMap.enabled=!0,this.renderer.shadowMap.type=i.PCFSoftShadowMap,this.renderer.shadowMap.autoUpdate=s.shadows?.autoUpdate??!1,this.renderer.outputColorSpace=i.SRGBColorSpace,this.renderer.toneMapping=i.NoToneMapping,this.renderer.toneMappingExposure=1,this.renderer.gammaFactor=1.4,b.renderingView=this,this.isDepthTextureExtensionSupported=X(this.renderer),t.replaceChildren(this.renderer.domElement),this.setupEventListeners(),this.aoMaskDepthRenderTarget=e.createAOMaskDepthRenderTarget(this.renderer,this.container),this.sceneColorRenderTarget=e.createSceneColorRenderTarget(this.renderer,this.container);const l=new i.Vector2(t.clientWidth,t.clientHeight),h=new v(this.scene,this.camera);this.composer.addPass(h);const c=new oe(l,1.5,.4,.85);c.threshold=1,c.strength=.9,c.radius=.5,this.bloomPass=c;const M=new V(this.scene,this.camera,t.clientWidth,t.clientWidth,{});M.normalRenderTarget?.dispose(),M.setGBuffer(this.gRenderTarget.depthTexture,this.gRenderTarget.textures[0]),M.output=V.OUTPUT.Default,M.enabled=!1,this.aoPass=M,re.fragmentShader=re.fragmentShader.replace("if(metalness==0.) return;","if(metalness<0.1) return;");const x=new ie({renderer:this.renderer,scene:this.scene,camera:this.camera,groundReflector:null,selects:[],normalTexture:this.gRenderTarget.textures[0],depthTexture:this.gRenderTarget.depthTexture});x.output=ie.OUTPUT.Default,x.blur=!0,x.fresnel=!1,x.distanceAttenuation=!0,x.maxDistance=50,x.selective=!0,x.bouncing=!1,x.opacity=.4,x.enabled=!1!==this.options?.reflection?.enabled,this.ssrPass=x,this.composer.addPass(x),!1!==this.options.ao?.enabled&&this.composer.addPass(M),this.composer.addPass(c),c.emissiveTexture=this.gRenderTarget.textures[1],this.renderer.info.autoReset=!1,this.options.bloom,this.volumetricFogPass=new ne(l),this.composer.addPass(this.volumetricFogPass),this.volumetricFogPass.enabled=!0,this.dofPass=new j(this.scene,this.camera,{focus:1,aperture:.025,maxblur:.01}),this.dofPass.enabled=!1,this.composer.addPass(this.dofPass);const y=new se;this.composer.addPass(y),this.colorPass=y,y.vignetteEnabled=!1,this.outlinePass=new Z(new i.Vector2(t.clientWidth,t.clientHeight),this.scene,this.camera),this.outlinePass.edgeGlow=0,this.outlinePass.edgeThickness=1.5,this.outlinePass.edgeStrength=5,this.outlinePass.clear=!1,this.outlinePass.enabled=!1,this.composer.addPass(this.outlinePass);const w=new P(p);w.uniforms.resolution.value.set(1/t.clientWidth,1/t.clientHeight),this.composer.addPass(w),this.fxaaPass=w,this.fxaaPass.enabled=!1,!0===s.enableOutlines&&this.setEnableOutlines(!0),new P(g).clear=!1,this.fixStatsStyle(),this.lutPass=new f({}),this.lutPass.enabled=!1,this.composer.addPass(this.lutPass);const R=new P(u,"prevtexture");R.enabled=!0,R.needsSwap=!1,R.material.uniforms.tDiffuse.value=this.gRenderTarget.textures[1],R.renderToScreen=!0;const S=new H;this.composer.addPass(S)}fixStatsStyle(){const e=this.stats.dom;e.style.position="absolute";const t=e.getElementsByTagName("canvas");for(let e=0;e<t.length;e++)t.item(e).style.display="inline-block"}setEnableOutlines(e){this.outlinePass.enabled=e,this.fxaaPass.enabled=e}setCamera(e){this.camera=e,this.composer.passes.forEach(t=>{t instanceof v?t.camera=e:t instanceof Z?t.renderCamera=e:(t instanceof K||t instanceof V)&&(t.camera=e)}),this.ssrPass&&(this.ssrPass.camera=e),this.aoPass&&(this.aoPass.camera=e),null==this.csm?(this.csm=new L({maxFar:100,lightFar:250,lightMargin:20,cascades:de?2:4,shadowMapSize:2048*(de?.5:1),lightDirection:new i.Vector3(.5,-1,-.6).normalize(),lightIntensity:.5*Math.PI,camera:this.camera,parent:this.scene,mode:"practical"}),this.csm&&Array.isArray(this.csm.lights),this.csm.fade=!0,h.lights_fragment_begin=M.lights_fragment_begin):(this.csm.camera=this.camera,this.camera),this.csm.updateFrustums()}setSelectedObjects(e){if(null==this.outlinePass)return;const t=new Map;for(const s of e)t.set(s.uuid,s);for(const s of e)s.traverse(e=>{e.uuid!==s.uuid&&t.has(e.uuid)&&t.delete(e.uuid)});this.outlinePass.selectedObjects=Array.from(t.values())}static createDepthRenderTarget(e,t,s){const r=new i.WebGLRenderTarget(t.clientWidth*s,t.clientHeight*s);return r.texture.minFilter=i.NearestFilter,r.texture.magFilter=i.NearestFilter,r.texture.generateMipmaps=!1,r.stencilBuffer=!1,r.depthTexture=new i.DepthTexture(128,128),r.depthTexture.type=i.UnsignedShortType,r.depthTexture.minFilter=i.NearestFilter,r.depthTexture.magFilter=i.NearestFilter,r}static createAOMaskDepthRenderTarget(e,t){const s=new i.WebGLRenderTarget(t.clientWidth*e.getPixelRatio(),t.clientHeight*e.getPixelRatio(),{type:i.HalfFloatType});return s.texture.minFilter=i.NearestFilter,s.texture.magFilter=i.NearestFilter,s.texture.generateMipmaps=!1,s.stencilBuffer=!1,s.depthTexture=new i.DepthTexture(128,128),s.depthTexture.type=i.UnsignedInt248Type,s.depthTexture.minFilter=i.NearestFilter,s.depthTexture.magFilter=i.NearestFilter,s}static createSceneColorRenderTarget(e,t){const s=new i.WebGLRenderTarget(t.clientWidth*e.getPixelRatio()*.5,t.clientHeight*e.getPixelRatio()*.5,{format:i.RGBAFormat,colorSpace:i.SRGBColorSpace,stencilBuffer:!1});return s.texture.minFilter=i.LinearFilter,s.texture.magFilter=i.LinearFilter,s.texture.generateMipmaps=!1,s}setupEventListeners(){window.addEventListener("resize",this.onResize),window.addEventListener("orientationchange",this.onResize),document.addEventListener("visibilitychange",this.onVisiblityChane)}stop(){this.running=!1,window.removeEventListener("resize",this.onResize),window.removeEventListener("orientationchange",this.onResize),document.removeEventListener("visibilitychange",this.onVisiblityChane),this.onLoopCallbacks=[],this.renderer.dispose(),this.gRenderTarget.dispose(),this.aoMaskDepthRenderTarget.dispose(),this.sceneColorRenderTarget.dispose(),this.csm.dispose(),this.container.replaceChildren(),this.volumetricFogPass.dispose(),b.clearSceneCache(this.scene)}onLoop(e){this.onLoopCallbacks.push(e)}removeOnLoop(e){const t=this.onLoopCallbacks.find(e);t>=0&&this.onLoopCallbacks.splice(t,1)}set showStats(e){this._showStats=e,this._showStats&&!this.container.contains(this.stats.dom)?this.container.appendChild(this.stats.dom):!this._showStats&&this.container.contains(this.stats.dom)&&this.container.removeChild(this.stats.dom)}get showStats(){return this._showStats}applyEnvMap(e){null!=this.scene.environment&&e instanceof R&&(null==e.envMap||e.userData.useSceneEnv)&&(e.userData.useSceneEnv=!0,null==e.uniforms.envMap&&(e.uniforms.envMap={value:this.scene.environment},e.uniformsNeedUpdate=!0),null==e.uniforms.envMapIntensity&&(e.uniforms.envMapIntensity={value:1*Math.PI},e.uniformsNeedUpdate=!0),e.uniforms.envMap.value=this.scene.environment,e.uniforms.envMapIntensity.value=this.scene.environmentIntensity*Math.PI,e.envMap=this.scene.environment)}setupCsm(e){if(e instanceof i.Mesh||e instanceof i.SkinnedMesh)if(e.material instanceof Array)for(const t of e.material)this.csm.setupMaterial(t);else this.csm.setupMaterial(e.material)}createGBufferMaterial(e){let t=this.gbufferMaterialCache.get(e);if(null==t){let r=I.normal;e instanceof i.MeshStandardMaterial||e instanceof i.MeshLambertMaterial||e instanceof i.MeshPhongMaterial?null!=e.normalMap&&(r=y(F(e.normalMap).sample(E.uv),e.normalScale.x??1)):e instanceof R&&null!=e.outputNormal&&(r=e.outputNormal),!0!==e.userData.disableAO&&(r=U("DOUBLE_SIDED",r,e=>W(new B("gl_FrontFacing"),e,e.multiplyScalar(-1))));let n=e.userData?.reflective?w(0):w(1);e instanceof i.MeshStandardMaterial?n=null!=e.roughnessMap?F(e.roughnessMap).sample(E.uv).r.multiply(e.roughness??1):w(e.roughness??1):e instanceof R&&null!=e.outputRoughness&&(n=e.outputRoughness);const a=!0===e.userData.hasBloom;let o=C(0);a&&(e instanceof i.MeshStandardMaterial||e instanceof i.MeshLambertMaterial||e instanceof i.MeshPhongMaterial?(o=null!=e.emissiveMap?F(e.emissiveMap).sample(E.uv).rgb.multiply(C(e.emissive??0)):C(e.emissive??0),o=o.multiplyScalar(e.emissiveIntensity)):e instanceof R&&(o=e.outputEmissive??e.outputColor.rgb.multiplyScalar(e.outputColor.a)));let l=w(1);e instanceof i.MeshStandardMaterial||e instanceof i.MeshLambertMaterial||e instanceof i.MeshBasicMaterial||e instanceof i.MeshToonMaterial||e instanceof i.MeshPhongMaterial?(null!=e.alphaMap&&(l=F(e.alphaMap).sample(E.uv).r),null!=e.map&&(l=l.multiply(F(e.map).sample(E.uv).a))):e instanceof R&&null!=e.opacity&&(l=e.outputOpacity.multiply(e.opacity??1));const h=e instanceof c&&null!=e.uniforms[k],d=e.transparent&&e.alphaTest<=0,u=e.alphaTest>0?l.lt(e.alphaTest):d?l.lt(.8):x(!1);let m;m=(s=r,S(s).multiplyScalar(.5).addScalar(.5)).rgba(n);let p=e instanceof R?e.outputPosition:void 0;t=new R({position:p,outputs:[m,o.rgba(e.alphaTest)],outputEncoding:!1,fog:!1,transparent:!1,lights:!1,discard:u}),t.toneMapped=!1,t.side=e.side,d&&a&&(t.transparent=!0,t.blending=i.AdditiveBlending),t.depthWrite=!0,t.depthTest=!0,t.visible=e.depthTest&&!h&&t.visible,e instanceof c&&Object.assign(t.uniforms,e.uniforms),this.gbufferMaterialCache.set(e,t)}var s;if(e instanceof c)for(const s in e.uniforms)null!=t.uniforms[s]&&(t.uniforms[s].value=e.uniforms[s].value);return t}loop(e,t=!1){const s=this.stats,r=s.addPanel(new O.Panel("Calls","#83f","#002")),n=s.addPanel(new O.Panel("Triangles","#c32","#002"));let l;navigator.userAgent.includes("Chrome")&&navigator.userAgent.includes("HologyEngine")&&(l=new J(this.renderer.getContext()),s.addPanel(l)),this.showStats=t;let h=10,d=1e3;const u=()=>{const e=this.renderer.info.render.calls;e>h&&(h=e,setTimeout(()=>h=10,5e3)),r.update(e,h);const t=this.renderer.info.render.triangles;t>d&&(d=t,setTimeout(()=>d=1e3,5e3)),n.update(t,d)};performance.now();i.Ray.prototype.intersectTriangle;const m=[],p=[],g=[];let f=0;const v=new a,P=new a;let T=0;let M=this.paused;const b=t=>{const r=this.renderer.getContext();if(this.paused&&this.running&&r.drawingBufferHeight>1)return setTimeout(()=>b(t),500),void(M=!0);this.renderer.clear(),this.applyPostProcessSettings(),this.renderer.autoClear=!1,this.renderer.clear(),this.renderer.setViewport(0,0,this.container.clientWidth,this.container.clientHeight),this.camera,s.begin(),this.showStats&&l?.startQuery(),this.ssrPass.gpuPanel=l;let n=(t*=.001)-f;if(f=t,M&&(n=.016,M=!1),v.copy(this.camera.matrixWorld),n>1){let t=n;for(;t>.05;)e(me),t-=me;e(t)}else e(n);this.onLoopCallbacks.forEach(e=>e(n)),this.camera?.updateMatrixWorld(),P.copy(this.camera.matrixWorld),t-T>.08&&!P.equals(v)&&(this.renderer.shadowMap.needsUpdate=!0,T=t),this.resizeRender(),this.csm.update();let a=!1;m.length=0,p.length=0,g.length=0;const h=ge;ve.multiplyMatrices(this.camera.projectionMatrix,this.camera.matrixWorldInverse),h.setFromProjectionMatrix(ve),this.scene.traverseVisible(e=>{if(this.setupCsm(e),this.outlineEffect.apply(e),(e instanceof o||e instanceof i.Sprite)&&(this.initResolutionUniform(e.material),this.initNormalUniform(e.material),null!=this.scene.environment&&function(e,t){if(Array.isArray(e.material))for(const s of e.material)t(s);else null!=e.material&&t(e.material)}(e,e=>this.applyEnvMap(e)),!0===e.material?.userData?.hasBloom&&(a=!0)),(e instanceof o||e instanceof i.Sprite)&&e.visible&&(e.material?.userData?.water||e.material?.uniforms&&null!=e.material?.uniforms[k])&&isObjectInFrustum(e,h)?this.initDepthUniform(e.material):e instanceof D&&(e.visible=!1,p.push(e)),(e instanceof o||e instanceof i.Sprite)&&e.material?.uniforms&&null!=e.material?.uniforms[Q]&&isObjectInFrustum(e,h)&&(e.visible=!1,g.push(e),e.material.uniforms[Q].value=this.sceneColorRenderTarget.texture),e instanceof o&&e.material?.uniforms&&null!=e.material?.uniforms[Y])e.material.uniforms[Y].value=t;else if(e instanceof o&&Array.isArray(e.material))for(const s of e.material)s.uniforms&&null!=s.uniforms[Y]&&(s.uniforms[Y].value=t)}),this.bloomPass.enabled=a;const d=new Map,x=new Map;ee(this.scene,e=>{if(e instanceof i.Sprite||e.isPoints||e.isLine||e.isTransformControls||e.isTransformControlsGizmo)return x.set(e,e.visible),e.visible=!1,!1;if(e instanceof o)if(d.set(e,e.material),Array.isArray(e.material)){e.material=e.material.slice();for(let s=0;s<e.material.length;s++)e.material[s]=this.createGBufferMaterial(e.material[s]),e.material[s]instanceof c&&null!=e.material[s].uniforms[Y]&&(e.material[s].uniforms[Y].value=t)}else e.material=this.createGBufferMaterial(e.material),e.material instanceof c&&null!=e.material.uniforms[Y]&&(e.material.uniforms[Y].value=t)});const y=this.renderer.shadowMap.autoUpdate;this.renderer.shadowMap.autoUpdate=!1,this.renderer.setRenderTarget(this.gRenderTarget),this.renderer.clear();try{this.renderer.render(this.scene,this.camera)}catch(e){console.warn("Render failed",e)}if(this.renderer.setRenderTarget(null),d.forEach((e,t)=>{t.material=e}),x.forEach((e,t)=>{t.visible=e}),this.renderer.shadowMap.autoUpdate=y,g.length>0){if(this.renderer.setRenderTarget(this.sceneColorRenderTarget),!this.paused&&null!=this.camera)try{this.renderer.clear(!0,!0,!1);try{this.renderer.render(this.scene,this.camera)}catch(e){}}catch(e){console.warn("Error rendering scene color:",e)}this.renderer.setRenderTarget(null)}m.forEach(e=>e.visible=!0),p.forEach(e=>e.visible=!0),g.forEach(e=>e.visible=!0),this.aoPass.enabled,this.ssrPass&&(this.ssrPass.elapsedTime=t),this.volumetricFogPass&&this.volumetricFogPass.update(this.camera,this.gRenderTarget,this.csm,this.scene);try{!this.paused&&this.running&&(this.render(n),this.showStats&&l?.endQuery(),this.showStats&&u(),this.renderer.info.reset(),this.renderOverlay())}catch(e){console.warn(e)}s.end(),this.csm?.update(),this.running&&!0!==this.options.enableXR&&(this.fpsCap?setTimeout(()=>{requestAnimationFrame(b)},1e3/this.fpsCap):requestAnimationFrame(b))};!0===this.options.enableXR?this.renderer.setAnimationLoop(b):requestAnimationFrame(b)}getEnvTexture(e){null==this.pmremGenerator&&(this.pmremGenerator=new i.PMREMGenerator(this.renderer),this.pmremGenerator.compileEquirectangularShader());let t=this.pmremGeneratorResults.get(e);return null==t&&(t=this.pmremGenerator.fromEquirectangular(e).texture,this.pmremGeneratorResults.set(e,t)),t}applyPostProcessSettings(){if(0==this.postProcessVolumes.length)return this.lutPass.enabled=!1,void(this.colorPass.enabled=!1);var e;(e=this.postProcessSettings).tonemapMapping=void 0,e.tonemapExposure=1,e.envIntensity=1,e.envTexture=void 0,e.vignetteIntensity=0,e.colorTint=new i.Color("white"),e.colorTintIntensity=0,e.depthFocus=void 0,e.depthAperture=void 0,e.depthMaxBlur=void 0,e.temperature=6500,e.temperatureTint=0,e.lut=void 0,e.lutIntensity=0;const t=this.postProcessSettings;let s,r=!1,n=!1,a=!1,o=!1;if(null==this.camera)return;const h=this.camera.getWorldPosition(Pe);let c=[];for(const e of this.postProcessVolumes){if(!pe(e.object))continue;let l=e.blendWeight??1;const d=e.distanceToPoint(h);d>e.blendRadius||(e.blendRadius>0&&(l*=te(1-d/e.blendRadius,0,1)),l>1&&(l=1),l>0&&(c.push(e),void 0!==e.settings.tonemapMapping&&(t.tonemapMapping=e.settings.tonemapMapping),void 0!==e.settings.tonemapExposure&&(t.tonemapExposure=i.MathUtils.lerp(t.tonemapExposure,e.settings.tonemapExposure,l)),void 0!==e.settings.envTexture&&(t.envTexture=e.settings.envTexture),void 0!==e.settings.envIntensity&&(t.envIntensity=i.MathUtils.lerp(t.envIntensity,e.settings.envIntensity,l)),void 0!==e.settings.vignetteIntensity&&(t.vignetteIntensity=i.MathUtils.lerp(t.vignetteIntensity,e.settings.vignetteIntensity,l),n=!0),void 0!==e.settings.colorTint&&void 0!==e.settings.colorTintIntensity&&e.settings.colorTintIntensity>0&&(t.colorTint=t.colorTint.lerp(e.settings.colorTint,l),o=!0),void 0!==e.settings.colorTintIntensity&&(t.colorTintIntensity=i.MathUtils.lerp(t.colorTintIntensity,e.settings.colorTintIntensity,l)),void 0!==e.settings.depthFocus&&(r=!0,t.depthFocus=void 0!==t.depthFocus?i.MathUtils.lerp(t.depthFocus,e.settings.depthFocus,l):e.settings.depthFocus),void 0!==e.settings.depthAperture&&(r=!0,t.depthAperture=void 0!==t.depthAperture?i.MathUtils.lerp(t.depthAperture,e.settings.depthAperture,l):e.settings.depthAperture),void 0!==e.settings.depthMaxBlur&&(r=!0,t.depthMaxBlur=void 0!==t.depthMaxBlur?i.MathUtils.lerp(t.depthMaxBlur,e.settings.depthMaxBlur,l):e.settings.depthMaxBlur),void 0!==e.settings.temperature&&(t.temperature=i.MathUtils.lerp(t.temperature,e.settings.temperature,l)),void 0!==e.settings.temperatureTint&&(t.temperatureTint=i.MathUtils.lerp(t.temperatureTint,e.settings.temperatureTint,l)),void 0!==e.settings.lut&&(s=e.settings.lut,a=!0),void 0!==e.settings.lutIntensity&&(t.lutIntensity=i.MathUtils.lerp(t.lutIntensity,e.settings.lutIntensity,l),a=!0)))}this.renderer.toneMapping=t.tonemapMapping??i.NoToneMapping,this.renderer.toneMappingExposure=t.tonemapExposure,null!=t.envTexture&&(this.scene.environment=this.getEnvTexture(t.envTexture)),this.scene.environmentIntensity=t.envIntensity,this.colorPass.vignetteIntensity=t.vignetteIntensity,this.colorPass.vignetteEnabled=n,this.colorPass.colorTint=t.colorTint,this.colorPass.colorTintIntensity=t.colorTintIntensity,this.colorPass.enabled=n||o,r&&this.camera instanceof l?(this.dofPass.enabled=!0,void 0!==t.depthFocus&&(this.dofPass.uniforms.focus.value=t.depthFocus),void 0!==t.depthAperture&&(this.dofPass.uniforms.aperture.value=t.depthAperture),void 0!==t.depthMaxBlur&&(this.dofPass.uniforms.maxblur.value=t.depthMaxBlur)):this.dofPass.enabled=!1,this.colorPass.temperature=t.temperature,this.colorPass.temperatureTint=t.temperatureTint,this.lutPass.enabled=a,a&&(null!=s&&(s.flipY=!0,s.generateMipmaps=!1),this.lutPass.lut=s,this.lutPass.intensity=t.lutIntensity)}renderOverlay(){const e=Array.from(this.overlayCameras.values()).slice(0,this.maxInsetCameras),t=this.container.clientWidth/2,s=e.length*this.insetWidth+(e.length-1)*this.insetMargin;for(let i=0;i<e.length;i++)this.renderer.clearDepth(),this.renderer.setViewport(t-s/2+this.insetWidth*i+this.insetMargin*i,this.insetOffsetY,this.insetWidth,this.insetHeight),this.renderer.render(this.scene,e[i])}addOverlayCamera(e){this.overlayCameras.add(e)}clearOverlayCameras(){this.overlayCameras.clear()}removeOverlayCamera(e){this.overlayCameras.delete(e)}render(e){if(0===this.composer.renderTarget1.width||0===this.composer.renderTarget1.height)return;if(0===this.composer.renderTarget2.width||0===this.composer.renderTarget2.height)return;let t=!1;if(this.ssrPass.enabled&&!1!==this.options?.reflection?.enabled){const e=this.ssrPass.selects??[];e.length=0,this.scene.traverseVisible(t=>{t instanceof o&&!0===t.material.userData?.reflective&&isObjectInFrustum(t,ge)&&e.push(t)}),this.ssrPass.selects=e,0==e.length&&(this.ssrPass.enabled=!1),t=!0}this.options.bloom,this.scene.matrixWorldAutoUpdate=!1,this.scene.matrixAutoUpdate=!1,this.composer.render(e),this.scene.matrixWorldAutoUpdate=!0,this.scene.matrixAutoUpdate=!0,t&&(this.ssrPass.enabled=!0)}hasBloom(){return null!=$(this.scene,e=>e instanceof o&&!0===e.material?.userData?.hasBloom)}darkenNonBloomed(e){if((e instanceof o||e instanceof i.Sprite||e instanceof i.Line)&&e.visible&&(null==e.material.userData||!0!==e.material.userData.hasBloom)){if(e.material?.id===he.id)return;this.bloomStoredMaterials[e.uuid]=e.material,!0!==e.material.transparent?e.material=he:(e.visible=!1,this.bloomHidden.push(e))}else"TransformControlsPlane"!==e.type&&"TransformControlsGizmo"!==e.type||(e.visible=!1,this.bloomHidden.push(e))}restoreMaterial(e){this.bloomStoredMaterials[e.uuid]&&(e.material=this.bloomStoredMaterials[e.uuid],delete this.bloomStoredMaterials[e.uuid],e.visible=!0)}initDepthUniform(e){e instanceof c&&(e.uniforms[k].value=this.gRenderTarget.depthTexture,this.camera instanceof l&&(null!=e.uniforms[z]&&(e.uniforms[z].value=this.camera.near),null!=e.uniforms[N]&&(e.uniforms[N].value=this.camera.far)))}initNormalUniform(e){e instanceof c&&null!=e.uniforms[q]&&(e.uniforms[q].value=this.gRenderTarget.textures[0])}initResolutionUniform(e){e instanceof c&&null!=e.uniforms[_]&&e.uniforms[_].value.set(this.container.clientWidth*this.renderer.getPixelRatio(),this.container.clientHeight*this.renderer.getPixelRatio())}};ue=e=t([G(),s("design:paramtypes",[HTMLElement,Object])],ue);export{ue as RenderingView};export function setRenderingPaused(e){null!=window.editor?.viewer?.renderingView&&(window.editor.viewer.renderingView.paused=e)}const me=.05;function pe(e){let t=e;for(;t;){if(!1===t.visible)return!1;t=t.parent}return!0}V.prototype.overrideVisibility=function(){const e=this.scene,t=this._visibilityCache;e.traverse(function(e){if(t.set(e,e.visible),(e.isPoints||e.isLine||e.isTransformControls||e.isSprite)&&(e.visible=!1),null!=e.material){let t=!1,s=!1;if(Array.isArray(e.material)){for(const s of e.material)if(null!=s.alphaTest&&s.alphaTest>0){t=!0;break}}else null!=e.material.alphaTest&&e.material.alphaTest>0?t=!0:!0===e.material.userData.isDecal&&(s=!0);s&&(e.visible=!1)}})};const ge=new i.Frustum,fe=new i.Box3,ve=new i.Matrix4;export function isObjectInFrustum(e,t){const s=fe.setFromObject(e);return t.intersectsBox(s)}const Pe=new i.Vector3;/*
|
|
1
|
+
var e;import{__decorate as t,__metadata as s}from"tslib";import*as i from"three";import{Color as r,Material as n,Matrix4 as a,Mesh as o,PerspectiveCamera as l,ShaderChunk as h,ShaderMaterial as c,WebGLRenderTarget as d}from"three";import{CopyShader as u,EffectComposer as m,FXAAShader as p,GammaCorrectionShader as f,LUTPass as g,RenderPass as v,ShaderPass as P,VRButton as M}from"three-stdlib";import{CSMShader as T,CSMUtil as b}from"./csm.js";import{bool as x,colorToNormal as y,float as w,NodeShaderMaterial as R,normalize as S,rgb as C,textureSampler2d as F,varyingAttributes as E,varyingTransformed as I,vec4 as A,BooleanExpression as B,select as W,ifDefApply as U}from"three-shader-graph";import{Reflector as D}from"three-stdlib";import{BokehPass as j,OutputPass as H}from"three/examples/jsm/Addons.js";import{CSM as L}from"three/examples/jsm/csm/CSM.js";import O from"three/examples/jsm/libs/stats.module.js";import{GTAOPass as V}from"three/examples/jsm/postprocessing/GTAOPass.js";import{Service as G}from"typedi";import{depthUniformName as k,farUniformName as N,nearUniformName as z,resolutionUniformName as _,sceneNormalUniformName as q,supportsDepthTextureExtension as X}from"./shader-nodes/depth.js";import{elapsedTimeUniformName as Y}from"./shader-nodes/time.js";import{sceneMapUniformName as Q}from"./shader-nodes/scene-sample.js";import{DepthPass as K}from"./utils/three/depth-pass.js";import{GPUStatsPanel as J}from"./utils/three/gpu-stats-panel.js";import{OutlinePass as Z}from"./utils/three/outline-pass.js";import{findFirstVisibleObject as $,traverseVisibleStop as ee}from"./utils/three/traverse.js";import{clamp as te}from"./utils/math.js";import{ColorPass as se}from"./rendering/color-pass.js";import{SSRPass as ie}from"./rendering/ssr/SSRPass.js";import{SSRShader as re}from"./rendering/ssr/SSRShader.js";import{VolumetricFogPass as ne}from"./rendering/fog/volumetric-fog-pass.js";import{OutlineEffect as ae}from"./rendering/outline-effect.js";import{UnrealBloomPass as oe}from"./rendering/bloom/UnrealBloomPass.js";b.patchSetupMaterial();const le=document.createElement("div");le.style.position="absolute",le.style.left="50%",le.style.top="50%",le.style.color="black",le.style.zIndex="999";(new i.Layers).set(9);const he=new i.MeshBasicMaterial({color:"black"}),ce=new i.MeshDepthMaterial;ce.depthPacking=i.RGBADepthPacking,ce.blending=i.NoBlending,ce.side=i.DoubleSide;const de=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);let ue=e=class{setPaused(e){this.paused=e}resizeRender(){this.previousClientWith===this.container.clientWidth&&this.previousClientHeight===this.container.clientHeight||0!==this.container.clientWidth&&0!==this.container.clientHeight&&(this.previousClientWith=this.container.clientWidth,this.previousClientHeight=this.container.clientHeight,this.camera instanceof l&&(this.camera.aspect=this.container.clientWidth/this.container.clientHeight,this.camera.updateProjectionMatrix()),this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(this.container.clientWidth,this.container.clientHeight),this.composer.setSize(this.container.clientWidth*this.resolutionScale,this.container.clientHeight*this.resolutionScale),this.dofPass.setSize(this.container.clientWidth*this.resolutionScale,this.container.clientHeight*this.resolutionScale),this.aoPass.setGBuffer(this.gRenderTarget.depthTexture,this.gRenderTarget.textures[0]),this.sceneColorRenderTarget.dispose(),this.sceneColorRenderTarget=e.createSceneColorRenderTarget(this.renderer,this.container))}addPostProcessVolume(e){if(0===this.postProcessVolumes.length)this.postProcessVolumes.push(e);else{let t=!1;for(let s=0;s<this.postProcessVolumes.length;s++)if(e.priority<this.postProcessVolumes[s].priority){this.postProcessVolumes.splice(s,0,e),t=!0;break}t||this.postProcessVolumes.push(e)}}removePostProcessVolume(e){const t=this.postProcessVolumes.indexOf(e);t>-1&&this.postProcessVolumes.splice(t,1)}constructor(t,s={}){this.container=t,this.options=s,this.windowVisible=!0,this.running=!0,this.paused=!1,this.fpsCap=null,this.postProcessVolumes=[],this.postProcessSettings={},this.resolutionScale=1,this.maxPixelRatio=de?1:window.devicePixelRatio,this.onResize=()=>{if(this.resizeRender(),!this.paused)try{this.render()}catch(e){}},this.onVisiblityChane=()=>{this.windowVisible=!document.hidden},this.isDepthTextureExtensionSupported=!0,this.onLoopCallbacks=[],this.stats=new O,this._showStats=!0,this.gbufferMaterialCache=new Map,this.pmremGeneratorResults=new WeakMap,this.insetHeight=200,this.insetWidth=this.insetHeight*(16/9),this.insetOffsetY=250,this.insetMargin=10,this.maxInsetCameras=4,this.overlayCameras=new Set,this.prevClearColor=new r,this.hadBloom=!1,this.bloomStoredMaterials={},this.bloomHidden=[],null!=s.maxPixelRatio&&(this.maxPixelRatio=s.maxPixelRatio),window.renderer=this.renderer=new i.WebGLRenderer({antialias:!1,powerPreference:"high-performance"});new i.MeshStandardMaterial({color:"#ccc"});this.scene=new i.Scene,this.scene.matrixWorldAutoUpdate=!0,this.renderer.setPixelRatio(Math.min(this.maxPixelRatio,window.devicePixelRatio)*this.resolutionScale),this.renderer.setSize(t.clientWidth,t.clientHeight),this.renderer.xr.enabled=this.options.enableXR??!1,!0===this.options.enableXR&&document.body.appendChild(M.createButton(this.renderer));const n=new ae(this.renderer,{defaultThickness:.005,defaultColor:[0,0,0],defaultAlpha:1,defaultKeepAlive:!0});this.outlineEffect=n,this.gRenderTarget=new d(t.clientWidth,t.clientHeight,{count:2,minFilter:i.NearestFilter,magFilter:i.NearestFilter,type:i.HalfFloatType,format:i.RGBAFormat}),this.gRenderTarget.depthTexture=new i.DepthTexture(t.clientWidth,t.clientHeight),this.gRenderTarget.depthTexture.type=i.UnsignedShortType,this.gRenderTarget.depthTexture.minFilter=i.NearestFilter,this.gRenderTarget.depthTexture.magFilter=i.NearestFilter,this.gRenderTarget.texture.generateMipmaps=!1,this.gRenderTarget.stencilBuffer=!1,this.composer=new m(this.renderer);var a=(t.clientWidth||1)/(t.clientHeight||1);const o=new i.PerspectiveCamera(45,a,.5,800);o.layers.enable(19),this.setCamera(o),this.renderer.shadowMap.enabled=!0,this.renderer.shadowMap.type=i.PCFSoftShadowMap,this.renderer.shadowMap.autoUpdate=s.shadows?.autoUpdate??!1,this.renderer.outputColorSpace=i.SRGBColorSpace,this.renderer.toneMapping=i.NoToneMapping,this.renderer.toneMappingExposure=1,this.renderer.gammaFactor=1.4,b.renderingView=this,this.isDepthTextureExtensionSupported=X(this.renderer),t.replaceChildren(this.renderer.domElement),this.setupEventListeners(),this.aoMaskDepthRenderTarget=e.createAOMaskDepthRenderTarget(this.renderer,this.container),this.sceneColorRenderTarget=e.createSceneColorRenderTarget(this.renderer,this.container);const l=new i.Vector2(t.clientWidth,t.clientHeight),h=new v(this.scene,this.camera);this.composer.addPass(h);const c=new oe(l,1.5,.4,.85);c.threshold=1,c.strength=.9,c.radius=.5,this.bloomPass=c;const T=new V(this.scene,this.camera,t.clientWidth,t.clientWidth,{});T.normalRenderTarget?.dispose(),T.setGBuffer(this.gRenderTarget.depthTexture,this.gRenderTarget.textures[0]),T.output=V.OUTPUT.Default,T.enabled=!1,this.aoPass=T,re.fragmentShader=re.fragmentShader.replace("if(metalness==0.) return;","if(metalness<0.1) return;");const x=new ie({renderer:this.renderer,scene:this.scene,camera:this.camera,groundReflector:null,selects:[],normalTexture:this.gRenderTarget.textures[0],depthTexture:this.gRenderTarget.depthTexture});x.output=ie.OUTPUT.Default,x.blur=!0,x.fresnel=!1,x.distanceAttenuation=!0,x.maxDistance=50,x.selective=!0,x.bouncing=!1,x.opacity=.4,x.enabled=!1!==this.options?.reflection?.enabled,this.ssrPass=x,this.composer.addPass(x),!1!==this.options.ao?.enabled&&this.composer.addPass(T),this.composer.addPass(c),c.emissiveTexture=this.gRenderTarget.textures[1],this.renderer.info.autoReset=!1,this.options.bloom,this.volumetricFogPass=new ne(l),this.composer.addPass(this.volumetricFogPass),this.volumetricFogPass.enabled=!0,this.dofPass=new j(this.scene,this.camera,{focus:1,aperture:.025,maxblur:.01}),this.dofPass.enabled=!1,this.composer.addPass(this.dofPass);const y=new se;this.composer.addPass(y),this.colorPass=y,y.vignetteEnabled=!1,this.outlinePass=new Z(new i.Vector2(t.clientWidth,t.clientHeight),this.scene,this.camera),this.outlinePass.edgeGlow=0,this.outlinePass.edgeThickness=1.5,this.outlinePass.edgeStrength=5,this.outlinePass.clear=!1,this.outlinePass.enabled=!1,this.composer.addPass(this.outlinePass);const w=new P(p);w.uniforms.resolution.value.set(1/t.clientWidth,1/t.clientHeight),this.composer.addPass(w),this.fxaaPass=w,this.fxaaPass.enabled=!1,!0===s.enableOutlines&&this.setEnableOutlines(!0),new P(f).clear=!1,this.fixStatsStyle(),this.lutPass=new g({}),this.lutPass.enabled=!1,this.composer.addPass(this.lutPass);const R=new P(u,"prevtexture");R.enabled=!0,R.needsSwap=!1,R.material.uniforms.tDiffuse.value=this.gRenderTarget.textures[1],R.renderToScreen=!0;const S=new H;this.composer.addPass(S)}fixStatsStyle(){const e=this.stats.dom;e.style.position="absolute";const t=e.getElementsByTagName("canvas");for(let e=0;e<t.length;e++)t.item(e).style.display="inline-block"}setEnableOutlines(e){this.outlinePass.enabled=e,this.fxaaPass.enabled=e}setCamera(e){this.camera=e,this.composer.passes.forEach(t=>{t instanceof v?t.camera=e:t instanceof Z?t.renderCamera=e:(t instanceof K||t instanceof V)&&(t.camera=e)}),this.ssrPass&&(this.ssrPass.camera=e),this.aoPass&&(this.aoPass.camera=e),null==this.csm?(this.csm=new L({maxFar:100,lightFar:250,lightMargin:20,cascades:de?2:4,shadowMapSize:2048*(de?.5:1),lightDirection:new i.Vector3(.5,-1,-.6).normalize(),lightIntensity:.5*Math.PI,camera:this.camera,parent:this.scene,mode:"practical"}),this.csm&&Array.isArray(this.csm.lights),this.csm.fade=!0,h.lights_fragment_begin=T.lights_fragment_begin):(this.csm.camera=this.camera,this.camera),this.csm.updateFrustums()}setSelectedObjects(e){if(null==this.outlinePass)return;const t=new Map;for(const s of e)t.set(s.uuid,s);for(const s of e)s.traverse(e=>{e.uuid!==s.uuid&&t.has(e.uuid)&&t.delete(e.uuid)});this.outlinePass.selectedObjects=Array.from(t.values())}static createDepthRenderTarget(e,t,s){const r=new i.WebGLRenderTarget(t.clientWidth*s,t.clientHeight*s);return r.texture.minFilter=i.NearestFilter,r.texture.magFilter=i.NearestFilter,r.texture.generateMipmaps=!1,r.stencilBuffer=!1,r.depthTexture=new i.DepthTexture(128,128),r.depthTexture.type=i.UnsignedShortType,r.depthTexture.minFilter=i.NearestFilter,r.depthTexture.magFilter=i.NearestFilter,r}static createAOMaskDepthRenderTarget(e,t){const s=new i.WebGLRenderTarget(t.clientWidth*e.getPixelRatio(),t.clientHeight*e.getPixelRatio(),{type:i.HalfFloatType});return s.texture.minFilter=i.NearestFilter,s.texture.magFilter=i.NearestFilter,s.texture.generateMipmaps=!1,s.stencilBuffer=!1,s.depthTexture=new i.DepthTexture(128,128),s.depthTexture.type=i.UnsignedInt248Type,s.depthTexture.minFilter=i.NearestFilter,s.depthTexture.magFilter=i.NearestFilter,s}static createSceneColorRenderTarget(e,t){const s=new i.WebGLRenderTarget(t.clientWidth*e.getPixelRatio()*.5,t.clientHeight*e.getPixelRatio()*.5,{format:i.RGBAFormat,colorSpace:i.SRGBColorSpace,stencilBuffer:!1});return s.texture.minFilter=i.LinearFilter,s.texture.magFilter=i.LinearFilter,s.texture.generateMipmaps=!1,s}setupEventListeners(){window.addEventListener("resize",this.onResize),window.addEventListener("orientationchange",this.onResize),document.addEventListener("visibilitychange",this.onVisiblityChane)}stop(){this.running=!1,window.removeEventListener("resize",this.onResize),window.removeEventListener("orientationchange",this.onResize),document.removeEventListener("visibilitychange",this.onVisiblityChane),this.onLoopCallbacks=[],this.renderer.dispose(),this.gRenderTarget.dispose(),this.aoMaskDepthRenderTarget.dispose(),this.sceneColorRenderTarget.dispose(),this.csm.dispose(),this.container.replaceChildren(),this.volumetricFogPass.dispose(),b.clearSceneCache(this.scene)}onLoop(e){this.onLoopCallbacks.push(e)}removeOnLoop(e){const t=this.onLoopCallbacks.find(e);t>=0&&this.onLoopCallbacks.splice(t,1)}set showStats(e){this._showStats=e,this._showStats&&!this.container.contains(this.stats.dom)?this.container.appendChild(this.stats.dom):!this._showStats&&this.container.contains(this.stats.dom)&&this.container.removeChild(this.stats.dom)}get showStats(){return this._showStats}applyEnvMap(e){null!=this.scene.environment&&e instanceof R&&(null==e.envMap||e.userData.useSceneEnv)&&(e.userData.useSceneEnv=!0,null==e.uniforms.envMap&&(e.uniforms.envMap={value:this.scene.environment},e.uniformsNeedUpdate=!0),null==e.uniforms.envMapIntensity&&(e.uniforms.envMapIntensity={value:1*Math.PI},e.uniformsNeedUpdate=!0),e.uniforms.envMap.value=this.scene.environment,e.uniforms.envMapIntensity.value=this.scene.environmentIntensity*Math.PI,e.envMap=this.scene.environment)}setupCsm(e){if(e instanceof i.Mesh||e instanceof i.SkinnedMesh)if(e.material instanceof Array)for(const t of e.material)this.csm.setupMaterial(t);else this.csm.setupMaterial(e.material)}createGBufferMaterial(e){let t=this.gbufferMaterialCache.get(e);if(null==t){let r=I.normal;e instanceof i.MeshStandardMaterial||e instanceof i.MeshLambertMaterial||e instanceof i.MeshToonMaterial||e instanceof i.MeshPhongMaterial?null!=e.normalMap&&(r=y(F(e.normalMap).sample(E.uv),e.normalScale.x??1)):e instanceof R&&null!=e.outputNormal&&(r=e.outputNormal),!0!==e.userData.disableAO&&(r=U("DOUBLE_SIDED",r,e=>W(new B("gl_FrontFacing"),e,e.multiplyScalar(-1))));let n=e.userData?.reflective?w(0):w(1);e instanceof i.MeshStandardMaterial?n=null!=e.roughnessMap?F(e.roughnessMap).sample(E.uv).r.multiply(e.roughness??1):w(e.roughness??1):e instanceof R&&null!=e.outputRoughness&&(n=e.outputRoughness);const a=!0===e.userData.hasBloom;let o=C(0);a&&(e instanceof i.MeshStandardMaterial||e instanceof i.MeshLambertMaterial||e instanceof i.MeshToonMaterial||e instanceof i.MeshPhongMaterial?(o=null!=e.emissiveMap?F(e.emissiveMap).sample(E.uv).rgb.multiply(C(e.emissive??0)):C(e.emissive??0),o=o.multiplyScalar(e.emissiveIntensity)):e instanceof R&&(o=e.outputEmissive??e.outputColor.rgb.multiplyScalar(e.outputColor.a)));let l=w(1);e instanceof i.MeshStandardMaterial||e instanceof i.MeshLambertMaterial||e instanceof i.MeshBasicMaterial||e instanceof i.MeshToonMaterial||e instanceof i.MeshPhongMaterial?(null!=e.alphaMap&&(l=F(e.alphaMap).sample(E.uv).r),null!=e.map&&(l=l.multiply(F(e.map).sample(E.uv).a))):e instanceof R&&null!=e.opacity&&(l=e.outputOpacity.multiply(e.opacity??1));const h=e instanceof c&&null!=e.uniforms[k],d=e.transparent&&e.alphaTest<=0,u=e.alphaTest>0?l.lt(e.alphaTest):d?l.lt(.8):x(!1);let m;m=(s=r,S(s).multiplyScalar(.5).addScalar(.5)).rgba(n);let p=e instanceof R?e.outputPosition:void 0;t=new R({position:p,outputs:[m,o.rgba(e.alphaTest)],outputEncoding:!1,fog:!1,transparent:!1,lights:!1,discard:u}),t.toneMapped=!1,t.side=e.side,d&&a&&(t.transparent=!0,t.blending=i.AdditiveBlending),t.depthWrite=!0,t.depthTest=!0,t.visible=e.visible&&e.depthTest&&!h&&t.visible,e instanceof c&&Object.assign(t.uniforms,e.uniforms),this.gbufferMaterialCache.set(e,t)}var s;if(e instanceof c)for(const s in e.uniforms)null!=t.uniforms[s]&&(t.uniforms[s].value=e.uniforms[s].value);return t}loop(e,t=!1){const s=this.stats,r=s.addPanel(new O.Panel("Calls","#83f","#002")),n=s.addPanel(new O.Panel("Triangles","#c32","#002"));let l;navigator.userAgent.includes("Chrome")&&navigator.userAgent.includes("HologyEngine")&&(l=new J(this.renderer.getContext()),s.addPanel(l)),this.showStats=t;let h=10,d=1e3;const u=()=>{const e=this.renderer.info.render.calls;e>h&&(h=e,setTimeout(()=>h=10,5e3)),r.update(e,h);const t=this.renderer.info.render.triangles;t>d&&(d=t,setTimeout(()=>d=1e3,5e3)),n.update(t,d)};performance.now();i.Ray.prototype.intersectTriangle;const m=[],p=[],f=[];let g=0;const v=new a,P=new a;let M=0;let T=this.paused;const b=t=>{const r=this.renderer.getContext();if(this.paused&&this.running&&r.drawingBufferHeight>1)return setTimeout(()=>b(t),500),void(T=!0);this.renderer.clear(),this.applyPostProcessSettings(),this.renderer.autoClear=!1,this.renderer.clear(),this.renderer.setViewport(0,0,this.container.clientWidth,this.container.clientHeight),this.camera,s.begin(),this.showStats&&l?.startQuery(),this.ssrPass.gpuPanel=l;let n=(t*=.001)-g;if(g=t,T&&(n=.016,T=!1),v.copy(this.camera.matrixWorld),n>1){let t=n;for(;t>.05;)e(me),t-=me;e(t)}else e(n);this.onLoopCallbacks.forEach(e=>e(n)),this.camera?.updateMatrixWorld(),P.copy(this.camera.matrixWorld),t-M>.08&&!P.equals(v)&&(this.renderer.shadowMap.needsUpdate=!0,M=t),this.resizeRender(),this.csm.update();let a=!1;m.length=0,p.length=0,f.length=0;const h=fe;ve.multiplyMatrices(this.camera.projectionMatrix,this.camera.matrixWorldInverse),h.setFromProjectionMatrix(ve),this.scene.traverseVisible(e=>{if(this.setupCsm(e),this.outlineEffect.apply(e),(e instanceof o||e instanceof i.Sprite)&&(this.initResolutionUniform(e.material),this.initNormalUniform(e.material),null!=this.scene.environment&&function(e,t){if(Array.isArray(e.material))for(const s of e.material)t(s);else null!=e.material&&t(e.material)}(e,e=>this.applyEnvMap(e)),!0===e.material?.userData?.hasBloom&&(a=!0)),(e instanceof o||e instanceof i.Sprite)&&e.visible&&(e.material?.userData?.water||e.material?.uniforms&&null!=e.material?.uniforms[k])&&isObjectInFrustum(e,h)?this.initDepthUniform(e.material):e instanceof D&&(e.visible=!1,p.push(e)),(e instanceof o||e instanceof i.Sprite)&&e.material?.uniforms&&null!=e.material?.uniforms[Q]&&isObjectInFrustum(e,h)&&(e.visible=!1,f.push(e),e.material.uniforms[Q].value=this.sceneColorRenderTarget.texture),e instanceof o&&e.material?.uniforms&&null!=e.material?.uniforms[Y])e.material.uniforms[Y].value=t;else if(e instanceof o&&Array.isArray(e.material))for(const s of e.material)s.uniforms&&null!=s.uniforms[Y]&&(s.uniforms[Y].value=t)}),this.bloomPass.enabled=a;const d=new Map,x=new Map;ee(this.scene,e=>{if(e instanceof i.Sprite||e.isPoints||e.isLine||e.isTransformControls||e.isTransformControlsGizmo)return x.set(e,e.visible),e.visible=!1,!1;if(e instanceof o)if(d.set(e,e.material),Array.isArray(e.material)){e.material=e.material.slice();for(let s=0;s<e.material.length;s++)e.material[s]=this.createGBufferMaterial(e.material[s]),e.material[s]instanceof c&&null!=e.material[s].uniforms[Y]&&(e.material[s].uniforms[Y].value=t)}else e.material=this.createGBufferMaterial(e.material),e.material instanceof c&&null!=e.material.uniforms[Y]&&(e.material.uniforms[Y].value=t)});const y=this.renderer.shadowMap.autoUpdate;this.renderer.shadowMap.autoUpdate=!1,this.renderer.setRenderTarget(this.gRenderTarget),this.renderer.clear();try{this.renderer.render(this.scene,this.camera)}catch(e){console.warn("Render failed",e)}if(this.renderer.setRenderTarget(null),d.forEach((e,t)=>{t.material=e}),x.forEach((e,t)=>{t.visible=e}),this.renderer.shadowMap.autoUpdate=y,f.length>0){if(this.renderer.setRenderTarget(this.sceneColorRenderTarget),!this.paused&&null!=this.camera)try{this.renderer.clear(!0,!0,!1);try{this.renderer.render(this.scene,this.camera)}catch(e){}}catch(e){console.warn("Error rendering scene color:",e)}this.renderer.setRenderTarget(null)}m.forEach(e=>e.visible=!0),p.forEach(e=>e.visible=!0),f.forEach(e=>e.visible=!0),this.aoPass.enabled,this.ssrPass&&(this.ssrPass.elapsedTime=t),this.volumetricFogPass&&this.volumetricFogPass.update(this.camera,this.gRenderTarget,this.csm,this.scene);try{!this.paused&&this.running&&(this.render(n),this.showStats&&l?.endQuery(),this.showStats&&u(),this.renderer.info.reset(),this.renderOverlay())}catch(e){console.warn(e)}s.end(),this.csm?.update(),this.running&&!0!==this.options.enableXR&&(this.fpsCap?setTimeout(()=>{requestAnimationFrame(b)},1e3/this.fpsCap):requestAnimationFrame(b))};!0===this.options.enableXR?this.renderer.setAnimationLoop(b):requestAnimationFrame(b)}getEnvTexture(e){null==this.pmremGenerator&&(this.pmremGenerator=new i.PMREMGenerator(this.renderer),this.pmremGenerator.compileEquirectangularShader());let t=this.pmremGeneratorResults.get(e);return null==t&&(t=this.pmremGenerator.fromEquirectangular(e).texture,this.pmremGeneratorResults.set(e,t)),t}applyPostProcessSettings(){if(0==this.postProcessVolumes.length)return this.lutPass.enabled=!1,void(this.colorPass.enabled=!1);var e;(e=this.postProcessSettings).tonemapMapping=void 0,e.tonemapExposure=1,e.envIntensity=1,e.envTexture=void 0,e.vignetteIntensity=0,e.colorTint=new i.Color("white"),e.colorTintIntensity=0,e.depthFocus=void 0,e.depthAperture=void 0,e.depthMaxBlur=void 0,e.temperature=6500,e.temperatureTint=0,e.lut=void 0,e.lutIntensity=0;const t=this.postProcessSettings;let s,r=!1,n=!1,a=!1,o=!1;if(null==this.camera)return;const h=this.camera.getWorldPosition(Pe);let c=[];for(const e of this.postProcessVolumes){if(!pe(e.object))continue;let l=e.blendWeight??1;const d=e.distanceToPoint(h);d>e.blendRadius||(e.blendRadius>0&&(l*=te(1-d/e.blendRadius,0,1)),l>1&&(l=1),l>0&&(c.push(e),void 0!==e.settings.tonemapMapping&&(t.tonemapMapping=e.settings.tonemapMapping),void 0!==e.settings.tonemapExposure&&(t.tonemapExposure=i.MathUtils.lerp(t.tonemapExposure,e.settings.tonemapExposure,l)),void 0!==e.settings.envTexture&&(t.envTexture=e.settings.envTexture),void 0!==e.settings.envIntensity&&(t.envIntensity=i.MathUtils.lerp(t.envIntensity,e.settings.envIntensity,l)),void 0!==e.settings.vignetteIntensity&&(t.vignetteIntensity=i.MathUtils.lerp(t.vignetteIntensity,e.settings.vignetteIntensity,l),n=!0),void 0!==e.settings.colorTint&&void 0!==e.settings.colorTintIntensity&&e.settings.colorTintIntensity>0&&(t.colorTint=t.colorTint.lerp(e.settings.colorTint,l),o=!0),void 0!==e.settings.colorTintIntensity&&(t.colorTintIntensity=i.MathUtils.lerp(t.colorTintIntensity,e.settings.colorTintIntensity,l)),void 0!==e.settings.depthFocus&&(r=!0,t.depthFocus=void 0!==t.depthFocus?i.MathUtils.lerp(t.depthFocus,e.settings.depthFocus,l):e.settings.depthFocus),void 0!==e.settings.depthAperture&&(r=!0,t.depthAperture=void 0!==t.depthAperture?i.MathUtils.lerp(t.depthAperture,e.settings.depthAperture,l):e.settings.depthAperture),void 0!==e.settings.depthMaxBlur&&(r=!0,t.depthMaxBlur=void 0!==t.depthMaxBlur?i.MathUtils.lerp(t.depthMaxBlur,e.settings.depthMaxBlur,l):e.settings.depthMaxBlur),void 0!==e.settings.temperature&&(t.temperature=i.MathUtils.lerp(t.temperature,e.settings.temperature,l)),void 0!==e.settings.temperatureTint&&(t.temperatureTint=i.MathUtils.lerp(t.temperatureTint,e.settings.temperatureTint,l)),void 0!==e.settings.lut&&(s=e.settings.lut,a=!0),void 0!==e.settings.lutIntensity&&(t.lutIntensity=i.MathUtils.lerp(t.lutIntensity,e.settings.lutIntensity,l),a=!0)))}this.renderer.toneMapping=t.tonemapMapping??i.NoToneMapping,this.renderer.toneMappingExposure=t.tonemapExposure,null!=t.envTexture&&(this.scene.environment=this.getEnvTexture(t.envTexture)),this.scene.environmentIntensity=t.envIntensity,this.colorPass.vignetteIntensity=t.vignetteIntensity,this.colorPass.vignetteEnabled=n,this.colorPass.colorTint=t.colorTint,this.colorPass.colorTintIntensity=t.colorTintIntensity,this.colorPass.enabled=n||o,r&&this.camera instanceof l?(this.dofPass.enabled=!0,void 0!==t.depthFocus&&(this.dofPass.uniforms.focus.value=t.depthFocus),void 0!==t.depthAperture&&(this.dofPass.uniforms.aperture.value=t.depthAperture),void 0!==t.depthMaxBlur&&(this.dofPass.uniforms.maxblur.value=t.depthMaxBlur)):this.dofPass.enabled=!1,this.colorPass.temperature=t.temperature,this.colorPass.temperatureTint=t.temperatureTint,this.lutPass.enabled=a,a&&(null!=s&&(s.flipY=!0,s.generateMipmaps=!1),this.lutPass.lut=s,this.lutPass.intensity=t.lutIntensity)}renderOverlay(){const e=Array.from(this.overlayCameras.values()).slice(0,this.maxInsetCameras),t=this.container.clientWidth/2,s=e.length*this.insetWidth+(e.length-1)*this.insetMargin;for(let i=0;i<e.length;i++)this.renderer.clearDepth(),this.renderer.setViewport(t-s/2+this.insetWidth*i+this.insetMargin*i,this.insetOffsetY,this.insetWidth,this.insetHeight),this.renderer.render(this.scene,e[i])}addOverlayCamera(e){this.overlayCameras.add(e)}clearOverlayCameras(){this.overlayCameras.clear()}removeOverlayCamera(e){this.overlayCameras.delete(e)}render(e){if(0===this.composer.renderTarget1.width||0===this.composer.renderTarget1.height)return;if(0===this.composer.renderTarget2.width||0===this.composer.renderTarget2.height)return;let t=!1;if(this.ssrPass.enabled&&!1!==this.options?.reflection?.enabled){const e=this.ssrPass.selects??[];e.length=0,this.scene.traverseVisible(t=>{t instanceof o&&!0===t.material.userData?.reflective&&isObjectInFrustum(t,fe)&&e.push(t)}),this.ssrPass.selects=e,0==e.length&&(this.ssrPass.enabled=!1),t=!0}this.options.bloom,this.scene.matrixWorldAutoUpdate=!1,this.scene.matrixAutoUpdate=!1,this.composer.render(e),this.scene.matrixWorldAutoUpdate=!0,this.scene.matrixAutoUpdate=!0,t&&(this.ssrPass.enabled=!0)}hasBloom(){return null!=$(this.scene,e=>e instanceof o&&!0===e.material?.userData?.hasBloom)}darkenNonBloomed(e){if((e instanceof o||e instanceof i.Sprite||e instanceof i.Line)&&e.visible&&(null==e.material.userData||!0!==e.material.userData.hasBloom)){if(e.material?.id===he.id)return;this.bloomStoredMaterials[e.uuid]=e.material,!0!==e.material.transparent?e.material=he:(e.visible=!1,this.bloomHidden.push(e))}else"TransformControlsPlane"!==e.type&&"TransformControlsGizmo"!==e.type||(e.visible=!1,this.bloomHidden.push(e))}restoreMaterial(e){this.bloomStoredMaterials[e.uuid]&&(e.material=this.bloomStoredMaterials[e.uuid],delete this.bloomStoredMaterials[e.uuid],e.visible=!0)}initDepthUniform(e){e instanceof c&&(e.uniforms[k].value=this.gRenderTarget.depthTexture,this.camera instanceof l&&(null!=e.uniforms[z]&&(e.uniforms[z].value=this.camera.near),null!=e.uniforms[N]&&(e.uniforms[N].value=this.camera.far)))}initNormalUniform(e){e instanceof c&&null!=e.uniforms[q]&&(e.uniforms[q].value=this.gRenderTarget.textures[0])}initResolutionUniform(e){e instanceof c&&null!=e.uniforms[_]&&e.uniforms[_].value.set(this.container.clientWidth*this.renderer.getPixelRatio(),this.container.clientHeight*this.renderer.getPixelRatio())}};ue=e=t([G(),s("design:paramtypes",[HTMLElement,Object])],ue);export{ue as RenderingView};export function setRenderingPaused(e){null!=window.editor?.viewer?.renderingView&&(window.editor.viewer.renderingView.paused=e)}const me=.05;function pe(e){let t=e;for(;t;){if(!1===t.visible)return!1;t=t.parent}return!0}V.prototype.overrideVisibility=function(){const e=this.scene,t=this._visibilityCache;e.traverse(function(e){if(t.set(e,e.visible),(e.isPoints||e.isLine||e.isTransformControls||e.isSprite)&&(e.visible=!1),null!=e.material){let t=!1,s=!1;if(Array.isArray(e.material)){for(const s of e.material)if(null!=s.alphaTest&&s.alphaTest>0){t=!0;break}}else null!=e.material.alphaTest&&e.material.alphaTest>0?t=!0:!0===e.material.userData.isDecal&&(s=!0);s&&(e.visible=!1)}})};const fe=new i.Frustum,ge=new i.Box3,ve=new i.Matrix4;export function isObjectInFrustum(e,t){const s=ge.setFromObject(e);return t.intersectsBox(s)}const Pe=new i.Vector3;/*
|
|
2
2
|
* Copyright (©) 2025 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Color, Material, Texture } from 'three';
|
|
1
|
+
import { Color, Material, Texture, Vector2 } from 'three';
|
|
2
2
|
import { Shader } from '../shader.js';
|
|
3
3
|
export declare class ToonShader extends Shader {
|
|
4
4
|
color: Color;
|
|
@@ -8,9 +8,14 @@ export declare class ToonShader extends Shader {
|
|
|
8
8
|
lightSteps: number[];
|
|
9
9
|
lightMap?: Texture;
|
|
10
10
|
lightMapIntensity?: number;
|
|
11
|
+
normalMap?: Texture;
|
|
12
|
+
normalScale?: Vector2;
|
|
11
13
|
aoMap?: Texture;
|
|
12
14
|
aoMapIntensity?: number;
|
|
13
15
|
alphaMap?: Texture;
|
|
16
|
+
emissive?: Color;
|
|
17
|
+
emissiveIntensity?: number;
|
|
18
|
+
emissiveMap?: Texture;
|
|
14
19
|
build(): Material;
|
|
15
20
|
}
|
|
16
21
|
//# sourceMappingURL=toon-shader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toon-shader.d.ts","sourceRoot":"","sources":["../../../src/shader/builtin/toon-shader.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"toon-shader.d.ts","sourceRoot":"","sources":["../../../src/shader/builtin/toon-shader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAe,QAAQ,EAA+B,OAAO,EAAE,OAAO,EAAC,MAAM,OAAO,CAAC;AAIlG,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,qBAAa,UAAW,SAAQ,MAAM;IACvB,KAAK,EAAE,KAAK,CAAwB;IACtB,OAAO,EAAE,MAAM,CAAI;IAClB,SAAS,EAAE,MAAM,CAAI;IACpC,GAAG,CAAC,EAAE,OAAO,CAAC;IAC2B,UAAU,EAAE,MAAM,EAAE,CAAK;IAClE,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,WAAW,CAAC,EAAE,OAAO,CAAoB;IACzC,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,KAAK,CAAC;IACF,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC1C,WAAW,CAAC,EAAE,OAAO,CAAC;IAGnC,KAAK,IAAI,QAAQ;CAgClB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{__decorate as t,__metadata as e}from"tslib";import{Color as o,DataTexture as i,MeshToonMaterial as p,RedFormat as
|
|
1
|
+
import{__decorate as t,__metadata as e}from"tslib";import{Color as o,DataTexture as i,MeshToonMaterial as p,RedFormat as s,Texture as a,Vector2 as n}from"three";import{removeObjectUndefined as r}from"../../utils/collections.js";import{Parameter as h}from"../parameter.js";import{Shader as d}from"../shader.js";export class ToonShader extends d{constructor(){super(...arguments),this.color=new o("#FFFFFF"),this.opacity=1,this.intensity=1,this.lightSteps=[],this.normalScale=new n(1,1)}build(){let t;if(null!=this.lightSteps&&this.lightSteps.length>0){const e=new Uint8Array(this.lightSteps.length);for(let t=0;t<=e.length;t++)e[t]=255*this.lightSteps[t];t=new i(e,e.length,1,s),t.needsUpdate=!0}return new p(r({color:this.color.multiplyScalar(this.intensity),opacity:this.opacity,map:this.map,gradientMap:t,lightMap:this.lightMap,lightMapIntensity:this.lightMapIntensity,normalMap:this.normalMap,normalScale:this.normalScale,emissive:this.emissive,emissiveIntensity:this.emissiveIntensity,emissiveMap:this.emissiveMap,aoMap:this.aoMap,aoMapIntensity:this.aoMapIntensity,alphaMap:this.alphaMap}))}}t([h(),e("design:type",o)],ToonShader.prototype,"color",void 0),t([h({range:[0,1]}),e("design:type",Number)],ToonShader.prototype,"opacity",void 0),t([h({range:[0,10]}),e("design:type",Number)],ToonShader.prototype,"intensity",void 0),t([h(),e("design:type",a)],ToonShader.prototype,"map",void 0),t([h({type:Number,array:!0,range:[0,1]}),e("design:type",Array)],ToonShader.prototype,"lightSteps",void 0),t([h(),e("design:type",a)],ToonShader.prototype,"lightMap",void 0),t([h(),e("design:type",Number)],ToonShader.prototype,"lightMapIntensity",void 0),t([h(),e("design:type",a)],ToonShader.prototype,"normalMap",void 0),t([h(),e("design:type",n)],ToonShader.prototype,"normalScale",void 0),t([h(),e("design:type",a)],ToonShader.prototype,"aoMap",void 0),t([h(),e("design:type",Number)],ToonShader.prototype,"aoMapIntensity",void 0),t([h(),e("design:type",a)],ToonShader.prototype,"alphaMap",void 0),t([h(),e("design:type",o)],ToonShader.prototype,"emissive",void 0),t([h({range:[0,10]}),e("design:type",Number)],ToonShader.prototype,"emissiveIntensity",void 0),t([h(),e("design:type",a)],ToonShader.prototype,"emissiveMap",void 0);/*
|
|
2
2
|
* Copyright (©) 2025 Hology Interactive AB. All rights reserved.
|
|
3
3
|
* See the LICENSE.md file for details.
|
|
4
4
|
*/
|