| . | |
| using System; |
| |
| using System.Collections.Generic; |
| |
| using UnityEngine; |
| |
| using UnityEngine.Serialization; |
| |
| using Valheim.SettingsGui; |
| |
| |
| |
| [RequireComponent(typeof(Camera))] |
| |
| [ExecuteAlways] |
| |
| public class ShieldDomeImageEffect : MonoBehaviour |
| |
| { |
| |
| private void Awake() |
| |
| { |
| |
| this.m_effectMaterial = new Material(Shader.Find("Hidden/ShieldDomePass")); |
| |
| this.m_gradientTex = new Texture2D(256, 1, TextureFormat.RGB24, false); |
| |
| this.m_gradientTex.wrapMode = TextureWrapMode.Clamp; |
| |
| for (int i = 0; i < 256; i++) |
| |
| { |
| |
| Color color = this.m_shieldColorGradient.Evaluate((float)i / 256f); |
| |
| this.m_gradientTex.SetPixel(i, 0, color); |
| |
| } |
| |
| this.m_gradientTex.Apply(); |
| |
| ShieldDomeImageEffect.s_staticGradient = this.m_shieldColorGradient; |
| |
| ShieldDomeImageEffect.Smoothing = this.m_smoothing; |
| |
| } |
| |
| |
| |
| [ImageEffectAllowedInSceneView] |
| |
| private void OnRenderImage(RenderTexture src, RenderTexture dest) |
| |
| { |
| |
| if (this.m_shieldDomes == null || this.m_shieldDomes.Length == 0) |
| |
| { |
| |
| Graphics.Blit(src, dest); |
| |
| return; |
| |
| } |
| |
| if (this.m_cam == null) |
| |
| { |
| |
| this.m_cam = Camera.main; |
| |
| } |
| |
| Vector3 direction = this.m_cam.ViewportPointToRay(new Vector3(0f, 1f, 0f)).direction; |
| |
| Vector3 direction2 = this.m_cam.ViewportPointToRay(new Vector3(1f, 1f, 0f)).direction; |
| |
| Vector3 direction3 = this.m_cam.ViewportPointToRay(new Vector3(0f, 0f, 0f)).direction; |
| |
| Vector3 direction4 = this.m_cam.ViewportPointToRay(new Vector3(1f, 0f, 0f)).direction; |
| |
| this.m_effectMaterial.SetVector(ShieldDomeImageEffect.Uniforms._TopLeft, direction); |
| |
| this.m_effectMaterial.SetVector(ShieldDomeImageEffect.Uniforms._TopRight, direction2); |
| |
| this.m_effectMaterial.SetVector(ShieldDomeImageEffect.Uniforms._BottomLeft, direction3); |
| |
| this.m_effectMaterial.SetVector(ShieldDomeImageEffect.Uniforms._BottomRight, direction4); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._Smoothing, this.m_smoothing); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._DepthFade, this.m_depthFadeDistance); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._EdgeGlow, this.m_edgeGlowDistance); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._DrawDistance, this.m_drawDistance); |
| |
| this.m_effectMaterial.SetTexture(ShieldDomeImageEffect.Uniforms._ShieldColorGradient, this.m_gradientTex); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._ShieldTime, Time.time); |
| |
| this.m_effectMaterial.SetTexture(ShieldDomeImageEffect.Uniforms._NoiseTexture, this.m_noiseTexture); |
| |
| this.m_effectMaterial.SetTexture(ShieldDomeImageEffect.Uniforms._CurlNoiseTexture, this.m_curlNoiseTexture); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._NoiseSize, this.m_noiseSize); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._CurlNoiseSize, this.m_curlSize); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._CurlNoiseStrength, this.m_curlStrength); |
| |
| int num = 32 + GraphicsModeManager.CurrentDeviceQualitySettings.Lod * 32; |
| |
| this.m_effectMaterial.SetInt(ShieldDomeImageEffect.Uniforms._MaxSteps, num); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._SurfaceDistance, this.m_surfaceDistance); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._NormalBias, this.m_normalBias); |
| |
| this.m_effectMaterial.SetFloat(ShieldDomeImageEffect.Uniforms._RefractStrength, this.m_refractStrength); |
| |
| this.PrepareComputeBuffer(); |
| |
| Graphics.Blit(src, dest, this.m_effectMaterial, 0); |
| |
| } |
| |
| |
| |
| private void PrepareComputeBuffer() |
| |
| { |
| |
| if (this.m_shieldDomes != null && this.m_shieldDomes.Length != 0) |
| |
| { |
| |
| if (this.m_shieldDomeBuffer == null || this.m_shieldDomeBuffer.count != this.m_shieldDomes.Length) |
| |
| { |
| |
| ComputeBuffer shieldDomeBuffer = this.m_shieldDomeBuffer; |
| |
| if (shieldDomeBuffer != null) |
| |
| { |
| |
| shieldDomeBuffer.Release(); |
| |
| } |
| |
| this.m_shieldDomeBuffer = new ComputeBuffer(this.m_shieldDomes.Length, this.m_ShieldDomeStride, ComputeBufferType.Structured); |
| |
| } |
| |
| this.m_shieldDomeBuffer.SetData(this.m_shieldDomes); |
| |
| this.m_effectMaterial.SetBuffer(ShieldDomeImageEffect.Uniforms._DomeBuffer, this.m_shieldDomeBuffer); |
| |
| this.m_effectMaterial.SetInt(ShieldDomeImageEffect.Uniforms._DomeCount, this.m_shieldDomes.Length); |
| |
| return; |
| |
| } |
| |
| ComputeBuffer shieldDomeBuffer2 = this.m_shieldDomeBuffer; |
| |
| if (shieldDomeBuffer2 == null) |
| |
| { |
| |
| return; |
| |
| } |
| |
| shieldDomeBuffer2.Release(); |
| |
| } |
| |
| |
| |
| public void SetShieldData(ShieldGenerator shield, Vector3 position, float radius, float fuelFactor, float lastHitTime) |
| |
| { |
| |
| this.m_shieldDomeData[shield] = new ShieldDomeImageEffect.ShieldDome |
| |
| { |
| |
| position = position, |
| |
| radius = radius, |
| |
| fuelFactor = fuelFactor, |
| |
| lastHitTime = lastHitTime |
| |
| }; |
| |
| this.updateShieldData(); |
| |
| } |
| |
| |
| |
| public void RemoveShield(ShieldGenerator shield) |
| |
| { |
| |
| if (this.m_shieldDomeData.Remove(shield)) |
| |
| { |
| |
| this.updateShieldData(); |
| |
| } |
| |
| } |
| |
| |
| |
| private void updateShieldData() |
| |
| { |
| |
| if (this.m_shieldDomes == null || this.m_shieldDomes.Length != this.m_shieldDomeData.Count) |
| |
| { |
| |
| this.m_shieldDomes = new ShieldDomeImageEffect.ShieldDome[this.m_shieldDomeData.Count]; |
| |
| } |
| |
| int num = 0; |
| |
| foreach (KeyValuePair<ShieldGenerator, ShieldDomeImageEffect.ShieldDome> keyValuePair in this.m_shieldDomeData) |
| |
| { |
| |
| this.m_shieldDomes[num++] = keyValuePair.Value; |
| |
| } |
| |
| } |
| |
| |
| |
| private void OnDestroy() |
| |
| { |
| |
| if (Application.isPlaying) |
| |
| { |
| |
| UnityEngine.Object.Destroy(this.m_effectMaterial); |
| |
| return; |
| |
| } |
| |
| UnityEngine.Object.DestroyImmediate(this.m_effectMaterial); |
| |
| } |
| |
| |
| |
| public static Color GetDomeColor(float fuelFactor) |
| |
| { |
| |
| return ShieldDomeImageEffect.s_staticGradient.Evaluate(fuelFactor); |
| |
| } |
| |
| |
| |
| private int m_ShieldDomeStride = 24; |
| |
| |
| |
| private Material m_effectMaterial; |
| |
| |
| |
| private Camera m_cam; |
| |
| |
| |
| private ComputeBuffer m_shieldDomeBuffer; |
| |
| |
| |
| private Texture2D m_gradientTex; |
| |
| |
| |
| private static Gradient s_staticGradient; |
| |
| |
| |
| public static float Smoothing; |
| |
| |
| |
| [Min(0.1f)] |
| |
| public float m_smoothing = 0.25f; |
| |
| |
| |
| public float m_depthFadeDistance = 3f; |
| |
| |
| |
| [Min(0.25f)] |
| |
| public float m_edgeGlowDistance = 1f; |
| |
| |
| |
| public Gradient m_shieldColorGradient; |
| |
| |
| |
| [Range(-10f, 10f)] |
| |
| public float m_refractStrength = 1f; |
| |
| |
| |
| private ShieldDomeImageEffect.ShieldDome[] m_shieldDomes; |
| |
| |
| |
| private Dictionary<ShieldGenerator, ShieldDomeImageEffect.ShieldDome> m_shieldDomeData = new Dictionary<ShieldGenerator, ShieldDomeImageEffect.ShieldDome>(); |
| |
| |
| |
| [Header("Textures")] |
| |
| public Texture2D m_noiseTexture; |
| |
| |
| |
| public float m_noiseSize = 15f; |
| |
| |
| |
| public Texture3D m_curlNoiseTexture; |
| |
| |
| |
| public float m_curlSize; |
| |
| |
| |
| public float m_curlStrength; |
| |
| |
| |
| [Header("Quality")] |
| |
| [Range(0f, 0.2f)] |
| |
| public float m_surfaceDistance = 0.001f; |
| |
| |
| |
| [Range(0f, 0.5f)] |
| |
| public float m_normalBias = 0.1f; |
| |
| |
| |
| public float m_drawDistance = 100f; |
| |
| |
| |
| [Serializable] |
| |
| public struct ShieldDome |
| |
| { |
| |
| public Vector3 position; |
| |
| |
| |
| public float radius; |
| |
| |
| |
| [FormerlySerializedAs("health")] |
| |
| [Range(0f, 1f)] |
| |
| public float fuelFactor; |
| |
| |
| |
| public float lastHitTime; |
| |
| } |
| |
| |
| |
| private static class Uniforms |
| |
| { |
| |
| internal static readonly int _TopLeft = Shader.PropertyToID("_TopLeft"); |
| |
| |
| |
| internal static readonly int _TopRight = Shader.PropertyToID("_TopRight"); |
| |
| |
| |
| internal static readonly int _BottomLeft = Shader.PropertyToID("_BottomLeft"); |
| |
| |
| |
| internal static readonly int _BottomRight = Shader.PropertyToID("_BottomRight"); |
| |
| |
| |
| internal static readonly int _Smoothing = Shader.PropertyToID("_Smoothing"); |
| |
| |
| |
| internal static readonly int _DepthFade = Shader.PropertyToID("_DepthFade"); |
| |
| |
| |
| internal static readonly int _EdgeGlow = Shader.PropertyToID("_EdgeGlow"); |
| |
| |
| |
| internal static readonly int _DrawDistance = Shader.PropertyToID("_DrawDistance"); |
| |
| |
| |
| internal static readonly int _DomeBuffer = Shader.PropertyToID("_DomeBuffer"); |
| |
| |
| |
| internal static readonly int _DomeCount = Shader.PropertyToID("_DomeCount"); |
| |
| |
| |
| internal static readonly int _MaxSteps = Shader.PropertyToID("_MaxSteps"); |
| |
| |
| |
| internal static readonly int _SurfaceDistance = Shader.PropertyToID("_SurfaceDistance"); |
| |
| |
| |
| internal static readonly int _NormalBias = Shader.PropertyToID("_NormalBias"); |
| |
| |
| |
| internal static readonly int _RefractStrength = Shader.PropertyToID("_RefractStrength"); |
| |
| |
| |
| internal static readonly int _ShieldColorGradient = Shader.PropertyToID("_ShieldColorGradient"); |
| |
| |
| |
| internal static readonly int _ShieldTime = Shader.PropertyToID("_ShieldTime"); |
| |
| |
| |
| internal static readonly int _NoiseTexture = Shader.PropertyToID("_NoiseTexture"); |
| |
| |
| |
| internal static readonly int _CurlNoiseTexture = Shader.PropertyToID("_CurlNoiseTexture"); |
| |
| |
| |
| internal static readonly int _NoiseSize = Shader.PropertyToID("_NoiseSize"); |
| |
| |
| |
| internal static readonly int _CurlNoiseSize = Shader.PropertyToID("_CurlNoiseSize"); |
| |
| |
| |
| internal static readonly int _CurlNoiseStrength = Shader.PropertyToID("_CurlNoiseStrength"); |
| |
| } |
| |
| } |
| |
| |