Last active
December 12, 2021 20:19
-
-
Save gamemachine/3fab49f683f045641ae56a89c3264f50 to your computer and use it in GitHub Desktop.
TerrainMod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GameCommon.Models; | |
using ProtoBuf; | |
using System; | |
using System.Collections.Generic; | |
using Unity.Mathematics; | |
namespace GameCommon.Features.Land | |
{ | |
[Serializable] | |
[ProtoContract] | |
public struct TerrainMod | |
{ | |
[ProtoMember(1)] | |
public float2 WorldPosition; | |
[ProtoMember(2)] | |
public float Height; | |
[ProtoMember(3)] | |
public TerrainSplatWeights Splat0; | |
[ProtoMember(4)] | |
public TerrainSplatWeights Splat1; | |
public int Key | |
{ | |
get | |
{ | |
return (int)math.hash(WorldPosition); | |
} | |
} | |
} | |
[Serializable] | |
[ProtoContract] | |
public struct TerrainSplatWeights | |
{ | |
[ProtoMember(1)] | |
public byte r; | |
[ProtoMember(2)] | |
public byte g; | |
[ProtoMember(3)] | |
public byte b; | |
[ProtoMember(4)] | |
public byte a; | |
public static TerrainSplatWeights FromFloat4(float4 value) | |
{ | |
return new TerrainSplatWeights | |
{ | |
r = ToByte(value.x), | |
g = ToByte(value.y), | |
b = ToByte(value.z), | |
a = ToByte(value.w) | |
}; | |
} | |
public static byte ToByte(float value) | |
{ | |
value = math.min(value, 1f); | |
return (byte)math.floor((value * 100f)); | |
} | |
public static float4x2 Normalize(TerrainSplatWeights w0, TerrainSplatWeights w1) | |
{ | |
float sum = w0.Sum + w1.Sum; | |
if (sum == 0f) | |
{ | |
return default; | |
} | |
float ratio = 1f / sum; | |
float4 v0 = w0.AsFloat4; | |
float4 v1 = w1.AsFloat4; | |
v0.x *= ratio; | |
v0.y *= ratio; | |
v0.z *= ratio; | |
v0.w *= ratio; | |
v1.x *= ratio; | |
v1.y *= ratio; | |
v1.z *= ratio; | |
v1.w *= ratio; | |
return new float4x2(v0, v1); | |
} | |
public float4 Normalize() | |
{ | |
float sum = Sum; | |
if (sum == 0f) | |
{ | |
return default; | |
} | |
float ratio = 1f / sum; | |
float4 v0 = AsFloat4; | |
v0.x *= ratio; | |
v0.y *= ratio; | |
v0.z *= ratio; | |
v0.w *= ratio; | |
return v0; | |
} | |
public float Sum | |
{ | |
get | |
{ | |
float4 value = AsFloat4; | |
return value.x + value.y + value.z + value.w; | |
} | |
} | |
public float4 AsFloat4 | |
{ | |
get | |
{ | |
return new float4(math.min(r / 100f, 1f), math.min(g / 100f, 1f), math.min(b / 100f, 1f), math.min(a / 100f, 1f)); | |
} | |
} | |
public override string ToString() | |
{ | |
return string.Format("{0}.{1}.{2}.{3}", r, g, b, a); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment