diff --git a/cocos/asset/assets/texture-base.ts b/cocos/asset/assets/texture-base.ts index ccd5371f7b..d3a11f924c 100644 --- a/cocos/asset/assets/texture-base.ts +++ b/cocos/asset/assets/texture-base.ts @@ -49,6 +49,14 @@ export class TextureBase extends Asset { || (this._format >= PixelFormat.RGB_A_PVRTC_2BPPV1 && this._format <= PixelFormat.RGBA_ETC1); } + /** + * @en Whether it is a compressed alpha channel texture + * @zh 是否为压缩的透明通道贴图 + */ + public get isAlphaAtlas (): boolean { + return (this._format === PixelFormat.RGBA_ETC1 || this._format === PixelFormat.RGB_A_PVRTC_4BPPV1 || this._format === PixelFormat.RGB_A_PVRTC_2BPPV1); + } + /** * @en Pixel width of the texture. * @zh 此贴图的像素宽度。 diff --git a/cocos/particle/animator/texture-animation.ts b/cocos/particle/animator/texture-animation.ts index 6df0511375..03f016e3f3 100644 --- a/cocos/particle/animator/texture-animation.ts +++ b/cocos/particle/animator/texture-animation.ts @@ -297,4 +297,13 @@ export default class TextureAnimationModule extends ParticleModuleBase { } } } + + /** + * @engineInternal + * @mangle + */ + public scaleNumTilesXY (scale: number): void { + this._numTilesX *= scale; + this._numTilesY *= scale; + } } diff --git a/cocos/particle/renderer/particle-system-renderer-cpu.ts b/cocos/particle/renderer/particle-system-renderer-cpu.ts index 8c2822d9e0..1d4c636f93 100644 --- a/cocos/particle/renderer/particle-system-renderer-cpu.ts +++ b/cocos/particle/renderer/particle-system-renderer-cpu.ts @@ -65,6 +65,7 @@ const _uvs = [ ]; const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE'; +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'; @@ -729,6 +730,11 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas } const textureModule = ps._textureAnimationModule; if (textureModule && textureModule.enable) { + const texture = mat.getProperty('mainTexture', 0) as Texture2D | null; + if (texture && texture.isAlphaAtlas) { + textureModule.scaleNumTilesXY(2); + this._defines[CC_USE_EMBEDDED_ALPHA] = true; + } Vec4.copy(this._tmp_velLenScale, vlenScale); // fix textureModule switch bug Vec2.set(this._tmp_velLenScale, textureModule.numTilesX, textureModule.numTilesY); pass.setUniform(this._uLenHandle, this._tmp_velLenScale); @@ -771,6 +777,10 @@ export default class ParticleSystemRendererCPU extends ParticleSystemRendererBas _matInsInfo.subModelIdx = 0; } mat = mat || this._defaultTrailMat!; + const texture = mat.getProperty('mainTexture', 0) as Texture2D | null; + if (texture && texture.isAlphaAtlas) { + this._trailDefines[CC_USE_EMBEDDED_ALPHA] = true; + } mat.recompileShaders(this._trailDefines); trailModule.updateMaterial(); } diff --git a/cocos/particle/renderer/particle-system-renderer-gpu.ts b/cocos/particle/renderer/particle-system-renderer-gpu.ts index a5a9ea1f28..31b94503cd 100644 --- a/cocos/particle/renderer/particle-system-renderer-gpu.ts +++ b/cocos/particle/renderer/particle-system-renderer-gpu.ts @@ -50,6 +50,7 @@ const _sample_num = 32; const _sample_interval = 1.0 / _sample_num; const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE'; +const CC_USE_EMBEDDED_ALPHA = 'CC_USE_EMBEDDED_ALPHA'; const CC_RENDER_MODE = 'CC_RENDER_MODE'; const RENDER_MODE_BILLBOARD = 0; @@ -608,7 +609,7 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas this._defaultMat.setProperty('mainTexture', this._renderInfo!.mainTexture); } } - const mat: Material | null = ps.getMaterialInstance(0) || this._defaultMat; + const mat: Material | null = ps.getMaterialInstance(0) || this._defaultMat!; ps.node.getWorldMatrix(_tempWorldTrans); @@ -635,6 +636,11 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas } const textureModule = ps._textureAnimationModule; if (textureModule && textureModule.enable) { + const texture = mat.getProperty('mainTexture', 0) as Texture2D | null; + if (texture && texture.isAlphaAtlas) { + textureModule.scaleNumTilesXY(2); + this._defines[CC_USE_EMBEDDED_ALPHA] = true; + } Vec2.set(this._frameTile_velLenScale, textureModule.numTilesX, textureModule.numTilesY); Vec4.copy(this._unifrom_velLenScale, this._frameTile_velLenScale); } else { @@ -643,12 +649,12 @@ export default class ParticleSystemRendererGPU extends ParticleSystemRendererBas Vec4.copy(this._unifrom_velLenScale, this._tmp_velLenScale); } - this.initShaderUniform(mat!); + this.initShaderUniform(mat); - mat!.recompileShaders(this._defines); + mat.recompileShaders(this._defines); if (this._model) { - this._model.updateMaterial(mat!); + this._model.updateMaterial(mat); } } diff --git a/editor/assets/effects/particles/builtin-billboard.effect b/editor/assets/effects/particles/builtin-billboard.effect index 323c4bb0f3..eafef0b3d5 100644 --- a/editor/assets/effects/particles/builtin-billboard.effect +++ b/editor/assets/effects/particles/builtin-billboard.effect @@ -132,6 +132,7 @@ CCProgram vert %{ CCProgram tinted-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -142,13 +143,13 @@ CCProgram tinted-fs %{ }; vec4 add () { - vec4 col = 2.0 * color * tintColor * texture(mainTexture, uv); + vec4 col = 2.0 * color * tintColor * CCSampleWithAlphaSeparated(mainTexture, uv); return CCFragOutput(col); } vec4 multiply () { vec4 col; - vec4 texColor = texture(mainTexture, uv); + vec4 texColor = CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0); col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0); return CCFragOutput(col); @@ -158,6 +159,7 @@ CCProgram tinted-fs %{ CCProgram no-tint-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -165,13 +167,13 @@ CCProgram no-tint-fs %{ uniform sampler2D mainTexture; vec4 addSmooth () { - vec4 col = color * texture(mainTexture, uv); + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb *= col.a; return CCFragOutput(col); } vec4 premultiplied () { - vec4 col = color * texture(mainTexture, uv) * color.a; + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv) * color.a; return CCFragOutput(col); } }% diff --git a/editor/assets/effects/particles/builtin-particle-gpu.effect b/editor/assets/effects/particles/builtin-particle-gpu.effect index 17757a2c44..fb77ad4b5b 100644 --- a/editor/assets/effects/particles/builtin-particle-gpu.effect +++ b/editor/assets/effects/particles/builtin-particle-gpu.effect @@ -114,6 +114,7 @@ CCEffect %{ CCProgram tinted-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -125,13 +126,13 @@ CCProgram tinted-fs %{ }; vec4 add () { - vec4 col = 2.0 * color * tintColor * texture(mainTexture, uv); + vec4 col = 2.0 * color * tintColor * CCSampleWithAlphaSeparated(mainTexture, uv); return CCFragOutput(col); } vec4 multiply () { vec4 col; - vec4 texColor = texture(mainTexture, uv); + vec4 texColor = CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0); return CCFragOutput(col); } @@ -140,6 +141,7 @@ CCProgram tinted-fs %{ CCProgram no-tint-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -147,13 +149,13 @@ CCProgram no-tint-fs %{ uniform sampler2D mainTexture; vec4 addSmooth () { - vec4 col = color * texture(mainTexture, uv); + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb *= col.a; return CCFragOutput(col); } vec4 premultiplied () { - vec4 col = color * texture(mainTexture, uv) * color.a; + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv) * color.a; return CCFragOutput(col); } }% diff --git a/editor/assets/effects/particles/builtin-particle-trail.effect b/editor/assets/effects/particles/builtin-particle-trail.effect index 3fdaac2fe6..1bd0598d2b 100644 --- a/editor/assets/effects/particles/builtin-particle-trail.effect +++ b/editor/assets/effects/particles/builtin-particle-trail.effect @@ -107,6 +107,7 @@ CCEffect %{ CCProgram tinted-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -120,7 +121,7 @@ CCProgram tinted-fs %{ }; vec4 add () { - vec4 col = 2.0 * color * tintColor * texture(mainTexture, uv); + vec4 col = 2.0 * color * tintColor * CCSampleWithAlphaSeparated(mainTexture, uv); #if CC_DRAW_WIRE_FRAME if (any(lessThan(vBarycentric, vec3(0.02)))) { col = vec4(0., 1., 1., 1.); @@ -131,7 +132,7 @@ CCProgram tinted-fs %{ vec4 multiply () { vec4 col; - vec4 texColor = texture(mainTexture, uv); + vec4 texColor = CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0); col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0); #if CC_DRAW_WIRE_FRAME @@ -146,6 +147,7 @@ CCProgram tinted-fs %{ CCProgram no-tint-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -153,13 +155,13 @@ CCProgram no-tint-fs %{ uniform sampler2D mainTexture; vec4 addSmooth () { - vec4 col = color * texture(mainTexture, uv); + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb *= col.a; return CCFragOutput(col); } vec4 premultiplied () { - vec4 col = color * texture(mainTexture, uv) * color.a; + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv) * color.a; return CCFragOutput(col); } }% diff --git a/editor/assets/effects/particles/builtin-particle-xr-trail.effect b/editor/assets/effects/particles/builtin-particle-xr-trail.effect index e4abd293ea..18de9dbe60 100644 --- a/editor/assets/effects/particles/builtin-particle-xr-trail.effect +++ b/editor/assets/effects/particles/builtin-particle-xr-trail.effect @@ -40,6 +40,7 @@ CCEffect %{ CCProgram tinted-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -53,7 +54,7 @@ CCProgram tinted-fs %{ }; vec4 add () { - vec4 col = 2.0 * color * tintColor * texture(mainTexture, uv); + vec4 col = 2.0 * color * tintColor * CCSampleWithAlphaSeparated(mainTexture, uv); #if CC_DRAW_WIRE_FRAME if (any(lessThan(vBarycentric, vec3(0.02)))) { col = vec4(0., 1., 1., 1.); diff --git a/editor/assets/effects/particles/builtin-particle.effect b/editor/assets/effects/particles/builtin-particle.effect index 057016f189..0be49d9997 100644 --- a/editor/assets/effects/particles/builtin-particle.effect +++ b/editor/assets/effects/particles/builtin-particle.effect @@ -114,6 +114,7 @@ CCEffect %{ CCProgram tinted-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -124,13 +125,13 @@ CCProgram tinted-fs %{ }; vec4 add () { - vec4 col = 2.0 * color * tintColor * texture(mainTexture, uv); + vec4 col = 2.0 * color * tintColor * CCSampleWithAlphaSeparated(mainTexture, uv); return CCFragOutput(col); } vec4 multiply () { vec4 col; - vec4 texColor = texture(mainTexture, uv); + vec4 texColor = CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0); return CCFragOutput(col); } @@ -139,6 +140,7 @@ CCProgram tinted-fs %{ CCProgram no-tint-fs %{ precision mediump float; #include + #include in vec2 uv; in vec4 color; @@ -146,13 +148,13 @@ CCProgram no-tint-fs %{ uniform sampler2D mainTexture; vec4 addSmooth () { - vec4 col = color * texture(mainTexture, uv); + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv); col.rgb *= col.a; return CCFragOutput(col); } vec4 premultiplied () { - vec4 col = color * texture(mainTexture, uv) * color.a; + vec4 col = color * CCSampleWithAlphaSeparated(mainTexture, uv) * color.a; return CCFragOutput(col); } }% diff --git a/native/cocos/core/assets/TextureBase.cpp b/native/cocos/core/assets/TextureBase.cpp index 696e0bc16c..b8629e946f 100644 --- a/native/cocos/core/assets/TextureBase.cpp +++ b/native/cocos/core/assets/TextureBase.cpp @@ -182,6 +182,10 @@ bool TextureBase::isCompressed() const { return (_format >= PixelFormat::RGB_ETC1 && _format <= PixelFormat::RGBA_ASTC_12X12) || (_format >= PixelFormat::RGB_A_PVRTC_2BPPV1 && _format <= PixelFormat::RGBA_ETC1); } +bool TextureBase::isAlphaAtlas() const { + return (_format == PixelFormat::RGBA_ETC1 || _format == PixelFormat::RGB_A_PVRTC_4BPPV1 || _format == PixelFormat::RGB_A_PVRTC_2BPPV1); +} + void TextureBase::notifySamplerUpdated() { // emit(EventTypesToJS::TEXTURE_BASE_GFX_SAMPLER_UPDATED, _gfxSampler); emit(_gfxSampler); diff --git a/native/cocos/core/assets/TextureBase.h b/native/cocos/core/assets/TextureBase.h index ffde4bb1a4..4486b34d02 100644 --- a/native/cocos/core/assets/TextureBase.h +++ b/native/cocos/core/assets/TextureBase.h @@ -78,6 +78,12 @@ public: */ bool isCompressed() const; + /** + * @en Whether it is a compressed alpha channel texture + * @zh 是否为压缩的透明通道贴图 + */ + bool isAlphaAtlas() const; + /** * @en Pixel width of the texture * @zh 此贴图的像素宽度。 diff --git a/native/tools/swig-config/assets.i b/native/tools/swig-config/assets.i index 0758e8e6f9..c855f2bd78 100644 --- a/native/tools/swig-config/assets.i +++ b/native/tools/swig-config/assets.i @@ -139,6 +139,7 @@ %attribute(cc::BufferAsset, cc::ArrayBuffer*, _nativeAsset, getNativeAssetForJS, setNativeAssetForJS); %attribute(cc::TextureBase, bool, isCompressed, isCompressed); +%attribute(cc::TextureBase, bool, isAlphaAtlas, isAlphaAtlas); %attribute(cc::TextureBase, uint32_t, _width, getWidth, setWidth); %attribute(cc::TextureBase, uint32_t, width, getWidth, setWidth); %attribute(cc::TextureBase, uint32_t, _height, getHeight, setHeight);