|
| 1 | +// Visual Pinball Engine |
| 2 | +// Copyright (C) 2020 freezy and VPE Team |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +// ReSharper disable StringLiteralTypo |
| 18 | +// ReSharper disable CheckNamespace |
| 19 | + |
| 20 | +using UnityEngine; |
| 21 | + |
| 22 | +namespace VisualPinball.Unity.Hdrp |
| 23 | +{ |
| 24 | + public class MaterialAdapter : IMaterialAdapter |
| 25 | + { |
| 26 | + #region Shader Properties |
| 27 | + |
| 28 | + private static readonly int SurfaceType = Shader.PropertyToID("_SurfaceType"); |
| 29 | + private static readonly int DstBlend = Shader.PropertyToID("_DstBlend"); |
| 30 | + private static readonly int ZWrite = Shader.PropertyToID("_ZWrite"); |
| 31 | + private static readonly int DoubleSidedEnable = Shader.PropertyToID("_DoubleSidedEnable"); |
| 32 | + private static readonly int DoubleSidedNormalMode = Shader.PropertyToID("_DoubleSidedNormalMode"); |
| 33 | + private static readonly int CullMode = Shader.PropertyToID("_CullMode"); |
| 34 | + private static readonly int CullModeForward = Shader.PropertyToID("_CullModeForward"); |
| 35 | + private static readonly int TransparentDepthPrepassEnable = Shader.PropertyToID("_TransparentDepthPrepassEnable"); |
| 36 | + private static readonly int AlphaDstBlend = Shader.PropertyToID("_AlphaDstBlend"); |
| 37 | + private static readonly int ZTestModeDistortion = Shader.PropertyToID("_ZTestModeDistortion"); |
| 38 | + private static readonly int AlphaCutoff = Shader.PropertyToID("_AlphaCutoff"); |
| 39 | + private static readonly int AlphaCutoffEnable = Shader.PropertyToID("_AlphaCutoffEnable"); |
| 40 | + private static readonly int NormalMap = Shader.PropertyToID("_NormalMap"); |
| 41 | + private static readonly int Metallic = Shader.PropertyToID("_Metallic"); |
| 42 | + |
| 43 | + #endregion |
| 44 | + |
| 45 | + public void SetOpaque(GameObject gameObject) |
| 46 | + { |
| 47 | + var material = gameObject.GetComponent<Renderer>().sharedMaterial; |
| 48 | + |
| 49 | + material.SetFloat(SurfaceType, 0); |
| 50 | + |
| 51 | + material.SetFloat(DstBlend, 0); |
| 52 | + material.SetFloat(ZWrite, 1); |
| 53 | + |
| 54 | + material.DisableKeyword("_ALPHATEST_ON"); |
| 55 | + material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT"); |
| 56 | + material.DisableKeyword("_BLENDMODE_PRE_MULTIPLY"); |
| 57 | + material.DisableKeyword("_BLENDMODE_PRESERVE_SPECULAR_LIGHTING"); |
| 58 | + } |
| 59 | + |
| 60 | + public void SetDoubleSided(GameObject gameObject) |
| 61 | + { |
| 62 | + var material = gameObject.GetComponent<Renderer>().sharedMaterial; |
| 63 | + |
| 64 | + material.EnableKeyword("_DOUBLESIDED_ON"); |
| 65 | + material.EnableKeyword("_NORMALMAP_TANGENT_SPACE"); |
| 66 | + |
| 67 | + material.SetInt(DoubleSidedEnable, 1); |
| 68 | + material.SetInt(DoubleSidedNormalMode, 1); |
| 69 | + |
| 70 | + material.SetInt(CullMode, 0); |
| 71 | + material.SetInt(CullModeForward, 0); |
| 72 | + } |
| 73 | + |
| 74 | + public void SetTransparentDepthPrepassEnabled(GameObject gameObject) |
| 75 | + { |
| 76 | + var material = gameObject.GetComponent<Renderer>().sharedMaterial; |
| 77 | + |
| 78 | + material.EnableKeyword("_DISABLE_SSR_TRANSPARENT"); |
| 79 | + |
| 80 | + material.SetInt(TransparentDepthPrepassEnable, 1); |
| 81 | + material.SetInt(AlphaDstBlend, 10); |
| 82 | + material.SetInt(ZTestModeDistortion, 4); |
| 83 | + |
| 84 | + material.SetShaderPassEnabled("TransparentDepthPrepass", true); |
| 85 | + material.SetShaderPassEnabled("RayTracingPrepass", true); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + public void SetAlphaCutOff(GameObject gameObject, float value) |
| 90 | + { |
| 91 | + var material = gameObject.GetComponent<Renderer>().sharedMaterial; |
| 92 | + |
| 93 | + // enable the property |
| 94 | + SetAlphaCutOffEnabled(gameObject); |
| 95 | + |
| 96 | + // set the cut-off value |
| 97 | + material.SetFloat(AlphaCutoff, value); |
| 98 | + } |
| 99 | + |
| 100 | + public void SetAlphaCutOffEnabled(GameObject gameObject) |
| 101 | + { |
| 102 | + var material = gameObject.GetComponent<Renderer>().sharedMaterial; |
| 103 | + |
| 104 | + material.EnableKeyword("_ALPHATEST_ON"); |
| 105 | + material.SetInt(AlphaCutoffEnable, 1); |
| 106 | + } |
| 107 | + |
| 108 | + public void SetNormalMapDisabled(GameObject gameObject) |
| 109 | + { |
| 110 | + var material = gameObject.GetComponent<Renderer>().sharedMaterial; |
| 111 | + |
| 112 | + material.SetTexture(NormalMap, null); |
| 113 | + material.DisableKeyword("_NORMALMAP"); |
| 114 | + } |
| 115 | + |
| 116 | + public void SetMetallic(GameObject gameObject, float value) |
| 117 | + { |
| 118 | + var material = gameObject.GetComponent<Renderer>().sharedMaterial; |
| 119 | + material.SetFloat(Metallic, value); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments