render stencil portals (stereo)

This commit is contained in:
2023-03-15 13:47:11 +01:00
parent 52fa079fce
commit c024ddfe5e
11 changed files with 578 additions and 643 deletions

View File

@@ -68,6 +68,16 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 419816428230682305, guid: c84e0198f66a41b4294f6e8e585192a4,
type: 3}
propertyPath: m_TrackingStateInput.m_Action.m_Id
value: 93c97b32-72bd-4ec9-8a00-456b65f0e211
objectReference: {fileID: 0}
- target: {fileID: 419816428230682305, guid: c84e0198f66a41b4294f6e8e585192a4,
type: 3}
propertyPath: m_TrackingStateInput.m_Action.m_Name
value: Tracking State Input
objectReference: {fileID: 0}
- target: {fileID: 419816428804372196, guid: c84e0198f66a41b4294f6e8e585192a4,
type: 3}
propertyPath: m_TrackingStateInput.m_Action.m_Id
@@ -78,6 +88,16 @@ PrefabInstance:
propertyPath: m_TrackingStateInput.m_Action.m_Name
value: Tracking State Input
objectReference: {fileID: 0}
- target: {fileID: 419816428871882042, guid: c84e0198f66a41b4294f6e8e585192a4,
type: 3}
propertyPath: m_TrackingStateInput.m_Action.m_Id
value: 6690d24e-8ae7-40dc-90bb-0c220c159514
objectReference: {fileID: 0}
- target: {fileID: 419816428871882042, guid: c84e0198f66a41b4294f6e8e585192a4,
type: 3}
propertyPath: m_TrackingStateInput.m_Action.m_Name
value: Tracking State Input
objectReference: {fileID: 0}
- target: {fileID: 419816429261119706, guid: c84e0198f66a41b4294f6e8e585192a4,
type: 3}
propertyPath: m_ModelPrefab

View File

@@ -1,140 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Portal
m_Shader: {fileID: 4800000, guid: 732968a50c15e194ba1e39cf62976ac1, type: 3}
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 1
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _LeftTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _RightTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _Texture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AlphaClip: 0
- _Blend: 0
- _BlendOp: 0
- _BumpScale: 1
- _ClearCoatMask: 0
- _ClearCoatSmoothness: 0
- _Cull: 2
- _Cutoff: 0.5
- _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _EnvironmentReflections: 1
- _GlossMapScale: 0
- _Glossiness: 0
- _GlossyReflections: 0
- _Metallic: 0
- _OcclusionStrength: 1
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _SampleGI: 0
- _Smoothness: 0.5
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _Surface: 0
- _WorkflowMode: 1
- _ZTest: 4
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []
--- !u!114 &8905466428277890777
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 5

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 603cda73335499d4980d9b171eaffe0d
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,68 +0,0 @@
Shader "Escape Room Engine/Portal"
{
Properties
{
_LeftTex("Left Eye Texture", 2D) = "" {}
_RightTex("Right Eye Texture", 2D) = "" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
// stencil the portal surface to cut it out from the portal frame
Stencil
{
Ref 1
Pass replace
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float4 positionScreen : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
uniform sampler2D _LeftTex, _RightTex;
Varyings vert(Attributes IN)
{
Varyings OUT;
UNITY_SETUP_INSTANCE_ID(IN);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
// calculate the screen position which is used to map the portal texture onto the portal
OUT.positionScreen = ComputeScreenPos(OUT.positionHCS);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
const float2 uv = IN.positionScreen.xy / IN.positionScreen.w;
// sample from the correct texture depending on the eye rendered
return unity_StereoEyeIndex == 0 ? tex2D(_LeftTex, uv) : tex2D(_RightTex, uv);
}
ENDHLSL
}
}
}

View File

@@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: 732968a50c15e194ba1e39cf62976ac1
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,6 +4,7 @@
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:15fc0a57446b3144c949da3e2b9737a9",
"GUID:df380645f10b7bc4b97d4f5eb6303d95",
"GUID:fe685ec1767f73d42b749ea8045bfe43",
"GUID:776d03a35f1b52c4a9aed9f56d7b4229",
"GUID:8804073475ba36c47830e8e19dc699ce",

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using EscapeRoomEngine.VR.Runtime;
using NaughtyAttributes;
using UnityEngine;
@@ -87,12 +88,28 @@ namespace EscapeRoomEngine.Portal.Runtime
}
}
public Camera SetUpCamera(Camera playerCamera)
public Camera SetUpCamera(Camera playerCamera, Camera.MonoOrStereoscopicEye monoOrEye)
{
// place camera
var m = portalTransform.localToWorldMatrix * HalfRotation *
linkedPortal.portalTransform.worldToLocalMatrix * playerCamera.transform.localToWorldMatrix;
playerCamera.transform.SetPositionAndRotation(m.GetPosition(), m.rotation);
Matrix4x4 cameraTransform;
if (monoOrEye == Camera.MonoOrStereoscopicEye.Mono)
{
cameraTransform = portalTransform.localToWorldMatrix * HalfRotation *
linkedPortal.portalTransform.worldToLocalMatrix *
playerCamera.transform.localToWorldMatrix;
}
else
{
// stereoRenderCounter = (stereoRenderCounter + 1) % 2;
var eye = monoOrEye == Camera.MonoOrStereoscopicEye.Left
? Camera.StereoscopicEye.Left
: Camera.StereoscopicEye.Right;
cameraTransform = portalTransform.localToWorldMatrix * HalfRotation *
linkedPortal.portalTransform.worldToLocalMatrix *
Player.Instance.GetEye(eye).localToWorldMatrix;
playerCamera.projectionMatrix = playerCamera.GetStereoProjectionMatrix(eye);
}
playerCamera.transform.SetPositionAndRotation(cameraTransform.GetPosition(), cameraTransform.rotation);
// set camera clip plane to portal (otherwise the wall behind the portal would be rendered)
// calculating the clip plane: https://computergraphics.stackexchange.com/a/1506

View File

@@ -16,9 +16,9 @@ namespace EscapeRoomEngine.Portal.Runtime
private class PortalCameraRenderPass : ScriptableRenderPass
{
private static readonly List<ShaderTagId> ShaderTagIds = new List<ShaderTagId>
private static readonly List<ShaderTagId> ShaderTagIds = new()
{
new("SRPDefaultUnlit"), new("UniversalForward"), new("UniversalForwardOnly")
new ShaderTagId("SRPDefaultUnlit"), new ShaderTagId("UniversalForward"), new ShaderTagId("UniversalForwardOnly")
};
private RenderStateBlock _renderStateBlock;
@@ -74,8 +74,15 @@ namespace EscapeRoomEngine.Portal.Runtime
var originalPosition = cameraTransform.position;
var originalRotation = cameraTransform.rotation;
var originalProjection = camera.projectionMatrix;
camera = linkedPortal.SetUpCamera(camera);
RenderingUtils.SetViewAndProjectionMatrices(_commandBuffer, camera.worldToCameraMatrix, GL.GetGPUProjectionMatrix(camera.projectionMatrix, true), false);
camera = linkedPortal.SetUpCamera(camera,
camera.stereoEnabled
? cameraData.xr.multipassId == 0
? Camera.MonoOrStereoscopicEye.Left
: Camera.MonoOrStereoscopicEye.Right
: Camera.MonoOrStereoscopicEye.Mono);
RenderingUtils.SetViewAndProjectionMatrices(_commandBuffer,
camera.worldToCameraMatrix,
GL.GetGPUProjectionMatrix(camera.projectionMatrix, true), false);
// execute command buffer
context.ExecuteCommandBuffer(_commandBuffer);

File diff suppressed because it is too large Load Diff

View File

@@ -84,7 +84,7 @@ MonoBehaviour:
- {fileID: 8829524859106569533}
- {fileID: -2735733284642257967}
- {fileID: -1692072991545479379}
m_renderMode: 1
m_renderMode: 0
m_depthSubmissionMode: 0
--- !u!114 &-4179021495097340258
MonoBehaviour:

View File

@@ -46,4 +46,3 @@ MonoBehaviour:
m_AutomaticRunning: 0
m_Loaders:
- {fileID: 11400000, guid: dfe61e10b085aa149bce80b468d7fab2, type: 2}
- {fileID: 11400000, guid: df4ed463f699a594593b8eb5ed412658, type: 2}