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

add a skeletal animation sample using ozz-animation library #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "ThirdParty/glfw"]
path = ThirdParty/glfw
url = https://github.com/DiligentGraphics/glfw.git
[submodule "ThirdParty/ozz-animation"]
path = ThirdParty/ozz-animation
url = https://github.com/guillaumeblanc/ozz-animation.git
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions Samples/Animation/Animation00_Playback/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
cmake_minimum_required (VERSION 3.6)

project(Animation00_Playback CXX)

set(SOURCE
src/Animation00_Playback.cpp
../Common/src/AnimationUtilities.cpp
../Common/src/PlaybackController.cpp
)

set(INCLUDE
src/Animation00_Playback.hpp
../Common/src/AnimationUtilities.hpp
../Common/src/PlaybackController.hpp
)

set(SHADERS
assets/skeleton.vsh
assets/skeleton.psh
assets/plane.vsh
assets/plane.psh
)

set(ASSETS
assets/pab_crossarms.ozz
assets/pab_skeleton.ozz
)

add_sample_app("Animation00_Playback" "DiligentSamples/Samples/Animation" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")

target_link_libraries(Animation00_Playback
PRIVATE
ozz_base
ozz_animation
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
32 changes: 32 additions & 0 deletions Samples/Animation/Animation00_Playback/assets/plane.psh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
struct PlanePSInput
{
float4 Pos : SV_POSITION;
float2 TexCoord : TEXCOORD;
};

struct PlanePSOutput
{
float4 Color : SV_TARGET;
};

void main(in PlanePSInput PSIn,
out PlanePSOutput PSOut)
{
float tol = 0.001000000;
float res = 0.05;
float4 backgroundColor = float4(0.5, 0.75, 0.85, 1.0f); // blue
float4 lineColor = float4(0.25, 0.25, 0.25, 1.0); // grey
float4 originColor = float4(0.0, 0.0, 0.0, 1.0); // black
if (((abs(((PSIn.TexCoord).x - 0.5)) <= tol) && (abs(((PSIn.TexCoord).y - 0.5)) <= tol)))
{
PSOut.Color = originColor;
}
else if (((((fmod((PSIn.TexCoord).x, res) >= (res - tol)) || (fmod((PSIn.TexCoord).x, res) < tol)) || (fmod((PSIn.TexCoord).y, res) >= (res - tol))) || (fmod((PSIn.TexCoord).y, res) < tol)))
{
PSOut.Color = lineColor;
}
else
{
PSOut.Color = backgroundColor;
}
}
32 changes: 32 additions & 0 deletions Samples/Animation/Animation00_Playback/assets/plane.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cbuffer Constants
{
float4x4 g_WorldViewProj;
}

struct PlanePSInput
{
float4 Pos : SV_POSITION;
float2 TexCoord : TEXCOORD;
};

void main(in uint VertId : SV_VertexID,
out PlanePSInput PSIn)
{
float PlaneExtent = 5.0;
float PlanePos = 0.0;

float4 Pos[4];
Pos[0] = float4(-PlaneExtent, PlanePos, -PlaneExtent, 1.0);
Pos[1] = float4(-PlaneExtent, PlanePos, +PlaneExtent, 1.0);
Pos[2] = float4(+PlaneExtent, PlanePos, -PlaneExtent, 1.0);
Pos[3] = float4(+PlaneExtent, PlanePos, +PlaneExtent, 1.0);

float2 Uv[4];
Uv[0] = float2(0.0, 0.0);
Uv[1] = float2(0.0, 1.0);
Uv[2] = float2(1.0, 0.0);
Uv[3] = float2(1.0, 1.0);

PSIn.Pos = mul(Pos[VertId], g_WorldViewProj);
PSIn.TexCoord = Uv[VertId];
}
31 changes: 31 additions & 0 deletions Samples/Animation/Animation00_Playback/assets/skeleton.psh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
struct PSInput
{
float4 Pos : SV_POSITION;
float3 Normal : NORMAL;
float4 Color : COLOR0;
};

struct PSOutput
{
float4 Color : SV_TARGET;
};

float4 GetAmbient(float3 WorldNormal)
{
float3 Normal = normalize(WorldNormal);
float3 Alpha = (Normal + 1.0) * 0.5;
float2 Bt = lerp(float2(0.3, 0.7), float2(0.4, 0.8), Alpha.xz);
float3 Ambine = lerp(float3(Bt.x, 0.3, Bt.x), float3(Bt.y, 0.8, Bt.y), Alpha.y);

return float4(Ambine, 1.0);
}

// Note that if separate shader objects are not supported (this is only the case for old GLES3.0 devices), vertex
// shader output variable name must match exactly the name of the pixel shader input variable.
// If the variable has structure type (like in this example), the structure declarations must also be indentical.
void main(in PSInput PSIn,
out PSOutput PSOut)
{
float4 Ambine = GetAmbient(PSIn.Normal);
PSOut.Color = Ambine * PSIn.Color;
}
53 changes: 53 additions & 0 deletions Samples/Animation/Animation00_Playback/assets/skeleton.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
cbuffer Constants
{
float4x4 g_WorldViewProj;
};

// Vertex shader takes two inputs: vertex position and color.
// By convention, Diligent Engine expects vertex shader inputs to be
// labeled 'ATTRIBn', where n is the attribute number.
struct VSInput
{
// Vertex attributes
float3 Pos : ATTRIB0;
float3 Normal : ATTRIB1;
float4 Color : ATTRIB2;

// Instance attributes
float4 MtrxRow0 : ATTRIB3;
float4 MtrxRow1 : ATTRIB4;
float4 MtrxRow2 : ATTRIB5;
float4 MtrxRow3 : ATTRIB6;
};

struct PSInput
{
float4 Pos : SV_POSITION;
float3 Normal : NORMAL;
float4 Color : COLOR0;
};

// Note that if separate shader objects are not supported (this is only the case for old GLES3.0 devices), vertex
// shader output variable name must match exactly the name of the pixel shader input variable.
// If the variable has structure type (like in this example), the structure declarations must also be indentical.
void main(in VSInput VSIn,
out PSInput PSIn)
{
// HLSL matrices are row-major while GLSL matrices are column-major. We will
// use convenience function MatrixFromRows() appropriately defined by the engine
float4x4 InstanceMatr = MatrixFromRows(VSIn.MtrxRow0, VSIn.MtrxRow1, VSIn.MtrxRow2, VSIn.MtrxRow3);
// Apply instance-specific transformation
float4 TransformedPos = mul(float4(VSIn.Pos, 1.0), InstanceMatr);
// Apply view-projection matrix
PSIn.Pos = mul(TransformedPos, g_WorldViewProj);

float3x3 CrossMatr = float3x3(
cross(InstanceMatr[1].xyz, InstanceMatr[2].xyz),
cross(InstanceMatr[2].xyz, InstanceMatr[0].xyz),
cross(InstanceMatr[0].xyz, InstanceMatr[1].xyz)
);
float Invdet = 1.0 / dot(CrossMatr[2], InstanceMatr[2].xyz);
float3x3 NormalMatr = CrossMatr * Invdet;
PSIn.Normal = mul(VSIn.Normal, NormalMatr);
PSIn.Color = VSIn.Color;
}
5 changes: 5 additions & 0 deletions Samples/Animation/Animation00_Playback/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Animation Playback Demo

![](Screenshot.png)

This sample demonstrates how to integrate ozz-animation library into an application to play skeletal animation.
Loading