Created
February 9, 2022 06:24
-
-
Save JohnnyTurbo/2256f23fd4351f5e46b60356c7ae5cda to your computer and use it in GitHub Desktop.
Code used in Material Property Overrides tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/XMRSNlTYHFY
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 Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Rendering; | |
using Unity.Transforms; | |
using UnityEngine; | |
namespace TMG.MaterialPropertyOverrides | |
{ | |
public class ColorChangerSystem : SystemBase | |
{ | |
protected override void OnStartRunning() | |
{ | |
Entities.ForEach((Entity e, int entityInQueryIndex, RandomColorData randomColorData, | |
ref URPMaterialPropertyBaseColor baseColor) => | |
{ | |
randomColorData.Random.InitState((uint) entityInQueryIndex); | |
baseColor.Value = randomColorData.Color(); | |
}).ScheduleParallel(); | |
} | |
protected override void OnUpdate() | |
{ | |
var elapsedTime = Time.ElapsedTime; | |
Entities.ForEach((Entity e, ref ColorOverride matCol, in Translation translation) => | |
{ | |
matCol.Value = CalculateColor(elapsedTime, translation.Value); | |
}).ScheduleParallel(); | |
} | |
private static float4 CalculateColor(double elapsedTime, float3 position) | |
{ | |
var hue = (float) ((5 * elapsedTime + position.x + (0.5f * position.y) + (0.5f * position.z)) % 50) / 50; | |
var newCol = Color.HSVToRGB(hue, 1f, 1f); | |
return new float4(newCol.r, newCol.g, newCol.b, newCol.a); | |
} | |
} | |
} | |
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 Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Rendering; | |
namespace TMG.MaterialPropertyOverrides | |
{ | |
[GenerateAuthoringComponent] | |
[MaterialProperty("TurboColor", MaterialPropertyFormat.Float4)] | |
public struct ColorOverride : IComponentData | |
{ | |
public float4 Value; | |
} | |
} |
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 Unity.Entities; | |
using Unity.Mathematics; | |
namespace TMG.MaterialPropertyOverrides | |
{ | |
[GenerateAuthoringComponent] | |
public struct RandomColorData : IComponentData | |
{ | |
public Random Random; | |
public float4 Min; | |
public float4 Max; | |
public float4 Color() | |
{ | |
return Random.NextFloat4(Min, Max); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment