| 1 |
// |
| 2 |
// The state structure is used internally within the fragment shader to |
| 3 |
// commonly used values. |
| 4 |
// |
| 5 |
struct State |
| 6 |
{ |
| 7 |
float4 tex_coord[4]; |
| 8 |
float3 tangent[1]; |
| 9 |
float3 binormal[1]; |
| 10 |
float3 position; |
| 11 |
float3 origin; |
| 12 |
float3 normal; |
| 13 |
float3 direction; |
| 14 |
float ray_length; |
| 15 |
}; |
| 16 |
|
| 17 |
|
| 18 |
#define MaxBones 58 |
| 19 |
float4x4 Bones[MaxBones]; |
| 20 |
bool UseBones |
| 21 |
< |
| 22 |
string SasUiLabel = "Use Bones"; |
| 23 |
string SasUiControl = "Any"; |
| 24 |
> = false; |
| 25 |
|
| 26 |
// |
| 27 |
// The following are parameters representing non-varying state variables |
| 28 |
// referenced by the shader. These should be set by the application at runtime. |
| 29 |
// Note that vector parameters should be provided in camera space. |
| 30 |
// |
| 31 |
float4x4 __object_to_ndc : WorldViewProjection |
| 32 |
< |
| 33 |
string UIWidget = "none"; |
| 34 |
>; |
| 35 |
float4x4 __world_to_object : WorldInverse |
| 36 |
< |
| 37 |
string UIWidget = "none"; |
| 38 |
>; |
| 39 |
float4x4 __object_to_world : World |
| 40 |
< |
| 41 |
string UIWidget = "none"; |
| 42 |
>; |
| 43 |
float4x4 __view : View |
| 44 |
< |
| 45 |
string SasUiControl = "none"; |
| 46 |
>; |
| 47 |
float4x4 __camera_to_world : ViewInverse |
| 48 |
< |
| 49 |
string UIWidget = "none"; |
| 50 |
>; |
| 51 |
float4x4 __projection : Projection |
| 52 |
< |
| 53 |
string SasUiControl = "none"; |
| 54 |
>; |
| 55 |
|
| 56 |
// |
| 57 |
// The App2vert structure defines the vertex attributes used by the vertex |
| 58 |
// shader. The application should supply a vertex stream containing these |
| 59 |
// elements. |
| 60 |
// |
| 61 |
struct App2vert |
| 62 |
{ |
| 63 |
float3 position : POSITION; |
| 64 |
float3 normal : NORMAL; |
| 65 |
// for skinning (not used by XSI however) |
| 66 |
float4 BoneIndices : BLENDINDICES0; |
| 67 |
float4 BoneWeights : BLENDWEIGHT0; |
| 68 |
}; |
| 69 |
|
| 70 |
// |
| 71 |
// The Vert2frag_out structure defines values output by the vertex shader. |
| 72 |
// |
| 73 |
struct Vert2frag_out |
| 74 |
{ |
| 75 |
float4 hpos : POSITION; |
| 76 |
float3 position : TEXCOORD6; |
| 77 |
float3 normal : TEXCOORD7; |
| 78 |
}; |
| 79 |
|
| 80 |
// |
| 81 |
// The Vert2frag_in structure defines values used by the fragment shader. |
| 82 |
// |
| 83 |
struct Vert2frag_in |
| 84 |
{ |
| 85 |
float4 hpos : POSITION; |
| 86 |
float3 position : TEXCOORD6; |
| 87 |
float3 normal : TEXCOORD7; |
| 88 |
}; |
| 89 |
|
| 90 |
// |
| 91 |
// This function is the main method of the vertex shader. |
| 92 |
// |
| 93 |
Vert2frag_out vertex_main( |
| 94 |
App2vert vs_in) |
| 95 |
{ |
| 96 |
Vert2frag_out vs_out; |
| 97 |
float4 position = float4(vs_in.position, 1); |
| 98 |
vs_out.hpos = mul(position, __object_to_ndc); |
| 99 |
vs_out.position = mul(position, __object_to_world).xyz; |
| 100 |
vs_out.normal = mul((float3x3)__world_to_object, vs_in.normal); |
| 101 |
return vs_out; |
| 102 |
} |
| 103 |
|
| 104 |
// |
| 105 |
// This function is the main method of the skinned vertex shader. |
| 106 |
// |
| 107 |
Vert2frag_out skinned_vertex_main( |
| 108 |
App2vert vs_in) |
| 109 |
{ |
| 110 |
Vert2frag_out vs_out; |
| 111 |
|
| 112 |
float4 position = float4(vs_in.position, 1); |
| 113 |
float4 HPOS = float4(0, 0, 0, 0); |
| 114 |
if( UseBones == true) |
| 115 |
{ |
| 116 |
float4x4 skinTransform = 0; |
| 117 |
// When the next 4 lines are commented out, the shader displays fine in Softimage. |
| 118 |
// But putting enabling one or more will make the shader disappear completely in the |
| 119 |
// Softimage viewport, no matter what value "UseBones" is set to |
| 120 |
skinTransform += Bones[vs_in.BoneIndices.x] * vs_in.BoneWeights.x; |
| 121 |
skinTransform += Bones[vs_in.BoneIndices.y] * vs_in.BoneWeights.y; |
| 122 |
skinTransform += Bones[vs_in.BoneIndices.z] * vs_in.BoneWeights.z; |
| 123 |
skinTransform += Bones[vs_in.BoneIndices.w] * vs_in.BoneWeights.w; |
| 124 |
|
| 125 |
// Skin the vertex position |
| 126 |
float4 weightedPosition = mul(position, skinTransform); |
| 127 |
HPOS = mul(mul(weightedPosition, __view), __projection); |
| 128 |
vs_out.normal = normalize(mul(float3(vs_in.normal.xyz), |
| 129 |
(float3x3)skinTransform)); |
| 130 |
vs_out.position = weightedPosition; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
HPOS = mul(position, __object_to_ndc); // TMP |
| 135 |
vs_out.position = mul(position, __object_to_world).xyz; |
| 136 |
vs_out.normal = mul((float3x3)__world_to_object, vs_in.normal); |
| 137 |
} |
| 138 |
vs_out.hpos = HPOS; |
| 139 |
return vs_out; |
| 140 |
} |
| 141 |
|
| 142 |
// |
| 143 |
// This function is the main method of the fragment shader. It initializes the |
| 144 |
// values in the state structure that are used by nodes in the shader graph |
| 145 |
// and produces the final result of the shader. |
| 146 |
// |
| 147 |
float4 fragment_main( |
| 148 |
Vert2frag_in fs_in) : COLOR |
| 149 |
{ |
| 150 |
State state; |
| 151 |
state.position = fs_in.position; |
| 152 |
state.normal = normalize(fs_in.normal); |
| 153 |
state.origin = __camera_to_world[3]; |
| 154 |
float3 eye_to_pos = state.position - state.origin; |
| 155 |
state.ray_length = length(eye_to_pos); |
| 156 |
state.direction = eye_to_pos/state.ray_length; |
| 157 |
return float4(0.4, 0.2, 0.8, 1.0); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
// |
| 162 |
// The following define the default technique and pass of the effect. |
| 163 |
// |
| 164 |
technique T0 |
| 165 |
{ |
| 166 |
pass P0 |
| 167 |
{ |
| 168 |
ZEnable = true; |
| 169 |
ZWriteEnable = true; |
| 170 |
CullMode = none; |
| 171 |
VertexShader = compile vs_3_0 skinned_vertex_main(); |
| 172 |
PixelShader = compile ps_3_0 fragment_main(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|