From ad7537c4cc81f5e430e5c69994da0dfc57ebcf5d Mon Sep 17 00:00:00 2001 From: yum Date: Sat, 8 Mar 2025 19:28:44 -0800 Subject: sketch out faceme feature --- 2ner.cginc | 10 +++++++++ 2ner.shader | 7 +++++++ cnlohr.cginc | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ face_me.cginc | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ features.cginc | 4 ++++ globals.cginc | 7 +++++++ 6 files changed, 154 insertions(+) create mode 100644 cnlohr.cginc create mode 100644 face_me.cginc diff --git a/2ner.cginc b/2ner.cginc index 4b04158..548ec87 100644 --- a/2ner.cginc +++ b/2ner.cginc @@ -5,6 +5,7 @@ #include "UnityLightingCommon.cginc" #include "eyes.cginc" +#include "face_me.cginc" #include "features.cginc" #include "globals.cginc" #include "interpolators.cginc" @@ -23,24 +24,28 @@ v2f vert(appdata v) { #endif #if defined(MASKED_STENCIL1_PASS) float masked_stencil1_mask = _Masked_Stencil1_Mask.SampleLevel(linear_repeat_s, v.uv01, 0); + [branch] if (masked_stencil1_mask < 0.5) { return (v2f) (0.0/0.0); } #endif #if defined(MASKED_STENCIL2_PASS) float masked_stencil2_mask = _Masked_Stencil2_Mask.SampleLevel(linear_repeat_s, v.uv01, 0); + [branch] if (masked_stencil2_mask < 0.5) { return (v2f) (0.0/0.0); } #endif #if defined(MASKED_STENCIL3_PASS) float masked_stencil3_mask = _Masked_Stencil3_Mask.SampleLevel(linear_repeat_s, v.uv01, 0); + [branch] if (masked_stencil3_mask < 0.5) { return (v2f) (0.0/0.0); } #endif #if defined(MASKED_STENCIL4_PASS) float masked_stencil4_mask = _Masked_Stencil4_Mask.SampleLevel(linear_repeat_s, v.uv01, 0); + [branch] if (masked_stencil4_mask < 0.5) { return (v2f) (0.0/0.0); } @@ -57,6 +62,7 @@ v2f vert(appdata v) { UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); #if defined(OUTLINE_PASS) + [branch] if (!_Outlines_Enabled_Dynamic) { return (v2f) (0.0/0.0); } @@ -71,6 +77,10 @@ v2f vert(appdata v) { v.tangent *= -1; #endif // OUTLINE_PASS +#if defined(_FACE_ME) + face_me(v); +#endif + #if defined(_VERTEX_DOMAIN_WARPING) float3 basePos = v.vertex.xyz; float offset = sin(_Time[0] * _Vertex_Domain_Warping_Speed) * diff --git a/2ner.shader b/2ner.shader index ea84e4d..ad29182 100644 --- a/2ner.shader +++ b/2ner.shader @@ -317,6 +317,13 @@ Shader "yum_food/2ner" //endex [HideInInspector] m_end_Decals("Decals", Float) = 0 + //ifex _Face_Me_Enabled==0 + [HideInInspector] m_start_Face_Me("Face me", Float) = 0 + [ThryToggle(_FACE_ME)] _Face_Me_Enabled("Enable", Float) = 0 + [MaterialToggle] _Face_Me_Enabled_Dynamic("Enable dynamic", Float) = 0 + [HideInInspector] m_end_Face_Me("Face me", Float) = 0 + //endex + //ifex _Vertex_Domain_Warping_Enabled==0 [HideInInspector] m_start_Vertex_Domain_Warping("Vertex domain warping", Float) = 0 [ThryToggle(_VERTEX_DOMAIN_WARPING)]_Vertex_Domain_Warping_Enabled("Enable", Float) = 0 diff --git a/cnlohr.cginc b/cnlohr.cginc new file mode 100644 index 0000000..52aca35 --- /dev/null +++ b/cnlohr.cginc @@ -0,0 +1,61 @@ +#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) || defined(UNITY_SINGLE_PASS_STEREO) + return (unity_StereoWorldSpaceCameraPos[0] + unity_StereoWorldSpaceCameraPos[1]) / 2; +#else + return isMirror() ? _VRChatMirrorCameraPos : _WorldSpaceCameraPos.xyz; +#endif +} + +bool isVR() { +#if defined(USING_STEREO_MATRICES) + return true; +#else + return false; +#endif +} + +#endif // __CNLOHR_INC diff --git a/face_me.cginc b/face_me.cginc new file mode 100644 index 0000000..f31b736 --- /dev/null +++ b/face_me.cginc @@ -0,0 +1,65 @@ +#ifndef __FACE_ME_INC +#define __FACE_ME_INC + +#include "cnlohr.cginc" +#include "interpolators.cginc" + +// Rotate the object's position and normal so that it always faces the camera. +void face_me(inout appdata v) { + [branch] + if (_Face_Me_Enabled_Dynamic) { + float3 object_center = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)); + // Get forward axis of object coordinate system, i.e. the orientation of + // the hip bone. + // Then project it onto the xz plane. + float3 forward_axis = mul(unity_ObjectToWorld, float3(0, 0, 1)); + forward_axis.y = 0; + forward_axis = normalize(forward_axis); + float4 worldPos = mul(unity_ObjectToWorld, v.vertex); + float3 rd = normalize(object_center - getCenterCamPos()); + // We apply a factor of -1 to shift the result forward by a phase shift of pi. + float cos_t = -dot(normalize(rd.xz), forward_axis.xz); + // We want to get sin(t) using the identity: + // || a x b || = || a || || b || sin(t) + // For normal vectors, this simplifies to: + // || a x b || = sin(t) + // The issue is that the norm operator loses the sign. + // We can estimate the sign by assuming that `rd` and `forward_axis` are on + // the xz plane. + // If that's the case, then the cross product is necessarily constrained to + // the y axis. + float sin_t_sign = sign(cross(rd, forward_axis).y); + // Here we use the identity: + // sin(t) = sqrt(1 - cos(t)^2) + // We simply apply the sign correction `sin_t_sign` to the result. + // We then invert it, since the goal is not to amplify the rotation, but + // to negate it. + // Finally, we add a phase correction to make the abomination face us. + float sin_t = -sqrt(1 - cos_t * cos_t) * sin_t_sign; + + // Double the angle using double-angle formulas + if (isVR()) { + float sin_2t = 2 * sin_t * cos_t; + float cos_2t = cos_t * cos_t - sin_t * sin_t; // or: 2 * cos_t * cos_t - 1 + sin_t = sin_2t; + cos_t = cos_2t; + } + + // Use the doubled angle values in your rotation matrix + float2x2 face_me_rot = float2x2(cos_t, -sin_t, sin_t, cos_t); + float2x2 face_me_rot_inv = float2x2(cos_t, sin_t, -sin_t, cos_t); + worldPos.xz = mul(face_me_rot, (worldPos.xz - object_center.xz)) + object_center.xz; + v.vertex = mul(unity_WorldToObject, worldPos); + + float3 world_normal = UnityObjectToWorldNormal(v.normal); + world_normal.xz = mul(face_me_rot_inv, world_normal.xz); + v.normal = normalize(mul(unity_WorldToObject, world_normal)); + + float3 world_tangent = UnityObjectToWorldDir(v.tangent.xyz); + world_tangent.xz = mul(face_me_rot_inv, world_tangent.xz); + v.tangent.xyz = normalize(mul(unity_WorldToObject, world_tangent)); + } +} + +#endif // __FACE_ME_INC + diff --git a/features.cginc b/features.cginc index 820c045..3e554cf 100644 --- a/features.cginc +++ b/features.cginc @@ -98,6 +98,10 @@ #pragma shader_feature_local _RIM_LIGHTING3_QUANTIZATION //endex +//ifex _Face_Me_Enabled==0 +#pragma shader_feature_local _FACE_ME +//endex + //ifex _Decal0_Enabled==0 #pragma shader_feature_local _DECAL0 #pragma shader_feature_local _DECAL0_NORMAL diff --git a/globals.cginc b/globals.cginc index a75f747..253c78e 100644 --- a/globals.cginc +++ b/globals.cginc @@ -16,6 +16,13 @@ half _BumpScale; float _BumpShadowHeightScale; float _BumpShadowHardness; +float _VRChatMirrorMode; +float3 _VRChatMirrorCameraPos; + +#if defined(_FACE_ME) +float _Face_Me_Enabled_Dynamic; +#endif + #if defined(_ALPHA_MULTIPLIER) float _Alpha_Multiplier; #endif -- cgit v1.2.3