UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

86 lines (85 loc) 3.05 kB
import { math } from "../../core/math/math.js"; import { LIGHTTYPE_OMNI, LIGHTTYPE_SPOT } from "../constants.js"; import { ShadowMap } from "./shadow-map.js"; import { RenderPassShadowLocalNonClustered } from "./render-pass-shadow-local-non-clustered.js"; class ShadowRendererLocal { // temporary list to collect lights to render shadows for shadowLights = []; renderer; shadowRenderer; device; constructor(renderer, shadowRenderer) { this.renderer = renderer; this.shadowRenderer = shadowRenderer; this.device = renderer.device; } // cull local shadow map cull(light, comp, casters = null) { const isClustered = this.renderer.scene.clusteredLightingEnabled; light.visibleThisFrame = true; if (!isClustered) { if (!light._shadowMap) { light._shadowMap = ShadowMap.create(this.device, light); } } const type = light._type; const faceCount = type === LIGHTTYPE_SPOT ? 1 : 6; for (let face = 0; face < faceCount; face++) { const lightRenderData = light.getRenderData(null, face); const shadowCam = lightRenderData.shadowCamera; shadowCam.nearClip = light.attenuationEnd / 1e3; shadowCam.farClip = light.attenuationEnd; const shadowCamNode = shadowCam._node; const lightNode = light._node; shadowCamNode.setPosition(lightNode.getPosition()); if (type === LIGHTTYPE_SPOT) { shadowCam.fov = light._outerConeAngle * 2; shadowCamNode.setRotation(lightNode.getRotation()); shadowCamNode.rotateLocal(-90, 0, 0); } else if (type === LIGHTTYPE_OMNI) { if (isClustered) { const tileSize = this.shadowRenderer.lightTextureAtlas.shadowAtlasResolution * light.atlasViewport.z / 3; const texelSize = 2 / tileSize; const filterSize = texelSize * this.shadowRenderer.lightTextureAtlas.shadowEdgePixels; shadowCam.fov = Math.atan(1 + filterSize) * math.RAD_TO_DEG * 2; } else { shadowCam.fov = 90; } } this.renderer.updateCameraFrustum(shadowCam); this.shadowRenderer.cullShadowCasters(comp, light, lightRenderData.visibleCasters, shadowCam, casters); } } prepareLights(shadowLights, lights) { let shadowCamera; for (let i = 0; i < lights.length; i++) { const light = lights[i]; if (this.shadowRenderer.needsShadowRendering(light) && light.atlasViewportAllocated) { shadowLights.push(light); for (let face = 0; face < light.numShadowFaces; face++) { shadowCamera = this.shadowRenderer.prepareFace(light, null, face); } } } return shadowCamera; } buildNonClusteredRenderPasses(frameGraph, localLights) { for (let i = 0; i < localLights.length; i++) { const light = localLights[i]; if (this.shadowRenderer.needsShadowRendering(light)) { const applyVsm = light._type === LIGHTTYPE_SPOT; const faceCount = light.numShadowFaces; for (let face = 0; face < faceCount; face++) { const renderPass = new RenderPassShadowLocalNonClustered(this.device, this.shadowRenderer, light, face, applyVsm); frameGraph.addRenderPass(renderPass); } } } } } export { ShadowRendererLocal };