Improved Particle System-dynamic shader Compilation

Source: Internet
Author: User

Dynamically determine the FX code to be compiled
I asked a few friends and finally decided to use the macro definition supported by FX to solve this problem.
You can define different macros Based on the transmitters and delimiters used by the particles. In the FX file, you can determine whether the corresponding macros are defined (# ifdef... # Endif) to determine whether to compile a code segment.
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 finitparticipant lewidth;
Float finitparticle height;
 
// Texture textureparticle;
Sampler participant sampler = sampler_state
{
// Texture = (textureparticle );

Addressu = wrap;
Addressv = wrap;
Addressw = wrap;
Minfilter = Linear;
Magfilter = Linear;
Mipfilter = Linear;
};
 
 
# Ifdef animationtexture
Int 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 fparticipant lewidth = finitparticipant lewidth;
Float fparticipant leheight = finitparticipant leheight;
# Ifdef v3dscaleaffector
Float fdeltascale = fscalepersecond * flivetime;
Fparticipant lewidth + = fdeltascale;
Fparticipant leheight + = fdeltascale;
If (fparticipant lewidth <0.f | fparticipant leheight <0.f)
Fparticipant lewidth = fparticipant leheight = 0.f;
# Endif
 
// Position = right + up + Pos;
Float4 right = rightvector * input. tex1.x * fparticipant lewidth;
Float4 up = upvector * input. tex1.y * fparticipant leheight;
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;
# 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 icurry = ianimtexcurrframe/ianimtexsidelength;
Float2 fanimtexcorrdxy;
If (input. tex1.x <0.f)
{
Fanimtexcorrdxy. x = (float) (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) (icurry + 1)/(float) (ianimtexsidelength );
}
Else if (input. tex1.x> 0.f)
{
Fanimtexcorrdxy. Y = (float) (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 [irottexbaseidx + 1]) + 0.5;
Frottexcorrdxy. Y = (fsinrot * frottexdatabase [irottexbaseidx])-(fcosrot * frottexdatabase [irottexbaseidx + 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 (participant sampler, In. tex0 );
Finalcolour. W = 1.f;

Return finalcolour;
}
 
Technique tec0
{
Pass P0
{
Vertexshader = compile vs_1_1 ();

# Ifdef v3dcolourimageaffector
Pixelshader = compile ps_1_1 ps_1_1 (); // NULL;
# Else
Pixelshader = NULL;
# Endif
}
}
Degree of support for the transmitter and receiver
Attributes supported by the shader Renderer
1. Default height
2, default width
3. the maximum number of particles indicates the number of particles that exist simultaneously.
4. Particle Orientation
5. Camera-oriented approach
6. Particle UP Vector
7. Whether it is a 2D Particle System
Support for all transmitters
8. special attributes of the transmitter are supported (such as the inner ring size and outer ring size of the ring TRANSMITTER)
9, Angle
10, starting color
11. End color
12, Direction
13. minimum lifetime
14. Maximum Lifetime
15. Minimum speed
16, maximum speed
17, location
Supported delimiters and attributes
18, color Attenuation
19. Linear External Force: "External Force" refers to acceleration A, which satisfies the formula S = vt + 1/2 * a * T * t. The Force mode does not work.
20. Rotate
21. Zoom
22. Color attenuation Diagram
 
The following table does not support status-independent delimiters:
1. Collision body
2. Random speed
3. Various force fields (linear force field and point force field)
Project changes
1. For particles that require GPU optimization or GPU enhancement, art Editors Use the "shader Renderer" version.
2. The naming rule for the shader Renderer implementation version is to add "_ shader" after the file name of the original billboard Renderer. The program uses the file name to distinguish different versions.
3. When loading particles in the game
If (with GPU implementation version) & (the video card supports the Vs and PS versions required by this particle ))
Load GPU implementation version
Else
Load the CPU implementation version

1. Rotate

2. Zoom. Most of the operations are video card operations.

3. Texture Animation

Todo
1. solve the problem that the effect of GPU processing and CPU processing on particles is somewhat different.
 
 
By fannyfish

Blog: http://blog.csdn.net/fannyfish

Amma@zsws.org

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fannyfish/archive/2006/06/22/823032.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.