Last active
May 22, 2018 19:21
-
-
Save kevinw/80593a34b7211d99d251d266e957fe87 to your computer and use it in GitHub Desktop.
Using CalculateFrustumCorners to raymarch in VR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static Matrix4x4 FrustumCornersMatrix(Camera cam, Camera.MonoOrStereoscopicEye eye) | |
| { | |
| var camtr = cam.transform; | |
| cam.CalculateFrustumCorners(new Rect(0, 0, 1, 1), cam.farClipPlane, eye, frustumCorners); | |
| Matrix4x4 frustumMatrix = Matrix4x4.identity; | |
| frustumMatrix.SetRow(0, camtr.TransformVector(frustumCorners[0])); | |
| frustumMatrix.SetRow(1, camtr.TransformVector(frustumCorners[3])); | |
| frustumMatrix.SetRow(2, camtr.TransformVector(frustumCorners[1])); | |
| frustumMatrix.SetRow(3, camtr.TransformVector(frustumCorners[2])); | |
| return frustumMatrix; | |
| } | |
| void UpdateCamera(Camera cam, Material m) { | |
| m.SetMatrix("_FrustumCorners", FrustumCornersMatrix(cam, cam.stereoActiveEye)); | |
| if (cam.stereoTargetEye != StereoTargetEyeMask.None && Application.isPlaying && UnityEngine.XR.XRSettings.enabled && UnityEngine.XR.XRDevice.isPresent) | |
| m.SetMatrix("_FrustumCorners2", FrustumCornersMatrix(cam, Camera.MonoOrStereoscopicEye.Right)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct v2f | |
| { | |
| float4 vertex : SV_POSITION; | |
| float4 interpolatedRay : TEXCOORD0; | |
| UNITY_VERTEX_INPUT_INSTANCE_ID | |
| UNITY_VERTEX_OUTPUT_STEREO | |
| }; | |
| uniform float4x4 _FrustumCorners; | |
| #if UNITY_SINGLE_PASS_STEREO | |
| uniform float4x4 _FrustumCorners2; | |
| #endif | |
| v2f vert(Input input) { | |
| /* ... */ | |
| int frustumIndex = input.vertex.x + 2 * (1 - input.vertex.y); | |
| #if UNITY_SINGLE_PASS_STEREO | |
| if (unity_StereoEyeIndex == 0) | |
| o.interpolatedRay = _FrustumCorners[frustumIndex]; | |
| else | |
| o.interpolatedRay = _FrustumCorners2[frustumIndex]; | |
| #else | |
| o.interpolatedRay = _FrustumCorners[frustumIndex]; | |
| #endif | |
| } | |
| FragOut frag(v2f i) { | |
| UNITY_SETUP_INSTANCE_ID(i); | |
| float3 rayDir = normalize(i.interpolatedRay); | |
| float3 rayOrigin = _WorldSpaceCameraPos + _ProjectionParams.y * rayDir; | |
| fixed4 color = doRaymarch(rayOrigin, rayDir); | |
| /* ... */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not fully following implementation of these snippets... my raymarching implementation (not using uraymarch but similar I guess?) is multi-pass only, but using the code above on the camera/mat and setting the vert to the "o.interpolatedRay = _FrustumCorners[frustumIndex];" I'm getting garbage rendering for some reason... not sure why