Skip to content

Commit

Permalink
expanded vrrig material array, store custom material at the end dynam…
Browse files Browse the repository at this point in the history
…ically. untested
  • Loading branch information
AHauntedArmy committed Feb 28, 2022
1 parent d1b64d5 commit 8c063e4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
26 changes: 20 additions & 6 deletions GorillaCosmetics/CustomCosmeticsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace GorillaCosmetics
{
public class CustomCosmeticsController : MonoBehaviour, ICustomCosmeticsController
{
public int MatIndex { get; private set; }

public GorillaHat CurrentHat { get; private set; }

public GorillaMaterial CurrentMaterial { get; private set; }
Expand All @@ -24,6 +26,7 @@ void Start()
{
rig = GetComponent<VRRig>();
defaultMaterial = rig.mainSkin.material;
MatIndex = rig.materialsToChangeTo.Length - 1;

Player player = rig.photonView?.Owner;
if (player != null)
Expand Down Expand Up @@ -96,18 +99,29 @@ public void ResetMaterial()
CurrentMaterial = null;
}

public void SetColor(float red, float blue, float green)
public void SetColor(float red, float green, float blue)
{
Plugin.Log($"Player: {NickName} changing color to {red}, {blue}, {green}");
defaultMaterial.color = new Color(red, blue, green);
Plugin.Log($"Player: {NickName} changing color to {red}, {green}, {blue}");

Color newColor = new Color(red, green, blue);
defaultMaterial.color = newColor;

if (CurrentMaterial != null)
{
Material myMat = CurrentMaterial.GetMaterial();
if(myMat != null && myMat.HasProperty("_Color"))
{
myMat.color = newColor;
}
}
}

void SetVRRigMaterial(Material material)
{
rig.materialsToChangeTo[0] = material;
if (rig.currentMatIndex == 0)
rig.materialsToChangeTo[MatIndex] = material;
if (rig.currentMatIndex == MatIndex)
{
rig.ChangeMaterialLocal(0);
rig.ChangeMaterialLocal(MatIndex);
}

rig.InitializeNoobMaterialLocal(defaultMaterial.color.r, defaultMaterial.color.g, defaultMaterial.color.b);
Expand Down
18 changes: 18 additions & 0 deletions GorillaCosmetics/HarmonyPatches/Patches/ChangeMaterialPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using HarmonyLib;

namespace GorillaCosmetics.HarmonyPatches.Patches
{
[HarmonyPatch(typeof(VRRig))]
[HarmonyPatch("ChangeMaterialLocal", MethodType.Normal)]
internal class ChangeMaterialPatch
{
internal static void Prefix(VRRig __instance, ref int materialIndex)
{
var controller = __instance.gameObject.GetComponent<ICustomCosmeticsController>();
if (controller != null && materialIndex == 0)
{
materialIndex = controller.MatIndex;
}
}
}
}
21 changes: 21 additions & 0 deletions GorillaCosmetics/HarmonyPatches/Patches/ColorPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,33 @@ internal class ColorPatch
internal static bool Prefix(VRRig __instance, float red, float green, float blue)
{
var controller = __instance.gameObject.GetComponent<ICustomCosmeticsController>();
if (controller == null)
{
return true;
}

controller.SetColor(red, green, blue);

Photon.Pun.PhotonView photView = __instance.photonView;
if (photView != null)
{
__instance.playerText.text = __instance.NormalizeName(true, photView.Owner.NickName);
}
else if (__instance.showName)
{
__instance.playerText.text = PlayerPrefs.GetString("playerName");
}

return false;

/*
if (controller != null)
{
controller.SetColor(red, green, blue);
}
var boolean = controller?.CurrentMaterial?.Descriptor.CustomColors ?? true;
return boolean;
*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ internal static void Postfix(VRRig __instance)
Photon.Realtime.Player player = __instance.photonView?.Owner;

Plugin.Log($"GorillaCosmetics: Creating CustomCosmeticsController for {player?.NickName ?? "SELF"}");

var tempMatArray = __instance.materialsToChangeTo;
__instance.materialsToChangeTo = new Material[tempMatArray.Length + 1];

for (int index = 0; index < tempMatArray.Length; index++) {
__instance.materialsToChangeTo[index] = tempMatArray[index];
}

__instance.materialsToChangeTo[__instance.materialsToChangeTo.Length - 1] = tempMatArray[0];

__instance.gameObject.AddComponent<CustomCosmeticsController>();
}
}
Expand Down
3 changes: 2 additions & 1 deletion GorillaCosmetics/ICustomCosmeticsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace GorillaCosmetics
{
public interface ICustomCosmeticsController
{
int MatIndex { get; }
GorillaHat CurrentHat { get; }
GorillaMaterial CurrentMaterial { get; }

void SetHat(GorillaHat hat);
void ResetHat();
void SetMaterial(GorillaMaterial material);
void ResetMaterial();
void SetColor(float red, float blue, float green);
void SetColor(float red, float green, float blue);
}
}

0 comments on commit 8c063e4

Please sign in to comment.