Skip to content

Commit 12e6e71

Browse files
authored
Merge pull request #203 from VaclavElias/main
feat: New example added - Example01_Basic3DScene_MeshLine
2 parents 55b164c + cf9f73a commit 12e6e71

File tree

5 files changed

+157
-10
lines changed

5 files changed

+157
-10
lines changed

Stride.CommunityToolkit.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example14_Raycast", "exampl
9191
EndProject
9292
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example15_Constraint", "examples\code-only\Example15_Constraint\Example15_Constraint.csproj", "{490BB399-8A47-43C7-9D8C-7D7DF900CB50}"
9393
EndProject
94+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example01_Basic3DScene_MeshLine", "examples\code-only\Example01_Basic3DScene_MeshLine\Example01_Basic3DScene_MeshLine.csproj", "{24CE40E8-F42C-44E4-936F-86BB74CAFDBF}"
95+
EndProject
9496
Global
9597
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9698
Debug|Any CPU = Debug|Any CPU
@@ -244,6 +246,10 @@ Global
244246
{490BB399-8A47-43C7-9D8C-7D7DF900CB50}.Debug|Any CPU.Build.0 = Debug|Any CPU
245247
{490BB399-8A47-43C7-9D8C-7D7DF900CB50}.Release|Any CPU.ActiveCfg = Release|Any CPU
246248
{490BB399-8A47-43C7-9D8C-7D7DF900CB50}.Release|Any CPU.Build.0 = Release|Any CPU
249+
{24CE40E8-F42C-44E4-936F-86BB74CAFDBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
250+
{24CE40E8-F42C-44E4-936F-86BB74CAFDBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
251+
{24CE40E8-F42C-44E4-936F-86BB74CAFDBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
252+
{24CE40E8-F42C-44E4-936F-86BB74CAFDBF}.Release|Any CPU.Build.0 = Release|Any CPU
247253
EndGlobalSection
248254
GlobalSection(SolutionProperties) = preSolution
249255
HideSolutionNode = FALSE
@@ -276,6 +282,7 @@ Global
276282
{A8DBD6E0-47F3-4CAC-A5F9-DA6E4AB78726} = {30457B66-9E10-4290-9744-D0079DE0448B}
277283
{B442A84F-737B-45CC-B2DE-89495E2FDDBC} = {30457B66-9E10-4290-9744-D0079DE0448B}
278284
{490BB399-8A47-43C7-9D8C-7D7DF900CB50} = {30457B66-9E10-4290-9744-D0079DE0448B}
285+
{24CE40E8-F42C-44E4-936F-86BB74CAFDBF} = {30457B66-9E10-4290-9744-D0079DE0448B}
279286
EndGlobalSection
280287
GlobalSection(ExtensibilityGlobals) = postSolution
281288
SolutionGuid = {C7A8C18E-C1F5-492F-8173-51EC9495B78C}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</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.Bepu\Stride.CommunityToolkit.Bepu.csproj" />
12+
<ProjectReference Include="..\..\..\src\Stride.CommunityToolkit.Skyboxes\Stride.CommunityToolkit.Skyboxes.csproj" />
13+
<ProjectReference Include="..\..\..\src\Stride.CommunityToolkit.Windows\Stride.CommunityToolkit.Windows.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Example01_Basic3DScene_MeshLine;
2+
3+
/// <summary>
4+
/// This empty class is here to make the namespace available in the nameof() operator in the main examples project.
5+
/// </summary>
6+
internal static class NamespaceAnchor;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Stride.CommunityToolkit.Bepu;
2+
using Stride.CommunityToolkit.Engine;
3+
using Stride.CommunityToolkit.Rendering.Gizmos;
4+
using Stride.CommunityToolkit.Rendering.ProceduralModels;
5+
using Stride.CommunityToolkit.Skyboxes;
6+
using Stride.Core.Mathematics;
7+
using Stride.Engine;
8+
using Stride.Graphics;
9+
using Stride.Rendering;
10+
using Buffer = Stride.Graphics.Buffer;
11+
12+
using var game = new Game();
13+
14+
game.Run(start: Start);
15+
16+
void Start(Scene rootScene)
17+
{
18+
game.SetupBase3DScene();
19+
game.AddSkybox();
20+
21+
var lineEntity = CreateLineEntity(game);
22+
23+
var entity1 = CreateSphereEntity(game);
24+
entity1.Transform.Position = new Vector3(0, 8, 0);
25+
entity1.AddChild(lineEntity);
26+
27+
var entity2 = CreateSphereEntity(game);
28+
entity2.Transform.Position = new Vector3(-0.01f, 9, -0.01f);
29+
30+
entity1.Scene = rootScene;
31+
entity2.Scene = rootScene;
32+
};
33+
34+
static Entity CreateSphereEntity(Game game)
35+
=> game.Create3DPrimitive(PrimitiveModelType.Sphere);
36+
37+
static Entity CreateLineEntity(Game game)
38+
{
39+
// Create vertex buffer with start and end points
40+
var vertices = new Vector3[] { new(0, 0, 0), new(1, 1, -1) };
41+
var vertexBuffer = Buffer.New(game.GraphicsDevice, vertices, BufferFlags.VertexBuffer);
42+
43+
// Create index buffer
44+
var indices = new short[] { 0, 1 };
45+
var indexBuffer = Buffer.New(game.GraphicsDevice, indices, BufferFlags.IndexBuffer);
46+
47+
var material = GizmoEmissiveColorMaterial.Create(game.GraphicsDevice, Color.DarkMagenta);
48+
// Or use this for a specific color
49+
//var material = game.CreateMaterial(Color.DarkMagenta);
50+
51+
var meshDraw = new MeshDraw
52+
{
53+
PrimitiveType = PrimitiveType.LineList,
54+
VertexBuffers = [new VertexBufferBinding(vertexBuffer, new VertexDeclaration(VertexElement.Position<Vector3>()), vertices.Length)],
55+
IndexBuffer = new IndexBufferBinding(indexBuffer, is32Bit: false, indices.Length),
56+
DrawCount = indices.Length
57+
};
58+
59+
var mesh = new Mesh { Draw = meshDraw };
60+
var model = new Model { mesh, material };
61+
62+
return new Entity { new ModelComponent(model) };
63+
}
Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
using Stride.BepuPhysics;
22
using Stride.CommunityToolkit.Bepu;
33
using Stride.CommunityToolkit.Engine;
4-
using Stride.CommunityToolkit.Helpers;
4+
using Stride.CommunityToolkit.Rendering.Gizmos;
55
using Stride.CommunityToolkit.Rendering.ProceduralModels;
66
using Stride.CommunityToolkit.Skyboxes;
77
using Stride.Core.Mathematics;
88
using Stride.Engine;
99
using Stride.Games;
10+
using Stride.Graphics;
1011
using Stride.Input;
12+
using Stride.Rendering;
13+
using Buffer = Stride.Graphics.Buffer;
1114

1215
CameraComponent? camera = null;
16+
ModelComponent? lineModelComponent = null;
17+
Entity? entity = null;
18+
BodyComponent? body = null;
19+
Buffer? vertexBuffer = null;
20+
Vector3[] vertices = new Vector3[2]; // Start and end points
21+
bool isMoving = false;
1322

1423
using var game = new Game();
1524

@@ -19,44 +28,90 @@ void Start(Scene scene)
1928
{
2029
game.SetupBase3DScene();
2130
game.AddSkybox();
31+
game.AddProfiler();
2232
game.AddGroundGizmo(new(-5, 0, -5), showAxisName: true);
2333

24-
var entity = game.Create3DPrimitive(PrimitiveModelType.Sphere);
25-
34+
entity = game.Create3DPrimitive(PrimitiveModelType.Sphere);
2635
entity.Transform.Position = new Vector3(0, 8, 0);
27-
36+
body = entity.Get<BodyComponent>();
2837
entity.Scene = scene;
2938

3039
camera = scene.GetCamera();
40+
41+
var lineEntity = CreateLineEntity(game);
42+
43+
entity.AddChild(lineEntity);
3144
}
3245

3346
void Update(Scene scene, GameTime time)
3447
{
3548
if (camera == null) return;
3649

37-
game.DebugTextSystem.Print("Click the sphere to apply a random impulse", new(5, 10));
50+
game.DebugTextSystem.Print("Click the ground to apply a direction impulse", new(5, 30));
51+
game.DebugTextSystem.Print("Click the sphere to stop moving", new(5, 50));
3852

3953
if (game.Input.IsMouseButtonPressed(MouseButton.Left))
4054
{
4155
var hit = camera.Raycast(game.Input.MousePosition, 100, out var hitInfo);
4256

4357
if (hit)
4458
{
45-
var body = hitInfo.Collidable.Entity.Get<BodyComponent>();
59+
Console.WriteLine($"Hit entity: {hitInfo.Collidable.Entity.Name}");
4660

47-
if (body is null) return;
61+
if (hitInfo.Collidable.Entity == entity && body != null)
62+
{
63+
body.LinearVelocity = Vector3.Zero;
64+
body.AngularVelocity = Vector3.Zero;
4865

49-
var randomDirection = VectorHelper.RandomVector3([-1, 1], [0, 1], [-1, 1]);
66+
return;
67+
}
5068

51-
Console.WriteLine($"{hitInfo.Collidable.Entity}, direction: {randomDirection}");
69+
// ToDo: This point needs to be corrected, because lineEntity is a chidld of entity
70+
// Update the end vertex
71+
vertices[1] = hitInfo.Point;
5272

53-
body.ApplyImpulse(randomDirection * 2, new());
73+
// Re-upload vertex data to GPU
74+
vertexBuffer?.SetData(game.GraphicsContext.CommandList, vertices);
5475

76+
//Console.WriteLine($"{hitInfo.Collidable.Entity}, direction: {randomDirection}");
77+
78+
if (body is null) return;
79+
80+
body.ApplyImpulse(hitInfo.Point * 0.5f, new());
5581
body.Awake = true;
82+
isMoving = true;
5683
}
5784
else
5885
{
5986
Console.WriteLine("No hit");
6087
}
6188
}
89+
}
90+
91+
Entity CreateLineEntity(Game game)
92+
{
93+
// Initialize vertices (start at origin, end at origin)
94+
vertices[0] = Vector3.Zero;
95+
vertices[1] = Vector3.One;
96+
97+
// Create vertex buffer with start and end points
98+
vertexBuffer = Buffer.New(game.GraphicsDevice, vertices, BufferFlags.VertexBuffer);
99+
100+
// Create index buffer
101+
var indices = new ushort[] { 0, 1 };
102+
var indexBuffer = Buffer.New(game.GraphicsDevice, indices, BufferFlags.IndexBuffer);
103+
104+
var meshDraw = new MeshDraw
105+
{
106+
PrimitiveType = PrimitiveType.LineList,
107+
VertexBuffers = [new VertexBufferBinding(vertexBuffer, new VertexDeclaration(VertexElement.Position<Vector3>()), vertices.Length)],
108+
IndexBuffer = new IndexBufferBinding(indexBuffer, is32Bit: false, indices.Length),
109+
DrawCount = indices.Length
110+
};
111+
112+
var mesh = new Mesh { Draw = meshDraw };
113+
114+
lineModelComponent = new ModelComponent { Model = new Model { mesh, GizmoEmissiveColorMaterial.Create(game.GraphicsDevice, Color.DarkMagenta) } };
115+
116+
return new Entity { lineModelComponent };
62117
}

0 commit comments

Comments
 (0)