From 0d9af9f200dba07e4d5bcf622958486734fc112b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Mon, 11 Apr 2022 09:32:59 +0200 Subject: [PATCH] Grass interaction (#413) * hairparticle simulation clamping * editor: hair particle interaction * update --- Editor/Editor.cpp | 21 +++++++++++++++++++ Editor/Editor.h | 1 + .../shaders/hairparticle_simulateCS.hlsl | 11 +++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 185d861fb..050ffa715 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -1124,6 +1124,11 @@ void EditorComponent::Update(float dt) Scene& scene = wi::scene::GetScene(); CameraComponent& camera = wi::scene::GetCamera(); + if (scene.forces.Contains(grass_interaction_entity)) + { + scene.Entity_Remove(grass_interaction_entity); + } + cameraWnd.Update(); animWnd.Update(); weatherWnd.Update(); @@ -1501,6 +1506,22 @@ void EditorComponent::Update(float dt) } } + if (scene.hairs.Contains(hovered.entity)) + { + XMVECTOR P = XMLoadFloat3(&hovered.position); + P += XMLoadFloat3(&hovered.normal) * 2; + if (grass_interaction_entity == INVALID_ENTITY) + { + grass_interaction_entity = CreateEntity(); + } + ForceFieldComponent& force = scene.forces.Create(grass_interaction_entity); + TransformComponent& transform = scene.transforms.Create(grass_interaction_entity); + force.type = ENTITY_TYPE_FORCEFIELD_POINT; + force.gravity = -80; + force.range_local = 3; + transform.Translate(P); + } + } } diff --git a/Editor/Editor.h b/Editor/Editor.h index 8326fad9c..85f05dca4 100644 --- a/Editor/Editor.h +++ b/Editor/Editor.h @@ -151,6 +151,7 @@ public: Translator translator; wi::scene::PickResult hovered; + wi::ecs::Entity grass_interaction_entity = wi::ecs::INVALID_ENTITY; void ClearSelected(); void AddSelected(wi::ecs::Entity entity); diff --git a/WickedEngine/shaders/hairparticle_simulateCS.hlsl b/WickedEngine/shaders/hairparticle_simulateCS.hlsl index ca41018bc..a8ebdb40b 100644 --- a/WickedEngine/shaders/hairparticle_simulateCS.hlsl +++ b/WickedEngine/shaders/hairparticle_simulateCS.hlsl @@ -159,9 +159,14 @@ void main(uint3 DTid : SV_DispatchThreadID, uint3 Gid : SV_GroupID, uint groupIn velocity += surface_velocity; // Apply forces: - velocity += force; - normal += velocity * delta_time; - normal = normalize(normal); + float3 newVelocity = velocity + force; + float3 newNormal = normal + newVelocity * delta_time; + newNormal = normalize(newNormal); + if (dot(target, newNormal) > 0.5) // clamp the offset + { + normal = newNormal; + velocity = newVelocity; + } // Drag: velocity *= 0.98f;