Skip to content

Commit 3de0313

Browse files
committed
api: Add diffusion profiles.
1 parent 9f115c6 commit 3de0313

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

Runtime/LightConverter.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public void SetColor(Light light, Color color)
5656
hdLight.color = color;
5757
}
5858
}
59+
public void SetShadow(Light light, bool enabled, bool isDynamic, float nearPlane = 0.01f)
60+
{
61+
var hdLight = light.GetComponent<HDAdditionalLightData>();
62+
if (hdLight != null) {
63+
hdLight.EnableShadows(enabled);
64+
hdLight.SetShadowNearPlane(nearPlane);
65+
hdLight.SetShadowUpdateMode(isDynamic ? ShadowUpdateMode.EveryFrame : ShadowUpdateMode.OnEnable);
66+
}
67+
}
5968

6069
public void SetIntensity(Light light, float intensityLumen)
6170
{
@@ -64,6 +73,13 @@ public void SetIntensity(Light light, float intensityLumen)
6473
hdLight.SetIntensity(intensityLumen, LightUnit.Lumen);
6574
}
6675
}
76+
public void SetTemperature(Light light, float temperature)
77+
{
78+
var hdLight = light.GetComponent<HDAdditionalLightData>();
79+
if (hdLight != null) {
80+
hdLight.SetColor(light.color, temperature);
81+
}
82+
}
6783

6884
public void SpotLight(Light light, float outer, float innerPercent)
6985
{

Runtime/MaterialConverter.cs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@
1919
// ReSharper disable CheckNamespace
2020

2121
using System;
22+
using System.Globalization;
23+
using System.Linq;
24+
using System.Reflection;
2225
using System.Text;
26+
using NLog;
27+
using UnityEditor;
2328
using UnityEngine;
2429
using VisualPinball.Engine.VPT;
2530
using VisualPinball.Unity;
31+
using Logger = NLog.Logger;
2632
using Material = UnityEngine.Material;
33+
using Object = UnityEngine.Object;
2734

2835
namespace VisualPinball.Engine.Unity.Hdrp
2936
{
@@ -43,9 +50,17 @@ public class MaterialConverter : IMaterialConverter
4350
private static readonly int NormalMap = Shader.PropertyToID("_NormalMap");
4451
private static readonly int UVChannelVertices = Shader.PropertyToID("_UVChannelVertices");
4552
private static readonly int UVChannelNormals = Shader.PropertyToID("_UVChannelNormals");
53+
private static readonly int DiffusionProfileAsset = Shader.PropertyToID("_DiffusionProfileAsset");
54+
private static readonly int DiffusionProfileHash = Shader.PropertyToID("_DiffusionProfileHash");
55+
private static readonly int MaterialID = Shader.PropertyToID("_MaterialID");
4656

4757
#endregion
4858

59+
private const string DiffusionProfilePlastic = "Packages/org.visualpinball.unity.assetlibrary/Assets/Settings/DiffusionProfiles/Plastic/PET_PETG.asset";
60+
61+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
62+
63+
4964
public Shader GetShader()
5065
{
5166
return Shader.Find("HDRP/Lit");
@@ -114,8 +129,7 @@ public Material CreateMaterial(PbrMaterial vpxMaterial, ITextureProvider texture
114129

115130
unityMaterial.SetFloat(Metallic, metallicValue);
116131

117-
// roughness / glossiness
118-
unityMaterial.SetFloat(Smoothness, vpxMaterial.Roughness);
132+
SetSmoothness(unityMaterial, vpxMaterial.Roughness);
119133

120134
// map
121135
if (vpxMaterial.HasMap && textureProvider != null) {
@@ -130,9 +144,21 @@ public Material CreateMaterial(PbrMaterial vpxMaterial, ITextureProvider texture
130144
unityMaterial.SetTexture( NormalMap, textureProvider.GetTexture(vpxMaterial.NormalMap.Name));
131145
}
132146

147+
// diffusion profile
148+
if (vpxMaterial.DiffusionProfile != DiffusionProfileTemplate.None) {
149+
SetDiffusionProfile(unityMaterial, vpxMaterial.DiffusionProfile);
150+
}
151+
152+
SetMaterialType(unityMaterial, vpxMaterial.MaterialType);
153+
133154
return unityMaterial;
134155
}
135156

157+
public void SetSmoothness(Material unityMaterial, float smoothness)
158+
{
159+
unityMaterial.SetFloat(Smoothness, smoothness);
160+
}
161+
136162
public Material MergeMaterials(PbrMaterial vpxMaterial, Material texturedMaterial)
137163
{
138164
var nonTexturedMaterial = CreateMaterial(vpxMaterial, null, null);
@@ -146,5 +172,62 @@ public Material MergeMaterials(PbrMaterial vpxMaterial, Material texturedMateria
146172

147173
return mergedMaterial;
148174
}
175+
176+
public void SetDiffusionProfile(Material material, DiffusionProfileTemplate template)
177+
{
178+
#if UNITY_EDITOR
179+
180+
var diffusionProfilePath = template switch {
181+
DiffusionProfileTemplate.Plastics => DiffusionProfilePlastic,
182+
_ => throw new ArgumentOutOfRangeException(nameof(template), template, "Invalid diffusion profile.")
183+
};
184+
185+
// unity, why tf would you make DiffusionProfileSettings internal!?!
186+
var diffusionProfile = AssetDatabase.LoadAssetAtPath<Object>(diffusionProfilePath);
187+
188+
if (diffusionProfile != null) {
189+
190+
// need to get those through reflection..
191+
var profile = diffusionProfile.GetType().GetRuntimeFields().First(f => f.Name == "profile").GetValue(diffusionProfile);
192+
var profileHash = (uint)profile.GetType().GetRuntimeFields().First(f => f.Name == "hash").GetValue(profile);
193+
194+
var guid = AssetDatabase.AssetPathToGUID(diffusionProfilePath);
195+
var newGuid = ConvertGUIDToVector4(guid);
196+
var hash = AsFloat(profileHash);
197+
198+
// encode back GUID and it's hash
199+
material.SetVector(DiffusionProfileAsset, newGuid);
200+
material.SetFloat(DiffusionProfileHash, hash);
201+
202+
} else {
203+
Logger.Warn($"Could not load diffusion profile at {diffusionProfilePath}");
204+
}
205+
206+
#endif
207+
}
208+
209+
public void SetMaterialType(Material material, MaterialType materialType)
210+
{
211+
material.SetFloat(MaterialID, (int)materialType);
212+
}
213+
214+
private static Vector4 ConvertGUIDToVector4(string guid)
215+
{
216+
Vector4 vector;
217+
byte[] bytes = new byte[16];
218+
219+
for (int i = 0; i < 16; i++)
220+
bytes[i] = byte.Parse(guid.Substring(i * 2, 2), NumberStyles.HexNumber);
221+
222+
unsafe
223+
{
224+
fixed (byte * b = bytes)
225+
vector = *(Vector4 *)b;
226+
}
227+
228+
return vector;
229+
}
230+
231+
private static float AsFloat(uint val) { unsafe { return *((float*)&val); } }
149232
}
150233
}

0 commit comments

Comments
 (0)