summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2024-09-12 09:02:27 -0700
committeryum <yum.food.vr@gmail.com>2024-09-12 09:02:27 -0700
commitc6705bf773143da77cb5a3bde4cd06f2679ea0f3 (patch)
tree0242f20ed53cc42da51ec222322aa1bcb0fd1fd0
parentdfb1ece3e78cf013866d18a3a234e2f4bf8834f8 (diff)
Replace all instances of _WorldSpaceCameraPos with center eye pos
Fixes left/right eye mismatch in VR.
-rw-r--r--cnlohr.cginc53
-rw-r--r--globals.cginc3
-rw-r--r--interpolators.cginc12
-rw-r--r--pbr.cginc4
-rw-r--r--tooner.shader2
-rw-r--r--tooner_lighting.cginc22
-rw-r--r--tooner_outline_pass.cginc2
7 files changed, 76 insertions, 22 deletions
diff --git a/cnlohr.cginc b/cnlohr.cginc
new file mode 100644
index 0000000..4df0ef1
--- /dev/null
+++ b/cnlohr.cginc
@@ -0,0 +1,53 @@
+#ifndef __CNLOHR_INC
+#define __CNLOHR_INC
+
+#include "globals.cginc"
+
+/*
+ * MIT License
+ *
+ * NOTE: Much content here is originally from others. Content in third party
+ * folder may not be fully MIT-licensable.
+ *
+ * Copyright (c) 2021 cnlohr, et. al.
+ *
+ * All other content in this repository falls under the following terms:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE
+ * SOFTWARE.
+ */
+
+// Source:
+// https://github.com/cnlohr/shadertrixx?tab=readme-ov-file#eye-center-position
+bool isMirror() { return _VRChatMirrorMode != 0; }
+
+// Source:
+// https://github.com/cnlohr/shadertrixx?tab=readme-ov-file#eye-center-position
+float3 getCenterCamPos() {
+#if defined(USING_STEREO_MATRICES)
+ return (unity_StereoWorldSpaceCameraPos[0] + unity_StereoWorldSpaceCameraPos[1]) / 2;
+#else
+ return isMirror() ? _VRChatMirrorCameraPos : _WorldSpaceCameraPos.xyz;
+#endif
+}
+
+#endif // __CNLOHR_INC
diff --git a/globals.cginc b/globals.cginc
index 527f13b..99db1e7 100644
--- a/globals.cginc
+++ b/globals.cginc
@@ -46,6 +46,9 @@ float3 shadowedReflections;
float3 _ReflShadows;
float3 _ReflShadowStrength;
+float _VRChatMirrorMode;
+float3 _VRChatMirrorCameraPos;
+
#if defined(_DISCARD)
float _Discard_Enable_Dynamic;
#endif
diff --git a/interpolators.cginc b/interpolators.cginc
index 37a544b..df645de 100644
--- a/interpolators.cginc
+++ b/interpolators.cginc
@@ -38,9 +38,7 @@ struct v2f
float3 worldPos : TEXCOORD9;
float3 normal : TEXCOORD10;
float3 objPos : TEXCOORD11;
- #if defined(SSR_ENABLED)
- float4 screenPos : TEXCOORD12;
- #endif
+ float3 centerCamPos : TEXCOORD12;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
@@ -83,13 +81,11 @@ struct v2f
float4 tangent : TEXCOORD10;
float3 worldPos : TEXCOORD11;
float3 objPos : TEXCOORD12;
+ float3 centerCamPos : TEXCOORD13;
- SHADOW_COORDS(13)
+ SHADOW_COORDS(14)
#if defined(VERTEXLIGHT_ON)
- float3 vertexLightColor : TEXCOORD14;
- #endif
- #if defined(SSR_ENABLED)
- float4 screenPos : TEXCOORD15;
+ float3 vertexLightColor : TEXCOORD15;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
diff --git a/pbr.cginc b/pbr.cginc
index 9f5600d..ab49e30 100644
--- a/pbr.cginc
+++ b/pbr.cginc
@@ -187,7 +187,7 @@ float4 getLitColor(
albedo.rgb = DiffuseAndSpecularFromMetallic(
albedo, metallic, specular_tint, one_minus_reflectivity);
- const float3 view_dir = normalize(_WorldSpaceCameraPos - worldPos);
+ const float3 view_dir = normalize(i.centerCamPos - worldPos);
uint normals_mode = round(_Mesh_Normals_Mode);
switch (normals_mode) {
case 0:
@@ -270,7 +270,7 @@ float4 getLitColor(
#if defined(_PROXIMITY_DIMMING)
{
- float cam_dist = length(_WorldSpaceCameraPos - worldPos);
+ float cam_dist = length(i.centerCamPos - worldPos);
// Map onto [min, max]
cam_dist = clamp(cam_dist, _Proximity_Dimming_Min_Dist,
_Proximity_Dimming_Max_Dist);
diff --git a/tooner.shader b/tooner.shader
index 2cdfde7..661b578 100644
--- a/tooner.shader
+++ b/tooner.shader
@@ -559,7 +559,6 @@ Shader "yum_food/tooner"
#include "tooner_lighting.cginc"
ENDCG
}
- /*
Pass {
Tags {
"RenderType"="Opaque"
@@ -639,7 +638,6 @@ Shader "yum_food/tooner"
#include "mochie_shadow_caster.cginc"
ENDCG
}
- */
}
CustomEditor "ToonerGUI"
}
diff --git a/tooner_lighting.cginc b/tooner_lighting.cginc
index 8c03644..5f75901 100644
--- a/tooner_lighting.cginc
+++ b/tooner_lighting.cginc
@@ -5,6 +5,7 @@
#include "audiolink.cginc"
#include "clones.cginc"
+#include "cnlohr.cginc"
#include "eyes.cginc"
#include "globals.cginc"
#include "halos.cginc"
@@ -29,7 +30,7 @@
void getVertexLightColor(inout v2f i)
{
#if defined(VERTEXLIGHT_ON)
- float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);
+ float3 view_dir = normalize(i.centerCamPos - i.worldPos);
uint normals_mode = round(_Mesh_Normals_Mode);
bool flat = (normals_mode == 0);
float3 flat_normal = normalize(
@@ -114,7 +115,7 @@ v2f vert(appdata v)
float3 n = UnityObjectToWorldNormal(unrotated_n);
float3 origin = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
- float3 view_dir = _WorldSpaceCameraPos - origin;
+ float3 view_dir = i.centerCamPos - origin;
// Project onto xz plane
n.y = 0;
view_dir.y = 0;
@@ -204,6 +205,7 @@ v2f vert(appdata v)
TRANSFER_SHADOW(o);
#endif
+ o.centerCamPos = getCenterCamPos();
getVertexLightColor(o);
return o;
@@ -463,7 +465,7 @@ RorschachPBR get_rorschach(float2 uv, RorschachParams p)
return result;
}
-float get_glitter(float2 uv, float3 worldPos,
+float get_glitter(float2 uv, float3 worldPos, float3 centerCamPos,
float3 normal, float density, float amount, float speed,
float mask, float angle, float power)
{
@@ -482,7 +484,7 @@ float get_glitter(float2 uv, float3 worldPos,
float glitter = result.albedo.r;
if (angle < 90) {
- float ndotl = abs(dot(normal, normalize(_WorldSpaceCameraPos.xyz - worldPos)));
+ float ndotl = abs(dot(normal, normalize(centerCamPos - worldPos)));
float cutoff = cos((angle / 180) * 3.14159);
glitter *= saturate(pow(ndotl / cutoff, power));
@@ -1241,7 +1243,7 @@ float4 pixellate_color(int2 px_res, float2 uv, float4 c)
float4 effect(inout v2f i)
{
- const float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);
+ const float3 view_dir = normalize(i.centerCamPos - i.worldPos);
// Not necessarily normalized after interpolation.
i.normal = normalize(i.normal);
i.tangent.xyz = normalize(i.tangent.xyz);
@@ -1660,7 +1662,7 @@ float4 effect(inout v2f i)
#if defined(_RIM_LIGHTING0_GLITTER)
float rl_glitter = get_glitter(
get_uv_by_channel(i, round(_Rim_Lighting0_Glitter_UV_Select)),
- i.worldPos, normal,
+ i.worldPos, i.centerCamPos, normal,
_Rim_Lighting0_Glitter_Density,
_Rim_Lighting0_Glitter_Amount, _Rim_Lighting0_Glitter_Speed,
/*mask=*/1, /*angle=*/91, /*power=*/1);
@@ -1740,7 +1742,7 @@ float4 effect(inout v2f i)
#if defined(_RIM_LIGHTING1_GLITTER)
float rl_glitter = get_glitter(
get_uv_by_channel(i, round(_Rim_Lighting1_Glitter_UV_Select)),
- i.worldPos, normal,
+ i.worldPos, i.centerCamPos, normal,
_Rim_Lighting1_Glitter_Density,
_Rim_Lighting1_Glitter_Amount, _Rim_Lighting1_Glitter_Speed,
/*mask=*/1, /*angle=*/91, /*power=*/1);
@@ -1820,7 +1822,7 @@ float4 effect(inout v2f i)
#if defined(_RIM_LIGHTING2_GLITTER)
float rl_glitter = get_glitter(
get_uv_by_channel(i, round(_Rim_Lighting2_Glitter_UV_Select)),
- i.worldPos, normal,
+ i.worldPos, i.centerCamPos, normal,
_Rim_Lighting2_Glitter_Density,
_Rim_Lighting2_Glitter_Amount, _Rim_Lighting2_Glitter_Speed,
/*mask=*/1, /*angle=*/91, /*power=*/1);
@@ -1900,7 +1902,7 @@ float4 effect(inout v2f i)
#if defined(_RIM_LIGHTING3_GLITTER)
float rl_glitter = get_glitter(
get_uv_by_channel(i, round(_Rim_Lighting3_Glitter_UV_Select)),
- i.worldPos, normal,
+ i.worldPos, i.centerCamPos, normal,
_Rim_Lighting3_Glitter_Density,
_Rim_Lighting3_Glitter_Amount, _Rim_Lighting3_Glitter_Speed,
/*mask=*/1, /*angle=*/91, /*power=*/1);
@@ -2025,7 +2027,7 @@ float4 effect(inout v2f i)
glitter_mask *= min(matcap_overwrite_mask[0], matcap_overwrite_mask[1]);
float glitter = get_glitter(
get_uv_by_channel(i, round(_Glitter_UV_Select)),
- i.worldPos, normal,
+ i.worldPos, i.centerCamPos, normal,
_Glitter_Density, _Glitter_Amount, _Glitter_Speed,
glitter_mask, _Glitter_Angle, _Glitter_Power);
glitter_color_unlit = glitter * _Glitter_Color;
diff --git a/tooner_outline_pass.cginc b/tooner_outline_pass.cginc
index f192cad..1c20c4a 100644
--- a/tooner_outline_pass.cginc
+++ b/tooner_outline_pass.cginc
@@ -5,6 +5,7 @@
#include "audiolink.cginc"
#include "clones.cginc"
+#include "cnlohr.cginc"
#include "globals.cginc"
#include "math.cginc"
#include "pbr.cginc"
@@ -86,6 +87,7 @@ v2f vert(appdata v)
o.lmuv = v.uv1 * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
+ o.centerCamPos = getCenterCamPos();
return o;
}