Improved particle system - dynamic compile SHADER

xiaoxiao2021-04-07  382

Improved particle system - dynamic compile SHADER

Dynamically determine the FX code that needs to be compiled

Ask a few friends, and finally decide to solve this problem with macro definitions supported by FX.

Depending on the transmitter and the effect of the particles and the effect definition, in the FX file, by judging whether the corresponding macro is defined (# ifdef ... # ENDIF) to determine whether to compile a certain piece of code.

The FX code is as follows:

Struct vs_INPUT

{

FLOAT3 POSITION: POSITION;

FLOAT2 TEX0: TEXCOORD0;

FLOAT3 TEX1: Texcoord1; // Upoffset, Leftoffset, TotalTimetolife

FLOAT3 TEX2: TEXCOORD2; // Velocity

FLOAT3 TEX3: TEXCOORD3; // Rotation

Float3 StartDiffuse: Color0;

}

Struct vs_output

{

FLOAT4 POSITION: Position;

FLOAT3 DIFFUSE: Color0;

FLOAT2 TEX0: TEXCOORD0;

FLOAT2 TEX1: Texcoord1;

}

Matrix matworldviewproj; // world-view-proj matrix

Float4 RightVector; // Right Vector

FLOAT4 UpVector; // Up Vector

FLOAT4 TIME_COLOUR; // Elasped Time, Delta Colour

Float4 acceleration;

Float finitparticlewidth;

Float finitparticleheight;

// Texture TexturePArticle;

Sampler Particlesampler = Sampler_State

{

// texture = (TexturePArticle);

Addressu = WRAP;

Addressv = Wrap;

Addressw = Wrap;

Minfilter = linear;

MAGFILTER = linear;

MIPFILTER = linear;

}

#ifdef AnimationTexture

IANIMTEXSIDELENGTH;

INT IANIMTEXNUMFRAME;

Float fanimtexduration;

#ENDIF

#ifdef v3dscaleAffector

Float fscalepersecond;

#ENDIF

#ifdef v3dcolourimageAffector

Texture TextureColour;

Sampler coloursampler = Sampler_State

{

Texture = (TextURECOLOUR);

Minfilter = POINT;

Magfilter = POINT;

MIPFILTER = POINT;

}

#ENDIF

Vs_output vs (const vs_input input)

{

Vs_output out = (vs_output) 0;

// live time = fmod (Elapsed Time, TotalTimetolife)

Float flivetime = fmod (time_colour.x, input.tex1.z);

Float fparticlewidth = finitparticlewidth;

Float fpArticleheight = FinitPArticleHeight;

#ifdef v3dscaleAffector

FLOAT FDELTASCALE = fscalepersecond * flivetime;

FPArticleWidth = fdeltascale;

FPArticleheight = fdeltascale;

IF (FPArticleWidth <0.F || fpArticleHeight <0.F)

FPArticleWidth = fparticleheight = 0.f;

#ENDIF

// Position = Right Up POS;

FLOAT4 Right = RightVector * Input.tex1.x * fparticlewidth;

FLOAT4 UP = UpVector * Input.tex1.y * fparticleheight;

Float4 POS = float4 (Input.position, 0) Right Up;

// position = POS VT 1/2 * V * T * T

Float4 deltavel = mul (float4 (input.tex2,0), flivetime);

#ifdef v3dlinearforceaffector

Deltavel = deltavel acceleration * flivetime * flivetime;

#ENDIF

POS = POS Deltavel;

POS.W = 1.0;

Out.position = MUL (POS, MATWORLDVIEWPROJ);

// color

Out.diffuse = INPUT.StartDiffuse;

#ifdef v3dcolourfaderaffector

Out.diffuse.x = input.startdiffuse.x Time_COLOUR.Y * flivetime;

Out.diffuse.y = input.startdiffuse.y time_colour.z * flivetime;

Out.diffuse.z = input.startdiffuse.z Time_COLOUR.W * flivetime

#ENDIF

// texcoord

Out.tex0 = input.tex0;

#ifdef AnimationTexture

FLOAT FANIMTEXLIVETIME = FMOD (Flivetime, Fanimtexduration);

INT IANIMTEXCURRFRAME = (fanimtexlivetime / fanimtexduration) * ianimtexnumframe

INT icurrx = IANIMTEXCURRFRAME% IANIMTEXSIDELENGTH;

Int icrry = IANIMTEXCURRFRAME / IANIMTEXSIDELENGTH; float2 fanimtexcorrdxy;

IF (Input.Tex1.x <0.f)

{

FANIMTEXCORRDXY.X = (iCurRX) / (FLOAT) (IANIMTEXSIDELENGTH);

}

Else IF (Input.Tex1.x> 0.F)

{

FANIMTEXCORRDXY.X = (FLOAT) (IcURRX 1) / (FLOAT) (IANIMTEXSIDELENGTH);

}

IF (Input.Tex1.y <0.f)

{

FanimTexcorrdxy.y = (float) (ianimtexsideLength) (IANIMTEXSIDELENGTH);

}

Else IF (Input.Tex1.x> 0.F)

{

Fanimtexcorrdxy.y = (iCurry) / (float) (IANIMTEXSIDELENGTH);

}

Out.tex0 = fanimtexcorrdxy;

#ENDIF

#ifdef v3dcolourimageAffector

Out.tex1 = float2 (flivetime, 0.f);

#ENDIF

#ifdef v3drotationAffector

Float frottexdatabase [8] = {

-0.5, 0.5,

0.5, 0.5,

-0.5, -0.5,

0.5, -0.5};

FLOAT frotation = input.tex3.x flivetime * input.tex3.y;

Float fsinrot, fcosrot;

Sincos (FROTATION, FSINROT, FCOSROT);

Float2 frottexcorrdxy;

INT IROTTEXBASEIDX = INPUT.TEX3.Z * 2;

Frottexcorrdxy.x = (fcosrot * frottexdatabase [irottexbaseidx]) (fsinrot * frottexdatabase [ottexbaseidx 1]) 0.5;

Frottexcorrdxy.y = (fsinrot * frottexdatabase) - (fcosrot * frottexdatabase [rttexbaseidx 1]) 0.5;

Out.tex0 = frottexcorrdxy;

#ENDIF

Return Out;

}

FLOAT4 PS_1_1 (vs_output in): Color0

{

FLOAT4 FINALCOLOUR = float4 (in.diffuse, 0.f);

#ifdef v3dcolourimageAffector

FinalColour * = TEX2D (Coloursampler, In.Tex1);

#ENDIF

FinalCOLOUR * = TEX2D (Particlesampler, In.Tex0);

FinalColour.w = 1.f;

Return FinalColour;

}

Technique TEC0

{

Pass P0

{

VertexShader = compile vs_1_1 vs ();

#ifdef v3dcolourimageAffector

Pixelshader = compile ps_1_1 ps_1_1 (); // NULL;

#ELSE

Pixelshader = NULL; #ENDIF

}

}

Support for transmitters and effects

Properties supported by Shader renderer

1, default height

2, default width

3, maximum number of particles

Here represents the number of particles existing at the same time

4, particle head

5, way to face the camera

6, particle UP vector

7. Is it a 2D particle system?

Support all transmitters

8, support transmitter unique properties (such as the inner ring size of the ring transmitter, outer ring size)

9, angle

10, start color

11, end color

12, direction

13, minimum survival

14, maximum survival

15, minimum speed

16, maximum speed

17, position

Supported effects and properties

18, color attenuation

19, linear external force: "external force" refers to acceleration A, meets the formula S = VT 1/2 * a * t * t, the force pattern does not work

20, rotation

21, zoom

22, color attenuation map

Do not support state-independent effects:

1, collision body

2, random speed

3, various power fields (straight line, point force field)

Project change

1. Optimize the GPU to optimize or require the use of GPU enhancements, art editing uses the "Shader Renderer" version.

2. The name rule for the SHADER renderer is to add "_shader" after the file name of the original Billboard renderer, and the program distinguishes different versions via the file name.

3, when loading particles in the game

IF ((with GPU Realization Version) && (graphics card supports VS, PS version))

Load GPU implementation version

Else

Load CPU implementation version

Screenshot

1, rotation

2, zoom, you can see that most of the graphics card operations

3, texture animation

Todo

1. Some different problems in solving particles using GPU processing and CPU processing

Author: fannyfish

BLOG: http://blog.9cbs.net/fannyfish

Amma@zsws.org

转载请注明原文地址:https://www.9cbs.com/read-132516.html

New Post(0)