Something like "TexGen EyeLinear" in CG

I’m working on a shader that applies a detail texture over the top of an existing mesh and then pans the detail texture. A problem I ran into is that since the detail texture is using the mesh’s UVs, the detail texture doesn’t look quite right. Also, if two meshes next to each other are using the same material, it would be cool if the detail texture appeared to be seamless.

I’m currently doing this to calculate the UVs of both textures, but I guess I’m looking for a different calculation for the DetailTex UVs:

 output.uv.xy = TRANSFORM_TEX(input.texcoord.xy, _MainTex);
 output.uv.zw = TRANSFORM_TEX(input.texcoord.xy, _DetailTex);

Because of this, I was looking for a way to mimik Shaderlab’s TexGen EyeLinear in CG, but I’m a bit confused by the math. Is it just converting the UV coordinates to world space? Are there any samples of how to do this in shader code? Any sample code or pointers to an explanation of the math would be much appreciated!

“Eye”, “View”, and “Camera” are synonymous spaces. Try multiplying your vertex positions by UNITY_MATRIX_MV and taking the .xy of those eye space positions. I bet that’s what EyeLinear is.

http://docs.unity3d.com/Documentation/Components/SL-BuiltinStateInPrograms.html

That work well, thanks for the tip.

One issue I see is that mesh faces that are slanted away from the camera start to stretch and tear. It makes sense because since I’m mapping vertex position onto the view, a slanted face will get odd values for the UVs - and I think Eye Linear does that too. I’m also trying to find some way to avoid that slanting behavior (maybe using the mesh’s normals? not sure yet). I’ll post back if I can find a solution for that.