Skip to content

Commit e772e56

Browse files
authored
Merge pull request #175 from Doprez/particle-example
feat: Particle example
2 parents 9f052a5 + 9721b77 commit e772e56

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

Stride.CommunityToolkit.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example11_ImGui", "examples
7575
EndProject
7676
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.CommunityToolkit.Bepu", "src\Stride.CommunityToolkit.Bepu\Stride.CommunityToolkit.Bepu.csproj", "{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}"
7777
EndProject
78+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example12_Particles", "examples\code-only\Example12_Particles\Example12_Particles\Example12_Particles.csproj", "{8821704B-5698-4C9A-B05F-C18B7122DD8B}"
79+
EndProject
7880
Global
7981
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8082
Debug|Any CPU = Debug|Any CPU
@@ -196,6 +198,10 @@ Global
196198
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
197199
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
198200
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Release|Any CPU.Build.0 = Release|Any CPU
201+
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
202+
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
203+
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
204+
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Release|Any CPU.Build.0 = Release|Any CPU
199205
EndGlobalSection
200206
GlobalSection(SolutionProperties) = preSolution
201207
HideSolutionNode = FALSE
@@ -222,6 +228,7 @@ Global
222228
{5996C17E-629C-4F58-88F9-494559EB4FBF} = {30457B66-9E10-4290-9744-D0079DE0448B}
223229
{2D37B4BA-50F7-4347-8DC0-FA58C2AC2803} = {30457B66-9E10-4290-9744-D0079DE0448B}
224230
{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008} = {30457B66-9E10-4290-9744-D0079DE0448B}
231+
{8821704B-5698-4C9A-B05F-C18B7122DD8B} = {30457B66-9E10-4290-9744-D0079DE0448B}
225232
EndGlobalSection
226233
GlobalSection(ExtensibilityGlobals) = postSolution
227234
SolutionGuid = {C7A8C18E-C1F5-492F-8173-51EC9495B78C}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\..\..\src\Stride.CommunityToolkit.ImGui\Stride.CommunityToolkit.ImGui.csproj" />
12+
<ProjectReference Include="..\..\..\..\src\Stride.CommunityToolkit.Skyboxes\Stride.CommunityToolkit.Skyboxes.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using Stride.CommunityToolkit.Engine;
2+
using Stride.CommunityToolkit.Games;
3+
using Stride.CommunityToolkit.Skyboxes;
4+
using Stride.Core.Mathematics;
5+
using Stride.Engine;
6+
using Stride.Particles;
7+
using Stride.Particles.Components;
8+
using Stride.Particles.Initializers;
9+
using Stride.Particles.Materials;
10+
using Stride.Particles.Modules;
11+
using Stride.Particles.ShapeBuilders;
12+
using Stride.Particles.Spawners;
13+
using Stride.Rendering.Materials.ComputeColors;
14+
15+
using var game = new Game();
16+
17+
game.Run(start: Start);
18+
19+
void Start(Scene scene)
20+
{
21+
SetupBaseScene();
22+
23+
game.AddSkybox();
24+
game.AddProfiler();
25+
26+
game.SetMaxFPS(60);
27+
28+
CreateParticleEffect();
29+
}
30+
31+
void SetupBaseScene()
32+
{
33+
game.AddGraphicsCompositor();
34+
game.Add3DCamera().Add3DCameraController();
35+
game.AddDirectionalLight();
36+
game.AddSkybox();
37+
game.Add3DGround();
38+
game.AddParticleRenderer();
39+
}
40+
41+
void CreateParticleEffect()
42+
{
43+
44+
var emitter = new ParticleEmitter
45+
{
46+
ParticleLifetime = new Vector2(0.5f, 0.5f),
47+
SimulationSpace = EmitterSimulationSpace.World,
48+
RandomSeedMethod = EmitterRandomSeedMethod.Time,
49+
ShapeBuilder = new ShapeBuilderBillboard(),
50+
51+
Material = new ParticleMaterialComputeColor()
52+
{
53+
ComputeColor = new ComputeColor()
54+
{
55+
Value = new Color4(0, 0, 1, 1)
56+
}
57+
},
58+
};
59+
60+
emitter.Spawners.Add(new SpawnerPerSecond()
61+
{
62+
LoopCondition = SpawnerLoopCondition.Looping,
63+
Delay = new Vector2(),
64+
Duration = new Vector2(1, 1),
65+
SpawnCount = 50,
66+
});
67+
68+
var sizeInitializer = new InitialSizeSeed()
69+
{
70+
ScaleUniform = 0.3f,
71+
RandomSize = new Vector2(0.1f, 0.5f),
72+
};
73+
74+
var positionInitializer = new InitialPositionSeed()
75+
{
76+
PositionMin = new Vector3(-0.03f, -0.03f, -0.03f),
77+
PositionMax = new Vector3(0.03f, 0.03f, 0.03f),
78+
};
79+
80+
var velocityInitialzer = new InitialVelocitySeed()
81+
{
82+
VelocityMin = new Vector3(0, 3, 0),
83+
VelocityMax = new Vector3(3, 4, 3),
84+
};
85+
86+
emitter.Initializers.Add(sizeInitializer);
87+
emitter.Initializers.Add(positionInitializer);
88+
emitter.Initializers.Add(velocityInitialzer);
89+
90+
emitter.Updaters.Add(new UpdaterGravity() { GravitationalAcceleration = new Vector3(0, -9.8f, 0) });
91+
92+
var particleSettings = new ParticleSystemSettings
93+
{
94+
WarmupTime = 0,
95+
};
96+
97+
ParticleSystemComponent particles = new()
98+
{
99+
Color = Color.White,
100+
RenderGroup = Stride.Rendering.RenderGroup.Group0,
101+
Speed = 1,
102+
};
103+
particles.ParticleSystem.Emitters.Add(emitter);
104+
particles.ParticleSystem.Settings = particleSettings;
105+
106+
var entity = new Entity
107+
{
108+
particles
109+
};
110+
entity.Name = "Particles";
111+
entity.Scene = game.SceneSystem.SceneInstance.RootScene;
112+
}

src/Stride.CommunityToolkit/Engine/GameExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,11 @@ public static void AddDebugShapes(this Game game, RenderGroup debugShapeRenderGr
666666
game.GameSystems.Add(debugDraw);
667667
}
668668

669+
public static void AddParticleRenderer(this Game game)
670+
{
671+
game.SceneSystem.GraphicsCompositor.AddParticleStagesAndFeatures();
672+
}
673+
669674
/// <summary>
670675
/// Adds an <see cref="EntityDebugSceneRenderer"/> to the game's <see cref="GraphicsCompositor"/> for rendering entity debug information.
671676
/// </summary>

src/Stride.CommunityToolkit/Rendering/Compositing/GraphicsCompositorExtensions.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Stride.CommunityToolkit.Rendering.DebugShapes;
2+
using Stride.Particles.Rendering;
23
using Stride.Rendering;
34
using Stride.Rendering.Compositing;
45
using Stride.Rendering.Images;
@@ -213,6 +214,34 @@ private static void AddRenderStagesAndFeatures(GraphicsCompositor graphicsCompos
213214
UpdateSceneRendererCollection(graphicsCompositor, cameraSlot, uiStage);
214215
}
215216

217+
public static void AddParticleStagesAndFeatures(this GraphicsCompositor graphicsCompositor)
218+
{
219+
if (!graphicsCompositor.TryGetRenderStage("Opaque", out var opaqueRenderStage))
220+
{
221+
throw new NullReferenceException("Opaque RenderStage not found");
222+
}
223+
224+
if (!graphicsCompositor.TryGetRenderStage("Transparent", out var transparentRenderStage))
225+
{
226+
throw new NullReferenceException("Transparent RenderStage not found");
227+
}
228+
229+
graphicsCompositor.RenderFeatures.Add(new ParticleEmitterRenderFeature()
230+
{
231+
Name = "ParticleEmitterRenderFeature",
232+
RenderStageSelectors =
233+
{
234+
new ParticleEmitterTransparentRenderStageSelector()
235+
{
236+
EffectName = "ParticleEmitterTransparent",
237+
RenderGroup = RenderGroupMaskAllExcludingGroup31(),
238+
OpaqueRenderStage = opaqueRenderStage,
239+
TransparentRenderStage = transparentRenderStage
240+
}
241+
}
242+
});
243+
}
244+
216245
private static void UpdateSceneRendererCollection(GraphicsCompositor graphicsCompositor, SceneCameraSlot cameraSlot, RenderStage uiStage)
217246
{
218247
graphicsCompositor.Game = new SceneRendererCollection {

0 commit comments

Comments
 (0)