PDA

View Full Version : CGFX Stretch Shader


DeadlyNightshade
08-06-2012, 11:26 AM
I've been trying to find a stretch shader for Autodesk Maya for quite some time now - a shader that displays pinching and stretching on a model based on two things:
-Texel density difference
-UV-shell difference compared to actual geometry

The best thing I've found is a CGFX stretch shader that only seems to work in Mudbox but not Maya. I'm no coder so I have no idea how it works, but maybe someone here does?

Source:

/**** UNTWEAKABLES: Hidden & Automatically-Tracked Parameters **********/

float4x4 view_proj_matrix : WorldViewProjection < string UIWidget="None"; >;

//////// CONNECTOR DATA STRUCTURES ///////////

struct vertexInput {
float3 Position : POSITION;
float2 TexCoord0 : TEXCOORD0;
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float2 TexCoord : TEXCOORD0;
float4 HPosCopy : TEXCOORD1;
};

///////// VERTEX SHADING /////////////////////

vertexOutput directVS(vertexInput IN)
{
vertexOutput OUT = (vertexOutput)0;
float4 Po = float4(IN.Position.xyz,1);
OUT.TexCoord = IN.TexCoord0;
OUT.HPosition = mul(view_proj_matrix,Po);
OUT.HPosCopy.xyz = Po.xyz;
return OUT;
}

///////// PIXEL SHADING /////////////////////

float3 directPS(vertexOutput IN) : COLOR0
{
float2 uv = IN.TexCoord.xy;
float3 pos = IN.HPosCopy.xyz;
float2 ddxUV = ddx(uv);
float2 ddyUV = ddy(uv);
float3 ddxPOS = ddx(pos);
float3 ddyPOS = ddy(pos);
float3 crossPos = cross(ddxPOS, ddyPOS);
float FaceArea = length(crossPos);

float3 crossUV = cross(float3(ddxUV,0), float3(ddyUV,0));
float UVArea = length(crossUV);

float xL = length(ddxPOS);
float yL = length(ddyPOS);
float2 rX = ddxUV/xL;
float2 rY = ddyUV/yL;
float xVar = rX.x * rX.x + rY.x * rY.x;
float yVar = rX.y * rX.y + rY.y * rY.y;
float density = sqrt(xVar + yVar);
density = UVArea/FaceArea;
density = 100000 * density; //scale multiplier !!
if(density < 0.5)
{
float ratio = saturate(density * 2);
return lerp(float4(1,0,0,1) , float4(0,1,0,1), ratio);
}
else
{
float ratio = saturate(density * 2 - 1);
return lerp(float4(0,1,0,1) , float4(0,0,1,1), ratio);
}
return float4(density.xxx,1);

}

///////////////////////////////////////
/// TECHNIQUES ////////////////////////
///////////////////////////////////////

technique direct0 <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexProgram = compile vp40 directVS();
DepthTestEnable = true;
DepthMask = true;
CullFaceEnable = true;
BlendEnable = false;
DepthFunc = LEqual;
FragmentProgram = compile fp40 directPS();
}
}
The shader can be loaded in Maya but gives an unspecified error (Error //). Mesh remains pink but some colors are drawn it seems (evident from the UV texture editor). Does anyone know how this shader works and what needs to be done to "fix it"?

CGTalk Moderation
08-06-2012, 11:26 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.