Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trail renderer #811

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Content/Documentation/ScriptingAPI-Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ This is a reference and explanation of Lua scripting features in Wicked Engine.
14. [Path finding](#path-finding)
1. [VoxelGrid](#voxelgrid)
2. [PathQuery](#pathquery)
15. [TrailRenderer](#trailrenderer)

## Introduction and usage
Scripting in Wicked Engine is powered by Lua, meaning that the user can make use of the
Expand Down Expand Up @@ -158,6 +159,7 @@ You can use the Renderer with the following functions, all of which are in the g
[outer]DEBUG_TEXT_CAMERA_SCALING -- text will be always the same size, independent of distance to camera
- DrawVoxelGrid(VoxelGrid voxelgrid) -- draws the voxel grid in the debug rendering phase. VoxelGrid object must not be destroyed until then!
- DrawPathQuery(PathQuery pathquery) -- draws the path query in the debug rendering phase. PathQuery object must not be destroyed until then!
- DrawTrail(TrailRenderer trail) -- draws the trail in the debug rendering phase. TrailRenderer object must not be destroyed until then!
- PutWaterRipple(Vector position) -- put down a water ripple with default embedded asset
- PutWaterRipple(string imagename, Vector position) -- put down water ripple texture from image asset file
- ClearWorld(opt Scene scene) -- Clears the scene and the associated renderer resources. If parmaeter is not specified, it will clear the global scene
Expand Down Expand Up @@ -1802,3 +1804,28 @@ Path finding operations can be made by using a voxel grid and path queries. The
- GetAgentHeight(int value) : int
- GetWaypointCount() : int -- returns the number of waypoints that were computed in Process()
- GetWaypoint(int index) : Vector returns the waypoint at specified index (direction: start -> goal)

### TrailRenderer
- [constructor] TrailRenderer()
- AddPoint(Vector pos, opt float width = 1, opt Vector color = Vector(1,1,1,1)) -- adds a new point to the trail
- Cut() -- cuts the trail at last point and starts a new trail
- Clear() -- removes all points and cuts from the trail
- GetPointCount() : int -- returns the number of points in the trail
- GetPoint() : Vector pos, float width -- returns the point of the trail on the specified index
- SetPoint(Vector pos, opt float width = 1, opt Vector color = Vector(1,1,1,1)) -- sets the point parameters on the specified index
- SetBlendMode(int blendmode) -- set blend mode of the whole trail
- GetBlendMode() : int
- SetSubdivision(int subdiv) -- set the subdivision amount of the whole trail
- GetSubdivision() : int
- SetWidth(float width) -- set the width of the whole trail
- GetWidth() : float
- SetColor(Vector color) -- set the color of the whole trail
- GetColor() : Vector
- SetTexture(Texture tex) -- set the texture of the whole trail
- GetTexture() : Texture
- SetTexture2(Texture tex) -- set the texture2 of the whole trail
- GetTexture2() : Texture
- SetTexMulAdd(Texture tex) -- set the texture UV tiling multiply-add value of the whole trail
- GetTexMulAdd() : Texture
- SetTexMulAdd2(Texture tex) -- set the texture2 UV tiling multiply-add value of the whole trail
- GetTexMulAdd2() : Texture
58 changes: 58 additions & 0 deletions Content/scripts/trail_renderer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
killProcesses() -- stops all running lua coroutine processes

backlog_post("---> START SCRIPT: trail_renderer.lua")

local trail = TrailRenderer()
trail.SetWidth(0.2)
trail.SetColor(Vector(10,0.1,0.1,1))
trail.SetBlendMode(BLENDMODE_ADDITIVE)
trail.SetSubdivision(100)

-- Trail begins as red:
trail.AddPoint(Vector(-5,2,-3), 4, Vector(10,0.1,0.1,1))
trail.AddPoint(Vector(5,1,1), 0.5, Vector(10,0.1,0.1,1))
trail.AddPoint(Vector(10,5,4), 1.2, Vector(10,0.1,0.1,1))
trail.AddPoint(Vector(6,8,2), 1, Vector(10,0.1,0.1,1))
trail.AddPoint(Vector(-6,5,0), 1, Vector(10,0.1,0.1,1))
-- Trail turn into green:
trail.AddPoint(Vector(0,2,-5), 1, Vector(0.1,100,0.1,1))
trail.AddPoint(Vector(1,3,5), 1, Vector(0.1,100,0.1,1))
trail.AddPoint(Vector(-3,2,8), 1, Vector(0.1,100,0.1,1))

trail.Cut() -- start a new trail without connecting to previous points

-- Last trail segment is blue:
trail.AddPoint(Vector(-5,0,-2), 1, Vector(0.1,0.1,100,1))
trail.AddPoint(Vector(5,8,5), 1, Vector(0.1,0.1,100,1))

-- First texture is a circle gradient, this makes the overall trail smooth at the edges:
local texture = texturehelper.CreateGradientTexture(
GradientType.Circular,
256, 256,
Vector(0.5, 0.5), Vector(0.5, 0),
GradientFlags.Inverse,
"rrrr"
)
trail.SetTexture(texture)

-- Second texture is a linear gradient that will be tiled and animated to achieve stippled look:
local texture2 = texturehelper.CreateGradientTexture(
GradientType.Linear,
256, 256,
Vector(0.5, 0), Vector(0, 0),
GradientFlags.Inverse | GradientFlags.Smoothstep,
"rrrr"
)
trail.SetTexture2(texture2)

runProcess(function()
local scrolling = 0
while true do
scrolling = scrolling - getDeltaTime()
trail.SetTexMulAdd2(Vector(10,1,scrolling,0))
DrawTrail(trail)
render() -- this loop will be blocked until render tick
end
end)

backlog_post("---> END SCRIPT: trail_renderer.lua")
4 changes: 4 additions & 0 deletions WickedEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ set(HEADER_FILES
wiPathQuery.h
wiVoxelGrid_BindLua.h
wiPathQuery_BindLua.h
wiTrailRenderer.h
wiTrailRenderer_BindLua.h
)

add_library(${TARGET_NAME} ${WICKED_LIBRARY_TYPE}
Expand Down Expand Up @@ -228,6 +230,8 @@ add_library(${TARGET_NAME} ${WICKED_LIBRARY_TYPE}
wiPathQuery.cpp
wiVoxelGrid_BindLua.cpp
wiPathQuery_BindLua.cpp
wiTrailRenderer.cpp
wiTrailRenderer_BindLua.cpp
${HEADER_FILES}
)
add_library(WickedEngine ALIAS ${TARGET_NAME})
Expand Down
1 change: 1 addition & 0 deletions WickedEngine/WickedEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "wiVideo.h"
#include "wiVoxelGrid.h"
#include "wiPathQuery.h"
#include "wiTrailRenderer.h"

#ifdef PLATFORM_WINDOWS_DESKTOP
#pragma comment(lib,"WickedEngine_Windows.lib")
Expand Down
4 changes: 4 additions & 0 deletions WickedEngine/WickedEngine_SOURCE.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@
<ClInclude Include="$(MSBuildThisFileDirectory)wiGraphicsDevice_Vulkan.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wiScene_Components.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wiTerrain.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wiTrailRenderer.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wiTrailRenderer_BindLua.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wiUnorderedSet.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wiInput.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)wiInput_BindLua.h" />
Expand Down Expand Up @@ -713,6 +715,8 @@
<ClCompile Include="$(MSBuildThisFileDirectory)wiSprite_BindLua.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)wiArguments.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)wiTextureHelper.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)wiTrailRenderer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)wiTrailRenderer_BindLua.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)wiVersion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)wiVideo.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)wiVoxelGrid.cpp" />
Expand Down
12 changes: 12 additions & 0 deletions WickedEngine/WickedEngine_SOURCE.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,12 @@
<ClInclude Include="$(MSBuildThisFileDirectory)wiPathQuery_BindLua.h">
<Filter>ENGINE\Scripting\LuaBindings</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)wiTrailRenderer.h">
<Filter>ENGINE\Graphics</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)wiTrailRenderer_BindLua.h">
<Filter>ENGINE\Scripting\LuaBindings</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)LUA\lapi.c">
Expand Down Expand Up @@ -1904,6 +1910,12 @@
<ClCompile Include="$(MSBuildThisFileDirectory)wiPathQuery_BindLua.cpp">
<Filter>ENGINE\Scripting\LuaBindings</Filter>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)wiTrailRenderer.cpp">
<Filter>ENGINE\Graphics</Filter>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)wiTrailRenderer_BindLua.cpp">
<Filter>ENGINE\Scripting\LuaBindings</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="$(MSBuildThisFileDirectory)ArchiveVersionHistory.txt" />
Expand Down
2 changes: 2 additions & 0 deletions WickedEngine/offlineshadercompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ wi::vector<ShaderEntry> shaders = {
{"ddgi_debugPS", wi::graphics::ShaderStage::PS },
{"copyDepthPS", wi::graphics::ShaderStage::PS },
{"copyStencilBitPS", wi::graphics::ShaderStage::PS },
{"trailPS", wi::graphics::ShaderStage::PS },


{"hairparticleVS", wi::graphics::ShaderStage::VS },
Expand Down Expand Up @@ -335,6 +336,7 @@ wi::vector<ShaderEntry> shaders = {
{"shadowVS_transparent", wi::graphics::ShaderStage::VS },
{"shadowVS_transparent_emulation", wi::graphics::ShaderStage::VS },
{"screenVS", wi::graphics::ShaderStage::VS },
{"trailVS", wi::graphics::ShaderStage::VS },



Expand Down
1 change: 1 addition & 0 deletions WickedEngine/shaders/ShaderInterop.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static const uint IndirectDispatchArgsAlignment = 4u;
#define CBSLOT_OTHER_GPUSORTLIB 4
#define CBSLOT_MSAO 4
#define CBSLOT_FSR 4
#define CBSLOT_TRAILRENDERER 3

#else
// Don't use overlapping slots on PS5:
Expand Down
8 changes: 8 additions & 0 deletions WickedEngine/shaders/ShaderInterop_Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1396,5 +1396,13 @@ struct VirtualTextureTileRequestsPush
int padding2;
};

CBUFFER(TrailRendererCB, CBSLOT_TRAILRENDERER)
{
float4x4 g_xTrailTransform;
float4 g_xTrailColor;
float4 g_xTrailTexMulAdd;
float4 g_xTrailTexMulAdd2;
};


#endif // WI_SHADERINTEROP_RENDERER_H
6 changes: 6 additions & 0 deletions WickedEngine/shaders/Shaders_SOURCE.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,12 @@
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Compute</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
</FxCompile>
<FxCompile Include="$(MSBuildThisFileDirectory)trailPS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
</FxCompile>
<FxCompile Include="$(MSBuildThisFileDirectory)trailVS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
</FxCompile>
<FxCompile Include="$(MSBuildThisFileDirectory)underwaterCS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Compute</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
Expand Down
6 changes: 6 additions & 0 deletions WickedEngine/shaders/Shaders_SOURCE.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,12 @@
<FxCompile Include="$(MSBuildThisFileDirectory)impostorPS_prepass_depthonly.hlsl">
<Filter>PS</Filter>
</FxCompile>
<FxCompile Include="$(MSBuildThisFileDirectory)trailVS.hlsl">
<Filter>VS</Filter>
</FxCompile>
<FxCompile Include="$(MSBuildThisFileDirectory)trailPS.hlsl">
<Filter>PS</Filter>
</FxCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(MSBuildThisFileDirectory)ShaderInterop.h">
Expand Down
11 changes: 11 additions & 0 deletions WickedEngine/shaders/trailPS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "globals.hlsli"

Texture2D tex : register(t0);
Texture2D tex2 : register(t1);

float4 main(float4 pos : SV_Position, float4 uv : TEXCOORD, float4 color : COLOR) : SV_TARGET
{
color *= tex.Sample(sampler_linear_mirror, uv.xy);
color *= tex2.Sample(sampler_linear_mirror, uv.zw);
return color;
}
18 changes: 18 additions & 0 deletions WickedEngine/shaders/trailVS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "globals.hlsli"

struct VertexOut
{
float4 pos : SV_Position;
float4 uv : TEXCOORD;
float4 color : COLOR;
};

VertexOut main(float3 pos : POSITION, float2 uv : TEXCOORD, float4 color : COLOR)
{
VertexOut Out;
Out.pos = mul(g_xTrailTransform, float4(pos, 1));
Out.uv.xy = mad(uv, g_xTrailTexMulAdd.xy, g_xTrailTexMulAdd.zw);
Out.uv.zw = mad(uv, g_xTrailTexMulAdd2.xy, g_xTrailTexMulAdd2.zw);
Out.color = color * g_xTrailColor;
return Out;
}
12 changes: 12 additions & 0 deletions WickedEngine/wiEmittedParticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,18 @@ namespace wi
bd.independent_blend_enable = false;
blendStates[BLENDMODE_PREMULTIPLIED] = bd;

bd.render_target[0].src_blend = Blend::DEST_COLOR;
bd.render_target[0].dest_blend = Blend::ZERO;
bd.render_target[0].blend_op = BlendOp::ADD;
bd.render_target[0].src_blend_alpha = Blend::DEST_ALPHA;
bd.render_target[0].dest_blend_alpha = Blend::ZERO;
bd.render_target[0].blend_op_alpha = BlendOp::ADD;
bd.render_target[0].blend_enable = true;
bd.render_target[0].render_target_write_mask = ColorWrite::ENABLE_ALL;
bd.alpha_to_coverage_enable = false;
bd.independent_blend_enable = false;
blendStates[BLENDMODE_MULTIPLY] = bd;

bd.render_target[0].blend_enable = false;
blendStates[BLENDMODE_OPAQUE] = bd;

Expand Down
1 change: 1 addition & 0 deletions WickedEngine/wiInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace wi::initializer
wi::jobsystem::Execute(ctx, [](wi::jobsystem::JobArgs args) { wi::GPUBVH::Initialize(); systems[INITIALIZED_SYSTEM_GPUBVH].store(true); });
wi::jobsystem::Execute(ctx, [](wi::jobsystem::JobArgs args) { wi::physics::Initialize(); systems[INITIALIZED_SYSTEM_PHYSICS].store(true); });
wi::jobsystem::Execute(ctx, [](wi::jobsystem::JobArgs args) { wi::audio::Initialize(); systems[INITIALIZED_SYSTEM_AUDIO].store(true); });
wi::jobsystem::Execute(ctx, [](wi::jobsystem::JobArgs args) { wi::TrailRenderer::Initialize(); systems[INITIALIZED_SYSTEM_TRAILRENDERER].store(true); });

// Initialize this immediately:
wi::lua::Initialize(); systems[INITIALIZED_SYSTEM_LUA].store(true);
Expand Down
1 change: 1 addition & 0 deletions WickedEngine/wiInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace wi::initializer
INITIALIZED_SYSTEM_PHYSICS,
INITIALIZED_SYSTEM_LUA,
INITIALIZED_SYSTEM_AUDIO,
INITIALIZED_SYSTEM_TRAILRENDERER,

INITIALIZED_SYSTEM_COUNT
};
Expand Down
2 changes: 2 additions & 0 deletions WickedEngine/wiLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "wiPhysics_BindLua.h"
#include "wiVoxelGrid_BindLua.h"
#include "wiPathQuery_BindLua.h"
#include "wiTrailRenderer_BindLua.h"
#include "wiTimer.h"
#include "wiVector.h"

Expand Down Expand Up @@ -205,6 +206,7 @@ namespace wi::lua
Physics_BindLua::Bind();
VoxelGrid_BindLua::Bind();
PathQuery_BindLua::Bind();
TrailRenderer_BindLua::Bind();

wi::backlog::post("wi::lua Initialized (" + std::to_string((int)std::round(timer.elapsed())) + " ms)");
}
Expand Down
14 changes: 14 additions & 0 deletions WickedEngine/wiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "wiTimer.h"
#include "wiUnorderedMap.h" // leave it here for shader dump!
#include "wiFont.h"
#include "wiVoxelGrid.h"
#include "wiPathQuery.h"
#include "wiTrailRenderer.h"

#include "shaders/ShaderInterop_Postprocess.h"
#include "shaders/ShaderInterop_Raytracing.h"
Expand Down Expand Up @@ -150,6 +153,7 @@ wi::vector<uint8_t> debugTextStorage; // A stream of DebugText struct + text cha
wi::vector<PaintRadius> paintrads;
wi::vector<const wi::VoxelGrid*> renderableVoxelgrids;
wi::vector<const wi::PathQuery*> renderablePathqueries;
wi::vector<const wi::TrailRenderer*> renderableTrails;

wi::SpinLock deferredMIPGenLock;
wi::vector<std::pair<Texture, bool>> deferredMIPGens;
Expand Down Expand Up @@ -6203,6 +6207,12 @@ void DrawDebugWorld(
}
renderablePathqueries.clear();

for (auto& x : renderableTrails)
{
x->Draw(camera, cmd);
}
renderableTrails.clear();

if (debugCameras)
{
device->EventBegin("DebugCameras", cmd);
Expand Down Expand Up @@ -16543,6 +16553,10 @@ void DrawPathQuery(const wi::PathQuery* pathquery)
{
renderablePathqueries.push_back(pathquery);
}
void DrawTrail(const wi::TrailRenderer* trail)
{
renderableTrails.push_back(trail);
}

void AddDeferredMIPGen(const Texture& texture, bool preserve_coverage)
{
Expand Down
5 changes: 5 additions & 0 deletions WickedEngine/wiRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace wi
{
struct VoxelGrid;
struct PathQuery;
struct TrailRenderer;
}

namespace wi::renderer
Expand Down Expand Up @@ -1151,6 +1152,10 @@ namespace wi::renderer
// WARNING: This retains pointer until next call to DrawDebugScene(), so path query must not be destroyed until then!
void DrawPathQuery(const wi::PathQuery* pathquery);

// Add trail to be drawn in debug rendering phase.
// WARNING: This retains pointer until next call to DrawDebugScene(), so trail must not be destroyed until then!
void DrawTrail(const wi::TrailRenderer* trail);

// Add a texture that should be mipmapped whenever it is feasible to do so
void AddDeferredMIPGen(const wi::graphics::Texture& texture, bool preserve_coverage = false);
void AddDeferredBlockCompression(const wi::graphics::Texture& texture_src, const wi::graphics::Texture& texture_bc);
Expand Down
Loading
Loading