Skip to content

Commit ad95776

Browse files
committed
rp: Add material adapter formerly in patcher.
1 parent e1978df commit ad95776

6 files changed

+137
-5
lines changed

Runtime/HDRenderPipeline.cs renamed to Runtime/HighDefinitionRenderPipeline.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
// ReSharper disable UnusedType.Global
18+
// ReSharper disable CheckNamespace
19+
1720
namespace VisualPinball.Unity.Hdrp
1821
{
19-
public class HDRenderPipeline : IRenderPipeline
22+
public class HighDefinitionRenderPipeline : IRenderPipeline
2023
{
2124
public string Name { get; } = "High Definition Render Pipeline";
2225

2326
public RenderPipelineType Type { get; } = RenderPipelineType.Hdrp;
2427
public IMaterialConverter MaterialConverter { get; }
28+
public IMaterialAdapter MaterialAdapter { get; }
2529
public ILightConverter LightConverter { get; }
2630

27-
public HDRenderPipeline()
31+
public HighDefinitionRenderPipeline()
2832
{
2933
MaterialConverter = new MaterialConverter();
3034
LightConverter = new LightConverter();
35+
MaterialAdapter = new MaterialAdapter();
3136
}
3237
}
3338
}
File renamed without changes.

Runtime/LightConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ public class LightConverter : ILightConverter
2828
{
2929
public void UpdateLight(Light light, LightData data)
3030
{
31-
// Set color and position
31+
// retrieve hdrp light
3232
var hdLight = light.GetComponent<HDAdditionalLightData>();
3333
if (hdLight == null) {
3434
hdLight = light.gameObject.AddComponent<HDAdditionalLightData>();
3535
HDAdditionalLightData.InitDefaultHDAdditionalLightData(hdLight);
3636
}
37+
38+
// color and position
3739
hdLight.color = data.Color2.ToUnityColor();
3840
hdLight.intensity = data.Intensity / 4f;
3941
hdLight.range = data.Falloff * 0.001f;
@@ -42,7 +44,6 @@ public void UpdateLight(Light light, LightData data)
4244
light.transform.localPosition = new Vector3(0f, 0f, 25f);
4345

4446
hdLight.EnableShadows(false);
45-
4647
}
4748
}
4849
}

Runtime/MaterialAdapter.cs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

Runtime/MaterialAdapter.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/VisualPinball.Engine.Unity.Hdrp.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"Unity.RenderPipelines.HighDefinition.Runtime",
55
"Unity.RenderPipelines.HighDefinition.Config.Runtime",
66
"VisualPinball.Engine",
7-
"VisualPinball.Unity"
7+
"VisualPinball.Unity",
8+
"VisualPinball.Unity.Patcher"
89
],
910
"includePlatforms": [],
1011
"excludePlatforms": [],

0 commit comments

Comments
 (0)