Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Shader "Unlit/OculusMotionVectorDebugShader"
{
Properties
{
_MainTex ("Texture", 2DArray) = "white" {}
_MinValue ("Min Value", Float) = 0
_MaxValue ("Max Value", Float) = 1
[Toggle] _OnlyRed ("Only Red", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#pragma require 2darray
#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};

UNITY_DECLARE_TEX2DARRAY(_MainTex);

float _MinValue;
float _MaxValue;
float _OnlyRed;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = float2(v.uv.x, 1 - v.uv.y);
return o;
}

float3 inv_lerp(const float from, const float to, const float3 value){
return (value - from) / (to - from);
}

fixed4 frag (v2f i) : SV_Target
{
// sample the texture
float3 col = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(i.uv, 0)).rgb;

if (_OnlyRed == 1.0)
{
col.gb = col.rr;
}

// Map to the specified range
col = inv_lerp(_MinValue, _MaxValue, col);

return float4(col, 1);
}
ENDCG
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;
using UnityEditor;
using UnityEngine.Rendering;

public class OculusMotionVectorWindow : EditorWindow {

private const int k_MotionVectorTextureSize = 512;

private Material m_DebugMotionVectorMaterial;
private Material m_DebugDepthMaterial;

private float m_MaxVectorValue = 0.1f;
private float m_MaxDepth = 0.01f;

[MenuItem ("Oculus/Debug Motion Vectors")]
public static void ShowWindow ()
{
EditorWindow.GetWindow(typeof(OculusMotionVectorWindow));
}

private Material GetNamedMaterial(string name) {
var shaderGuid = AssetDatabase.FindAssets($"t:Shader {name}").FirstOrDefault();

return shaderGuid != null ? new Material(AssetDatabase.LoadAssetAtPath<Shader>(AssetDatabase.GUIDToAssetPath(shaderGuid))) : null;
}

private void InitMotionVectors() {

if (UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.motionVectorRenderTarget == null)
{
UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.motionVectorRenderTargetDesc = new RenderTextureDescriptor(k_MotionVectorTextureSize, k_MotionVectorTextureSize, RenderTextureFormat.ARGBFloat, 0) { vrUsage = VRTextureUsage.TwoEyes, dimension = TextureDimension.Tex2DArray, volumeDepth = 2 };
UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.motionVectorRenderTarget = new RenderTexture(UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.motionVectorRenderTargetDesc);
}

if (UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.depthRenderTarget == null)
{
UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.depthRenderTargetDesc = new RenderTextureDescriptor(k_MotionVectorTextureSize, k_MotionVectorTextureSize, RenderTextureFormat.Depth, 24) { vrUsage = VRTextureUsage.TwoEyes, dimension = TextureDimension.Tex2DArray, volumeDepth = 2 };
UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.depthRenderTarget = new RenderTexture(UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.depthRenderTargetDesc);
}
}

private void Awake() {

InitMotionVectors();
m_DebugMotionVectorMaterial = GetNamedMaterial("OculusMotionVectorDebugShader");
m_DebugMotionVectorMaterial.SetFloat("_OnlyRed", 0.0f);
m_DebugDepthMaterial = GetNamedMaterial("OculusMotionVectorDebugShader");
m_DebugDepthMaterial.SetFloat("_OnlyRed", 1.0f);
}

private void OnDestroy() {

DestroyImmediate(m_DebugMotionVectorMaterial);
}

void Update() {

Repaint();
}

void OnGUI () {

InitMotionVectors();
if (m_DebugMotionVectorMaterial == null) {
Awake();
}

UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.enableDebugMotionVectors =
EditorGUILayout.Toggle("Enable Motion Vectors",
UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.enableDebugMotionVectors);

EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();

m_DebugMotionVectorMaterial.SetFloat("_MinValue", -m_MaxVectorValue);
m_DebugMotionVectorMaterial.SetFloat("_MaxValue", m_MaxVectorValue);
EditorGUI.DrawPreviewTexture(
EditorGUILayout.GetControlRect(false, GUILayout.Height(k_MotionVectorTextureSize), GUILayout.Width(k_MotionVectorTextureSize)),
UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.motionVectorRenderTarget,
m_DebugMotionVectorMaterial);

m_MaxVectorValue = 1 / EditorGUILayout.Slider("Vector Scale", 1 / m_MaxVectorValue, 1, 100);
EditorGUILayout.EndVertical();

EditorGUILayout.BeginVertical();

m_DebugDepthMaterial.SetFloat("_MinValue", 0);
m_DebugDepthMaterial.SetFloat("_MaxValue", m_MaxDepth);

EditorGUI.DrawPreviewTexture(
EditorGUILayout.GetControlRect(false, GUILayout.Height(k_MotionVectorTextureSize), GUILayout.Width(k_MotionVectorTextureSize)),
UnityEngine.Rendering.Universal.DebugOculusMotionVectorSettings.depthRenderTarget,
m_DebugDepthMaterial);

m_MaxDepth = 1 / EditorGUILayout.Slider("Depth Scale", 1 / m_MaxDepth, 1, 100);
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ half4 frag(PackedVaryings packedInput) : SV_TARGET
Varyings unpacked = UnpackVaryings(packedInput);
UNITY_SETUP_INSTANCE_ID(unpacked);

#if _ALPHATEST_ON
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);

clip(surfaceDescription.Alpha - surfaceDescription.AlphaClipThreshold);
#endif

float3 screenPos = unpacked.curPositionCS.xyz / unpacked.curPositionCS.w;
float3 screenPosPrev = unpacked.prevPositionCS.xyz / unpacked.prevPositionCS.w;
half4 color = (1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@
float3 _LightPosition;
#endif

#ifdef VARYINGS_NEED_PREVIOUS_POSITION_CS
bool IsSmoothRotation(float3 prevAxis1, float3 prevAxis2, float3 currAxis1, float3 currAxis2)
{
float angleThreshold = 0.984f; // cos(10 degrees)
float2 angleDot = float2(dot(normalize(prevAxis1), normalize(currAxis1)), dot(normalize(prevAxis2), normalize(currAxis2)));
return all(angleDot > angleThreshold);
}
#endif

#if defined(FEATURES_GRAPH_VERTEX)
#if defined(HAVE_VFX_MODIFICATION)
VertexDescription BuildVertexDescription(Attributes input, AttributesElement element)
Expand Down Expand Up @@ -207,24 +198,14 @@ Varyings BuildVaryings(Attributes input)
#ifdef VARYINGS_NEED_PREVIOUS_POSITION_CS
if (unity_MotionVectorsParams.y == 0.0)
{
output.prevPositionCS = float4(0.0, 0.0, 0.0, 1.0);
output.prevPositionCS = output.curPositionCS;
}
else
{
bool hasDeformation = unity_MotionVectorsParams.x > 0.0;
float3 effectivePositionOS = (hasDeformation ? input.uv4.xyz : input.positionOS.xyz);
float3 previousWS = TransformPreviousObjectToWorld(effectivePositionOS);

float4x4 previousOTW = GetPrevObjectToWorldMatrix();
float4x4 currentOTW = GetObjectToWorldMatrix();
if (!IsSmoothRotation(previousOTW._11_21_31, previousOTW._12_22_32, currentOTW._11_21_31, currentOTW._12_22_32))
{
output.prevPositionCS = output.curPositionCS;
}
else
{
output.prevPositionCS = TransformWorldToPrevHClip(previousWS);
}
output.prevPositionCS = TransformWorldToPrevHClip(previousWS);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,14 +892,15 @@ public static PassDescriptor MotionVectors(UniversalTarget target)

// Port Mask
validVertexBlocks = CoreBlockMasks.Vertex,
validPixelBlocks = CoreBlockMasks.FragmentAlphaOnly,

// Fields
structs = CoreStructCollections.Default,
requiredFields = CoreRequiredFields.MotionVectors,
fieldDependencies = CoreFieldDependencies.Default,

// Conditional State
renderStates = CoreRenderStates.Default,
renderStates = CoreRenderStates.DepthNormalsOnly(target),
pragmas = CorePragmas.DOTSInstanced,
includes = CoreIncludes.MotionVectors,
defines = new DefineCollection(),
Expand Down Expand Up @@ -1509,11 +1510,11 @@ static class CoreIncludes
public static readonly IncludeCollection MotionVectors = new IncludeCollection
{
// Pre-graph
{ CoreIncludes.CorePregraph },
{ CoreIncludes.ShaderGraphPregraph },
{ CorePregraph },
{ ShaderGraphPregraph },

// Post-graph
{ CoreIncludes.CorePostgraph },
{ CorePostgraph },
{ kMotionVectors, IncludeLocation.Postgraph },
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#if UNITY_EDITOR && ENABLE_VR && ENABLE_XR_MODULE

using System;
using System.Collections.Generic;
using UnityEngine.XR;

namespace UnityEngine.Rendering.Universal
{

public static class DebugOculusMotionVectorSettings
{
public static bool enableDebugMotionVectors;

public static RenderTexture motionVectorRenderTarget;
public static RenderTextureDescriptor motionVectorRenderTargetDesc;
public static RenderTexture depthRenderTarget;
public static RenderTextureDescriptor depthRenderTargetDesc;
}

}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ public class OculusMotionVectorPass : ScriptableRenderPass
FilteringSettings m_FilteringSettings;
ProfilingSampler m_ProfilingSampler;

private static readonly ShaderTagId s_MotionVectorTag = new ShaderTagId("MotionVectors");

RenderTargetIdentifier motionVectorColorIdentifier;
RenderTargetIdentifier motionVectorDepthIdentifier;

private static readonly int s_MotionVectorsEnabledID = Shader.PropertyToID("_MotionVectorsEnabled");

public OculusMotionVectorPass(string profilerTag, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
{
base.profilingSampler = new ProfilingSampler(nameof(OculusMotionVectorPass));
Expand Down Expand Up @@ -57,10 +61,16 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
cmd.Clear();

Camera camera = renderingData.cameraData.camera;
Shader.SetGlobalFloat(s_MotionVectorsEnabledID, (float)(camera.depthTextureMode & DepthTextureMode.MotionVectors));

var filterSettings = m_FilteringSettings;

var drawSettings = CreateDrawingSettings(new ShaderTagId("MotionVectors"), ref renderingData, renderingData.cameraData.defaultOpaqueSortFlags);
var drawSettings = CreateDrawingSettings(s_MotionVectorTag, ref renderingData, renderingData.cameraData.defaultOpaqueSortFlags);
drawSettings.perObjectData = PerObjectData.MotionVectors;

context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings);
drawSettings.perObjectData = PerObjectData.None;
filterSettings.excludeMotionVectorObjects = true;
context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filterSettings);
}
context.ExecuteCommandBuffer(cmd);
Expand Down
Loading