From f31cb598e3e4ba494297ee1c477c6f7781bdc52f Mon Sep 17 00:00:00 2001 From: SmallMain Date: Sat, 25 Jun 2022 00:27:59 +0800 Subject: [PATCH] =?UTF-8?q?[engine]=20=E4=BF=AE=E5=A4=8D=20Effect=20?= =?UTF-8?q?=E8=A2=AB=E4=BF=AE=E6=94=B9=E5=90=8E=E5=85=B6=E5=8F=98=E4=BD=93?= =?UTF-8?q?=E7=9A=84=20hash=20=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/assets/material/effect-variant.ts | 2 +- engine/cocos2d/core/assets/material/effect.ts | 3 ++ engine/cocos2d/core/assets/material/utils.js | 38 ++++++++++--------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/engine/cocos2d/core/assets/material/effect-variant.ts b/engine/cocos2d/core/assets/material/effect-variant.ts index 5623ed1c..ba22a926 100644 --- a/engine/cocos2d/core/assets/material/effect-variant.ts +++ b/engine/cocos2d/core/assets/material/effect-variant.ts @@ -75,7 +75,7 @@ export default class EffectVariant extends EffectBase { let effect = this._effect; if (effect) { - hash += utils.serializePasses(effect.passes); + hash += effect._id; } this._hash = murmurhash2(hash, 666); diff --git a/engine/cocos2d/core/assets/material/effect.ts b/engine/cocos2d/core/assets/material/effect.ts index b0327e0e..73a71041 100644 --- a/engine/cocos2d/core/assets/material/effect.ts +++ b/engine/cocos2d/core/assets/material/effect.ts @@ -7,6 +7,8 @@ export default class Effect extends EffectBase { _techniques: Technique[] = []; _asset = null; + static id = 0; + _id = ''; get technique () { return this._technique; @@ -22,6 +24,7 @@ export default class Effect extends EffectBase { constructor (name, techniques, techniqueIndex, asset) { super(); this.init(name, techniques, techniqueIndex, asset, true); + this._id = '|' + Effect.id++; } init (name, techniques, techniqueIndex, asset, createNative) { diff --git a/engine/cocos2d/core/assets/material/utils.js b/engine/cocos2d/core/assets/material/utils.js index 52767efa..f72941c3 100644 --- a/engine/cocos2d/core/assets/material/utils.js +++ b/engine/cocos2d/core/assets/material/utils.js @@ -18,12 +18,14 @@ import enums from '../../../renderer/enums'; const hashArray = []; function serializeDefines (defines, names) { - const len = names.length; - for (let i = 0; i < len; i++) { - const name = names[i]; - hashArray[i] = name + defines[name]; + let i = 0; + for (const name in defines) { + if (Object.hasOwnProperty.call(defines, name)) { + hashArray[i] = name + defines[name]; + i++; + } } - hashArray.length = len; + hashArray.length = i; return hashArray.join(''); } @@ -63,21 +65,23 @@ function serializePasses (passes) { function serializeUniforms (uniforms, names) { let index = 0; - for (let i = 0, len = names.length; i < len; i++) { - let param = uniforms[names[i]]; - let prop = param.value; + for (const name in uniforms) { + if (Object.hasOwnProperty.call(uniforms, name)) { + const param = uniforms[name]; + let prop = param.value; - if (!prop) { - continue; - } + if (!prop) { + continue; + } - if (param.type === enums.PARAM_TEXTURE_2D || param.type === enums.PARAM_TEXTURE_CUBE) { - hashArray[index] = prop._id; + if (param.type === enums.PARAM_TEXTURE_2D || param.type === enums.PARAM_TEXTURE_CUBE) { + hashArray[index] = prop._id; + } + else { + hashArray[index] = prop.toString(); + } + index++; } - else { - hashArray[index] = prop.toString(); - } - index++ } hashArray.length = index; return hashArray.join(';');