Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LOD can not work normally while node's active changed dynamically #15641

Merged
merged 12 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions cocos/3d/lod/lodgroup-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
*/
public setRenderer (index: number, renderer: MeshRenderer): void {
if (index < 0 || index >= this.rendererCount) {
console.error('setRenderer to LOD error, index out of range');

Check failure on line 208 in cocos/3d/lod/lodgroup-component.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
return;
}
this.deleteRenderer(index);
Expand Down Expand Up @@ -242,6 +242,8 @@

private _eventRegistered = false;

private _forceUsedLevels: number[] = [];

constructor () {
super();
}
Expand Down Expand Up @@ -378,12 +380,12 @@
*/
public eraseLOD (index: number): LOD | null {
if (index < 0 || index >= this.lodCount) {
console.warn('eraseLOD error, index out of range');

Check failure on line 383 in cocos/3d/lod/lodgroup-component.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
return null;
}
const lod = this._LODs[index];
if (!lod) {
console.warn('eraseLOD error, LOD not exist at specified index.');

Check failure on line 388 in cocos/3d/lod/lodgroup-component.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
return null;
}
this._LODs.splice(index, 1);
Expand All @@ -401,7 +403,7 @@
*/
public getLOD (index: number): LOD | null {
if (index < 0 || index >= this.lodCount) {
console.warn('getLOD error, index out of range');

Check failure on line 406 in cocos/3d/lod/lodgroup-component.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
return null;
}
return this._LODs[index];
Expand All @@ -415,7 +417,7 @@
*/
public setLOD (index: number, lod: LOD): void {
if (index < 0 || index >= this.lodCount) {
console.warn('setLOD error, index out of range');

Check failure on line 420 in cocos/3d/lod/lodgroup-component.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
return;
}
this._LODs[index] = lod;
Expand Down Expand Up @@ -543,7 +545,20 @@
* lodLevel @en The LOD level to use. Passing lodLevel < 0 will return to standard LOD processing. @zh 要使用的LOD层级,为负数时使用标准的处理流程
*/
public forceLOD (lodLevel: number): void {
this.lodGroup.lockLODLevels(lodLevel < 0 ? [] : [lodLevel]);
this._forceUsedLevels = lodLevel < 0 ? [] : [lodLevel];
this.lodGroup.lockLODLevels(this._forceUsedLevels);
}

/**
* @en Force multi LOD level to use, This function is only called in editor.<br/>
* @zh 强制使用某几级的LOD,该接口只会在编辑器下调用。
* lodIndexArray @en The LOD level array. Passing [] will return to standard LOD processing. @zh 要使用的LOD层级数组,传[]时将使用标准的处理流程。
*/
public forceLODs (lodIndexArray: number[]): void {
if (EDITOR) {
this._forceUsedLevels = lodIndexArray.slice();
this.lodGroup.lockLODLevels(this._forceUsedLevels);
}
}

onLoad (): void {
Expand Down Expand Up @@ -587,6 +602,7 @@
if (this.objectSize === 0) {
this.recalculateBounds();
}
this.lodGroup.lockLODLevels(this._forceUsedLevels);

// cache lod for scene
if (this.lodCount > 0 && this._lodGroup.lodCount < 1) {
Expand All @@ -609,6 +625,7 @@

onDisable (): void {
this._detachFromScene();
this.lodGroup.lockLODLevels([]);
}

private _attachToScene (): void {
Expand All @@ -625,9 +642,6 @@
if (this._lodGroup.scene) { this._lodGroup.scene.removeLODGroup(this._lodGroup); }
}

/**
* @engineInternal
*/
private _emitChangeNode (node: Node): void {
if (EDITOR) {
EditorExtends.Node.emit('change', node.uuid, node);
Expand Down
2 changes: 1 addition & 1 deletion cocos/render-scene/scene/lod-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class LODGroup {
}
}
}
this._lockedLODLevelVec = lockLev;
this._lockedLODLevelVec = lockLev.slice();
}

isLockLevelChanged (): boolean {
Expand Down
9 changes: 0 additions & 9 deletions cocos/rendering/lod-group-editor-utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@ export class LODGroupEditorUtility {
return this.distanceToRelativeHeight(camera, distance, this.getWorldSpaceSize(lodGroup));
}

/**
* @zh 强制使用某几级的LOD
* @en Force multi LOD level to use.
* lodIndexArray @en The LOD level array. Passing [] will return to standard LOD processing. @zh 要使用的LOD层级数组,传[]时将使用标准的处理流程。
*/
static forceLODs (lodGroup: LODGroup, lodIndexArray: number[]) {
lodGroup.lodGroup.lockLODLevels(lodIndexArray);
}

private static distanceToRelativeHeight (camera: Camera, distance: number | undefined, size: number): number {
if (camera.projectionType === CameraProjection.PERSPECTIVE) {
assertIsTrue(typeof distance === 'number', 'distance must be present for perspective projection');
Expand Down
2 changes: 1 addition & 1 deletion cocos/scene-graph/node-activator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@

function _componentCorrupted (node: Node, comp: Component, index: number): void {
errorID(3817, node.name, index);
console.log('Corrupted component value:', comp);

Check failure on line 119 in cocos/scene-graph/node-activator.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
if (comp) {
node._removeComponent(comp);
} else {
Expand All @@ -129,7 +129,7 @@
* @zh 用于执行节点和组件的激活和停用操作的管理器。
*/
export default class NodeActivator {
public resetComp?: ((comp: Component, didResetToDefault: boolean) => void);
public declare resetComp?: ((comp: Component, didResetToDefault: boolean) => void);
protected _activatingStack!: ActivateTask[];

constructor () {
Expand Down Expand Up @@ -242,7 +242,7 @@
}
}

protected _activateNodeRecursively (node: Node, preloadInvoker: UnsortedInvoker, onLoadInvoker: OneOffInvoker, onEnableInvoker: OneOffInvoker): void {

Check warning on line 245 in cocos/scene-graph/node-activator.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 154. Maximum allowed is 150
if (node._objFlags & Deactivating) {
// en:
// Forbid reactive the same node during its deactivating procedure
Expand Down Expand Up @@ -325,7 +325,7 @@

if (EDITOR) {
const callPreloadInTryCatch = tryCatchFunctor_EDITOR('__preload');
const callOnLoadInTryCatch = function (c: Component): void {

Check warning on line 328 in cocos/scene-graph/node-activator.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
try {
c.internalOnLoad?.();
} catch (e) {
Expand All @@ -351,7 +351,7 @@
}
};

NodeActivator.prototype.activateComp = (comp: Component, preloadInvoker: UnsortedInvoker, onLoadInvoker: OneOffInvoker, onEnableInvoker: OneOffInvoker): void => {

Check warning on line 354 in cocos/scene-graph/node-activator.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 166. Maximum allowed is 150
if (!isValid(comp, true)) {
// destroyed before activating
return;
Expand Down Expand Up @@ -401,7 +401,7 @@
if (comp.internalOnDestroy && (comp._objFlags & IsOnLoadCalled)) {
// NOTE: _executeInEditMode is dynamically injected on Editor environment
if (legacyCC.GAME_VIEW || (comp.constructor as any)._executeInEditMode) {
callOnDestroyInTryCatch && callOnDestroyInTryCatch(comp);

Check warning on line 404 in cocos/scene-graph/node-activator.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected an assignment or function call and instead saw an expression
}
}
};
Expand Down
Loading