V3.6.0 instanced particle shader fix (#11063)

* add particle instance support

* do not change particle-system.ts

* add param type to fix eslint error

* fix memory leak

* fix native destroy error

* fix particle shader bug

* remove #else

* change attribute name

* use vec2 for texcoord

* fix shader

* fix gpu shader bug

* compile native shader
This commit is contained in:
zxx43
2022-05-14 15:56:37 +08:00
committed by GitHub
parent 085a86be6e
commit 630fd4731e
9 changed files with 104 additions and 84 deletions
+25 -19
View File
@@ -325,7 +325,7 @@ export const effects = [
"shaders": [
{
"name": "particle-gpu|particle-vs-gpu:gpvs_main|tinted-fs:add",
"hash": 441769894,
"hash": 2909083171,
"builtins": {
"statistics": { "CC_EFFECT_USED_VERTEX_UNIFORM_VECTORS": 65, "CC_EFFECT_USED_FRAGMENT_UNIFORM_VECTORS": 41 },
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCCamera", "defines": [] }], "samplerTextures": [], "buffers": [], "images": [] },
@@ -333,6 +333,7 @@ export const effects = [
},
"defines": [
{ "name": "CC_RENDER_MODE", "type": "number", "range": [0, 4] },
{ "name": "CC_INSTANCE_PARTICLE", "type": "boolean" },
{ "name": "USE_VK_SHADER", "type": "boolean" },
{ "name": "COLOR_OVER_TIME_MODULE_ENABLE", "type": "boolean" },
{ "name": "ROTATION_OVER_TIME_MODULE_ENABLE", "type": "boolean" },
@@ -344,15 +345,18 @@ export const effects = [
],
"attributes": [
{ "name": "a_position_starttime", "defines": [], "format": 44, "location": 0 },
{ "name": "a_size_uv", "defines": [], "format": 44, "location": 1 },
{ "name": "a_rotation_uv", "defines": [], "format": 44, "location": 2 },
{ "name": "a_color", "defines": [], "format": 44, "location": 3 },
{ "name": "a_dir_life", "defines": [], "format": 44, "location": 4 },
{ "name": "a_rndSeed", "defines": [], "format": 11, "location": 5 },
{ "name": "a_texCoord", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 6 },
{ "name": "a_texCoord3", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 7 },
{ "name": "a_normal", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 8 },
{ "name": "a_color1", "defines": ["CC_RENDER_MODE"], "format": 44, "location": 9 }
{ "name": "a_color", "defines": [], "format": 44, "location": 1 },
{ "name": "a_dir_life", "defines": [], "format": 44, "location": 2 },
{ "name": "a_rndSeed", "defines": [], "format": 11, "location": 3 },
{ "name": "a_size_uv", "defines": ["!CC_INSTANCE_PARTICLE"], "format": 44, "location": 4 },
{ "name": "a_rotation_uv", "defines": ["!CC_INSTANCE_PARTICLE"], "format": 44, "location": 5 },
{ "name": "a_size_fid", "defines": ["CC_INSTANCE_PARTICLE"], "format": 44, "location": 6 },
{ "name": "a_rotation", "defines": ["CC_INSTANCE_PARTICLE"], "format": 32, "location": 7 },
{ "name": "a_uv", "defines": ["CC_INSTANCE_PARTICLE"], "format": 32, "location": 8 },
{ "name": "a_texCoord", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 9 },
{ "name": "a_texCoord3", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 10 },
{ "name": "a_normal", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 11 },
{ "name": "a_color1", "defines": ["CC_RENDER_MODE"], "format": 44, "location": 12 }
],
"blocks": [
{"name": "Constants", "defines": [], "binding": 0, "stageFlags": 1, "members": [
@@ -465,7 +469,7 @@ export const effects = [
"shaders": [
{
"name": "particle|particle-vs-legacy:lpvs_main|tinted-fs:add",
"hash": 2896871743,
"hash": 3975181301,
"builtins": {
"statistics": { "CC_EFFECT_USED_VERTEX_UNIFORM_VECTORS": 54, "CC_EFFECT_USED_FRAGMENT_UNIFORM_VECTORS": 41 },
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCCamera", "defines": [] }], "samplerTextures": [], "buffers": [], "images": [] },
@@ -473,18 +477,20 @@ export const effects = [
},
"defines": [
{ "name": "CC_RENDER_MODE", "type": "number", "range": [0, 4] },
{ "name": "CC_INSTANCE_PARTICLE", "type": "boolean" },
{ "name": "CC_USE_WORLD_SPACE", "type": "boolean" },
{ "name": "ROTATION_OVER_TIME_MODULE_ENABLE", "type": "boolean" }
],
"attributes": [
{ "name": "a_position", "defines": [], "format": 32, "location": 0 },
{ "name": "a_texCoord", "defines": [], "format": 32, "location": 1 },
{ "name": "a_texCoord1", "defines": [], "format": 32, "location": 2 },
{ "name": "a_texCoord2", "defines": [], "format": 32, "location": 3 },
{ "name": "a_color", "defines": [], "format": 44, "location": 4 },
{ "name": "a_color1", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 8 },
{ "name": "a_texCoord3", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 6 },
{ "name": "a_normal", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 7 }
{ "name": "a_texCoord1", "defines": [], "format": 32, "location": 0 },
{ "name": "a_texCoord2", "defines": [], "format": 32, "location": 1 },
{ "name": "a_color", "defines": [], "format": 44, "location": 2 },
{ "name": "a_texCoord", "defines": [], "format": 32, "location": 3 },
{ "name": "a_position", "defines": ["!CC_INSTANCE_PARTICLE"], "format": 32, "location": 4 },
{ "name": "a_texCoord4", "defines": ["CC_INSTANCE_PARTICLE"], "format": 44, "location": 5 },
{ "name": "a_color1", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 9 },
{ "name": "a_texCoord3", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 7 },
{ "name": "a_normal", "defines": ["CC_RENDER_MODE"], "format": 32, "location": 8 }
],
"blocks": [
{"name": "Constants", "defines": [], "binding": 0, "stageFlags": 1, "members": [
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -46,6 +46,13 @@ const _uvs = [
1, 1, // top-right
];
const _uvs_ins = [
0, 0, 0, // bottom-left
1, 0, 0, // bottom-right
0, 1, 0, // top-left
1, 1, 0, // top-right
];
export default class ParticleBatchModel extends scene.Model {
private _capacity: number;
private _vertAttrs: Attribute[] | null;
@@ -295,8 +302,8 @@ export default class ParticleBatchModel extends scene.Model {
}
} else {
const vbFloatArray = new Float32Array(vBuffer);
for (let i = 0; i < _uvs.length; ++i) {
vbFloatArray[i] = _uvs[i];
for (let i = 0; i < _uvs_ins.length; ++i) {
vbFloatArray[i] = _uvs_ins[i];
}
}
vertexBuffer.update(vBuffer);
@@ -100,28 +100,28 @@ const _vertex_attrs_mesh = [
];
const _vertex_attrs_ins = [
new Attribute(AttributeName.ATTR_POSITION, Format.RGBA32F, false, 0, true), // position,frame idx
new Attribute(AttributeName.ATTR_TEX_COORD4, Format.RGBA32F, false, 0, true), // position,frame idx
new Attribute(AttributeName.ATTR_TEX_COORD1, Format.RGB32F, false, 0, true), // size
new Attribute(AttributeName.ATTR_TEX_COORD2, Format.RGB32F, false, 0, true), // rotation
new Attribute(AttributeName.ATTR_COLOR, Format.RGBA8, true, 0, true), // color
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RG32F, false, 1), // uv
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RGB32F, false, 1), // uv
];
const _vertex_attrs_stretch_ins = [
new Attribute(AttributeName.ATTR_POSITION, Format.RGBA32F, false, 0, true), // position,frame idx
new Attribute(AttributeName.ATTR_TEX_COORD4, Format.RGBA32F, false, 0, true), // position,frame idx
new Attribute(AttributeName.ATTR_TEX_COORD1, Format.RGB32F, false, 0, true), // size
new Attribute(AttributeName.ATTR_TEX_COORD2, Format.RGB32F, false, 0, true), // rotation
new Attribute(AttributeName.ATTR_COLOR, Format.RGBA8, true, 0, true), // color
new Attribute(AttributeName.ATTR_COLOR1, Format.RGB32F, false, 0, true), // particle velocity
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RG32F, false, 1), // uv
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RGB32F, false, 1), // uv
];
const _vertex_attrs_mesh_ins = [
new Attribute(AttributeName.ATTR_POSITION, Format.RGBA32F, false, 0, true), // particle position,frame idx
new Attribute(AttributeName.ATTR_TEX_COORD4, Format.RGBA32F, false, 0, true), // particle position,frame idx
new Attribute(AttributeName.ATTR_TEX_COORD1, Format.RGB32F, false, 0, true), // size
new Attribute(AttributeName.ATTR_TEX_COORD2, Format.RGB32F, false, 0, true), // rotation
new Attribute(AttributeName.ATTR_COLOR, Format.RGBA8, true, 0, true), // particle color
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RG32F, false, 1), // mesh uv
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RGB32F, false, 1), // mesh uv
new Attribute(AttributeName.ATTR_TEX_COORD3, Format.RGB32F, false, 1), // mesh position
new Attribute(AttributeName.ATTR_NORMAL, Format.RGB32F, false, 1), // mesh normal
new Attribute(AttributeName.ATTR_COLOR1, Format.RGBA8, true, 1), // mesh color
@@ -109,7 +109,7 @@ const _gpu_vert_attr_ins = [
new Attribute(_vert_attr_name.COLOR, Format.RGBA32F, false, 0, true),
new Attribute(_vert_attr_name.DIR_LIFE, Format.RGBA32F, false, 0, true),
new Attribute(_vert_attr_name.RANDOM_SEED, Format.R32F, false, 0, true),
new Attribute(_vert_attr_name.VERT_UV, Format.RG32F, false, 1),
new Attribute(_vert_attr_name.VERT_UV, Format.RGB32F, false, 1),
];
const _gpu_vert_attr_mesh_ins = [
@@ -119,7 +119,7 @@ const _gpu_vert_attr_mesh_ins = [
new Attribute(_vert_attr_name.COLOR, Format.RGBA32F, false, 0, true),
new Attribute(_vert_attr_name.DIR_LIFE, Format.RGBA32F, false, 0, true),
new Attribute(_vert_attr_name.RANDOM_SEED, Format.R32F, false, 0, true),
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RG32F, false, 1), // mesh uv
new Attribute(AttributeName.ATTR_TEX_COORD, Format.RGB32F, false, 1), // mesh uv
new Attribute(AttributeName.ATTR_TEX_COORD3, Format.RGB32F, false, 1), // mesh position
new Attribute(AttributeName.ATTR_NORMAL, Format.RGB32F, false, 1), // mesh normal
new Attribute(AttributeName.ATTR_COLOR1, Format.RGBA8, true, 1), // mesh color
+30 -28
View File
@@ -32,31 +32,23 @@ uniform TickConstants {
vec4 u_timeDelta;
};
in vec4 a_position_starttime; // center position,particle start time
in vec4 a_color;
in vec4 a_dir_life; // xyz:particle start velocity,w:particle lifetime
in float a_rndSeed;
#if !CC_INSTANCE_PARTICLE
in vec4 a_position_starttime; // center position,particle start time
in vec4 a_size_uv; // xyz:size, w:uv_0
in vec4 a_rotation_uv; // xyz:rotation, w:uv_1
in vec4 a_color;
in vec4 a_dir_life; // xyz:particle start velocity,w:particle lifetime
in float a_rndSeed;
#else
in vec4 a_position_starttime; // center position,particle start time
#endif
#if CC_INSTANCE_PARTICLE
in vec4 a_size_fid; // xyz:size, w:fid
in vec3 a_rotation; // xyz:rotation
in vec4 a_color;
in vec4 a_dir_life; // xyz:particle start velocity,w:particle lifetime
in float a_rndSeed;
#if CC_RENDER_MODE != RENDER_MODE_MESH
in vec2 a_uv;
#endif
in vec3 a_uv;
#endif
#if CC_RENDER_MODE == RENDER_MODE_MESH
#if !CC_INSTANCE_PARTICLE
in vec3 a_texCoord; // mesh uv
#else
in vec2 a_texCoord;
#endif
in vec3 a_texCoord; // mesh uv
in vec3 a_texCoord3; // mesh vertices
in vec3 a_normal; // mesh normal
in vec4 a_color1; // mesh color
@@ -103,7 +95,8 @@ float pseudoRandom(float x) {
v *= 0.7071067812; // fix sheared 'distance' measurements - but this is 1D so unsure it's even a good thing to try
v = y < 0.0 ? -v : v;
return v;
#else
#endif
#if !USE_VK_SHADER
float seed = mod(x, 233280.);
float q = (seed * 9301. + 49297.) / 233280.;
return fract(q);
@@ -193,11 +186,13 @@ vec4 gpvs_main () {
#if CC_RENDER_MODE == RENDER_MODE_MESH
vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);
#else
#endif
#if CC_RENDER_MODE != RENDER_MODE_MESH
#if !CC_INSTANCE_PARTICLE
vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);
#else
vec2 vertIdx = a_uv;
#endif
#if CC_INSTANCE_PARTICLE
vec2 vertIdx = a_uv.xy;
#endif
#endif
@@ -207,7 +202,8 @@ vec4 gpvs_main () {
// size
#if !CC_INSTANCE_PARTICLE
vec3 size = a_size_uv.xyz;
#else
#endif
#if CC_INSTANCE_PARTICLE
vec3 size = a_size_fid.xyz;
#endif
#if SIZE_OVER_TIME_MODULE_ENABLE
@@ -275,7 +271,8 @@ vec4 gpvs_main () {
// rotation
#if !CC_INSTANCE_PARTICLE
vec3 startRotation = a_rotation_uv.xyz;
#else
#endif
#if CC_INSTANCE_PARTICLE
vec3 startRotation = a_rotation;
#endif
#if CC_RENDER_MODE != RENDER_MODE_MESH
@@ -283,11 +280,13 @@ vec4 gpvs_main () {
vec3 rotEuler = startRotation.xyz;
#elif CC_RENDER_MODE == RENDER_MODE_STRETCHED_BILLBOARD
vec3 rotEuler = vec3(0.);
#else
#endif
#if CC_RENDER_MODE != RENDER_MODE_BILLBOARD && CC_RENDER_MODE != RENDER_MODE_STRETCHED_BILLBOARD
vec3 rotEuler = vec3(0., 0., startRotation.z);
#endif
vec4 rot = quaternionFromEuler(rotEuler);
#else
#endif
#if CC_RENDER_MODE == RENDER_MODE_MESH
vec4 rot = quaternionFromEuler(startRotation);
#endif
@@ -323,7 +322,8 @@ vec4 gpvs_main () {
float factor_c = pseudoRandom(a_rndSeed + COLOR_OVERTIME_RAND_OFFSET);
color = a_color * mix(color_0, color_1, factor_c);
}
#else
#endif
#if !COLOR_OVER_TIME_MODULE_ENABLE
color = a_color;
#endif
@@ -345,12 +345,14 @@ vec4 gpvs_main () {
, frameTile_velLenScale.w
#if !CC_INSTANCE_PARTICLE
, a_size_uv.w
#else
#endif
#if CC_INSTANCE_PARTICLE
, a_uv.x
#endif
#endif
);
#else
#endif
#if CC_RENDER_MODE == RENDER_MODE_MESH
mat3 rotMat = quatToMat3(rot);
mat3 nodeMat = quatToMat3(nodeRotation);
rotMat = nodeMat * rotMat;
+26 -21
View File
@@ -4,27 +4,22 @@ precision highp float;
#include <particle-common>
#include <cc-local>
in vec3 a_texCoord1; // size
in vec3 a_texCoord2; // rotation
in vec4 a_color;
in vec3 a_texCoord; // xy:vertex index,z:frame index
#if !CC_INSTANCE_PARTICLE
in vec3 a_position; // center position
in vec3 a_texCoord; // xy:vertex index,z:frame index
in vec3 a_texCoord1; // size
in vec3 a_texCoord2; // rotation
in vec4 a_color;
#else
in vec4 a_position; // xyz:position,z:frame index
in vec3 a_texCoord1; // size
in vec3 a_texCoord2; // rotation
in vec4 a_color;
in vec3 a_position; // center position
#endif
#if CC_INSTANCE_PARTICLE
in vec4 a_texCoord4; // xyz:position,z:frame index
#endif
#if CC_RENDER_MODE == RENDER_MODE_STRETCHED_BILLBOARD
in vec3 a_color1; // velocity.x, velocity.y, velocity.z, scale
#endif
#if CC_INSTANCE_PARTICLE
in vec2 a_texCoord; // uv
#endif
#if CC_RENDER_MODE == RENDER_MODE_MESH
in vec3 a_texCoord3; // mesh vertices
in vec3 a_normal; // mesh normal
@@ -33,7 +28,12 @@ precision highp float;
vec4 lpvs_main () {
vec3 compScale = scale.xyz * a_texCoord1;
vec4 pos = vec4(a_position.xyz, 1);
#if !CC_INSTANCE_PARTICLE
vec4 pos = vec4(a_position.xyz, 1);
#endif
#if CC_INSTANCE_PARTICLE
vec4 pos = vec4(a_texCoord4.xyz, 1);
#endif
#if CC_RENDER_MODE == RENDER_MODE_STRETCHED_BILLBOARD
vec4 velocity = vec4(a_color1.xyz, 0);
#endif
@@ -57,17 +57,20 @@ vec4 lpvs_main () {
}
vec4 rot = vec4(rotTmp, 0.0);
rot.w = mulFactor * sqrt(abs(1.0 - rot.x * rot.x - rot.y * rot.y - rot.z * rot.z));
#else
#endif
#if !ROTATION_OVER_TIME_MODULE_ENABLE
#if CC_RENDER_MODE != RENDER_MODE_MESH
#if CC_RENDER_MODE == RENDER_MODE_BILLBOARD
vec3 rotEuler = a_texCoord2;
#elif CC_RENDER_MODE == RENDER_MODE_STRETCHED_BILLBOARD
vec3 rotEuler = vec3(0.);
#else
#endif
#if CC_RENDER_MODE != RENDER_MODE_BILLBOARD && CC_RENDER_MODE != RENDER_MODE_STRETCHED_BILLBOARD
vec3 rotEuler = vec3(0., 0., a_texCoord2.z);
#endif
vec4 rot = quaternionFromEuler(rotEuler);
#else
#endif
#if CC_RENDER_MODE == RENDER_MODE_MESH
vec4 rot = quaternionFromEuler(a_texCoord2);
#endif
#endif
@@ -84,7 +87,8 @@ vec4 lpvs_main () {
#endif
color = a_color;
#else
#endif
#if CC_RENDER_MODE == RENDER_MODE_MESH
mat3 rotMat = quatToMat3(rot);
mat3 nodeMat = quatToMat3(nodeRotation);
rotMat = nodeMat * rotMat;
@@ -99,8 +103,9 @@ vec4 lpvs_main () {
#if !CC_INSTANCE_PARTICLE
uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;
#else
uv = computeUV(a_position.w, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;
#endif
#if CC_INSTANCE_PARTICLE
uv = computeUV(a_texCoord4.w, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;
#endif
pos = cc_matViewProj * pos;