From f5d4786441d7d3b88ea198792e9965f935c6f23c Mon Sep 17 00:00:00 2001 From: llsansun Date: Wed, 2 Sep 2026 17:10:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?When=20copying=20the=20old=20buffer=20value?= =?UTF-8?q?,=20if=20the=20old=20value=20is=20`undefined`=20(out=E2=80=91of?= =?UTF-8?q?=E2=80=91bounds=20access=20to=20an=20empty=20array),=20preserve?= =?UTF-8?q?=20the=20default=20value=20of=20`-1`=20instead=20of=20writing?= =?UTF-8?q?=20`undefined`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/terrain/terrain.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cocos/terrain/terrain.ts b/cocos/terrain/terrain.ts index 167f47a2b1..5cab6b542a 100644 --- a/cocos/terrain/terrain.ts +++ b/cocos/terrain/terrain.ts @@ -1815,11 +1815,11 @@ export class Terrain extends Component { * @zh 获得纹理层 */ public getLayer (id: number): TerrainLayer | null { - if (id === -1) { + if (id === -1 || id === undefined) { return null; } - return this._layerList[id]; + return this._layerList[id] ?? null; } /** @@ -2584,7 +2584,8 @@ export class Terrain extends Component { const index1 = j * this.blockCount[0] + i; for (let l = 0; l < TERRAIN_MAX_BLEND_LAYERS; ++l) { - layerBuffer[index0 * TERRAIN_MAX_BLEND_LAYERS + l] = this._layerBuffer[index1 * TERRAIN_MAX_BLEND_LAYERS + l]; + const oldVal = this._layerBuffer[index1 * TERRAIN_MAX_BLEND_LAYERS + l]; + layerBuffer[index0 * TERRAIN_MAX_BLEND_LAYERS + l] = oldVal !== undefined ? oldVal : -1; } } } From 1a541fbbc94a22844b0d1dc1415b48b21f9676d6 Mon Sep 17 00:00:00 2001 From: llsansun Date: Wed, 2 Sep 2026 17:44:47 +0800 Subject: [PATCH 2/2] opt --- cocos/terrain/terrain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/terrain/terrain.ts b/cocos/terrain/terrain.ts index 5cab6b542a..e1675961c0 100644 --- a/cocos/terrain/terrain.ts +++ b/cocos/terrain/terrain.ts @@ -1819,7 +1819,7 @@ export class Terrain extends Component { return null; } - return this._layerList[id] ?? null; + return this._layerList[id] || null; } /**