Fixed an error when using compress texture for 3D particle (#18630)

This commit is contained in:
zhefeng.zhang
2025-04-18 13:44:24 +08:00
committed by GitHub
parent 00611215fe
commit 56fcaea788
12 changed files with 74 additions and 21 deletions
+8
View File
@@ -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 此贴图的像素宽度。
@@ -297,4 +297,13 @@ export default class TextureAnimationModule extends ParticleModuleBase {
}
}
}
/**
* @engineInternal
* @mangle
*/
public scaleNumTilesXY (scale: number): void {
this._numTilesX *= scale;
this._numTilesY *= scale;
}
}
@@ -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();
}
@@ -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);
}
}
@@ -132,6 +132,7 @@ CCProgram vert %{
CCProgram tinted-fs %{
precision mediump float;
#include <legacy/output>
#include <builtin/internal/embedded-alpha>
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 <legacy/output>
#include <builtin/internal/embedded-alpha>
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);
}
}%
@@ -114,6 +114,7 @@ CCEffect %{
CCProgram tinted-fs %{
precision mediump float;
#include <legacy/output>
#include <builtin/internal/embedded-alpha>
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 <legacy/output>
#include <builtin/internal/embedded-alpha>
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);
}
}%
@@ -107,6 +107,7 @@ CCEffect %{
CCProgram tinted-fs %{
precision mediump float;
#include <legacy/output>
#include <builtin/internal/embedded-alpha>
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 <legacy/output>
#include <builtin/internal/embedded-alpha>
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);
}
}%
@@ -40,6 +40,7 @@ CCEffect %{
CCProgram tinted-fs %{
precision mediump float;
#include <legacy/output>
#include <builtin/internal/embedded-alpha>
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.);
@@ -114,6 +114,7 @@ CCEffect %{
CCProgram tinted-fs %{
precision mediump float;
#include <legacy/output>
#include <builtin/internal/embedded-alpha>
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 <legacy/output>
#include <builtin/internal/embedded-alpha>
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);
}
}%
+4
View File
@@ -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<SamplerUpdated>(_gfxSampler);
+6
View File
@@ -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 此贴图的像素宽度。
+1
View File
@@ -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);