Skip to content

Commit b9cdd83

Browse files
[Rendering] WIP on Standard shader and lighting;
1 parent b818867 commit b9cdd83

File tree

3 files changed

+189
-63
lines changed

3 files changed

+189
-63
lines changed

BuiltinResources/Hidden/Shaders/Default/Standard.shader

Lines changed: 92 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ Variants VERTEX_COLORS, LIT, HALF_LAMBERT, PER_VERTEX_LIGHTING, NORMALMAP, CUTOU
66

77
Begin Parameters
88

9-
varying vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0)
10-
varying vec3 v_worldPos : TEXCOORD1
11-
varying vec3 v_normal : NORMAL
12-
varying vec3 v_lightNormal : TEXCOORD3
13-
varying vec4 v_color : COLOR
14-
varying float v_instanceID : TEXCOORD2
15-
varying vec3 v_tangent : TANGENT
16-
varying vec3 v_bitangent : BITANGENT
17-
9+
uniform vec3 viewPosition;
1810
uniform texture ambientOcclusionTexture
1911
uniform color diffuseColor = #FFFFFFFF
2012
uniform texture diffuseTexture = WHITE
@@ -33,93 +25,130 @@ End Parameters
3325
Begin Instancing
3426
End Instancing
3527

36-
Begin Vertex
28+
Begin Common
3729

38-
$input a_position, a_texcoord0, a_normal, a_color0, a_tangent, a_bitangent
39-
$output v_texcoord0, v_worldPos, v_normal, v_color, v_instanceID, v_tangent, v_bitangent, v_lightNormal
30+
cbuffer Uniforms
31+
{
32+
float3 viewPosition;
33+
Sampler2D ambientOcclusionTexture;
34+
float4 diffuseColor;
35+
Sampler2D diffuseTexture;
36+
Sampler2D displacementTexture;
37+
float4 emissiveColor;
38+
Sampler2D emissiveTexture;
39+
Sampler2D heightTexture;
40+
Sampler2D normalTexture;
41+
float4 specularColor;
42+
Sampler2D specularTexture;
43+
float cutout;
44+
float alphaThreshold;
45+
};
46+
47+
struct VertexOutput
48+
{
49+
float3 position : SV_Position;
50+
float3 worldPosition;
51+
float3 lightNormal;
52+
float2 coords;
53+
float3 normal;
54+
float3 tangent;
55+
float3 bitangent;
56+
float4 color;
57+
uint instanceID;
58+
};
59+
60+
End Common
4061

41-
#include "StapleLighting.sh"
62+
Begin Vertex
4263

43-
void main()
64+
//TODO: Get slang to handle multiple preprocessor conditions
65+
66+
struct Input
67+
{
68+
float3 position : POSITION;
69+
float2 coords : TEXCOORD0;
70+
float3 normal : NORMAL;
71+
float3 tangent : TANGENT;
72+
float3 bitangent : BITANGENT;
73+
float4 color : COLOR0;
74+
uint instanceID : SV_InstanceID;
75+
};
76+
77+
[shader("vertex")]
78+
VertexOutput VertexMain(Input input)
4479
{
45-
mat4 model = StapleModelMatrix;
80+
VertexOutput output;
4681

47-
#ifdef SKINNING
48-
model = StapleGetSkinningMatrix(model, a_indices, a_weight);
49-
#endif
82+
float4x4 model = world;
5083

51-
mat4 projViewWorld = mul(mul(u_proj, u_view), model);
52-
mat4 viewWorld = mul(u_view, model);
84+
float4x4 projectionViewWorld = mul(mul(projection, view), model);
85+
float4x4 viewWorld = mul(view, model);
5386

54-
vec4 v_pos = mul(projViewWorld, vec4(a_position, 1.0));
87+
float4 vertexPosition = mul(projectionViewWorld, float4(input.position, 1.0));
5588

56-
gl_Position = v_pos;
89+
output.position = vertexPosition.xyz;
5790

58-
v_worldPos = mul(model, vec4(a_position, 1.0)).xyz;
91+
output.worldPosition = mul(model, float4(input.position, 1.0)).xyz;
5992

60-
v_texcoord0 = a_texcoord0;
61-
v_normal = a_normal;
62-
63-
v_lightNormal = StapleLightNormal(a_normal, model);
64-
65-
v_tangent = a_tangent;
66-
v_bitangent = a_bitangent;
93+
output.coords = input.coords;
94+
output.normal = input.normal;
95+
output.tangent = input.tangent;
96+
output.bitangent = input.bitangent;
97+
output.lightNormal = StapleLightNormal(input.normal, model);
98+
output.instanceID = input.instanceID;
6799

68-
v_instanceID = StapleInstanceID;
100+
//#if LIT && PER_VERTEX_LIGHTING
101+
//output.color = float4x4(diffuseColor.rgb * StapleProcessLights(viewPosition, output.worldPosition, input.normal), diffuseColor.a);
69102

70-
#if LIT && PER_VERTEX_LIGHTING
71-
v_color = vec4(diffuseColor.rgb * StapleProcessLights(int(v_instanceID), u_viewPos, v_worldPos, v_normal), diffuseColor.a);
72-
73-
#if VERTEX_COLORS
74-
v_color = a_color0 * v_color;
75-
#endif
76-
#else
77-
v_color = a_color0;
78-
#endif
103+
//#ifdef VERTEX_COLORS
104+
// output.color = input.color * output.color;
105+
// #endif
106+
//#else
107+
output.color = input.color;
108+
//#endif
109+
110+
return output;
79111
}
80112
End Vertex
81113

82114
Begin Fragment
83115

84-
$input v_texcoord0, v_worldPos, v_normal, v_color, v_instanceID, v_tangent, v_bitangent, v_lightNormal
85-
86-
#include "StapleLighting.sh"
87-
88-
void main()
116+
[shader("fragment")]
117+
float4 FragmentMain(VertexOutput input) : SV_Target
89118
{
90-
#if VERTEX_COLORS || PER_VERTEX_LIGHTING
91-
vec4 diffuse = v_color * diffuseColor;
92-
#else
93-
vec4 diffuse = texture2D(diffuseTexture, v_texcoord0) * diffuseColor;
94-
#endif
119+
//#if VERTEX_COLORS || PER_VERTEX_LIGHTING
120+
// float4 diffuse = input.color * diffuseColor;
121+
//#else
122+
float4 diffuse = diffuseTexture.Sample(input.coords) * diffuseColor;
123+
//#endif
95124

96-
#if CUTOUT
125+
#ifdef CUTOUT
97126
if(diffuse.a < alphaThreshold)
98127
{
99128
discard;
100-
101-
return;
102129
}
103130
#endif
104131

105-
#if LIT && PER_VERTEX_LIGHTING
106-
gl_FragColor = diffuse;
132+
//#if LIT && PER_VERTEX_LIGHTING
133+
return diffuse;
134+
/*
107135
#elif LIT
108136
109137
#if NORMALMAP
110-
mat3 tbn = mtxFromCols(normalize(v_tangent), normalize(v_bitangent), normalize(v_normal));
138+
float3x3 tbn = float3x3(normalize(input.tangent), normalize(input.bitangent), normalize(input.normal));
111139
112-
vec3 normalMapNormal = normalize(texture2D(normalTexture, v_texcoord0).xyz * 2.0 - 1.0);
140+
float3 normalMapNormal = normalize(normalTexture.Sample(v_texcoord0).xyz * 2.0 - 1.0);
113141
114-
vec3 light = StapleProcessLightsTangent(int(v_instanceID), u_viewPos, v_worldPos, normalMapNormal, tbn);
142+
float3 light = StapleProcessLightsTangent(viewPosition, input.worldPosition, normalMapNormal, tbn);
115143
#else
116-
vec3 light = StapleProcessLights(int(v_instanceID), u_viewPos, v_worldPos, v_lightNormal);
144+
float3 light = StapleProcessLights(viewPosition, input.world, input.lightNormal);
117145
#endif
118146
119-
gl_FragColor = vec4(light, 1) * diffuse;
120-
#else
121-
gl_FragColor = diffuse;
122-
#endif
147+
return float4(light, 1) * diffuse;
148+
*/
149+
//#else
150+
//return diffuse;
151+
//#endif
123152
}
124153

125154
End Fragment

Tools/ShaderIncludes/Staple.slang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Staple;
22

33
__include StapleMath;
4+
__include StapleLighting;
45

56
#define STAPLE_SKINNING_STAGE_INDEX 15
67
#define STAPLE_LIGHTING_NORMAL_MATRIX_STAGE_INDEX 14
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
implementing Staple;
2+
3+
#define STAPLE_MAX_LIGHTS 16
4+
5+
public struct LightSample
6+
{
7+
float3 diffuse;
8+
float3 direction;
9+
};
10+
11+
public interface ILight
12+
{
13+
LightSample Sample(float3 target);
14+
}
15+
16+
public struct PointLight : ILight
17+
{
18+
float3 position;
19+
float3 diffuse;
20+
21+
LightSample Sample(float3 target)
22+
{
23+
float3 delta = target - position;
24+
float distance = length(delta);
25+
26+
LightSample sample;
27+
28+
sample.direction = normalize(delta);
29+
sample.diffuse = diffuse;
30+
31+
return sample;
32+
}
33+
};
34+
35+
public struct DirectionalLight : ILight
36+
{
37+
float3 direction;
38+
float3 diffuse;
39+
40+
LightSample Sample(float3 target)
41+
{
42+
LightSample sample;
43+
44+
sample.direction = normalize(direction);
45+
sample.diffuse = diffuse;
46+
47+
return sample;
48+
}
49+
};
50+
51+
public float3 StapleLightNormal(float3 normal, float4x4 modelView)
52+
{
53+
float4x4 inverseMatrix = transpose(inverse(modelView));
54+
55+
float3x3 normalMatrix = float3x3(inverseMatrix[0].xyz,
56+
inverseMatrix[1].xyz,
57+
inverseMatrix[2].xyz);
58+
59+
return normalize(mul(normalMatrix, normal));
60+
}
61+
62+
public float StapleLightScaling(float nDotL)
63+
{
64+
#ifdef HALF_LAMBERT
65+
return pow(nDotL * 0.5 + 0.5, 2);
66+
#else
67+
return nDotL;
68+
#endif
69+
}
70+
71+
public float4 StapleLightDiffuse<L: ILight>(float3 position, float3 normal, L light)
72+
{
73+
LightSample sample = light.Sample(position);
74+
75+
float nDotL = max(0, StapleLightScaling(dot(normal, sample.direction)));
76+
77+
return float4(sample.diffuse * nDotL, 1);
78+
}
79+
80+
public float4 StapleProcessLights<L: ILight>(float3 viewPosition, float3 worldPosition, float3 normal, float4 ambient,
81+
float4 albedo, ILight[] lights)
82+
{
83+
if(lights.getCount() == 0)
84+
{
85+
return ambient;
86+
}
87+
88+
float4 diffuse;
89+
90+
for(int i = 0; i < lights.getCount(); i++)
91+
{
92+
diffuse += StapleLightDiffuse(worldPosition, normal, lights[i]);
93+
}
94+
95+
return ambient + diffuse;
96+
}

0 commit comments

Comments
 (0)