From 92a46ea5e0d4a7080786289a4859f668606b24bc Mon Sep 17 00:00:00 2001 From: Stedd Date: Sun, 8 Oct 2023 02:28:17 +0200 Subject: [PATCH] cleanup --- Assets/Scripts/ComputeController.cs | 57 +++++++++------------------- Assets/Shaders/ColorInverter.compute | 2 +- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/Assets/Scripts/ComputeController.cs b/Assets/Scripts/ComputeController.cs index f28e8b4..4856993 100644 --- a/Assets/Scripts/ComputeController.cs +++ b/Assets/Scripts/ComputeController.cs @@ -1,6 +1,4 @@ using UnityEngine; -using UnityEngine.Serialization; -using UnityEngine.UIElements; public class ComputeController : MonoBehaviour { @@ -9,14 +7,10 @@ public class ComputeController : MonoBehaviour [SerializeField] private Texture2D _inputTexture; [SerializeField] private RenderTexture _resultTexture; - [Header("Config")] + [Header("State")] [SerializeField] private int _width; [SerializeField] private int _height; - [SerializeField] private float _scale = 1; - [SerializeField] private bool _resize; - - [Header("state")] - [SerializeField] private float sin; + [SerializeField] private float _sin; private int _kernelHandle; private const int NumThreadX = 8; @@ -32,36 +26,37 @@ public class ComputeController : MonoBehaviour private void Start() { - SetWidthHeight(); + InitializeTextures(); - - _kernelHandle = _computeShader.FindKernel("cs_main"); + InitializeShader(); RunComputeShader(); } - private void RunComputeShader() + private void InitializeTextures() { + _width = Mathf.RoundToInt(_inputTexture.width); + _height = Mathf.RoundToInt(_inputTexture.height); _resultTexture = new(_width, _height, 24); _resultTexture.name = "ResultTexture"; _resultTexture.enableRandomWrite = true; _resultTexture.Create(); - - _computeShader.SetTexture(_kernelHandle, SourceTexture, _inputTexture); - _computeShader.SetTexture(_kernelHandle, Result, _resultTexture); - - DispatchShader(); } - private void DispatchShader() + private void InitializeShader() + { + _kernelHandle = _computeShader.FindKernel("cs_main"); + _computeShader.SetTexture(_kernelHandle, SourceTexture, _inputTexture); + _computeShader.SetTexture(_kernelHandle, Result, _resultTexture); + } + + private void RunComputeShader() { _computeShader.Dispatch(_kernelHandle, _width / NumThreadX, _height / NumThreadY, 1); } private void OnGUI() { - CheckScale(); - ShaderUpdate(); GUI.DrawTexture(new Rect(0, 0, _width, _height), _inputTexture); @@ -70,24 +65,8 @@ public class ComputeController : MonoBehaviour private void ShaderUpdate() { - sin = (Mathf.Sin(Time.unscaledTime) + 1) / 2; - _computeShader.SetFloat(SinScaled, sin); - DispatchShader(); - } - - private void CheckScale() - { - if (_resize) - { - _resize = false; - SetWidthHeight(); - RunComputeShader(); - } - } - - private void SetWidthHeight() - { - _width = Mathf.RoundToInt(_inputTexture.width * _scale); - _height = Mathf.RoundToInt(_inputTexture.height * _scale); + _sin = (Mathf.Sin(Time.unscaledTime) + 1) / 2; + _computeShader.SetFloat(SinScaled, _sin); + RunComputeShader(); } } \ No newline at end of file diff --git a/Assets/Shaders/ColorInverter.compute b/Assets/Shaders/ColorInverter.compute index 1b23b3d..1351466 100644 --- a/Assets/Shaders/ColorInverter.compute +++ b/Assets/Shaders/ColorInverter.compute @@ -9,6 +9,6 @@ float sin_scaled; void cs_main(uint3 id : SV_DispatchThreadID) { float4 color = source_texture[id.xy]; - color.rgb = color.rgb * sin_scaled * 2; + color.rgb = color.rgb * sin_scaled; result[id.xy] = color; }