|
|
2 ماه پیش | |
|---|---|---|
| .. | ||
| .config | 2 ماه پیش | |
| .vscode | 2 ماه پیش | |
| Core | 2 ماه پیش | |
| Platforms | 2 ماه پیش | |
| Processor | 2 ماه پیش | |
| README.md | 2 ماه پیش | |
| ShatterEffectSample.sln | 2 ماه پیش | |
This sample shows how you can apply an effect on any model in your game to shatter it apart. The effect is simulated with a vertex shader.
The shatter effect operates independently on every triangle in the model. For every triangle in the model, the vertex shader rotates the vertices of the triangle around the x, y, and z axes by random velocities. At the same time, the triangle is translated along its normal. This creates the appearance of the entire model shattering outwards into small pieces. To get this effect to work properly, the model must be processed beforehand with a custom processor that derives from ModelProcessor.
This sample uses the following keyboard and gamepad controls:
| Action | Keyboard control | Gamepad control |
|---|---|---|
| Shatter the model | UP ARROW | A |
| Reverse the shatter effect | DOWN ARROW | B |
| Exit the sample | ESC or ALT+F4 | BACK |
The shatter effect is achieved with the help of both a processor and an effect.
The processor, ShatterProcessor, is derived from ModelProcessor and overrides the Process method. The processor manipulates the model’s data in three ways before passing it to ModelProcessor to produce the processor’s final output.
This step reverses the effect of indexing a model’s vertices to break up the triangles. While it will increase the number of vertices for the model on the GPU, this is required so that we can independently manipulate every triangle without affecting nearby ones. An indexed model does not give us such freedom.
Once the model’s triangles have been broken down, the processor calculates the center point of every triangle using the formula:
TriangleCenter = (Vertex_1 + Vertex_2 + Vertex_3) / 3
The triangle centers are then saved in a Vector3[] array. For every triangle in the model, the triangle center is used in the vertex shader as the point to rotate the triangle around.
Now that the triangle centers have been calculated, the processor creates a per-vertex channel to store them. For every three vertices, we store the triangle center that corresponds to the triangle they form. The channel is called triangleCenterChannel. This channel adds a float3 TEXCOORD1 field to the vertex declaration for the vertex shader to use. The channel is created with the help of a helper function called ReplicateTriangleDataToEachVertex. This function uses the Vector3[] array created earlier and returns the corresponding center for every vertex in the model.
The second channel the processor creates is called rotationalVelocityChannel. This channel creates a float3 TEXCOORD2 field in the vertex declaration for the model. For every vertex, the processor generates a random set of x, y, and z velocities in the range of –1 to 1. The vertex shader uses these values to determine how to rotate each vertex as the model is shattered.
The processor then passes the newly created model to the parent processor, ModelProcessor, to complete processing it.
The sample can be extended in a variety of ways:
Core/ — Shared game logic and content pipeline filesCore/Content/ — Prebuilt .xnb content and related assetsPlatforms/Windows/ — Windows-specific project (net8.0-windows)Platforms/DesktopGL/ — DesktopGL cross-platform project (net8.0)Platforms/Android/ — Android project (net8.0-android)Platforms/iOS/ — iOS project (net8.0-ios)launch.json and tasks.json to build and run:
Ctrl+Shift+B to build (choose build-windows or build-desktopgl)F5 to launchdotnet build from the command line:
dotnet build Platforms/Android/ShatterEffectSample.Android.csprojdotnet build Platforms/iOS/ShatterEffectSample.iOS.csprojContent/ folder.#if blocks.