diff --git a/cocos/particle/renderer/particle-system-renderer-cpu.ts b/cocos/particle/renderer/particle-system-renderer-cpu.ts index 686d80d75e..03d489cb91 100644 --- a/cocos/particle/renderer/particle-system-renderer-cpu.ts +++ b/cocos/particle/renderer/particle-system-renderer-cpu.ts @@ -69,6 +69,7 @@ const CC_USE_EMBEDDED_ALPHA = 'CC_USE_EMBEDDED_ALPHA'; const CC_RENDER_MODE = 'CC_RENDER_MODE'; const ROTATION_OVER_TIME_MODULE_ENABLE = 'ROTATION_OVER_TIME_MODULE_ENABLE'; +const USE_NORMAL_MAP = 'USE_NORMAL_MAP'; const INSTANCE_PARTICLE = 'CC_INSTANCE_PARTICLE'; const RENDER_MODE_BILLBOARD = 0; const RENDER_MODE_STRETCHED_BILLBOARD = 1; @@ -402,9 +403,11 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas } ps.node.getWorldMatrix(_tempWorldTrans); const mat: Material | null = ps.getMaterialInstance(0) || this._defaultMat; - const pass = mat!.passes[0]; - this.doUpdateScale(pass); - this.doUpdateRotation(pass); + const pass = mat?.passes[0]; + if (pass) { + this.doUpdateScale(pass); + this.doUpdateRotation(pass); + } this._updateList.forEach((value: IParticleModule, key: string): void => { // TODO(cjh): Bug here? _updateList is a Map, the old code uses `this._updateList['some_key'] = some_value;` @@ -695,7 +698,8 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas this._renderInfo!.mainTexture = shareMaterial.getProperty('mainTexture', 0) as Texture2D; } - if (ps.sharedMaterial == null && this._defaultMat == null) { + // Renderer can destroy the cached instance when replacing a preview material. + if (ps.sharedMaterial == null && (!this._defaultMat || !this._defaultMat.passes.length)) { _matInsInfo.parent = builtinResMgr.get('default-particle-material'); _matInsInfo.owner = this._particleSystem; _matInsInfo.subModelIdx = 0; @@ -708,6 +712,11 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas } } const mat: Material = ps.getMaterialInstance(0) || this._defaultMat!; + // A custom material may still have an invalid or unavailable effect. + if (!mat || !mat.passes.length) { + warn('Particle material has no render passes. Check its effect import and selected technique.'); + return; + } if (ps.simulationSpace === ParticleSpace.World) { this._defines[CC_USE_WORLD_SPACE] = true; } else { @@ -754,6 +763,10 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas const roationModule = this._particleSystem._rotationOvertimeModule; enable = roationModule ? roationModule.enable : false; this._defines[ROTATION_OVER_TIME_MODULE_ENABLE] = enable; + // Explicitly clear the override when the material omits the disabled macro. + const materialDefines = (shareMaterial || mat.parent || mat).passes[0]?.defines; + this._defines[USE_NORMAL_MAP] = !!materialDefines?.[USE_NORMAL_MAP]; + this._defines[INSTANCE_PARTICLE] = this._useInstance; const matIns: MaterialInstance | null = ps.getMaterialInstance(0); @@ -781,7 +794,7 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas this._trailDefines[CC_USE_WORLD_SPACE] = false; } let mat: Material | null = ps.getMaterialInstance(1); - if (mat === null && this._defaultTrailMat === null) { + if (mat === null && (!this._defaultTrailMat || !this._defaultTrailMat.passes.length)) { _matInsInfo.parent = builtinResMgr.get('default-trail-material'); _matInsInfo.owner = this._particleSystem; _matInsInfo.subModelIdx = 1; diff --git a/cocos/particle/renderer/particle-system-renderer-gpu.ts b/cocos/particle/renderer/particle-system-renderer-gpu.ts index 5f6cd5fdbb..00d1681ca7 100644 --- a/cocos/particle/renderer/particle-system-renderer-gpu.ts +++ b/cocos/particle/renderer/particle-system-renderer-gpu.ts @@ -61,6 +61,7 @@ const RENDER_MODE_MESH = 4; const COLOR_OVER_TIME_MODULE_ENABLE = 'COLOR_OVER_TIME_MODULE_ENABLE'; const ROTATION_OVER_TIME_MODULE_ENABLE = 'ROTATION_OVER_TIME_MODULE_ENABLE'; +const USE_NORMAL_MAP = 'USE_NORMAL_MAP'; const SIZE_OVER_TIME_MODULE_ENABLE = 'SIZE_OVER_TIME_MODULE_ENABLE'; const VELOCITY_OVER_TIME_MODULE_ENABLE = 'VELOCITY_OVER_TIME_MODULE_ENABLE'; const FORCE_OVER_TIME_MODULE_ENABLE = 'FORCE_OVER_TIME_MODULE_ENABLE'; @@ -229,7 +230,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas this.initShaderUniform(mat); mat.recompileShaders(this._defines); if (this._model) { - this._model.setSubModelMaterial(0, mat); + this._model.updateMaterial(mat); } } @@ -372,6 +373,9 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas } const pass = mat.passes[0]; + if (!pass) { + return; + } _tempVec4.x = this._particleSystem.time; _tempVec4.y = dt; pass.setUniform(this._uTimeHandle, _tempVec4); @@ -384,6 +388,13 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas public initShaderUniform (mat: Material): void { const pass = mat.passes[0]; + if (!pass) { + return; + } + + // Explicitly clear the override when the material omits the disabled macro. + const materialDefines = (this._particleSystem?.sharedMaterial || mat.parent || mat).passes[0]?.defines; + this._defines[USE_NORMAL_MAP] = !!materialDefines?.[USE_NORMAL_MAP]; this._uTimeHandle = pass.getHandle('u_timeDelta'); this._uRotHandle = pass.getHandle('u_worldRot'); @@ -606,7 +617,8 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas this._renderInfo!.mainTexture = shareMaterial.getProperty('mainTexture', 0) as Texture2D; } - if (ps.sharedMaterial == null && this._defaultMat == null) { + // Recreate the fallback if its passes have been destroyed. + if (ps.sharedMaterial == null && (!this._defaultMat || !this._defaultMat.passes.length)) { _matInsInfo.parent = builtinResMgr.get('default-particle-gpu-material'); _matInsInfo.owner = ps; _matInsInfo.subModelIdx = 0; @@ -619,6 +631,11 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas } } const mat: Material | null = ps.getMaterialInstance(0) || this._defaultMat!; + // A custom material may still have an invalid or unavailable effect. + if (!mat || !mat.passes.length) { + warn('Particle material has no render passes. Check its effect import and selected technique.'); + return; + } ps.node.getWorldMatrix(_tempWorldTrans); diff --git a/editor/assets/chunks/builtin/internal/particle-common.chunk b/editor/assets/chunks/builtin/internal/particle-common.chunk index 4aba5d57da..c61ca742e4 100644 --- a/editor/assets/chunks/builtin/internal/particle-common.chunk +++ b/editor/assets/chunks/builtin/internal/particle-common.chunk @@ -1,3 +1,4 @@ +#pragma define-meta USE_NORMAL_MAP default(false) #pragma define-meta CC_RENDER_MODE range([0, 4]) #pragma define RENDER_MODE_BILLBOARD 0 #pragma define RENDER_MODE_STRETCHED_BILLBOARD 1 @@ -19,6 +20,13 @@ uniform Constants { out mediump vec2 uv; out mediump vec4 color; +#if USE_NORMAL_MAP + out highp vec3 particlePositionWS; + #if CC_RENDER_MODE == RENDER_MODE_MESH + out highp vec3 particleNormalWS; + #endif +#endif + void computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s #if CC_RENDER_MODE == RENDER_MODE_BILLBOARD || CC_RENDER_MODE == RENDER_MODE_VERTICAL_BILLBOARD , mat4 viewInv diff --git a/editor/assets/chunks/builtin/internal/particle-normal.chunk b/editor/assets/chunks/builtin/internal/particle-normal.chunk new file mode 100644 index 0000000000..e2644550a7 --- /dev/null +++ b/editor/assets/chunks/builtin/internal/particle-normal.chunk @@ -0,0 +1,56 @@ +#pragma define-meta USE_NORMAL_MAP default(false) +// Both stages must declare the range: Mesh uses 4, beyond the inferred [0, 3]. +#pragma define-meta CC_RENDER_MODE range([0, 4]) +#pragma extension([GL_OES_standard_derivatives, __VERSION__ < 300]) + +#include + +#if USE_NORMAL_MAP + in highp vec3 particlePositionWS; + #if CC_RENDER_MODE == 4 + in highp vec3 particleNormalWS; + #endif + + uniform sampler2D normalMap; + uniform ParticleNormalConstants { + vec4 particleNormalParams; + }; + + vec4 ApplyParticleNormalLighting (vec4 col, vec2 texCoord) { + // Derive the tangent frame from the animated UVs and final world position. + // This also handles billboard rotation and meshes without tangent attributes. + highp vec3 dpdx = dFdx(particlePositionWS); + highp vec3 dpdy = dFdy(particlePositionWS); + highp vec2 duvdx = dFdx(texCoord); + highp vec2 duvdy = dFdy(texCoord); + #if CC_RENDER_MODE == 4 + highp vec3 n = normalize(particleNormalWS); + n *= gl_FrontFacing ? 1.0 : -1.0; + #else + highp vec3 n = normalize(cross(dpdx, dpdy)); + // Keep the two-sided billboard's surface normal facing the viewer. + n *= dot(n, cc_cameraPos.xyz - particlePositionWS) < 0.0 ? -1.0 : 1.0; + #endif + highp vec3 perpendicularY = cross(dpdy, n); + highp vec3 perpendicularX = cross(n, dpdx); + highp vec3 tangent = perpendicularY * duvdx.x + perpendicularX * duvdy.x; + highp vec3 bitangent = perpendicularY * duvdx.y + perpendicularX * duvdy.y; + highp float frameLength = max(dot(tangent, tangent), dot(bitangent, bitangent)); + highp float inverseLength = inversesqrt(max(frameLength, 1e-20)); + vec3 mappedNormal = texture(normalMap, texCoord).xyz * 2.0 - 1.0; + mappedNormal.xy *= particleNormalParams.x; + highp vec3 perturbedNormal = (tangent * mappedNormal.x + bitangent * mappedNormal.y) * inverseLength + n * mappedNormal.z; + n = dot(perturbedNormal, perturbedNormal) > 1e-12 ? normalize(perturbedNormal) : n; + + vec3 lighting = mix(cc_ambientGround.rgb, cc_ambientSky.rgb, n.y * 0.5 + 0.5) * cc_ambientSky.w; + #if !CC_DISABLE_DIRECTIONAL_LIGHT + lighting += cc_mainLitColor.rgb * cc_mainLitColor.w * max(dot(n, -cc_mainLitDir.xyz), 0.0); + #endif + col.rgb *= lighting; + return col; + } +#else + vec4 ApplyParticleNormalLighting (vec4 col, vec2 texCoord) { + return col; + } +#endif diff --git a/editor/assets/chunks/builtin/internal/particle-vs-gpu.chunk b/editor/assets/chunks/builtin/internal/particle-vs-gpu.chunk index 16e06edc48..d6c95c26c6 100644 --- a/editor/assets/chunks/builtin/internal/particle-vs-gpu.chunk +++ b/editor/assets/chunks/builtin/internal/particle-vs-gpu.chunk @@ -365,6 +365,14 @@ vec4 gpvs_main () { color *= a_color1; #endif + #if USE_NORMAL_MAP + particlePositionWS = pos.xyz; + #if CC_RENDER_MODE == RENDER_MODE_MESH + // Inverse-transpose of particle rotation and scale, including mirrored scale. + highp vec3 safeScale = mix(vec3(-1.0), vec3(1.0), step(vec3(0.0), compScale)) * max(abs(compScale), vec3(0.000001)); + particleNormalWS = mat3(xformNoScale) * (a_normal / safeScale); + #endif + #endif pos = cc_matViewProj * pos; float frameIndex = 0.; diff --git a/editor/assets/chunks/builtin/internal/particle-vs-legacy.chunk b/editor/assets/chunks/builtin/internal/particle-vs-legacy.chunk index 1747a56202..55d8bb9640 100644 --- a/editor/assets/chunks/builtin/internal/particle-vs-legacy.chunk +++ b/editor/assets/chunks/builtin/internal/particle-vs-legacy.chunk @@ -108,6 +108,14 @@ vec4 lpvs_main () { uv = computeUV(a_texCoord4.w, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw; #endif + #if USE_NORMAL_MAP + particlePositionWS = pos.xyz; + #if CC_RENDER_MODE == RENDER_MODE_MESH + // Inverse-transpose of particle rotation and scale, including mirrored scale. + highp vec3 safeScale = mix(vec3(-1.0), vec3(1.0), step(vec3(0.0), compScale)) * max(abs(compScale), vec3(0.000001)); + particleNormalWS = mat3(xformNoScale) * (a_normal / safeScale); + #endif + #endif pos = cc_matViewProj * pos; return pos; diff --git a/editor/assets/effects/particles/builtin-particle-gpu.effect b/editor/assets/effects/particles/builtin-particle-gpu.effect index fb77ad4b5b..a0c6c86c2f 100644 --- a/editor/assets/effects/particles/builtin-particle-gpu.effect +++ b/editor/assets/effects/particles/builtin-particle-gpu.effect @@ -26,6 +26,8 @@ CCEffect %{ r1: &r1 { cullMode: none } p1: &p1 mainTexture: { value: grey } + normalMap: { value: normal, editor: { parent: USE_NORMAL_MAP } } + normalStrength: { value: 1.0, target: particleNormalParams.x, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5], step: 0.01 } } mainTiling_Offset: { value: [1, 1, 0, 0] } p2: &p2 <<: *p1 @@ -119,6 +121,8 @@ CCProgram tinted-fs %{ in vec2 uv; in vec4 color; + #include + uniform sampler2D mainTexture; uniform FragConstants { @@ -127,14 +131,14 @@ CCProgram tinted-fs %{ vec4 add () { vec4 col = 2.0 * color * tintColor * CCSampleWithAlphaSeparated(mainTexture, uv); - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } vec4 multiply () { vec4 col; vec4 texColor = CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0); - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } }% @@ -146,16 +150,18 @@ CCProgram no-tint-fs %{ in vec2 uv; in vec4 color; + #include + uniform sampler2D mainTexture; vec4 addSmooth () { vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb *= col.a; - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } vec4 premultiplied () { vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv) * color.a; - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } }% diff --git a/editor/assets/effects/particles/builtin-particle.effect b/editor/assets/effects/particles/builtin-particle.effect index 0be49d9997..dcfa0ab2f0 100644 --- a/editor/assets/effects/particles/builtin-particle.effect +++ b/editor/assets/effects/particles/builtin-particle.effect @@ -26,6 +26,8 @@ CCEffect %{ r1: &r1 { cullMode: none } p1: &p1 mainTexture: { value: grey } + normalMap: { value: normal, editor: { parent: USE_NORMAL_MAP } } + normalStrength: { value: 1.0, target: particleNormalParams.x, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 5], step: 0.01 } } mainTiling_Offset: { value: [1, 1, 0, 0] } p2: &p2 <<: *p1 @@ -119,6 +121,8 @@ CCProgram tinted-fs %{ in vec2 uv; in vec4 color; + #include + uniform sampler2D mainTexture; uniform FragConstants { vec4 tintColor; @@ -126,14 +130,14 @@ CCProgram tinted-fs %{ vec4 add () { vec4 col = 2.0 * color * tintColor * CCSampleWithAlphaSeparated(mainTexture, uv); - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } vec4 multiply () { vec4 col; vec4 texColor = CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0); - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } }% @@ -145,16 +149,18 @@ CCProgram no-tint-fs %{ in vec2 uv; in vec4 color; + #include + uniform sampler2D mainTexture; vec4 addSmooth () { vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb *= col.a; - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } vec4 premultiplied () { vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv) * color.a; - return CCFragOutput(col); + return CCFragOutput(ApplyParticleNormalLighting(col, uv)); } }%