Skip to content

Commit

Permalink
feat: added Utility shader (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonahex authored Apr 21, 2024
1 parent 42a13ef commit 7ff9ed7
Show file tree
Hide file tree
Showing 7 changed files with 2,084 additions and 195 deletions.
16 changes: 16 additions & 0 deletions package/Shaders/Common/LodLandscape.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
float4 AdjustLodLandscapeVertexPositionMS(float4 positionMS, row_major float4x4 world, float4 cellParams)
{
float4 positionWS = mul(world, positionMS);
float worldXShift = positionWS.x - cellParams.x;
float worldYShift = positionWS.y - cellParams.y;
if ((abs(worldXShift) < cellParams.z) && (abs(worldYShift) < cellParams.w)) {
positionMS.z -= (230 + positionWS.z / 1e9);
}
return positionMS;
}

float4 AdjustLodLandscapeVertexPositionCS(float4 positionCS)
{
positionCS.z += min(1, 1e-4 * max(0, positionCS.z - 70000)) * 0.5;
return positionCS;
}
46 changes: 46 additions & 0 deletions package/Shaders/Common/Skinned.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
cbuffer PreviousBonesBuffer : register(b9)
{
float4 PreviousBones[240] : packoffset(c0);
}

cbuffer BonesBuffer : register(b10)
{
float4 Bones[240] : packoffset(c0);
}

float3x4 GetBoneTransformMatrix(float4 bonePositions[240], int4 boneIndices, float3 pivot, float4 boneWeights)
{
float3x4 pivotMatrix = transpose(float4x3(0.0.xxx, 0.0.xxx, 0.0.xxx, pivot));

float3x4 boneMatrix1 =
float3x4(bonePositions[boneIndices.x], bonePositions[boneIndices.x + 1], bonePositions[boneIndices.x + 2]);
float3x4 boneMatrix2 =
float3x4(bonePositions[boneIndices.y], bonePositions[boneIndices.y + 1], bonePositions[boneIndices.y + 2]);
float3x4 boneMatrix3 =
float3x4(bonePositions[boneIndices.z], bonePositions[boneIndices.z + 1], bonePositions[boneIndices.z + 2]);
float3x4 boneMatrix4 =
float3x4(bonePositions[boneIndices.w], bonePositions[boneIndices.w + 1], bonePositions[boneIndices.w + 2]);

float3x4 unitMatrix = float3x4(1.0.xxxx, 1.0.xxxx, 1.0.xxxx);
float3x4 weightMatrix1 = unitMatrix * boneWeights.x;
float3x4 weightMatrix2 = unitMatrix * boneWeights.y;
float3x4 weightMatrix3 = unitMatrix * boneWeights.z;
float3x4 weightMatrix4 = unitMatrix * boneWeights.w;

return (boneMatrix1 - pivotMatrix) * weightMatrix1 +
(boneMatrix2 - pivotMatrix) * weightMatrix2 +
(boneMatrix3 - pivotMatrix) * weightMatrix3 +
(boneMatrix4 - pivotMatrix) * weightMatrix4;
}

float3x3 GetBoneRSMatrix(float4 bonePositions[240], int4 boneIndices, float4 boneWeights)
{
float3x3 result;
for (int rowIndex = 0; rowIndex < 3; ++rowIndex) {
result[rowIndex] = boneWeights.xxx * bonePositions[boneIndices.x + rowIndex].xyz +
boneWeights.yyy * bonePositions[boneIndices.y + rowIndex].xyz +
boneWeights.zzz * bonePositions[boneIndices.z + rowIndex].xyz +
boneWeights.www * bonePositions[boneIndices.w + rowIndex].xyz;
}
return result;
}
54 changes: 1 addition & 53 deletions package/Shaders/Effect.hlsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "Common/Skinned.hlsli"

#define EFFECT

Expand Down Expand Up @@ -114,59 +115,6 @@ cbuffer IndexedTexcoordBuffer : register(b11)
float4 IndexedTexCoord[128] : packoffset(c0);
}

# if defined(SKINNED)
# if defined(MOTIONVECTORS_NORMALS)
cbuffer PreviousBonesBuffer : register(b9)
{
float4 PreviousBones[240] : packoffset(c0);
}
# endif

cbuffer BonesBuffer : register(b10)
{
float4 Bones[240] : packoffset(c0);
}
# endif

# if defined(SKINNED)
float3x4 GetBoneTransformMatrix(float4 bones[240], int4 actualIndices, float3 pivot, float4 weights)
{
float3x4 pivotMatrix = transpose(float4x3(0.0.xxx, 0.0.xxx, 0.0.xxx, pivot));

float3x4 boneMatrix1 =
float3x4(bones[actualIndices.x], bones[actualIndices.x + 1], bones[actualIndices.x + 2]);
float3x4 boneMatrix2 =
float3x4(bones[actualIndices.y], bones[actualIndices.y + 1], bones[actualIndices.y + 2]);
float3x4 boneMatrix3 =
float3x4(bones[actualIndices.z], bones[actualIndices.z + 1], bones[actualIndices.z + 2]);
float3x4 boneMatrix4 =
float3x4(bones[actualIndices.w], bones[actualIndices.w + 1], bones[actualIndices.w + 2]);

float3x4 unitMatrix = float3x4(1.0.xxxx, 1.0.xxxx, 1.0.xxxx);
float3x4 weightMatrix1 = unitMatrix * weights.x;
float3x4 weightMatrix2 = unitMatrix * weights.y;
float3x4 weightMatrix3 = unitMatrix * weights.z;
float3x4 weightMatrix4 = unitMatrix * weights.w;

return (boneMatrix1 - pivotMatrix) * weightMatrix1 +
(boneMatrix2 - pivotMatrix) * weightMatrix2 +
(boneMatrix3 - pivotMatrix) * weightMatrix3 +
(boneMatrix4 - pivotMatrix) * weightMatrix4;
}

float3x3 GetBoneRSMatrix(float4 bones[240], int4 actualIndices, float4 weights)
{
float3x3 result;
for (int rowIndex = 0; rowIndex < 3; ++rowIndex) {
result[rowIndex] = weights.xxx * bones[actualIndices.x + rowIndex].xyz +
weights.yyy * bones[actualIndices.y + rowIndex].xyz +
weights.zzz * bones[actualIndices.z + rowIndex].xyz +
weights.www * bones[actualIndices.w + rowIndex].xyz;
}
return result;
}
# endif

# define M_HALFPI 1.57079637;
# define M_PI 3.141593

Expand Down
74 changes: 5 additions & 69 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "Common/Color.hlsl"
#include "Common/FrameBuffer.hlsl"
#include "Common/LightingData.hlsl"
#include "Common/LodLandscape.hlsli"
#include "Common/MotionBlur.hlsl"
#include "Common/Permutation.hlsl"
#include "Common/Skinned.hlsli"

#define PI 3.1415927

Expand Down Expand Up @@ -148,15 +150,6 @@ cbuffer PerGeometry : register(b2)
# endif // VR
};

# if defined(SKINNED)
cbuffer PreviousBonesBuffer : register(b9)
{
float4 PreviousBones[240] : packoffset(c0);
}

cbuffer BonesBuffer : register(b10) { float4 Bones[240] : packoffset(c0); }
# endif

cbuffer VS_PerFrame : register(b12)
{
# if !defined(VR)
Expand Down Expand Up @@ -187,58 +180,6 @@ float2 GetTreeShiftVector(float4 position, float4 color)
}
# endif // TREE_ANIM

# if defined(SKINNED)
float3x4 GetBoneMatrix(float4 bones[240], int4 actualIndices, float3 pivot, float4 weights)
{
/*float3x4 result;
for (int rowIndex = 0; rowIndex < 3; ++rowIndex)
{
float4 pivotRow = float4(0, 0, 0, pivot[rowIndex]);
result[rowIndex] = 0.0.xxxx;
for (int boneIndex = 0; boneIndex < 4; ++boneIndex)
{
result[rowIndex] += (bones[actualIndices[boneIndex] +
rowIndex] - pivotRow) * weights[boneIndex];
}
}
return result;*/

float3x4 pivotMatrix = transpose(float4x3(0.0.xxx, 0.0.xxx, 0.0.xxx, pivot));

float3x4 boneMatrix1 =
float3x4(bones[actualIndices.x], bones[actualIndices.x + 1], bones[actualIndices.x + 2]);
float3x4 boneMatrix2 =
float3x4(bones[actualIndices.y], bones[actualIndices.y + 1], bones[actualIndices.y + 2]);
float3x4 boneMatrix3 =
float3x4(bones[actualIndices.z], bones[actualIndices.z + 1], bones[actualIndices.z + 2]);
float3x4 boneMatrix4 =
float3x4(bones[actualIndices.w], bones[actualIndices.w + 1], bones[actualIndices.w + 2]);

float3x4 unitMatrix = float3x4(1.0.xxxx, 1.0.xxxx, 1.0.xxxx);
float3x4 weightMatrix1 = unitMatrix * weights.x;
float3x4 weightMatrix2 = unitMatrix * weights.y;
float3x4 weightMatrix3 = unitMatrix * weights.z;
float3x4 weightMatrix4 = unitMatrix * weights.w;

return (boneMatrix1 - pivotMatrix) * weightMatrix1 +
(boneMatrix2 - pivotMatrix) * weightMatrix2 +
(boneMatrix3 - pivotMatrix) * weightMatrix3 +
(boneMatrix4 - pivotMatrix) * weightMatrix4;
}

float3x3 GetBoneRSMatrix(float4 bones[240], int4 actualIndices, float4 weights)
{
float3x3 result;
for (int rowIndex = 0; rowIndex < 3; ++rowIndex) {
result[rowIndex] = weights.xxx * bones[actualIndices.x + rowIndex].xyz +
weights.yyy * bones[actualIndices.y + rowIndex].xyz +
weights.zzz * bones[actualIndices.z + rowIndex].xyz +
weights.www * bones[actualIndices.w + rowIndex].xyz;
}
return result;
}
# endif // SKINNED

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT vsout;
Expand All @@ -251,12 +192,7 @@ VS_OUTPUT main(VS_INPUT input)
# endif
);
# if defined(LODLANDNOISE) || defined(LODLANDSCAPE)
float4 rawWorldPosition = float4(mul(World[eyeIndex], inputPosition), 1);
float worldXShift = rawWorldPosition.x - HighDetailRange[eyeIndex].x;
float worldYShift = rawWorldPosition.y - HighDetailRange[eyeIndex].y;
if ((abs(worldXShift) < HighDetailRange[eyeIndex].z) && (abs(worldYShift) < HighDetailRange[eyeIndex].w)) {
inputPosition.z -= (230 + rawWorldPosition.z / 1e9);
}
inputPosition = AdjustLodLandscapeVertexPositionMS(inputPosition, float4x4(World[eyeIndex], float4(0, 0, 0, 1)), HighDetailRange[eyeIndex]);
# endif // defined(LODLANDNOISE) || defined(LODLANDSCAPE) \

precise float4 previousInputPosition = inputPosition;
Expand All @@ -273,11 +209,11 @@ VS_OUTPUT main(VS_INPUT input)
precise int4 actualIndices = 765.01.xxxx * input.BoneIndices.xyzw;

float3x4 previousWorldMatrix =
GetBoneMatrix(PreviousBones, actualIndices, PreviousBonesPivot[eyeIndex], input.BoneWeights);
GetBoneTransformMatrix(PreviousBones, actualIndices, PreviousBonesPivot[eyeIndex], input.BoneWeights);
precise float4 previousWorldPosition =
float4(mul(inputPosition, transpose(previousWorldMatrix)), 1);

float3x4 worldMatrix = GetBoneMatrix(Bones, actualIndices, BonesPivot[eyeIndex], input.BoneWeights);
float3x4 worldMatrix = GetBoneTransformMatrix(Bones, actualIndices, BonesPivot[eyeIndex], input.BoneWeights);
precise float4 worldPosition = float4(mul(inputPosition, transpose(worldMatrix)), 1);

float4 viewPos = mul(ViewProj[eyeIndex], worldPosition);
Expand Down
Loading

0 comments on commit 7ff9ed7

Please sign in to comment.