0

A little basic question i can't get my head around. I need to create a shader that works like a facing ratio shader however also containing up-down and left-right (in screen space).

Just like this:
just like this

I want it to work without having the original geometry, only having the normals, point position and the camera position. How should i approach this?

user2339945
  • 623
  • 1
  • 9
  • 14
  • Well, basically you want to project normals onto screen and then use components of the projected vector as color components. If you want more specific help, then show us your shader source here. – Sergey Krusch Jun 08 '14 at 16:45
  • Well the thing is i am trying to find a purely vector math based approach, one that could work by just having the normals, point position and the camera position (not the geometry). – user2339945 Jun 08 '14 at 17:01
  • Are you sure that you understand what you are talking about? – Sergey Krusch Jun 08 '14 at 17:17
  • 1
    You already have position and normal. What kind of geometry information you want to avoid? :) – Sergey Krusch Jun 08 '14 at 17:21
  • Yes, i do understand what i am talking about, i am just super limited with the coding environment i am in. I was wondering if there was a simple equation to get the result described above without having to re-project onto the scene. Like the dot product just with the 2 other axis. – user2339945 Jun 08 '14 at 17:51
  • 1
    Dot product of normal with camera's up direction gives you "up ratio" and dot product of normal with camera's right direction gives you "right ratio". Just make sure that all the vectors are in the same coordinate space. – Sergey Krusch Jun 08 '14 at 18:07
  • You can camera's directions from UNITY_MATRIX_V matrix (they are in world space), or set them explicitly as material's properties. – Sergey Krusch Jun 08 '14 at 18:16
  • You know. I have just now realized that you don't even need to do anything. Just multiply your normal by UNITY_MATRIX_IT_MV in vertex shader and you are done. Then use it's value in fragment shader: x is "right ratio" and y is "up ratio" (just don't forget to normalize it first). – Sergey Krusch Jun 08 '14 at 19:11

2 Answers2

1

Here's your shader:

#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

struct app2vert
{
    float4 position: POSITION;
    float3 normal: NORMAL;
};

struct vert2frag
{
    float4 position: POSITION;
    float3 normal: TEXCOORD0;
};

vert2frag vert(app2vert input)
{
    vert2frag output;
    output.position = mul(UNITY_MATRIX_MVP, input.position);
    output.normal = mul((float3x3)UNITY_MATRIX_IT_MV, input.normal);
    return output;
}

float4 frag(vert2frag input) : COLOR
{
    float3 normal = normalize(input.normal);
    float4 output = fixed4(normal.x, normal.y, normal.z, 1.0f);

    return output;
}
Sergey Krusch
  • 1,928
  • 16
  • 17
0

Eventually ended up doing 2 transformation matrixes with -90,0,0 and 0,90,0 based on the cameras transformation and did a dot product from thoes 2 to the scene normal. Giving me the exact result i was looking for.

user2339945
  • 623
  • 1
  • 9
  • 14