Skip to content

Commit

Permalink
Add sine wave animation for health HUD
Browse files Browse the repository at this point in the history
  • Loading branch information
Fueredoriku committed Dec 22, 2024
1 parent eb06089 commit 69240de
Show file tree
Hide file tree
Showing 6 changed files with 3,076 additions and 3 deletions.
132 changes: 132 additions & 0 deletions Assets/Materials/UI/HealthBar.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: HealthBar
m_Shader: {fileID: -6465566751694194690, guid: f7ecd39441a849e4d87870dcd5428b65, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AlphaClip: 0
- _AlphaToMask: 0
- _Blend: 0
- _BlendModePreserveSpecular: 1
- _BumpScale: 1
- _ClearCoatMask: 0
- _ClearCoatSmoothness: 0
- _Cull: 2
- _Cutoff: 0.5
- _DetailAlbedoMapScale: 1
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _DstBlendAlpha: 0
- _EnvironmentReflections: 1
- _GlossMapScale: 0
- _Glossiness: 0
- _GlossyReflections: 0
- _Metallic: 0
- _OcclusionStrength: 1
- _Parallax: 0.005
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0.5
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _SrcBlendAlpha: 1
- _Surface: 0
- _WorkflowMode: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
m_BuildTextureStacks: []
--- !u!114 &6404493371688589894
MonoBehaviour:
m_ObjectHideFlags: 11
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 7
8 changes: 8 additions & 0 deletions Assets/Materials/UI/HealthBar.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Assets/Prefabs/Input/NetworkPlayer.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ PrefabInstance:
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6721764986224867716, guid: 2872a05bf8e94d541a70726bb35085f3, type: 3}
propertyPath: m_RenderingLayerMask
value: 4
objectReference: {fileID: 0}
- target: {fileID: 8325376438412411157, guid: 2872a05bf8e94d541a70726bb35085f3, type: 3}
propertyPath: m_Materials.Array.data[0]
value:
objectReference: {fileID: 2100000, guid: 0ae925ac9e062854c9254d87fa608379, type: 2}
- target: {fileID: 8692423856261334150, guid: 2872a05bf8e94d541a70726bb35085f3, type: 3}
propertyPath: _assetId
value: 3821175277
Expand Down Expand Up @@ -167,13 +175,13 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7fa6481de280fb5489fa0e0e0d7a7026, type: 3}
m_Name:
m_EditorClassIdentifier:
id: 0
color: {r: 0, g: 0, b: 0, a: 0}
playerName:
isAI: 0
body: {fileID: 0}
barrel: {fileID: 0}
extension: {fileID: 0}
id: 0
--- !u!95 &3043983234383150226 stripped
Animator:
m_CorrespondingSourceObject: {fileID: 3755527737191155748, guid: 2872a05bf8e94d541a70726bb35085f3, type: 3}
Expand Down
22 changes: 20 additions & 2 deletions Assets/Scripts/UI/PlayerHUDController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void Start()
chipBox.anchoredPosition = new Vector2(originalChipX, -originalChipY);

healthBarScaleX = healthBar.transform.localScale.x;
healthBar.color = healthMax;
healthBar.material.SetColor("_Color", HealthPalette(1f));
healthTextPosition = healthText.transform.localPosition;

crosshair.rectTransform.sizeDelta *= SettingsDataManager.Singleton.Data.CrosshairSize;
Expand Down Expand Up @@ -271,7 +271,7 @@ private void UpdateHealthBar(float currentHealth, float maxHealth)
width = Mathf.Max(width, 0.001f);

LeanTween.value(healthBar.gameObject, SetHealthBar, healthBar.transform.localScale.x, width, tweenDuration);
healthBar.color = Color.Lerp(healthMin, healthMax, (Mathf.Max(currentHealth, 0) / maxHealth));
healthBar.material.SetColor("_Color", HealthPalette(Mathf.Max(currentHealth, 0) / maxHealth));
}

private void SetHealthBar(float width)
Expand Down Expand Up @@ -406,4 +406,22 @@ public void UpdateOnInitialize(GunStats stats)
else
crosshairMaterial.EnableKeyword("_MODE_" + mode.ToString().ToUpper());
}

// Implementation based of https://iquilezles.org/articles/palettes/
// Health is expected to be normalized.
public Color HealthPalette(float health)
{
health = Mathf.SmoothStep(0.25f, 0.68f, health);
Vector3 contrast = new Vector3(0.6f, 0.6f, 0f);
Vector3 brightness = new Vector3(0.6f, 0.3f, 0.5f);
Vector3 oscilations = new Vector3(0.9f, 0.9f, 0f);
Vector3 phase = new Vector3(0.69f, 0.35f, 0.6f);
Vector3 octave = Mathf.PI * 2f * (oscilations * health + phase);
Vector3 rgb = new Vector3(Mathf.Cos(octave.x), Mathf.Cos(octave.y), Mathf.Cos(octave.z));
Vector3 palette = contrast + new Vector3(
brightness.x * rgb.x,
brightness.y * rgb.y,
brightness.z * rgb.z);
return new Color(palette.x, palette.y, palette.z);
}
}
Loading

0 comments on commit 69240de

Please sign in to comment.