Hi everyone,
So after being contacted on Reddit about it I’ve decided to delve again in my attempt to give Unity a proper viewmodel shader. I’m planning on building it for URP afterward, but for the moment I’m trying to make it work in built-in. Not gonna bother with HDRP as I have no interest in this pipeline and I feel like URP gives so much more freedom overall, I sincerely hope URP is the future for Unity, with a bit of work it could become the best tool for fully customizable graphics, as evidenced by my recent attempt at a Toon shader with more controls over lighting. Anyway on to the topic at hand.
Ideally, for this shader to be interesting for FPS devs, I’d want to be able to customize the FOV of the viewmodel (player’s weapon). Problem is, Unity gives you no choice but to execute an ObjectToClipPos with the surface vertex shader output data. As has been mentioned in the long past in this forum, the best way to go about this is to inverse what ObjectToClipPos does to the vertex data, so its action is nullified.
As such my current workflow has a script sending a global containing a perspective projection Matrix calculated using Matrix4x4.Perspective. To nullify Unity’s MVP matrix I’ve tried 2 different solutions :
v.vertex = mul(customMVP, mul(unity_WorldToObject, mul(_invViewProjMatrix, v.vertex)));
where _invViewProjMatrix is the inverse of the view projection matrix, as calculated in the same script that sends my custom projection matrix, then sent as a global.
v.vertex = mul(customMVP, mul(transpose(UNITY_MATRIX_IT_MV), mul(unity_CameraInvProjection, v.vertex)));
Which as far as I understand should first nullify projection and then model and view.
Unfortunately, neither of these worked. I even tried replacing customMVP with UNITY_MATRIX_MVP, which should result in a completely unchanged visual result, but it does change. The model appears flattened, and when rotating in editor, moves in weird patterns.
This is the last roadblock for me in this endeavour and it’s really frustrating, particularly as it feels like it should work, using, as far as I know, the correct inverse of all the matrix that I need.
Thanks in advance to anyone who can help me with this and have a great day everyone
Edit : This hasn’t been solved BUT I realized a big mistake from my part which is that I did the operations on the entire vertices. Now the model properly follows the camera, and actually sees some change when changing my FOV value. However, even using Unity’s MVP matrix, I’m getting a vertically (Y axis relative to the camera) squished model. Gonna inspect that some more but I don’t see where that could come from. For the record, I changed all v.vertex to v.vertex.xyz.