diff --git a/Game.Client/FunctionProxy.cs b/Game.Client/FunctionProxy.cs index 22391b423..43f05ecc6 100644 --- a/Game.Client/FunctionProxy.cs +++ b/Game.Client/FunctionProxy.cs @@ -25,9 +25,13 @@ public bool Init(IMaterial material, KeyValues keyValues, ReadOnlySpan key } // Look for array specification... - Span pTemp = stackalloc char[256]; - // TODO ^^^^^^^^^^^^^^^ - FloatVecComp = -1; + int bracket = varName.IndexOf('['); + if (bracket >= 0) { + FloatVecComp = ParseVecComp(varName[(bracket + 1)..]); + varName = varName[..bracket]; + } + else + FloatVecComp = -1; bool foundVar; FloatVar = material.FindVar(varName, out foundVar, true); @@ -45,6 +49,15 @@ public bool Init(IMaterial material, KeyValues keyValues, ReadOnlySpan key return true; } + public static int ParseVecComp(ReadOnlySpan str) { + int end = 0; + if (end < str.Length && (str[end] == '-' || str[end] == '+')) + end++; + while (end < str.Length && char.IsAsciiDigit(str[end])) + end++; + return int.TryParse(str[..end], out int value) ? value : 0; + } + public float GetFloat(){ if (FloatVar == null) return Value; @@ -73,10 +86,10 @@ public virtual bool Init(IMaterial material, KeyValues keyValues) { if (result.IsEmpty) return false; - Span temp = stackalloc char[256]; - if (result.Contains('[')) { - // todo - ResultVecComp = -1; + int bracket = result.IndexOf('['); + if (bracket >= 0) { + ResultVecComp = FloatInput.ParseVecComp(result[(bracket + 1)..]); + result = result[..bracket]; } else ResultVecComp = -1; @@ -115,3 +128,53 @@ protected void SetFloatResult(float result) { protected IMaterialVar? Result; protected int ResultVecComp; } + +public abstract class FunctionProxy : ResultProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + if (!base.Init(material, keyValues)) + return false; + + ReadOnlySpan srcVar1 = keyValues.GetString("srcVar1"); + if (srcVar1.IsEmpty) + return false; + + bool foundVar; + Src1 = material.FindVar(srcVar1, out foundVar, true); + if (!foundVar) + return false; + + ReadOnlySpan srcVar2 = keyValues.GetString("srcVar2"); + if (!srcVar2.IsEmpty) { + Src2 = material.FindVar(srcVar2, out foundVar, true); + if (!foundVar) + return false; + } + else + Src2 = null; + + return true; + } + + protected void ComputeResultType(out MaterialVarType resultType, ref int vecSize) { + resultType = Result!.GetVarType(); + if (resultType == MaterialVarType.Vector) { + if (ResultVecComp >= 0) + resultType = MaterialVarType.Float; + vecSize = Result.VectorSize(); + } + else if (resultType == MaterialVarType.Undefined) { + resultType = Src1!.GetVarType(); + if (resultType == MaterialVarType.Vector) + vecSize = Src1.VectorSize(); + else if ((resultType == MaterialVarType.Undefined) && Src2 != null) { + resultType = Src2.GetVarType(); + if (resultType == MaterialVarType.Vector) + vecSize = Src2.VectorSize(); + } + } + } + + protected IMaterialVar? Src1; + protected IMaterialVar? Src2; +} diff --git a/Game.Client/HLClient.cs b/Game.Client/HLClient.cs index 7303db04e..0590c4c4f 100644 --- a/Game.Client/HLClient.cs +++ b/Game.Client/HLClient.cs @@ -130,6 +130,8 @@ public bool DispatchUserMessage(int msgType, bf_read msgData) { } public bool Init() { + randomgaussian.AttachToStream(random); + #if GMOD_DLL get.IntroScreen()!.Start(); get.IntroScreen()!.Update("Start", true); diff --git a/Game.Client/MathProxy.cs b/Game.Client/MathProxy.cs index 9704a1c97..77d0116fd 100644 --- a/Game.Client/MathProxy.cs +++ b/Game.Client/MathProxy.cs @@ -46,3 +46,616 @@ public override void OnBind(object? o) { readonly FloatInput SineMin = new(); readonly FloatInput SineTimeOffset = new(); } + +[ExposeMaterialProxy(Name = "Add")] +public class AddProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + bool ok = base.Init(material, keyValues); + ok = ok && Src2 != null; + return ok; + } + + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4], b = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + Src2!.GetVecValue(b[..vecSize]); + for (int i = 0; i < vecSize; i++) + a[i] += b[i]; + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: + SetFloatResult(Src1!.GetFloatValue() + Src2!.GetFloatValue()); + break; + + case MaterialVarType.Int: + Result!.SetFloatValue(Src1!.GetIntValue() + Src2!.GetIntValue()); + break; + } + } +} + +[ExposeMaterialProxy(Name = "Subtract")] +public class SubtractProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + bool ok = base.Init(material, keyValues); + ok = ok && Src2 != null; + return ok; + } + + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4], b = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + Src2!.GetVecValue(b[..vecSize]); + for (int i = 0; i < vecSize; i++) + a[i] -= b[i]; + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: + SetFloatResult(Src1!.GetFloatValue() - Src2!.GetFloatValue()); + break; + + case MaterialVarType.Int: + Result!.SetFloatValue(Src1!.GetIntValue() - Src2!.GetIntValue()); + break; + } + } +} + +[ExposeMaterialProxy(Name = "Multiply")] +public class MultiplyProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + bool ok = base.Init(material, keyValues); + ok = ok && Src2 != null; + return ok; + } + + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4], b = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + Src2!.GetVecValue(b[..vecSize]); + for (int i = 0; i < vecSize; i++) + a[i] *= b[i]; + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: + SetFloatResult(Src1!.GetFloatValue() * Src2!.GetFloatValue()); + break; + + case MaterialVarType.Int: + Result!.SetFloatValue(Src1!.GetIntValue() * Src2!.GetIntValue()); + break; + } + } +} + +[ExposeMaterialProxy(Name = "Divide")] +public class DivideProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + bool ok = base.Init(material, keyValues); + ok = ok && Src2 != null; + return ok; + } + + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4], b = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + Src2!.GetVecValue(b[..vecSize]); + for (int i = 0; i < vecSize; i++) + a[i] /= b[i]; + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: + if (Src2!.GetFloatValue() != 0) + SetFloatResult(Src1!.GetFloatValue() / Src2.GetFloatValue()); + else + SetFloatResult(Src1!.GetFloatValue()); + break; + + case MaterialVarType.Int: + if (Src2!.GetIntValue() != 0) + Result!.SetFloatValue(Src1!.GetIntValue() / Src2.GetIntValue()); + else + Result!.SetFloatValue(Src1!.GetIntValue()); + break; + } + } +} + +[ExposeMaterialProxy(Name = "Clamp")] +public class ClampProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + if (!base.Init(material, keyValues)) + return false; + + if (!Min.Init(material, keyValues, "min", 0)) + return false; + + if (!Max.Init(material, keyValues, "max", 1)) + return false; + + return true; + } + + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + float min = Min.GetFloat(); + float max = Max.GetFloat(); + + if (min > max) + (min, max) = (max, min); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + for (int i = 0; i < vecSize; ++i) { + if (a[i] < min) + a[i] = min; + else if (a[i] > max) + a[i] = max; + } + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: { + float src = Src1!.GetFloatValue(); + if (src < min) + src = min; + else if (src > max) + src = max; + SetFloatResult(src); + } + break; + + case MaterialVarType.Int: { + int src = Src1!.GetIntValue(); + if (src < min) + src = (int)min; + else if (src > max) + src = (int)max; + Result!.SetIntValue(src); + } + break; + } + } + + readonly FloatInput Min = new(); + readonly FloatInput Max = new(); +} + +[ExposeMaterialProxy(Name = "Equals")] +public class EqualsProxy : FunctionProxy +{ + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: + SetFloatResult(Src1!.GetFloatValue()); + break; + + case MaterialVarType.Int: + Result!.SetIntValue(Src1!.GetIntValue()); + break; + } + } +} + +[ExposeMaterialProxy(Name = "Frac")] +public class FracProxy : FunctionProxy +{ + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + a[0] -= (int)a[0]; + a[1] -= (int)a[1]; + a[2] -= (int)a[2]; + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: { + float a = Src1!.GetFloatValue(); + a -= (int)a; + SetFloatResult(a); + } + break; + + case MaterialVarType.Int: + Result!.SetIntValue(Src1!.GetIntValue()); + break; + } + } +} + +[ExposeMaterialProxy(Name = "Int")] +public class IntProxy : FunctionProxy +{ + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + a[0] = (int)a[0]; + a[1] = (int)a[1]; + a[2] = (int)a[2]; + Result!.SetVecValue(a[..vecSize]); + } + break; + + case MaterialVarType.Float: { + float a = Src1!.GetFloatValue(); + a = (int)a; + SetFloatResult(a); + } + break; + + case MaterialVarType.Int: + Result!.SetIntValue(Src1!.GetIntValue()); + break; + } + } +} + +[ExposeMaterialProxy(Name = "LinearRamp")] +public class LinearRampProxy : ResultProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + if (!base.Init(material, keyValues)) + return false; + + if (!Rate.Init(material, keyValues, "rate", 1)) + return false; + + if (!InitialValue.Init(material, keyValues, "initialValue", 0)) + return false; + + return true; + } + + public override void OnBind(object? o) { + float value = (float)(Rate.GetFloat() * gpGlobals.CurTime + InitialValue.GetFloat()); + SetFloatResult(value); + } + + readonly FloatInput Rate = new(); + readonly FloatInput InitialValue = new(); +} + +[ExposeMaterialProxy(Name = "UniformNoise")] +public class UniformNoiseProxy : ResultProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + if (!base.Init(material, keyValues)) + return false; + + if (!MinVal.Init(material, keyValues, "minVal", 0)) + return false; + + if (!MaxVal.Init(material, keyValues, "maxVal", 1)) + return false; + + return true; + } + + public override void OnBind(object? o) { + SetFloatResult(random.RandomFloat(MinVal.GetFloat(), MaxVal.GetFloat())); + } + + readonly FloatInput MinVal = new(); + readonly FloatInput MaxVal = new(); +} + +[ExposeMaterialProxy(Name = "GaussianNoise")] +public class GaussianNoiseProxy : ResultProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + if (!base.Init(material, keyValues)) + return false; + + if (!Mean.Init(material, keyValues, "mean", 0.0f)) + return false; + + if (!StdDev.Init(material, keyValues, "halfwidth", 1.0f)) + return false; + + if (!MinVal.Init(material, keyValues, "minVal", -float.MaxValue)) + return false; + + if (!MaxVal.Init(material, keyValues, "maxVal", float.MaxValue)) + return false; + + return true; + } + + public override void OnBind(object? o) { + float mean = Mean.GetFloat(); + float stdDev = StdDev.GetFloat(); + float val = randomgaussian.RandomFloat(mean, stdDev); + float maxVal = MaxVal.GetFloat(); + float minVal = MinVal.GetFloat(); + + if (minVal > maxVal) + (minVal, maxVal) = (maxVal, minVal); + + if (val < minVal) + val = minVal; + else if (val > maxVal) + val = maxVal; + + SetFloatResult(val); + } + + readonly FloatInput Mean = new(); + readonly FloatInput StdDev = new(); + readonly FloatInput MinVal = new(); + readonly FloatInput MaxVal = new(); +} + +[ExposeMaterialProxy(Name = "Exponential")] +public class ExponentialProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + if (!base.Init(material, keyValues)) + return false; + + if (!Scale.Init(material, keyValues, "scale", 1.0f)) + return false; + + if (!Offset.Init(material, keyValues, "offset", 0.0f)) + return false; + + if (!MinVal.Init(material, keyValues, "minVal", -float.MaxValue)) + return false; + + if (!MaxVal.Init(material, keyValues, "maxVal", float.MaxValue)) + return false; + + return true; + } + + public override void OnBind(object? o) { + float val = Scale.GetFloat() * MathF.Exp(Src1!.GetFloatValue() + Offset.GetFloat()); + + float maxVal = MaxVal.GetFloat(); + float minVal = MinVal.GetFloat(); + + if (minVal > maxVal) + (minVal, maxVal) = (maxVal, minVal); + + if (val < minVal) + val = minVal; + else if (val > maxVal) + val = maxVal; + + SetFloatResult(val); + } + + readonly FloatInput Scale = new(); + readonly FloatInput Offset = new(); + readonly FloatInput MinVal = new(); + readonly FloatInput MaxVal = new(); +} + +[ExposeMaterialProxy(Name = "Abs")] +public class AbsProxy : FunctionProxy +{ + public override void OnBind(object? o) { + SetFloatResult(MathF.Abs(Src1!.GetFloatValue())); + } +} + +[ExposeMaterialProxy(Name = "Empty")] +public class EmptyProxy : IMaterialProxy +{ + public bool Init(IMaterial material, KeyValues keyValues) => true; + public void OnBind(object? o) { } + public void Release() { } + public IMaterial GetMaterial() => null!; +} + +[ExposeMaterialProxy(Name = "LessOrEqual")] +public class LessOrEqualProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + ReadOnlySpan lessEqualVar = keyValues.GetString("lessEqualVar"); + if (lessEqualVar.IsEmpty) + return false; + + bool foundVar; + LessVar = material.FindVar(lessEqualVar, out foundVar, true); + if (!foundVar) + return false; + + ReadOnlySpan greaterVar = keyValues.GetString("greaterVar"); + if (greaterVar.IsEmpty) + return false; + + GreaterVar = material.FindVar(greaterVar, out foundVar, true); + if (!foundVar) + return false; + + bool ok = base.Init(material, keyValues); + ok = ok && Src2 != null; + return ok; + } + + public override void OnBind(object? o) { + IMaterialVar sourceVar; + if (Src1!.GetFloatValue() <= Src2!.GetFloatValue()) + sourceVar = LessVar!; + else + sourceVar = GreaterVar!; + + int vecSize = 0; + MaterialVarType resultType = Result!.GetVarType(); + if (resultType == MaterialVarType.Vector) { + if (ResultVecComp >= 0) + resultType = MaterialVarType.Float; + vecSize = Result.VectorSize(); + } + else if (resultType == MaterialVarType.Undefined) { + resultType = sourceVar.GetVarType(); + if (resultType == MaterialVarType.Vector) + vecSize = sourceVar.VectorSize(); + } + + switch (resultType) { + case MaterialVarType.Vector: { + Span src = stackalloc float[4]; + sourceVar.GetVecValue(src[..vecSize]); + Result.SetVecValue(src[..vecSize]); + } + break; + + case MaterialVarType.Float: + SetFloatResult(sourceVar.GetFloatValue()); + break; + + case MaterialVarType.Int: + Result.SetFloatValue(sourceVar.GetIntValue()); + break; + } + } + + IMaterialVar? LessVar; + IMaterialVar? GreaterVar; +} + +[ExposeMaterialProxy(Name = "WrapMinMax")] +public class WrapMinMaxProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + if (!base.Init(material, keyValues)) + return false; + + if (!MinVal.Init(material, keyValues, "minVal", 0)) + return false; + + if (!MaxVal.Init(material, keyValues, "maxVal", 1)) + return false; + + return true; + } + + public override void OnBind(object? o) { + if (MaxVal.GetFloat() <= MinVal.GetFloat()) + SetFloatResult(MinVal.GetFloat()); + else { + float result = (Src1!.GetFloatValue() - MinVal.GetFloat()) / (MaxVal.GetFloat() - MinVal.GetFloat()); + + if (result >= 0.0f) + result -= (int)result; + else + result -= ((int)result) - 1; + + result *= MaxVal.GetFloat() - MinVal.GetFloat(); + result += MinVal.GetFloat(); + + SetFloatResult(result); + } + } + + readonly FloatInput MinVal = new(); + readonly FloatInput MaxVal = new(); +} + +[ExposeMaterialProxy(Name = "SelectFirstIfNonZero")] +public class SelectFirstIfNonZeroProxy : FunctionProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + bool ok = base.Init(material, keyValues); + ok = ok && Src2 != null; + return ok; + } + + public override void OnBind(object? o) { + int vecSize = 0; + ComputeResultType(out MaterialVarType resultType, ref vecSize); + + switch (resultType) { + case MaterialVarType.Vector: { + Span a = stackalloc float[4], b = stackalloc float[4]; + Src1!.GetVecValue(a[..vecSize]); + Src2!.GetVecValue(b[..vecSize]); + + const float tolerance = 0.01f; + bool aIsZero = a[0] > -tolerance && a[0] < tolerance && + a[1] > -tolerance && a[1] < tolerance && + a[2] > -tolerance && a[2] < tolerance; + + if (!aIsZero) + Result!.SetVecValue(a[..vecSize]); + else + Result!.SetVecValue(b[..vecSize]); + } + break; + + case MaterialVarType.Float: + if (Src1!.GetFloatValue() != 0) + SetFloatResult(Src1.GetFloatValue()); + else + SetFloatResult(Src2!.GetFloatValue()); + break; + + case MaterialVarType.Int: + if (Src1!.GetIntValue() != 0) + Result!.SetFloatValue(Src1.GetIntValue()); + else + Result!.SetFloatValue(Src2!.GetIntValue()); + break; + } + } +} diff --git a/Game.Client/MatrixProxy.cs b/Game.Client/MatrixProxy.cs new file mode 100644 index 000000000..069b1979b --- /dev/null +++ b/Game.Client/MatrixProxy.cs @@ -0,0 +1,105 @@ +using Source.Common.Formats.Keyvalues; +using Source.Common.MaterialSystem; +using Source.Common.Mathematics; + +using System.Numerics; + +namespace Game.Client; + +[ExposeMaterialProxy(Name = "TextureTransform")] +public class TextureTransformProxy : ResultProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + CenterVar = null; + ScaleVar = null; + RotateVar = null; + TranslateVar = null; + + ReadOnlySpan varName = keyValues.GetString("centerVar"); + if (!varName.IsEmpty) + CenterVar = material.FindVar(varName, out _, false); + + varName = keyValues.GetString("scaleVar"); + if (!varName.IsEmpty) + ScaleVar = material.FindVar(varName, out _, false); + + varName = keyValues.GetString("rotateVar"); + if (!varName.IsEmpty) + RotateVar = material.FindVar(varName, out _, false); + + varName = keyValues.GetString("translateVar"); + if (!varName.IsEmpty) + TranslateVar = material.FindVar(varName, out _, false); + + return base.Init(material, keyValues); + } + + public override void OnBind(object? o) { + Span center = [0.5f, 0.5f]; + Span translation = [0, 0]; + + CenterVar?.GetVecValue(center); + MathLib.MatrixBuildTranslation(out Matrix4x4 mat, -center[0], -center[1], 0.0f); + Matrix4x4 temp; + + if (ScaleVar != null) { + Span scale = stackalloc float[2]; + ScaleVar.GetVecValue(scale); + MathLib.MatrixBuildScale(out temp, scale[0], scale[1], 1.0f); + MathLib.MatrixMultiply(temp, mat, out mat); + } + + if (RotateVar != null) { + float angle = RotateVar.GetFloatValue(); + MathLib.MatrixBuildRotateZ(out temp, angle); + MathLib.MatrixMultiply(temp, mat, out mat); + } + MathLib.MatrixBuildTranslation(out temp, center[0], center[1], 0.0f); + MathLib.MatrixMultiply(temp, mat, out mat); + + if (TranslateVar != null) { + TranslateVar.GetVecValue(translation); + MathLib.MatrixBuildTranslation(out temp, translation[0], translation[1], 0.0f); + MathLib.MatrixMultiply(temp, mat, out mat); + } + + Result!.SetMatrixValue(mat); + } + + IMaterialVar? CenterVar; + IMaterialVar? ScaleVar; + IMaterialVar? RotateVar; + IMaterialVar? TranslateVar; +} + +[ExposeMaterialProxy(Name = "MatrixRotate")] +public class MatrixRotateProxy : ResultProxy +{ + public override bool Init(IMaterial material, KeyValues keyValues) { + AxisVar = null; + + ReadOnlySpan varName = keyValues.GetString("axisVar"); + if (!varName.IsEmpty) + AxisVar = material.FindVar(varName, out _, false); + + if (!Angle.Init(material, keyValues, "angle", 0)) + return false; + + return base.Init(material, keyValues); + } + + public override void OnBind(object? o) { + Vector3 axis = new(0, 0, 1); + if (AxisVar != null) { + AxisVar.GetVecValue(out axis); + if (MathLib.VectorNormalize(ref axis) < 1e-3) + axis = new(0, 0, 1); + } + + MathLib.MatrixBuildRotationAboutAxis(out Matrix4x4 mat, axis, Angle.GetFloat()); + Result!.SetMatrixValue(mat); + } + + readonly FloatInput Angle = new(); + IMaterialVar? AxisVar; +} diff --git a/Game.Client/SourceDllMain.cs b/Game.Client/SourceDllMain.cs index 0cffda1b6..f4202d3e2 100644 --- a/Game.Client/SourceDllMain.cs +++ b/Game.Client/SourceDllMain.cs @@ -79,6 +79,7 @@ public static void Link(IServiceCollection services) { [Dependency] public static IVDebugOverlay debugoverlay { get; private set; } = null!; [Dependency] public static IClientLeafSystem clientLeafSystem { get; private set; } = null!; [Dependency] public static IUniformRandomStream random { get; private set; } = null!; + public static readonly GaussianRandomStream randomgaussian = new(); [Dependency] public static IPredictableList predictables { get; private set; } = null!; [Dependency] public static ILocalize localize { get; private set; } = null!; [Dependency] public static IViewEffects vieweffects { get; private set; } = null!; diff --git a/Game.Client/TextureScrollMaterialProxy.cs b/Game.Client/TextureScrollMaterialProxy.cs new file mode 100644 index 000000000..b3e10886e --- /dev/null +++ b/Game.Client/TextureScrollMaterialProxy.cs @@ -0,0 +1,72 @@ +using Source.Common.Formats.Keyvalues; +using Source.Common.MaterialSystem; +using Source.Common.Mathematics; + +using System.Numerics; + +namespace Game.Client; + +[ExposeMaterialProxy(Name = "TextureScroll")] +public class TextureScrollMaterialProxy : IMaterialProxy +{ + public bool Init(IMaterial material, KeyValues keyValues) { + ReadOnlySpan scrollVarName = keyValues.GetString("textureScrollVar"); + if (scrollVarName.IsEmpty) + return false; + + bool foundVar; + TextureScrollVar = material.FindVar(scrollVarName, out foundVar, false); + if (!foundVar) + return false; + + TextureScrollRate.Init(material, keyValues, "textureScrollRate", 1.0f); + TextureScrollAngle.Init(material, keyValues, "textureScrollAngle", 0.0f); + TextureScale.Init(material, keyValues, "textureScale", 1.0f); + + return true; + } + + public void OnBind(object? o) { + if (TextureScrollVar == null) + return; + + float rate, angle, scale; + + rate = TextureScrollRate.GetFloat(); + angle = TextureScrollAngle.GetFloat(); + scale = TextureScale.GetFloat(); + + float sOffset, tOffset; + + sOffset = (float)(gpGlobals.CurTime * MathF.Cos(angle * (MathF.PI / 180.0f)) * rate); + tOffset = (float)(gpGlobals.CurTime * MathF.Sin(angle * (MathF.PI / 180.0f)) * rate); + + if (sOffset < 0.0f) + sOffset += 1.0f + -(int)sOffset; + if (tOffset < 0.0f) + tOffset += 1.0f + -(int)tOffset; + + sOffset -= (int)sOffset; + tOffset -= (int)tOffset; + + if (TextureScrollVar.GetVarType() == MaterialVarType.Matrix) { + Matrix4x4 mat = default; + mat.Init(scale, 0.0f, 0.0f, sOffset, + 0.0f, scale, 0.0f, tOffset, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f); + TextureScrollVar.SetMatrixValue(mat); + } + else + TextureScrollVar.SetVecValue(sOffset, tOffset, 0.0f); + } + + public void Release() { } + + public IMaterial GetMaterial() => TextureScrollVar!.GetOwningMaterial(); + + IMaterialVar? TextureScrollVar; + readonly FloatInput TextureScrollRate = new(); + readonly FloatInput TextureScrollAngle = new(); + readonly FloatInput TextureScale = new(); +} diff --git a/Source.Common/IStudioRender.cs b/Source.Common/IStudioRender.cs index fd3cf6272..a01e3cf5e 100644 --- a/Source.Common/IStudioRender.cs +++ b/Source.Common/IStudioRender.cs @@ -101,6 +101,7 @@ public interface IStudioRender { void UnloadModel(StudioHWData hardwareData); int GetMaterialList(StudioHeader studioHDR, Span materials); + int GetMaterialListFromBodyAndSkin(MDLHandle_t studio, int skin, int body, Span outputMaterials); Span LockBoneMatrices(int boneCount); void UnlockBoneMatrices(); void LockFlexWeights(int weightCount, out Span flexWeights); diff --git a/Source.Common/Mathematics/MathLib.cs b/Source.Common/Mathematics/MathLib.cs index 9335f17ef..c83432731 100644 --- a/Source.Common/Mathematics/MathLib.cs +++ b/Source.Common/Mathematics/MathLib.cs @@ -1660,6 +1660,51 @@ public static void MatrixBuildTranslation(out Matrix4x4 dst, float x, float y, f dst[2, 3] = z; } + public static void MatrixBuildRotateZ(out Matrix4x4 dst, float angleDegrees) { + float radians = angleDegrees * (MathF.PI / 180.0f); + + float fSin = MathF.Sin(radians); + float fCos = MathF.Cos(radians); + + dst = default; + dst[0, 0] = fCos; dst[0, 1] = -fSin; dst[0, 2] = 0.0f; dst[0, 3] = 0.0f; + dst[1, 0] = fSin; dst[1, 1] = fCos; dst[1, 2] = 0.0f; dst[1, 3] = 0.0f; + dst[2, 0] = 0.0f; dst[2, 1] = 0.0f; dst[2, 2] = 1.0f; dst[2, 3] = 0.0f; + dst[3, 0] = 0.0f; dst[3, 1] = 0.0f; dst[3, 2] = 0.0f; dst[3, 3] = 1.0f; + } + + public static void MatrixBuildRotationAboutAxis(out Matrix4x4 dst, in Vector3 axisOfRot, float angleDegrees) { + float radians = angleDegrees * (MathF.PI / 180.0f); + float fSin = MathF.Sin(radians); + float fCos = MathF.Cos(radians); + + float axisXSquared = axisOfRot.X * axisOfRot.X; + float axisYSquared = axisOfRot.Y * axisOfRot.Y; + float axisZSquared = axisOfRot.Z * axisOfRot.Z; + + dst = default; + dst[0, 0] = axisXSquared + (1 - axisXSquared) * fCos; + dst[1, 0] = axisOfRot.X * axisOfRot.Y * (1 - fCos) + axisOfRot.Z * fSin; + dst[2, 0] = axisOfRot.Z * axisOfRot.X * (1 - fCos) - axisOfRot.Y * fSin; + + dst[0, 1] = axisOfRot.X * axisOfRot.Y * (1 - fCos) - axisOfRot.Z * fSin; + dst[1, 1] = axisYSquared + (1 - axisYSquared) * fCos; + dst[2, 1] = axisOfRot.Y * axisOfRot.Z * (1 - fCos) + axisOfRot.X * fSin; + + dst[0, 2] = axisOfRot.Z * axisOfRot.X * (1 - fCos) + axisOfRot.Y * fSin; + dst[1, 2] = axisOfRot.Y * axisOfRot.Z * (1 - fCos) - axisOfRot.X * fSin; + dst[2, 2] = axisZSquared + (1 - axisZSquared) * fCos; + + dst[0, 3] = 0; + dst[1, 3] = 0; + dst[2, 3] = 0; + + dst[3, 0] = 0; + dst[3, 1] = 0; + dst[3, 2] = 0; + dst[3, 3] = 1; + } + public static void BasisToQuaternion(in Vector3 forward, in Vector3 right, in Vector3 up, out Quaternion q) { Assert(MathF.Abs(forward.LengthSquared() - 1.0f) < 1e-3); Assert(MathF.Abs(right.LengthSquared() - 1.0f) < 1e-3); diff --git a/Source.Common/Random.cs b/Source.Common/Random.cs index 684918799..fca0eb92b 100644 --- a/Source.Common/Random.cs +++ b/Source.Common/Random.cs @@ -15,6 +15,7 @@ public interface IUniformRandomStream public static class RandomGlobals { static readonly UniformRandomStream s_UniformStream = new(); + internal static IUniformRandomStream UniformStream => s_UniformStream; public static void RandomSeed(int seed) => s_UniformStream.SetSeed(seed); public static float RandomFloat(float min, float max) => s_UniformStream.RandomFloat(min, max); public static float RandomFloatExp(float min, float max, float exponent) => s_UniformStream.RandomFloatExp(min, max, exponent); @@ -130,3 +131,41 @@ public void SetSeed(int seed) { m_iy = 0; } } + +public class GaussianRandomStream +{ + IUniformRandomStream? UniformStream; + bool HaveValue; + float RandomValue; + + public GaussianRandomStream(IUniformRandomStream? uniformStream = null) => AttachToStream(uniformStream); + + public void AttachToStream(IUniformRandomStream? uniformStream) { + UniformStream = uniformStream; + HaveValue = false; + } + + public float RandomFloat(float mean = 0.0f, float stdDev = 1.0f) { + IUniformRandomStream uniformStream = UniformStream ?? RandomGlobals.UniformStream; + float fac, rsq, v1, v2; + + if (!HaveValue) { + do { + v1 = 2.0f * uniformStream.RandomFloat(0.0f, 1.0f) - 1.0f; + v2 = 2.0f * uniformStream.RandomFloat(0.0f, 1.0f) - 1.0f; + rsq = v1 * v1 + v2 * v2; + } while ((rsq > 1.0f) || (rsq == 0.0f)); + + fac = MathF.Sqrt(-2.0f * MathF.Log(rsq) / rsq); + + RandomValue = v1 * fac; + HaveValue = true; + + return stdDev * (v2 * fac) + mean; + } + else { + HaveValue = false; + return stdDev * RandomValue + mean; + } + } +} diff --git a/Source.Engine/ClientLauncherAPI.cs b/Source.Engine/ClientLauncherAPI.cs index cd9e97732..5933a527f 100644 --- a/Source.Engine/ClientLauncherAPI.cs +++ b/Source.Engine/ClientLauncherAPI.cs @@ -103,7 +103,9 @@ private IClientLauncherAPI.Result Main() { } private bool Init() { - // if (!game.Init()) + materials.SetMaterialProxyFactory(Shader.s_MaterialProxyFactory); + + // if (!game.Init()) // goto onStartupError; if (!videomode.Init()) diff --git a/Source.Engine/ModelInfo.cs b/Source.Engine/ModelInfo.cs index 97010f3f8..6be7fd4af 100644 --- a/Source.Engine/ModelInfo.cs +++ b/Source.Engine/ModelInfo.cs @@ -264,7 +264,8 @@ public void GetModelBounds(Model? model, out Vector3 mins, out Vector3 maxs) { public bool IsTranslucent(Model? model) => model != null && (model.Flags & ModelFlag.Translucent) != 0; public void RecomputeTranslucency(Model? model, int skin, int nBody, object? clientRenderable, float instanceAlphaModulate = 1) { - throw new NotImplementedException(); + if (model != null) + ((ModelLoader)modelloader).Mod_RecomputeTranslucency(model, skin, nBody, clientRenderable, instanceAlphaModulate); } public int GetModelMaterialCount(Model? model) { diff --git a/Source.Engine/ModelLoader.cs b/Source.Engine/ModelLoader.cs index 90d24fa55..852860592 100644 --- a/Source.Engine/ModelLoader.cs +++ b/Source.Engine/ModelLoader.cs @@ -547,6 +547,60 @@ private void Studio_LoadModel(Model model, bool touchAllData) { } } + internal void Mod_RecomputeTranslucency(Model mod, int skin, int body, object? clientRenderable, float instanceAlphaModulate) { + if (instanceAlphaModulate < 1.0f) { + mod.Flags |= ModelFlag.Translucent; + return; + } + + mod.Flags &= ~ModelFlag.Translucent; + + switch (mod.Type) { + case ModelType.Brush: { + for (int i = 0; i < mod.Brush.NumModelSurfaces; ++i) { + ref BSPMSurface2 surfID = ref SurfaceHandleFromIndex(mod.Brush.FirstModelSurface + i, mod.Brush.Shared); + if ((MSurf_Flags(ref surfID) & SurfDraw.NoDraw) != 0) + continue; + + IMaterial material = MSurf_TexInfo(ref surfID, mod.Brush.Shared).Material!; + if (material.IsTranslucent()) { + mod.Flags |= ModelFlag.Translucent; + break; + } + } + } + break; + + case ModelType.Studio: { + StudioHeader studioHdr = MDLCache.GetStudioHdr(mod.Studio)!; + if ((studioHdr.Flags & StudioHdrFlags.ForceOpaque) != 0) + return; + + IMaterial[] materialsArray = ArrayPool.Shared.Rent(128); + try { + Span materials = materialsArray.AsSpan(0, 128); + int materialCount = StudioRender.GetMaterialListFromBodyAndSkin(mod.Studio, skin, body, materials); + for (int i = 0; i < materialCount; i++) { + if (materials[i] != null) { + using MatRenderContextPtr renderContext = new(SourceDllMain.materials); + renderContext.Bind(materials[i], clientRenderable); + bool isTranslucent = materials[i].IsTranslucent(); + + if (isTranslucent) { + mod.Flags |= ModelFlag.Translucent; + break; + } + } + } + } + finally { + ArrayPool.Shared.Return(materialsArray, true); + } + } + break; + } + } + internal int Mod_GetModelMaterials(Model model, Span materials) { StudioHeader studioHdr; int found = 0; diff --git a/Source.MaterialSystem/MatRenderContext.cs b/Source.MaterialSystem/MatRenderContext.cs index 4a2c98de1..5ddbfb58d 100644 --- a/Source.MaterialSystem/MatRenderContext.cs +++ b/Source.MaterialSystem/MatRenderContext.cs @@ -220,6 +220,9 @@ public void Bind(IMaterial iMaterial, object? proxyData) { } SetCurrentProxy(proxyData); + if (materials.GetThreadMode() == MaterialThreadMode.SingleThreaded) + GetCurrentMaterialInternal()!.CallBindProxy(proxyData); + shaderAPI.Bind(GetCurrentMaterialInternal()); } diff --git a/Source.MaterialSystem/MaterialSystem.cs b/Source.MaterialSystem/MaterialSystem.cs index c0a08a5f1..a7633ce6d 100644 --- a/Source.MaterialSystem/MaterialSystem.cs +++ b/Source.MaterialSystem/MaterialSystem.cs @@ -1081,8 +1081,147 @@ public void UpdateLightmap(int lightmapPageID, Span lightmapSize, Span public void BindStandardTexture(Sampler sampler, StandardTextureId id) => GetRenderContextInternal().BindStandardTexture(sampler, id); + bool ReplacementFilesValid; + readonly Dictionary Replacements = new(StringComparer.OrdinalIgnoreCase); + + static readonly string[] ReplacementForceCopy = ["$nocull"]; + + void ScanDirForReplacements(ReadOnlySpan pathName) { + string baseName = $"{pathName}/replacements.vmt"; + if (FileSystem.FileExists(baseName)) { + KeyValues? kv = FileSystem.LoadKeyValues(IFileSystem.KeyValuesPreloadType.VMT, baseName); + if (kv != null) + Replacements.TryAdd($"{pathName}/", kv); + } + + ReadOnlySpan findFileName = FileSystem.FindFirstEx($"{pathName}/*", null, out ulong findHandle); + + while (!findFileName.IsEmpty) { + if (FileSystem.FindIsDirectory(findHandle)) { + if (!findFileName.SequenceEqual(".") && !findFileName.SequenceEqual("..")) + ScanDirForReplacements($"{pathName}/{findFileName}"); + } + + findFileName = FileSystem.FindNext(findHandle); + } + + FileSystem.FindClose(findHandle); + } + + void PreloadReplacements() { + Replacements.Clear(); + + ScanDirForReplacements("materials"); + + ReplacementFilesValid = true; + } + + static string ExtractFilePath(string path) { + int src = path.Length > 0 ? path.Length - 1 : 0; + while (src != 0 && path[src - 1] != '/' && path[src - 1] != '\\') + src--; + return path[..src]; + } + public IMaterialProxy? DetermineProxyReplacements(IMaterial? material, KeyValues? fallbackKeyValues) { - throw new NotImplementedException(); + ReplacementProxy? replacementProxy = null; + + if (!HardwareConfig.SupportsPixelShaders_2_0()) + return null; + + if (!ReplacementFilesValid) + PreloadReplacements(); + + string materialName = new(material!.GetName()); + + string shaderName = fallbackKeyValues!.Name; + + string lastPath = materialName; + int length = lastPath.Length - ReplacementProxy.REPLACEMENT_NAME.Length; + if (length > 0 && lastPath.AsSpan(length).Equals(ReplacementProxy.REPLACEMENT_NAME, StringComparison.OrdinalIgnoreCase)) + return null; + + while (true) { + string checkPath = ExtractFilePath(lastPath); + + string checkName = $"materials/{checkPath.AsSpan().TrimStart(['/', '\\'])}"; + + if (Replacements.TryGetValue(checkName, out KeyValues? kv)) { + KeyValues? templatesKV = kv.FindKey("templates"); + KeyValues? patternsKV = kv.FindKey("patterns"); + + int slash = materialName.AsSpan().LastIndexOfAny('/', '\\'); + ReadOnlySpan fileName = slash >= 0 ? materialName.AsSpan(slash + 1) : materialName; + + if (templatesKV == null || patternsKV == null) + Warning($"Replacements: Invalid KV file {checkName}\n"); + else { + for (KeyValues? subKey = patternsKV.GetFirstSubKey(); subKey != null; subKey = subKey.GetNextKey()) { + string replacementName = subKey.Name; + + if (fileName.Length >= replacementName.Length && fileName[..replacementName.Length].Equals(replacementName, StringComparison.OrdinalIgnoreCase)) { + KeyValues? templateNameKV = subKey.FindKey("template"); + KeyValues? replacementMaterial = null; + + if (templateNameKV != null) { + KeyValues? templateKV = templatesKV.FindKey(templateNameKV.GetString()); + if (templateKV != null) { + templateKV = templateKV.FindKey(shaderName); + + if (templateKV != null && templateKV.GetFirstSubKey() != null) + replacementMaterial = templateKV.GetFirstSubKey()!.MakeCopy(); + } + } + else { + if (subKey.GetFirstSubKey() != null) + replacementMaterial = subKey.GetFirstSubKey()!.MakeCopy(); + } + + if (replacementMaterial == null) + break; + + if (replacementMaterial.GetInt("$copyall") == 1) { + for (KeyValues? copyKV = fallbackKeyValues.GetFirstSubKey(); copyKV != null; copyKV = copyKV.GetNextKey()) { + if (replacementMaterial.FindKey(copyKV.Name) == null) + replacementMaterial.SetString(copyKV.Name, copyKV.GetString()); + } + } + else { + foreach (string forceCopy in ReplacementForceCopy) { + KeyValues? copyKV = fallbackKeyValues.FindKey(forceCopy); + if (copyKV != null) + replacementMaterial.SetString(forceCopy, copyKV.GetString()); + } + } + + for (KeyValues? searchKV = replacementMaterial.GetFirstSubKey(); searchKV != null; searchKV = searchKV.GetNextKey()) { + ReadOnlySpan value = searchKV.GetString(); + if (!value.IsEmpty && value[0] == '$') { + KeyValues? copyKV = fallbackKeyValues.FindKey(value); + if (copyKV != null) + searchKV.SetStringValue(copyKV.GetString()); + else + searchKV.SetStringValue(""); + } + } + replacementProxy = new ReplacementProxy(this); + replacementProxy.Init(material, replacementMaterial); + + break; + } + } + } + + break; + } + + if (checkPath.Length == 0) + break; + + lastPath = checkPath; + } + + return replacementProxy; } IMaterialProxyFactory? MaterialProxyFactory; @@ -1297,9 +1436,8 @@ public int GetShaders(int firstShader, Span shaderList) { throw new NotImplementedException("Incomplete port of IMaterialSystem"); } - public MaterialThreadMode GetThreadMode() { - throw new NotImplementedException("Incomplete port of IMaterialSystem"); - } + MaterialThreadMode ThreadMode = MaterialThreadMode.SingleThreaded; + public MaterialThreadMode GetThreadMode() => ThreadMode; public ref readonly MaterialSystemHardwareIdentifier GetVideoCardIdentifier() { throw new NotImplementedException("Incomplete port of IMaterialSystem"); diff --git a/Source.MaterialSystem/MaterialVar.cs b/Source.MaterialSystem/MaterialVar.cs index 096e1167f..d675c5ebc 100644 --- a/Source.MaterialSystem/MaterialVar.cs +++ b/Source.MaterialSystem/MaterialVar.cs @@ -222,7 +222,24 @@ public override void SetValueAutodetectType(ReadOnlySpan val) { } public override void SetVecComponentValue(float val, int component) { - throw new NotImplementedException(); + if (Type == MaterialVarType.Vector && VecVal[component] == val) + return; + + FlushIfCurrentMaterial(); + + Type = MaterialVarType.Vector; + Assert(component <= 3); + + if (NumVectorComps < component) { + for (int i = NumVectorComps; i != component; ++i) + VecVal[i] = 0.0f; + + NumVectorComps = (byte)component; + } + + VecVal[component] = val; + + VarChanged(); } void SetVecValueInternal(in Vector4 vec, int comps) { diff --git a/Source.MaterialSystem/ReplacementProxy.cs b/Source.MaterialSystem/ReplacementProxy.cs new file mode 100644 index 000000000..ea929fe89 --- /dev/null +++ b/Source.MaterialSystem/ReplacementProxy.cs @@ -0,0 +1,37 @@ +using Source.Common.Commands; +using Source.Common.Formats.Keyvalues; +using Source.Common.MaterialSystem; + +namespace Source.MaterialSystem; + +public class ReplacementProxy(IMaterialSystem materials) : IMaterialProxy +{ + public const string REPLACEMENT_NAME = "_replacement"; + + IMaterial? ReplaceMaterial; + + public bool Init(IMaterial material, KeyValues keyValues) { + ReadOnlySpan fileName = material.GetName(); + string newName = $"{fileName}{REPLACEMENT_NAME}"; + + ReplaceMaterial = materials.CreateMaterial(newName, keyValues); + ReplaceMaterial.IncrementReferenceCount(); + + return true; + } + + public void OnBind(object? o) { } + + public void Release() => ReplaceMaterial!.DecrementReferenceCount(); + + static readonly ConVarRef localplayer_visionflags = new("localplayer_visionflags"); + + public IMaterial GetMaterial() { + bool visionOverride = localplayer_visionflags.IsValid() && (localplayer_visionflags.GetInt() & 0x01) != 0; + + if (visionOverride) + return ReplaceMaterial!; + + return null!; + } +} diff --git a/Source.StudioRender/StudioRenderContext.cs b/Source.StudioRender/StudioRenderContext.cs index 48875b2e0..98eb25fbc 100644 --- a/Source.StudioRender/StudioRenderContext.cs +++ b/Source.StudioRender/StudioRenderContext.cs @@ -43,6 +43,7 @@ public class StudioRenderCtx public class StudioRenderContext(IMaterialSystem materialSystem, IStudioDataCache studioDataCache, StudioRender studioRenderImp) : IStudioRender { readonly IMaterialSystemHardwareConfig hardwareConfig = Singleton(); + IMDLCache mdlcache => field ??= Singleton(); public void BeginFrame() { RC.Config.SupportsVertexAndPixelShaders = hardwareConfig.SupportsVertexAndPixelShaders(); @@ -57,6 +58,48 @@ public void EndFrame() { public void UpdateConfig(in StudioRenderConfig config) => RC.Config = config; + public int GetMaterialListFromBodyAndSkin(MDLHandle_t studio, int skin, int body, Span outputMaterials) { + int found = 0; + + StudioHWData? studioHWData = mdlcache.GetHardwareData(studio); + if (studioHWData == null) + return 0; + + for (int lodID = studioHWData.RootLOD; lodID < studioHWData.NumLODs; lodID++) { + StudioHeader studioHdr = mdlcache.GetStudioHdr(studio)!; + Span inputMaterials = studioHWData.LODs![lodID].Materials; + + if (skin >= studioHdr.NumSkinFamilies) + skin = 0; + + Span skinRef = studioHdr.SkinRef(0)[(skin * studioHdr.NumSkinRef)..]; + + for (int i = 0; i < studioHdr.NumBodyParts; i++) { + studioRenderImp.R_StudioSetupModel(i, body, out MStudioModel? model, studioHdr); + + for (int k = 0; k < model!.NumMeshes; ++k) { + MStudioMesh mesh = model.Mesh(k); + IMaterial material = inputMaterials[skinRef[mesh.Material]]; + Assert(material != null); + + int m; + for (m = 0; m < found; m++) { + if (outputMaterials[m] == material) + break; + } + if (m >= found) { + outputMaterials[found++] = material!; + + if (found >= outputMaterials.Length) + return found; + } + } + } + } + + return found; + } + public int GetMaterialList(StudioHeader studioHDR, Span materials) { AssertMsg(studioHDR != null, "Don't ignore this assert! StudioRenderContext.GetMaterialList() has null studioHDR.");