mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
23455e75c6
* when checking useSystemFont, the fontFamily should appear (#12684) Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com> * remove rebundant useLocalData (#12640) (cherry picked from commit 7cca98171d765a32e8732ad01b994bc10e861314) Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com> * Modify version number (#12726) Co-authored-by: liuyi <liuyi@cocos.com> * fix builtin asset hot-reload (#12736) * improve: move xr init to infrastructure (#12765) * improve: move xr init to infrastructure * fix: move initxr, before gfx * fix: format warning * [Feature] build template add config file (#12701) * part of build template add config json for creating build templates and version checks for each platform * update default template version * add RenderQueue custom ability to classic pipeline (#12760) * Wgpu 3.6.2 WGPU_CI_ON (#12731) * fix okhttp onClosing not call close (#12783) * fix transparency, emissive and specularfactor with dcc effects (#12798) * reverse occlusion parameter in effects (#12795) * [native] Fix getting wrong value of globals.ambient.skyLightingColor (#12810) * refine: remove unneeded code (#12808) * feat: update meta files (#12807) * middleware2d components rendering in native mode (#12788) * middleware2d components rendering in native mode * support spine/dragonbones set enable batch * support spine rendering in batching mode * get batcher2d instance from root * process about with type conversion * fix some code style * use material hash to merge draw batch * deal with fill z * fix dragonbones fill z * remove some code no used any more * refactoring some code * modify some code * add free memory * fix clang-tidy * delete mesh-buffer-layout in destructor * byte alignment * [ci skip][AUTO]: Automated code generating update: 88c3a7ffdd8f95cd2c7776d6d3d6a8e36862d0bf (#12788) (#12818) Co-authored-by: cocos-robot <cocos-robot@cocos.com> * simplify (#12782) * [code] move decode32 to unpack.chunk (#12823) * [Optimize] Cache current time for audio player in minigame. (#12814) * Cache current time for minigames * Save current time as private member * Make cache time changed in callback * update cache time to abort middle state for seek * LogRemote upgrade configuration protocol (#12833) * update config * Update LogRemote.cpp * update default_skybox and sun direction (#12811) * fix submeshes have different instancing options (#12786) * [ci skip][AUTO]: Automated code generating update: 3200403d155bbfbdef67440c48febc087ac1f0e5 (#12786) (#12871) Co-authored-by: cocos-robot <cocos-robot@cocos.com> * spine/dragonbones support batch mode in web platform (#12847) * spine/dragonbones support batch mode in web platform * set data hash 0 in commitMiddleware * optimizate merge draw batch for middleware * keep commitIA in batcher2d * modify deprecated about commitIA * feat: support webgpu on build-engine (#12627) * feat: support webgpu * build-engine-feat: support building external asset * update * Publish @cocos/build-engine@4.4.0-webgpu.0 * fix tiled merge draw call with sprite (#12877) * fix:CC_ASSERT(!_currentApp.expired()); (#12844) application has destroyed when WebSocketServer process callback in game thread * [Bug fix] remove the player of the audio source when its clip is set to null (#12865) * remove player when the audio source's clip is set to null * fix binary format spine load crash (#12894) * wgpu: remove reinit on same object (#12799) * add i18n for skybox (#12899) * 3.6.2 fix android orientation crash (#12885) * do not cache argument in global space * add doc * add a comment for readAttachment (#12902) * If it is preview, replace overrideUuid directly (#12905) * sort the attributes (#12904) Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com> * add i18n for shadow (#12906) * update default-skybox and sun direction (#12917) * fix:WebSocket connection to 'ws://XXXX/' failed: A server must not mask any frames that it sends to the client. (#12882) * remove USE_BATCHING (#12892) * fix a native middleware blend problem (#12914) * migrated remove USE_BATCHING (#12895) * [feature] support output bullet.wasm on runtime based platform which the using of wasm api is more similar with weixin (#12881) * V3.6.2 fix particle crash (#12913) * fix native crash * revert to pointer * revert * do not use reference * if PREVIEW load cce scripts before loadProjectBundles (#12916) * If it is preview, replace overrideUuid directly * load cce scripts before loadProjectBundles * Update game.ts * Update game.ts * Update game.ts * Update game.ts * fix download url request not encode space (#12826) * Export texture info for wgpu WGPU_CI_ON (#12926) * export texture info for wgpu * missing js file * update external config Co-authored-by: Zeqiang Li <zeqiang-li@outlook.com.com> * fix: build-engine (#12929) * richtext addChild optimization (#12928) * 1. remove rebundant invoking 2. ttf canvas texture resizes to width and height as (1,1) * remove start() and move it to onLoad() Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com> * fix animation asset on inspector preview bug (#12900) * optimize label component editing * fix animation asset on inspector preview bug * optimize i18n break words * update node-executable by env (#12930) * Fix: toDataURL is not supported for this content on iPhone 7p (#12830) * Fix: toDataURL is not supported for this content on iPhone 7p * Add srgb transform. (#12873) * add localCfg.cmake (#12932) * _loadCCEScripts will be browser-preview only (#12925) * _loadCCEScripts will be browser-preview only * add comment * a compatible way of data copy & default buffer size for uniform buffer (#12935) * a compatible way of data copy & default buffer size for uniform buffer * update external config * [fix] default scene ambient and dirlight color (#12955) * sync readme for wgpu (#12966) * [fix] specularIntensity is invalid with builtin-standard (#12971) * [native] type2writer:add FLOAT3 to Color case for compatiblity (#12962) * V3.6.2 format return null error fix (#12973) * add i18n for model (#12985) * fix potential mem leak (#12995) * [fix] camera clear flag is skybox but skybox is disabled #13589 (#12964) * [fix] shader compilation error with CC_DEVICE_SUPPORT_FLOAT_TEXTURE=0 (#13015) * fix import * sync tojs to swig about middleware and 2d module (#4) * sync tojs to swig about scene module (#5) * remove Model::getInstancedAttributeBlock Co-authored-by: Zhang mingzhen <32831993+Zhmz@users.noreply.github.com> Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com> Co-authored-by: LIUYIa <102024185+LIUYIa@users.noreply.github.com> Co-authored-by: liuyi <liuyi@cocos.com> Co-authored-by: fqamic <fqamic@vip.qq.com> Co-authored-by: minggo <nudtzxm@gmail.com> Co-authored-by: changhua <39176194+changhua0118@users.noreply.github.com> Co-authored-by: hyde zhou <zlzhou.sh@outlook.com> Co-authored-by: Zeqiang Li <zeqiang-li@outlook.com> Co-authored-by: mmyduckx <308303735@qq.com> Co-authored-by: 徐兵 <49358166+xubing0906@users.noreply.github.com> Co-authored-by: James Chen <dumganhar@gmail.com> Co-authored-by: bofeng-song <bofeng.song@chukong-inc.com> Co-authored-by: 袁炜海 <35713518+dogodo-cc@users.noreply.github.com> Co-authored-by: zhakesi <liqiao@cocos.com> Co-authored-by: Cocos Robot <48829427+cocos-robot@users.noreply.github.com> Co-authored-by: cocos-robot <cocos-robot@cocos.com> Co-authored-by: 江战 <40414978+PatriceJiang@users.noreply.github.com> Co-authored-by: jk20012001 <jkregister@163.com> Co-authored-by: timlyeee <43355299+timlyeee@users.noreply.github.com> Co-authored-by: PP <chinese1236@qq.com> Co-authored-by: oahc09 <oahc09@gmail.com> Co-authored-by: zhangxin <59186205+chichinohaha@users.noreply.github.com> Co-authored-by: troublemaker52025 <troublemaker52025@outlook.com> Co-authored-by: CC9701 <73738313+CC9701@users.noreply.github.com> Co-authored-by: zxx43 <zxx43@hotmail.com> Co-authored-by: Zeqiang Li <zeqiang-li@outlook.com.com> Co-authored-by: 黄森斌 <arsen2010@126.com> Co-authored-by: ycg <1670543365@qq.com> Co-authored-by: AFeiYA <386665678@qq.com> Co-authored-by: zhakesi <zhakesi@foxmail.com>
520 lines
24 KiB
TypeScript
520 lines
24 KiB
TypeScript
/*
|
|
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
|
|
|
|
https://www.cocos.com/
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
import { BatchingSchemes, Pass } from '../render-scene/core/pass';
|
|
import { Model } from '../render-scene/scene/model';
|
|
import { PipelineStateManager } from './pipeline-state-manager';
|
|
import { Vec3, nextPow2, Mat4, Color } from '../core/math';
|
|
import { intersect } from '../core/geometry';
|
|
import { Device, RenderPass, Buffer, BufferUsageBit, MemoryUsageBit,
|
|
BufferInfo, BufferViewInfo, CommandBuffer } from '../gfx';
|
|
import { Pool } from '../core/memop';
|
|
import { RenderBatchedQueue } from './render-batched-queue';
|
|
import { RenderInstancedQueue } from './render-instanced-queue';
|
|
import { SphereLight } from '../render-scene/scene/sphere-light';
|
|
import { SpotLight } from '../render-scene/scene/spot-light';
|
|
import { SubModel } from '../render-scene/scene/submodel';
|
|
import { getPhaseID } from './pass-phase';
|
|
import { Light, LightType } from '../render-scene/scene/light';
|
|
import { SetIndex, UBOForwardLight, UBOShadow, UNIFORM_SHADOWMAP_BINDING,
|
|
UNIFORM_SPOT_SHADOW_MAP_TEXTURE_BINDING, supportsR32FloatTexture } from './define';
|
|
import { Camera, ShadowType } from '../render-scene/scene';
|
|
import { GlobalDSManager } from './global-descriptor-set-manager';
|
|
import { PipelineUBO } from './pipeline-ubo';
|
|
import { PipelineRuntime } from './custom/pipeline';
|
|
|
|
interface IAdditiveLightPass {
|
|
subModel: SubModel;
|
|
passIdx: number;
|
|
dynamicOffsets: number[];
|
|
lights: Light[];
|
|
}
|
|
|
|
const _lightPassPool = new Pool<IAdditiveLightPass>(() => ({ subModel: null!, passIdx: -1, dynamicOffsets: [], lights: [] }), 16);
|
|
|
|
const _vec4Array = new Float32Array(4);
|
|
const _dynamicOffsets: number[] = [];
|
|
const _lightIndices: number[] = [];
|
|
const _matShadowView = new Mat4();
|
|
const _matShadowViewProj = new Mat4();
|
|
|
|
function cullSphereLight (light: SphereLight, model: Model) {
|
|
return !!(model.worldBounds && !intersect.aabbWithAABB(model.worldBounds, light.aabb));
|
|
}
|
|
|
|
function cullSpotLight (light: SpotLight, model: Model) {
|
|
return !!(model.worldBounds
|
|
&& (!intersect.aabbWithAABB(model.worldBounds, light.aabb) || !intersect.aabbFrustum(model.worldBounds, light.frustum)));
|
|
}
|
|
|
|
const _phaseID = getPhaseID('forward-add');
|
|
const _lightPassIndices: number[] = [];
|
|
function getLightPassIndices (subModels: SubModel[], lightPassIndices: number[]) {
|
|
lightPassIndices.length = 0;
|
|
let hasValidLightPass = false;
|
|
for (let j = 0; j < subModels.length; j++) {
|
|
const { passes } = subModels[j];
|
|
let lightPassIndex = -1;
|
|
for (let k = 0; k < passes.length; k++) {
|
|
if (passes[k].phase === _phaseID) {
|
|
lightPassIndex = k;
|
|
hasValidLightPass = true;
|
|
break;
|
|
}
|
|
}
|
|
lightPassIndices.push(lightPassIndex);
|
|
}
|
|
return hasValidLightPass;
|
|
}
|
|
|
|
function isInstancedOrBatched (model: Model) {
|
|
const subModels = model.subModels;
|
|
for (let m = 0; m < subModels.length; ++m) {
|
|
const passes = subModels[m].passes;
|
|
for (let p = 0; p < passes.length; ++p) {
|
|
const pass = passes[p];
|
|
const batchingScheme = pass.batchingScheme;
|
|
if (batchingScheme === BatchingSchemes.INSTANCING) {
|
|
return true;
|
|
}
|
|
if (batchingScheme === BatchingSchemes.VB_MERGING) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @zh 叠加光照队列。
|
|
*/
|
|
export class RenderAdditiveLightQueue {
|
|
private _pipeline: PipelineRuntime;
|
|
private _device: Device;
|
|
private _lightPasses: IAdditiveLightPass[] = [];
|
|
private _instancedLightPassPool = _lightPassPool.alloc();
|
|
private _batchedLightPassPool = _lightPassPool.alloc();
|
|
private _shadowUBO = new Float32Array(UBOShadow.COUNT);
|
|
private _lightBufferCount = 16;
|
|
private _lightBufferStride: number;
|
|
private _lightBufferElementCount: number;
|
|
private _lightBuffer: Buffer;
|
|
private _firstLightBufferView: Buffer;
|
|
private _lightBufferData: Float32Array;
|
|
private _instancedQueue: RenderInstancedQueue;
|
|
private _batchedQueue: RenderBatchedQueue;
|
|
private _lightMeterScale = 10000.0;
|
|
|
|
constructor (pipeline: PipelineRuntime) {
|
|
this._pipeline = pipeline;
|
|
this._device = pipeline.device;
|
|
this._instancedQueue = new RenderInstancedQueue();
|
|
this._batchedQueue = new RenderBatchedQueue();
|
|
|
|
const alignment = this._device.capabilities.uboOffsetAlignment;
|
|
this._lightBufferStride = Math.ceil(UBOForwardLight.SIZE / alignment) * alignment;
|
|
this._lightBufferElementCount = this._lightBufferStride / Float32Array.BYTES_PER_ELEMENT;
|
|
|
|
this._lightBuffer = this._device.createBuffer(new BufferInfo(
|
|
BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST,
|
|
MemoryUsageBit.HOST | MemoryUsageBit.DEVICE,
|
|
this._lightBufferStride * this._lightBufferCount,
|
|
this._lightBufferStride,
|
|
));
|
|
|
|
this._firstLightBufferView = this._device.createBuffer(new BufferViewInfo(this._lightBuffer, 0, UBOForwardLight.SIZE));
|
|
|
|
this._lightBufferData = new Float32Array(this._lightBufferElementCount * this._lightBufferCount);
|
|
}
|
|
|
|
public clear () {
|
|
this._instancedQueue.clear();
|
|
this._batchedQueue.clear();
|
|
|
|
for (let i = 0; i < this._lightPasses.length; i++) {
|
|
const lp = this._lightPasses[i];
|
|
lp.dynamicOffsets.length = 0;
|
|
lp.lights.length = 0;
|
|
}
|
|
_lightPassPool.freeArray(this._lightPasses);
|
|
this._lightPasses.length = 0;
|
|
|
|
this._instancedLightPassPool.dynamicOffsets.length = 0;
|
|
this._instancedLightPassPool.lights.length = 0;
|
|
this._batchedLightPassPool.dynamicOffsets.length = 0;
|
|
this._batchedLightPassPool.lights.length = 0;
|
|
}
|
|
|
|
public destroy () {
|
|
const descriptorSetMap = this._pipeline.globalDSManager.descriptorSetMap;
|
|
const keys = descriptorSetMap.keys;
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
const key = keys[i];
|
|
const descriptorSet = descriptorSetMap.get(key)!;
|
|
if (descriptorSet) {
|
|
descriptorSet.getBuffer(UBOShadow.BINDING).destroy();
|
|
descriptorSet.getTexture(UNIFORM_SHADOWMAP_BINDING).destroy();
|
|
descriptorSet.getTexture(UNIFORM_SPOT_SHADOW_MAP_TEXTURE_BINDING).destroy();
|
|
descriptorSet.destroy();
|
|
}
|
|
descriptorSetMap.delete(key);
|
|
}
|
|
}
|
|
|
|
public gatherLightPasses (camera: Camera, cmdBuff: CommandBuffer) {
|
|
this.clear();
|
|
|
|
const validPunctualLights = this._pipeline.pipelineSceneData.validPunctualLights;
|
|
if (!validPunctualLights.length) { return; }
|
|
|
|
this._updateUBOs(camera, cmdBuff);
|
|
this._updateLightDescriptorSet(camera, cmdBuff);
|
|
const renderObjects = this._pipeline.pipelineSceneData.renderObjects;
|
|
for (let i = 0; i < renderObjects.length; i++) {
|
|
const ro = renderObjects[i];
|
|
const { model } = ro;
|
|
const { subModels } = model;
|
|
if (!getLightPassIndices(subModels, _lightPassIndices)) { continue; }
|
|
|
|
_lightIndices.length = 0;
|
|
|
|
this._lightCulling(model, validPunctualLights);
|
|
|
|
if (!_lightIndices.length) { continue; }
|
|
|
|
for (let j = 0; j < subModels.length; j++) {
|
|
const lightPassIdx = _lightPassIndices[j];
|
|
if (lightPassIdx < 0) { continue; }
|
|
const subModel = subModels[j];
|
|
const pass = subModel.passes[lightPassIdx];
|
|
// object has translucent base pass, prohibiting forward-add pass for multi light sources lighting
|
|
const isTransparent = subModel.passes[0].blendState.targets[0].blend;
|
|
if (isTransparent) {
|
|
continue;
|
|
}
|
|
subModel.descriptorSet.bindBuffer(UBOForwardLight.BINDING, this._firstLightBufferView);
|
|
subModel.descriptorSet.update();
|
|
|
|
this._addRenderQueue(pass, subModel, model, lightPassIdx);
|
|
}
|
|
}
|
|
|
|
// only for instanced and batched, no light culling applied
|
|
for (let l = 0; l < validPunctualLights.length; l++) {
|
|
const light = validPunctualLights[l];
|
|
this._instancedLightPassPool.lights.push(light);
|
|
this._instancedLightPassPool.dynamicOffsets.push(this._lightBufferStride * l);
|
|
this._batchedLightPassPool.lights.push(light);
|
|
this._batchedLightPassPool.dynamicOffsets.push(this._lightBufferStride * l);
|
|
}
|
|
this._instancedQueue.uploadBuffers(cmdBuff);
|
|
this._batchedQueue.uploadBuffers(cmdBuff);
|
|
}
|
|
|
|
public recordCommandBuffer (device: Device, renderPass: RenderPass, cmdBuff: CommandBuffer) {
|
|
const globalDSManager: GlobalDSManager = this._pipeline.globalDSManager;
|
|
for (let j = 0; j < this._instancedLightPassPool.lights.length; ++j) {
|
|
const light = this._instancedLightPassPool.lights[j];
|
|
_dynamicOffsets[0] = this._instancedLightPassPool.dynamicOffsets[j];
|
|
const descriptorSet = globalDSManager.getOrCreateDescriptorSet(light);
|
|
this._instancedQueue.recordCommandBuffer(device, renderPass, cmdBuff, descriptorSet, _dynamicOffsets);
|
|
}
|
|
|
|
for (let j = 0; j < this._batchedLightPassPool.lights.length; ++j) {
|
|
const light = this._batchedLightPassPool.lights[j];
|
|
_dynamicOffsets[0] = this._batchedLightPassPool.dynamicOffsets[j];
|
|
const descriptorSet = globalDSManager.getOrCreateDescriptorSet(light);
|
|
this._batchedQueue.recordCommandBuffer(device, renderPass, cmdBuff, descriptorSet, _dynamicOffsets);
|
|
}
|
|
|
|
for (let i = 0; i < this._lightPasses.length; i++) {
|
|
const { subModel, passIdx, dynamicOffsets, lights } = this._lightPasses[i];
|
|
const pass = subModel.passes[passIdx];
|
|
const shader = subModel.shaders[passIdx];
|
|
const ia = subModel.inputAssembler;
|
|
const pso = PipelineStateManager.getOrCreatePipelineState(device, pass, shader, renderPass, ia);
|
|
const matDS = pass.descriptorSet;
|
|
const localDS = subModel.descriptorSet;
|
|
|
|
cmdBuff.bindPipelineState(pso);
|
|
cmdBuff.bindDescriptorSet(SetIndex.MATERIAL, matDS);
|
|
cmdBuff.bindInputAssembler(ia);
|
|
|
|
for (let j = 0; j < dynamicOffsets.length; ++j) {
|
|
const light = lights[j];
|
|
const descriptorSet = globalDSManager.getOrCreateDescriptorSet(light)!;
|
|
_dynamicOffsets[0] = dynamicOffsets[j];
|
|
cmdBuff.bindDescriptorSet(SetIndex.GLOBAL, descriptorSet);
|
|
cmdBuff.bindDescriptorSet(SetIndex.LOCAL, localDS, _dynamicOffsets);
|
|
cmdBuff.draw(ia);
|
|
}
|
|
}
|
|
}
|
|
|
|
// light culling
|
|
protected _lightCulling (model:Model, validPunctualLights: Light[]) {
|
|
let isCulled = false;
|
|
const isNeedCulling = !isInstancedOrBatched(model);
|
|
for (let l = 0; l < validPunctualLights.length; l++) {
|
|
const light = validPunctualLights[l];
|
|
switch (light.type) {
|
|
case LightType.SPHERE:
|
|
if (isNeedCulling) { isCulled = cullSphereLight(light as SphereLight, model); }
|
|
break;
|
|
case LightType.SPOT:
|
|
if (isNeedCulling) { isCulled = cullSpotLight(light as SpotLight, model); }
|
|
break;
|
|
default:
|
|
}
|
|
if (!isCulled) {
|
|
_lightIndices.push(l);
|
|
}
|
|
}
|
|
}
|
|
|
|
// add renderQueue
|
|
protected _addRenderQueue (pass: Pass, subModel: SubModel, model: Model, lightPassIdx: number) {
|
|
const validPunctualLights = this._pipeline.pipelineSceneData.validPunctualLights;
|
|
const { batchingScheme } = pass;
|
|
if (batchingScheme === BatchingSchemes.INSTANCING) { // instancing
|
|
const buffer = pass.getInstancedBuffer();
|
|
buffer.merge(subModel, lightPassIdx);
|
|
buffer.dynamicOffsets[0] = this._lightBufferStride;
|
|
this._instancedQueue.queue.add(buffer);
|
|
} else if (batchingScheme === BatchingSchemes.VB_MERGING) { // vb-merging
|
|
const buffer = pass.getBatchedBuffer();
|
|
buffer.merge(subModel, lightPassIdx, model);
|
|
buffer.dynamicOffsets[0] = this._lightBufferStride;
|
|
this._batchedQueue.queue.add(buffer);
|
|
} else { // standard draw
|
|
const lp = _lightPassPool.alloc();
|
|
lp.subModel = subModel;
|
|
lp.passIdx = lightPassIdx;
|
|
for (let l = 0; l < _lightIndices.length; l++) {
|
|
const lightIdx = _lightIndices[l];
|
|
const light = validPunctualLights[lightIdx];
|
|
lp.lights.push(light);
|
|
lp.dynamicOffsets.push(this._lightBufferStride * lightIdx);
|
|
}
|
|
this._lightPasses.push(lp);
|
|
}
|
|
}
|
|
|
|
// update light DescriptorSet
|
|
protected _updateLightDescriptorSet (camera: Camera, cmdBuff: CommandBuffer) {
|
|
const device = this._pipeline.device;
|
|
const sceneData = this._pipeline.pipelineSceneData;
|
|
const shadowInfo = sceneData.shadows;
|
|
const shadowFrameBufferMap = sceneData.shadowFrameBufferMap;
|
|
const mainLight = camera.scene!.mainLight;
|
|
const packing = supportsR32FloatTexture(device) ? 0.0 : 1.0;
|
|
const globalDSManager: GlobalDSManager = this._pipeline.globalDSManager;
|
|
const validPunctualLights = sceneData.validPunctualLights;
|
|
const cap = this._pipeline.device.capabilities;
|
|
|
|
for (let i = 0; i < validPunctualLights.length; i++) {
|
|
const light = validPunctualLights[i];
|
|
const descriptorSet = globalDSManager.getOrCreateDescriptorSet(light);
|
|
if (!descriptorSet) { continue; }
|
|
let matShadowProj : Mat4;
|
|
let matShadowInvProj : Mat4;
|
|
switch (light.type) {
|
|
case LightType.SPHERE: {
|
|
// planar PROJ
|
|
if (mainLight) {
|
|
PipelineUBO.updatePlanarNormalAndDistance(shadowInfo, this._shadowUBO);
|
|
}
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 0] = shadowInfo.size.x;
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 1] = shadowInfo.size.y;
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = 1.0;
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = 0.0;
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 0] = 2.0;
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 1] = packing;
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = 0.0;
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 3] = 0.0;
|
|
|
|
// Reserve sphere light shadow interface
|
|
Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET);
|
|
break;
|
|
}
|
|
case LightType.SPOT: {
|
|
const spotLight = light as SpotLight;
|
|
|
|
// planar PROJ
|
|
if (mainLight) {
|
|
PipelineUBO.updatePlanarNormalAndDistance(shadowInfo, this._shadowUBO);
|
|
}
|
|
|
|
// light view
|
|
Mat4.invert(_matShadowView, (light as SpotLight).node!.getWorldMatrix());
|
|
|
|
// light proj
|
|
Mat4.perspective(_matShadowViewProj, (light as SpotLight).angle, 1.0, 0.001, (light as SpotLight).range,
|
|
true, cap.clipSpaceMinZ, cap.clipSpaceSignY, 0);
|
|
matShadowProj = _matShadowViewProj.clone();
|
|
matShadowInvProj = _matShadowViewProj.clone().invert();
|
|
|
|
// light viewProj
|
|
Mat4.multiply(_matShadowViewProj, _matShadowViewProj, _matShadowView);
|
|
|
|
Mat4.toArray(this._shadowUBO, _matShadowView, UBOShadow.MAT_LIGHT_VIEW_OFFSET);
|
|
Mat4.toArray(this._shadowUBO, _matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET);
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 0] = 0.01;
|
|
this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 1] = (light as SpotLight).range;
|
|
this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 2] = 0.0;
|
|
this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 3] = 0.0;
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 0] = shadowInfo.size.x;
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 1] = shadowInfo.size.y;
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = spotLight.shadowPcf;
|
|
this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = spotLight.shadowBias;
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 0] = 1.0;
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 1] = packing;
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = spotLight.shadowNormalBias;
|
|
this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 3] = 0.0;
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_DEPTH_INFO_OFFSET + 0] = matShadowProj.m10;
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_DEPTH_INFO_OFFSET + 1] = matShadowProj.m14;
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_DEPTH_INFO_OFFSET + 2] = matShadowProj.m11;
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_DEPTH_INFO_OFFSET + 3] = matShadowProj.m15;
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_INV_PROJ_DEPTH_INFO_OFFSET + 0] = matShadowInvProj.m10;
|
|
this._shadowUBO[UBOShadow.SHADOW_INV_PROJ_DEPTH_INFO_OFFSET + 1] = matShadowInvProj.m14;
|
|
this._shadowUBO[UBOShadow.SHADOW_INV_PROJ_DEPTH_INFO_OFFSET + 2] = matShadowInvProj.m11;
|
|
this._shadowUBO[UBOShadow.SHADOW_INV_PROJ_DEPTH_INFO_OFFSET + 3] = matShadowInvProj.m15;
|
|
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_INFO_OFFSET + 0] = matShadowProj.m00;
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_INFO_OFFSET + 1] = matShadowProj.m05;
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_INFO_OFFSET + 2] = 1.0 / matShadowProj.m00;
|
|
this._shadowUBO[UBOShadow.SHADOW_PROJ_INFO_OFFSET + 3] = 1.0 / matShadowProj.m05;
|
|
|
|
Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET);
|
|
|
|
// Spot light sampler binding
|
|
if (shadowFrameBufferMap.has(light)) {
|
|
const texture = shadowFrameBufferMap.get(light)?.colorTextures[0];
|
|
if (texture) {
|
|
descriptorSet.bindTexture(UNIFORM_SPOT_SHADOW_MAP_TEXTURE_BINDING, texture);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
}
|
|
descriptorSet.update();
|
|
|
|
cmdBuff.updateBuffer(descriptorSet.getBuffer(UBOShadow.BINDING)!, this._shadowUBO);
|
|
}
|
|
}
|
|
|
|
protected _updateUBOs (camera: Camera, cmdBuff: CommandBuffer) {
|
|
const { exposure } = camera;
|
|
const sceneData = this._pipeline.pipelineSceneData;
|
|
const isHDR = sceneData.isHDR;
|
|
const shadowInfo = sceneData.shadows;
|
|
const validPunctualLights = sceneData.validPunctualLights;
|
|
|
|
if (validPunctualLights.length > this._lightBufferCount) {
|
|
this._firstLightBufferView.destroy();
|
|
|
|
this._lightBufferCount = nextPow2(validPunctualLights.length);
|
|
this._lightBuffer.resize(this._lightBufferStride * this._lightBufferCount);
|
|
this._lightBufferData = new Float32Array(this._lightBufferElementCount * this._lightBufferCount);
|
|
|
|
this._firstLightBufferView.initialize(new BufferViewInfo(this._lightBuffer, 0, UBOForwardLight.SIZE));
|
|
}
|
|
|
|
for (let l = 0, offset = 0; l < validPunctualLights.length; l++, offset += this._lightBufferElementCount) {
|
|
const light = validPunctualLights[l];
|
|
|
|
switch (light.type) {
|
|
case LightType.SPHERE:
|
|
// UBOForwardLight
|
|
Vec3.toArray(_vec4Array, (light as SphereLight).position);
|
|
_vec4Array[3] = 0;
|
|
this._lightBufferData.set(_vec4Array, offset + UBOForwardLight.LIGHT_POS_OFFSET);
|
|
|
|
_vec4Array[0] = (light as SphereLight).size;
|
|
_vec4Array[1] = (light as SphereLight).range;
|
|
_vec4Array[2] = 0.0;
|
|
_vec4Array[3] = 0.0;
|
|
this._lightBufferData.set(_vec4Array, offset + UBOForwardLight.LIGHT_SIZE_RANGE_ANGLE_OFFSET);
|
|
|
|
Vec3.toArray(_vec4Array, light.color);
|
|
if (light.useColorTemperature) {
|
|
const tempRGB = light.colorTemperatureRGB;
|
|
_vec4Array[0] *= tempRGB.x;
|
|
_vec4Array[1] *= tempRGB.y;
|
|
_vec4Array[2] *= tempRGB.z;
|
|
}
|
|
if (isHDR) {
|
|
_vec4Array[3] = (light as SphereLight).luminance * exposure * this._lightMeterScale;
|
|
} else {
|
|
_vec4Array[3] = (light as SphereLight).luminance;
|
|
}
|
|
this._lightBufferData.set(_vec4Array, offset + UBOForwardLight.LIGHT_COLOR_OFFSET);
|
|
break;
|
|
case LightType.SPOT:
|
|
// UBOForwardLight
|
|
Vec3.toArray(_vec4Array, (light as SpotLight).position);
|
|
_vec4Array[3] = 1;
|
|
this._lightBufferData.set(_vec4Array, offset + UBOForwardLight.LIGHT_POS_OFFSET);
|
|
|
|
_vec4Array[0] = (light as SpotLight).size;
|
|
_vec4Array[1] = (light as SpotLight).range;
|
|
_vec4Array[2] = (light as SpotLight).spotAngle;
|
|
_vec4Array[3] = (shadowInfo.enabled && (light as SpotLight).shadowEnabled && shadowInfo.type === ShadowType.ShadowMap) ? 1 : 0;
|
|
this._lightBufferData.set(_vec4Array, offset + UBOForwardLight.LIGHT_SIZE_RANGE_ANGLE_OFFSET);
|
|
|
|
Vec3.toArray(_vec4Array, (light as SpotLight).direction);
|
|
this._lightBufferData.set(_vec4Array, offset + UBOForwardLight.LIGHT_DIR_OFFSET);
|
|
|
|
Vec3.toArray(_vec4Array, light.color);
|
|
if (light.useColorTemperature) {
|
|
const tempRGB = light.colorTemperatureRGB;
|
|
_vec4Array[0] *= tempRGB.x;
|
|
_vec4Array[1] *= tempRGB.y;
|
|
_vec4Array[2] *= tempRGB.z;
|
|
}
|
|
if (isHDR) {
|
|
_vec4Array[3] = (light as SpotLight).luminance * exposure * this._lightMeterScale;
|
|
} else {
|
|
_vec4Array[3] = (light as SpotLight).luminance;
|
|
}
|
|
this._lightBufferData.set(_vec4Array, offset + UBOForwardLight.LIGHT_COLOR_OFFSET);
|
|
break;
|
|
default:
|
|
}
|
|
}
|
|
|
|
cmdBuff.updateBuffer(this._lightBuffer, this._lightBufferData);
|
|
}
|
|
}
|