Skip to content

Commit

Permalink
Merge pull request #175 from Doprez/particle-example
Browse files Browse the repository at this point in the history
feat: Particle example
  • Loading branch information
VaclavElias authored Nov 9, 2024
2 parents 9f052a5 + 9721b77 commit e772e56
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Stride.CommunityToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example11_ImGui", "examples
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.CommunityToolkit.Bepu", "src\Stride.CommunityToolkit.Bepu\Stride.CommunityToolkit.Bepu.csproj", "{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example12_Particles", "examples\code-only\Example12_Particles\Example12_Particles\Example12_Particles.csproj", "{8821704B-5698-4C9A-B05F-C18B7122DD8B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -196,6 +198,10 @@ Global
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60A78B61-DCCC-4EBC-A617-234C54DFB9D4}.Release|Any CPU.Build.0 = Release|Any CPU
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8821704B-5698-4C9A-B05F-C18B7122DD8B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -222,6 +228,7 @@ Global
{5996C17E-629C-4F58-88F9-494559EB4FBF} = {30457B66-9E10-4290-9744-D0079DE0448B}
{2D37B4BA-50F7-4347-8DC0-FA58C2AC2803} = {30457B66-9E10-4290-9744-D0079DE0448B}
{EE31DB88-2FE2-45ED-B80F-76F0C1CB2008} = {30457B66-9E10-4290-9744-D0079DE0448B}
{8821704B-5698-4C9A-B05F-C18B7122DD8B} = {30457B66-9E10-4290-9744-D0079DE0448B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C7A8C18E-C1F5-492F-8173-51EC9495B78C}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Stride.CommunityToolkit.ImGui\Stride.CommunityToolkit.ImGui.csproj" />
<ProjectReference Include="..\..\..\..\src\Stride.CommunityToolkit.Skyboxes\Stride.CommunityToolkit.Skyboxes.csproj" />
</ItemGroup>

</Project>
112 changes: 112 additions & 0 deletions examples/code-only/Example12_Particles/Example12_Particles/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Games;
using Stride.CommunityToolkit.Skyboxes;
using Stride.Core.Mathematics;
using Stride.Engine;
using Stride.Particles;
using Stride.Particles.Components;
using Stride.Particles.Initializers;
using Stride.Particles.Materials;
using Stride.Particles.Modules;
using Stride.Particles.ShapeBuilders;
using Stride.Particles.Spawners;
using Stride.Rendering.Materials.ComputeColors;

using var game = new Game();

game.Run(start: Start);

void Start(Scene scene)
{
SetupBaseScene();

game.AddSkybox();
game.AddProfiler();

game.SetMaxFPS(60);

CreateParticleEffect();
}

void SetupBaseScene()
{
game.AddGraphicsCompositor();
game.Add3DCamera().Add3DCameraController();
game.AddDirectionalLight();
game.AddSkybox();
game.Add3DGround();
game.AddParticleRenderer();
}

void CreateParticleEffect()
{

var emitter = new ParticleEmitter
{
ParticleLifetime = new Vector2(0.5f, 0.5f),
SimulationSpace = EmitterSimulationSpace.World,
RandomSeedMethod = EmitterRandomSeedMethod.Time,
ShapeBuilder = new ShapeBuilderBillboard(),

Material = new ParticleMaterialComputeColor()
{
ComputeColor = new ComputeColor()
{
Value = new Color4(0, 0, 1, 1)
}
},
};

emitter.Spawners.Add(new SpawnerPerSecond()
{
LoopCondition = SpawnerLoopCondition.Looping,
Delay = new Vector2(),
Duration = new Vector2(1, 1),
SpawnCount = 50,
});

var sizeInitializer = new InitialSizeSeed()
{
ScaleUniform = 0.3f,
RandomSize = new Vector2(0.1f, 0.5f),
};

var positionInitializer = new InitialPositionSeed()
{
PositionMin = new Vector3(-0.03f, -0.03f, -0.03f),
PositionMax = new Vector3(0.03f, 0.03f, 0.03f),
};

var velocityInitialzer = new InitialVelocitySeed()
{
VelocityMin = new Vector3(0, 3, 0),
VelocityMax = new Vector3(3, 4, 3),
};

emitter.Initializers.Add(sizeInitializer);
emitter.Initializers.Add(positionInitializer);
emitter.Initializers.Add(velocityInitialzer);

emitter.Updaters.Add(new UpdaterGravity() { GravitationalAcceleration = new Vector3(0, -9.8f, 0) });

var particleSettings = new ParticleSystemSettings
{
WarmupTime = 0,
};

ParticleSystemComponent particles = new()
{
Color = Color.White,
RenderGroup = Stride.Rendering.RenderGroup.Group0,
Speed = 1,
};
particles.ParticleSystem.Emitters.Add(emitter);
particles.ParticleSystem.Settings = particleSettings;

var entity = new Entity
{
particles
};
entity.Name = "Particles";
entity.Scene = game.SceneSystem.SceneInstance.RootScene;
}
5 changes: 5 additions & 0 deletions src/Stride.CommunityToolkit/Engine/GameExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,11 @@ public static void AddDebugShapes(this Game game, RenderGroup debugShapeRenderGr
game.GameSystems.Add(debugDraw);
}

public static void AddParticleRenderer(this Game game)
{
game.SceneSystem.GraphicsCompositor.AddParticleStagesAndFeatures();
}

/// <summary>
/// Adds an <see cref="EntityDebugSceneRenderer"/> to the game's <see cref="GraphicsCompositor"/> for rendering entity debug information.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Stride.CommunityToolkit.Rendering.DebugShapes;
using Stride.Particles.Rendering;
using Stride.Rendering;
using Stride.Rendering.Compositing;
using Stride.Rendering.Images;
Expand Down Expand Up @@ -213,6 +214,34 @@ private static void AddRenderStagesAndFeatures(GraphicsCompositor graphicsCompos
UpdateSceneRendererCollection(graphicsCompositor, cameraSlot, uiStage);
}

public static void AddParticleStagesAndFeatures(this GraphicsCompositor graphicsCompositor)
{
if (!graphicsCompositor.TryGetRenderStage("Opaque", out var opaqueRenderStage))
{
throw new NullReferenceException("Opaque RenderStage not found");
}

if (!graphicsCompositor.TryGetRenderStage("Transparent", out var transparentRenderStage))
{
throw new NullReferenceException("Transparent RenderStage not found");
}

graphicsCompositor.RenderFeatures.Add(new ParticleEmitterRenderFeature()
{
Name = "ParticleEmitterRenderFeature",
RenderStageSelectors =
{
new ParticleEmitterTransparentRenderStageSelector()
{
EffectName = "ParticleEmitterTransparent",
RenderGroup = RenderGroupMaskAllExcludingGroup31(),
OpaqueRenderStage = opaqueRenderStage,
TransparentRenderStage = transparentRenderStage
}
}
});
}

private static void UpdateSceneRendererCollection(GraphicsCompositor graphicsCompositor, SceneCameraSlot cameraSlot, RenderStage uiStage)
{
graphicsCompositor.Game = new SceneRendererCollection {
Expand Down

0 comments on commit e772e56

Please sign in to comment.