Is there an easy way to get the camera’s forward vector in a surface shader (the near clip plane’s normal)? Perhaps a built-in shader constant or input struct variable? I want something like the viewDir
variable from here:
but just the forward vector, not a ray per-pixel.
I’m trying to do some calculations off pixel distances projected onto the near clipping plane’s normal instead of the distance from the camera position.
The near clip plane doesn't have a single normal in world space unless you're using an orthographic projection. You're dealing with a frustum, and not necessarily even an isosceles one.
– WilfredwilfredaHmm, well then forget that near clipping plane's normal stuff. I would still like the camera's forward vector in the surface shader though - if it's possible to do it without sending it as a uniform.
– AbrasionUnity provides the matrix that transforms from view to object space, so you can get the vector you want, without calculations (you'll need to rotate again if you need it in world space). I don't know surface shaders, so can't tell you what the uniform is called, in them, or if you can just grab the [2].xyz of it, though I suspect so. In GLSL, it's called gl_ModelViewMatrixInverseTranspose, and it may be UNITY_MATRIX_IT_MV in a surface shader.
– WilfredwilfredaUNITY_MATRIX_IT_MV[2].xyz works great, thx.
– Abrasion@Abrasion @Wilfredwilfreda Thanks guys,
– HistoricismUNITY_MATRIX_IT_MV[2].xyz
worked for me too. Maybe turn it into an answer for other users?