UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 7.62 kB
import{EventHandler as t}from"../core/event-handler.js";import{Color as e}from"../core/math/color.js";import{Vec3 as s}from"../core/math/vec3.js";import{Quat as i}from"../core/math/quat.js";import{math as a}from"../core/math/math.js";import{Mat3 as n}from"../core/math/mat3.js";import{Mat4 as h}from"../core/math/mat4.js";import{ADDRESS_CLAMP_TO_EDGE as r,FILTER_LINEAR as o,PIXELFORMAT_RGBA8 as l}from"../platform/graphics/constants.js";import{BAKE_COLORDIR as m,FOG_NONE as g,GAMMA_SRGB as p,LAYERID_IMMEDIATE as y}from"./constants.js";import{LightingParams as _}from"./lighting/lighting-params.js";import{Sky as u}from"./skybox/sky.js";import{Immediate as b}from"./immediate/immediate.js";import{EnvLighting as d}from"./graphics/env-lighting.js";class k extends t{constructor(t){super(),this.ambientBake=!1,this.ambientBakeOcclusionBrightness=0,this.ambientBakeOcclusionContrast=0,this.ambientLight=new e(0,0,0),this.ambientLuminance=0,this.exposure=1,this.fogColor=new e(0,0,0),this.fogDensity=0,this.fogEnd=1e3,this.fogStart=1,this.lightmapSizeMultiplier=1,this.lightmapMaxResolution=2048,this.lightmapMode=m,this.lightmapFilterEnabled=!1,this.lightmapHDR=!1,this.root=null,this.physicalUnits=!1,this._envAtlas=null,this._skyboxCubeMap=null,this.device=t,this._gravity=new s(0,-9.8,0),this._layers=null,this._fog=g,this._gammaCorrection=p,this._toneMapping=0,this._prefilteredCubemaps=[],this._internalEnvAtlas=null,this._skyboxIntensity=1,this._skyboxLuminance=0,this._skyboxMip=0,this._skyboxRotationShaderInclude=!1,this._skyboxRotation=new i,this._skyboxRotationMat3=new n,this._skyboxRotationMat4=new h,this._ambientBakeNumSamples=1,this._ambientBakeSpherePart=.4,this._lightmapFilterRange=10,this._lightmapFilterSmoothness=.2,this._clusteredLightingEnabled=!0,this._lightingParams=new _(this.device.supportsAreaLights,this.device.maxTextureSize,(()=>{this.updateShaders=!0})),this._sky=new u(this),this._stats={meshInstances:0,lights:0,dynamicLights:0,bakedLights:0,updateShadersTime:0},this.updateShaders=!0,this._shaderVersion=0,this.immediate=new b(this.device)}get defaultDrawLayer(){return this.layers.getLayerById(y)}set ambientBakeNumSamples(t){this._ambientBakeNumSamples=a.clamp(Math.floor(t),1,255)}get ambientBakeNumSamples(){return this._ambientBakeNumSamples}set ambientBakeSpherePart(t){this._ambientBakeSpherePart=a.clamp(t,.001,1)}get ambientBakeSpherePart(){return this._ambientBakeSpherePart}set clusteredLightingEnabled(t){this.device.isWebGPU&&!t||(this._clusteredLightingEnabled||!t?this._clusteredLightingEnabled=t:console.error("Turning on disabled clustered lighting is not currently supported"))}get clusteredLightingEnabled(){return this._clusteredLightingEnabled}set envAtlas(t){t!==this._envAtlas&&(this._envAtlas=t,t&&(t.addressU=r,t.addressV=r,t.minFilter=o,t.magFilter=o,t.mipmaps=!1),this._prefilteredCubemaps=[],this._internalEnvAtlas&&(this._internalEnvAtlas.destroy(),this._internalEnvAtlas=null),this._resetSkyMesh())}get envAtlas(){return this._envAtlas}set fog(t){t!==this._fog&&(this._fog=t,this.updateShaders=!0)}get fog(){return this._fog}set gammaCorrection(t){t!==this._gammaCorrection&&(this._gammaCorrection=t,this.updateShaders=!0)}get gammaCorrection(){return this._gammaCorrection}set layers(t){const e=this._layers;this._layers=t,this.fire("set:layers",e,t)}get layers(){return this._layers}get sky(){return this._sky}get lighting(){return this._lightingParams}set lightmapFilterRange(t){this._lightmapFilterRange=Math.max(t,.001)}get lightmapFilterRange(){return this._lightmapFilterRange}set lightmapFilterSmoothness(t){this._lightmapFilterSmoothness=Math.max(t,.001)}get lightmapFilterSmoothness(){return this._lightmapFilterSmoothness}set prefilteredCubemaps(t){t=t||[];const e=this._prefilteredCubemaps;if(e.length!==t.length||e.some(((e,s)=>e!==t[s]))){6===t.length&&t.every((t=>!!t))?(this._internalEnvAtlas=d.generatePrefilteredAtlas(t,{target:this._internalEnvAtlas}),this._envAtlas=this._internalEnvAtlas):(this._internalEnvAtlas&&(this._internalEnvAtlas.destroy(),this._internalEnvAtlas=null),this._envAtlas=null),this._prefilteredCubemaps=t.slice(),this._resetSkyMesh()}}get prefilteredCubemaps(){return this._prefilteredCubemaps}set skybox(t){t!==this._skyboxCubeMap&&(this._skyboxCubeMap=t,this._resetSkyMesh())}get skybox(){return this._skyboxCubeMap}set skyboxIntensity(t){t!==this._skyboxIntensity&&(this._skyboxIntensity=t,this._resetSkyMesh())}get skyboxIntensity(){return this._skyboxIntensity}set skyboxLuminance(t){t!==this._skyboxLuminance&&(this._skyboxLuminance=t,this._resetSkyMesh())}get skyboxLuminance(){return this._skyboxLuminance}set skyboxMip(t){t!==this._skyboxMip&&(this._skyboxMip=t,this._resetSkyMesh())}get skyboxMip(){return this._skyboxMip}set skyboxRotation(t){if(!this._skyboxRotation.equals(t)){const e=t.equals(i.IDENTITY);this._skyboxRotation.copy(t),e?this._skyboxRotationMat3.setIdentity():(this._skyboxRotationMat4.setTRS(s.ZERO,t,s.ONE),this._skyboxRotationMat3.invertMat4(this._skyboxRotationMat4)),this._skyboxRotationShaderInclude||e||(this._skyboxRotationShaderInclude=!0,this._resetSkyMesh())}}get skyboxRotation(){return this._skyboxRotation}set toneMapping(t){t!==this._toneMapping&&(this._toneMapping=t,this.updateShaders=!0)}get toneMapping(){return this._toneMapping}destroy(){this._resetSkyMesh(),this.root=null,this.off()}drawLine(t,s,i=e.WHITE,a=!0,n=this.defaultDrawLayer){this.immediate.getBatch(n,a).addLines([t,s],[i,i])}drawLines(t,e,s=!0,i=this.defaultDrawLayer){this.immediate.getBatch(i,s).addLines(t,e)}drawLineArrays(t,e,s=!0,i=this.defaultDrawLayer){this.immediate.getBatch(i,s).addLinesArrays(t,e)}applySettings(t){var e,s,a,n;const h=t.physics,r=t.render;this._gravity.set(h.gravity[0],h.gravity[1],h.gravity[2]),this.ambientLight.set(r.global_ambient[0],r.global_ambient[1],r.global_ambient[2]),this.ambientLuminance=r.ambientLuminance,this._fog=r.fog,this.fogColor.set(r.fog_color[0],r.fog_color[1],r.fog_color[2]),this.fogStart=r.fog_start,this.fogEnd=r.fog_end,this.fogDensity=r.fog_density,this._gammaCorrection=r.gamma_correction,this._toneMapping=r.tonemapping,this.lightmapSizeMultiplier=r.lightmapSizeMultiplier,this.lightmapMaxResolution=r.lightmapMaxResolution,this.lightmapMode=r.lightmapMode,this.exposure=r.exposure,this._skyboxIntensity=null!=(e=r.skyboxIntensity)?e:1,this._skyboxLuminance=null!=(s=r.skyboxLuminance)?s:2e4,this._skyboxMip=null!=(a=r.skyboxMip)?a:0,r.skyboxRotation&&(this.skyboxRotation=(new i).setFromEulerAngles(r.skyboxRotation[0],r.skyboxRotation[1],r.skyboxRotation[2])),this.sky.applySettings(r),this.clusteredLightingEnabled=null!=(n=r.clusteredLightingEnabled)&&n,this.lighting.applySettings(r),["lightmapFilterEnabled","lightmapFilterRange","lightmapFilterSmoothness","ambientBake","ambientBakeNumSamples","ambientBakeSpherePart","ambientBakeOcclusionBrightness","ambientBakeOcclusionContrast"].forEach((t=>{r.hasOwnProperty(t)&&(this[t]=r[t])})),this._resetSkyMesh()}_getSkyboxTex(){const t=this._prefilteredCubemaps;if(this._skyboxMip){return t[[0,1,3,4,5,6][this._skyboxMip]]||this._envAtlas||t[0]||this._skyboxCubeMap}return this._skyboxCubeMap||t[0]||this._envAtlas}_updateSkyMesh(){this.sky.skyMesh||this.sky.updateSkyMesh(),this.sky.update()}_resetSkyMesh(){this.sky.resetSkyMesh(),this.updateShaders=!0}setSkybox(t){t?(this.skybox=t[0]||null,t[1]&&!t[1].cubemap?this.envAtlas=t[1]:this.prefilteredCubemaps=t.slice(1)):(this.skybox=null,this.envAtlas=null)}get lightmapPixelFormat(){return this.lightmapHDR&&this.device.getRenderableHdrFormat()||l}}k.EVENT_SETLAYERS="set:layers",k.EVENT_SETSKYBOX="set:skybox";export{k as Scene};