1 | struct outPixel |
---|
2 | { |
---|
3 | float4 colour : COLOR0; |
---|
4 | }; |
---|
5 | |
---|
6 | // General functions |
---|
7 | |
---|
8 | // Expand a range-compressed vector |
---|
9 | half4 expand(half4 v) |
---|
10 | { |
---|
11 | return v * 2 - 1; |
---|
12 | } |
---|
13 | half3 expand(half3 v) |
---|
14 | { |
---|
15 | return v * 2 - 1; |
---|
16 | } |
---|
17 | half2 expand(half2 v) |
---|
18 | { |
---|
19 | return v * 2 - 1; |
---|
20 | } |
---|
21 | half1 expand(half1 v) |
---|
22 | { |
---|
23 | return v * 2 - 1; |
---|
24 | } |
---|
25 | |
---|
26 | // Returns light direction from light position and vertex position |
---|
27 | half3 getLightDirection(float4 lightPosition, float4 position) |
---|
28 | { |
---|
29 | // calculate tangent space light vector |
---|
30 | // Get object space light direction |
---|
31 | // Non-normalised since we'll do that in the fragment program anyway |
---|
32 | return lightPosition.xyz - (position.xyz * lightPosition.w); |
---|
33 | } |
---|
34 | |
---|
35 | // Returns eye direction from eye position and vertex position |
---|
36 | half3 getEyeDirection(float3 eyePosition, float4 position) |
---|
37 | { |
---|
38 | return eyePosition - position.xyz; |
---|
39 | } |
---|
40 | |
---|
41 | // Returns a Tangent Binormal Normal matrix |
---|
42 | half3x3 getTBNMatrix(float3 tangent, float3 normal) |
---|
43 | { |
---|
44 | // Calculate the binormal (NB we assume both normal and tangent are |
---|
45 | // already normalised) |
---|
46 | // NB looks like nvidia cross params are BACKWARDS to what you'd expect |
---|
47 | // this equates to NxT, not TxN |
---|
48 | float3 binormal = cross(tangent, normal); |
---|
49 | |
---|
50 | // Form a rotation matrix out of the vectors |
---|
51 | return half3x3(tangent, binormal, normal); |
---|
52 | } |
---|
53 | |
---|
54 | // Returns expanded normal vector from texture map |
---|
55 | half3 getNormalMapVector(sampler2D normalMap, float2 uv) |
---|
56 | { |
---|
57 | // get bump map vector, again expand from range-compressed |
---|
58 | return expand(tex2D(normalMap, uv).xyz); |
---|
59 | } |
---|
60 | |
---|
61 | // Returns displacement vector from normalmaps alpha channel |
---|
62 | half getDisplacement(sampler2D normalMap, float2 uv, half scale, half bias) |
---|
63 | { |
---|
64 | // get the height using the tex coords |
---|
65 | half height = tex2D(normalMap, uv).a; |
---|
66 | |
---|
67 | // calculate displacement |
---|
68 | return (height * scale) + bias; |
---|
69 | } |
---|
70 | |
---|
71 | // Returns a specular component from normal vector, specular colour and specular power |
---|
72 | half3 getSpecularComponent(float3 normal, float3 halfAngle, float3 specularcolour, float specularPower) |
---|
73 | { |
---|
74 | return pow(saturate(dot(normal, halfAngle)), specularPower) * specularcolour; |
---|
75 | } |
---|
76 | |
---|
77 | // Returns a per-pixel lighted component from normal vector, lightdir and colour |
---|
78 | half3 getLightingComponent(float3 normal, float3 lightDir, float3 colour) |
---|
79 | { |
---|
80 | return saturate(dot(normal, lightDir)) * colour; |
---|
81 | } |
---|