Skip to content

Commit

Permalink
Debugging the Foliage componente in the GardenerScene
Browse files Browse the repository at this point in the history
  • Loading branch information
Selinux24 committed May 27, 2024
1 parent 12d7401 commit 5939d6b
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 35 deletions.
7 changes: 5 additions & 2 deletions Engine.Shaders/ForwardFoliage/Foliage.gs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ cbuffer cbPerFrame : register(b0)

cbuffer cbPerPatch : register(b1)
{
float3 gPointOfView;
float PAD12;

float3 gWindDirection;
float gWindStrength;

float gStartRadius;
float gEndRadius;
uint gInstances;
float PAD12;
float PAD13;

float3 gDelta;
float gWindEffect;
Expand Down Expand Up @@ -50,7 +53,7 @@ inline void createPatches(uint primID, float3 position, float2 size)
{
position.y += (size.y * (0.5f + gDelta.y));

float3 look = gPerFrame.EyePosition - position;
float3 look = gPointOfView - position;
float len = length(look);
if ((gStartRadius > 0 && len < gStartRadius) || (gEndRadius > 0 && len > gEndRadius))
{
Expand Down
4 changes: 4 additions & 0 deletions Engine/BuiltIn/Foliage/BuiltInFoliageState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public struct BuiltInFoliageState
/// </summary>
public EngineShaderResourceView NormalMaps { get; set; }
/// <summary>
/// Point of view
/// </summary>
public Vector3 PointOfView { get; set; }
/// <summary>
/// Wind direction
/// </summary>
public Vector3 WindDirection { get; set; }
Expand Down
22 changes: 15 additions & 7 deletions Engine/BuiltIn/Foliage/Foliage.Buffers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public static PerPatch Build(BuiltInFoliageState state)
{
return new PerPatch
{
PointOfView = state.PointOfView,

WindDirection = state.WindDirection,
WindStrength = state.WindStrength,

Expand All @@ -73,41 +75,47 @@ public static PerPatch Build(BuiltInFoliageState state)
}

/// <summary>
/// Wind direction
/// Point of view
/// </summary>
[FieldOffset(0)]
public Vector3 PointOfView;

/// <summary>
/// Wind direction
/// </summary>
[FieldOffset(16)]
public Vector3 WindDirection;
/// <summary>
/// Wind strength
/// </summary>
[FieldOffset(12)]
[FieldOffset(28)]
public float WindStrength;

/// <summary>
/// Rotation
/// </summary>
[FieldOffset(16)]
[FieldOffset(32)]
public float StartRadius;
/// <summary>
/// Texture count
/// </summary>
[FieldOffset(20)]
[FieldOffset(36)]
public float EndRadius;
/// <summary>
/// Instance count
/// </summary>
[FieldOffset(24)]
[FieldOffset(40)]
public uint Instances;

/// <summary>
/// Position delta for additional instances
/// </summary>
[FieldOffset(32)]
[FieldOffset(48)]
public Vector3 Delta;
/// <summary>
/// Wind effect
/// </summary>
[FieldOffset(44)]
[FieldOffset(60)]
public float WindEffect;

/// <inheritdoc/>
Expand Down
24 changes: 22 additions & 2 deletions Engine/Foliage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ public sealed class Foliage(Scene scene, string id, string name) : Drawable<Foli
/// </summary>
public float NodeSortingSeconds { get; set; } = 5f;
/// <summary>
/// Point of view
/// </summary>
public Vector3 PointOfView { get; set; }
/// <summary>
/// Point of view frustum
/// </summary>
public BoundingFrustum PointOfViewFrustum { get; set; }
/// <summary>
/// Uses the camera as point of view
/// </summary>
public bool UseCameraAsPointOfView { get; set; } = true;
/// <summary>
/// Wind direction
/// </summary>
public Vector3 WindDirection { get; set; }
Expand Down Expand Up @@ -272,6 +284,12 @@ public override void Update(UpdateContext context)
return;
}

if (UseCameraAsPointOfView)
{
PointOfView = Scene.Camera.Position;
PointOfViewFrustum = Scene.Camera.Frustum;
}

if (updatingNodes)
{
//Node update task runing
Expand All @@ -282,9 +300,9 @@ public override void Update(UpdateContext context)

//Copy variables for the async task
float time = context.GameTime.ElapsedSeconds;
var eyePosition = Scene.Camera.Position;
var frustum = PointOfViewFrustum;
var eyePosition = PointOfView;
foliageSphere.Center = eyePosition;
var frustum = Scene.Camera.Frustum;
var sph = foliageSphere;

Task.Run(() =>
Expand Down Expand Up @@ -663,6 +681,8 @@ private bool DrawPatches(IEngineDeviceContext dc)
MaterialIndex = foliageMaterial.ResourceIndex,
RandomTexture = textureRandom,

PointOfView = PointOfView,

WindDirection = WindDirection,
WindStrength = WindStrength * channelData.WindEffect,
};
Expand Down
Loading

0 comments on commit 5939d6b

Please sign in to comment.