From 4924f9abe5d8613d3cfa58a25c6a948181812ba0 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sun, 8 Oct 2023 01:46:49 +0200 Subject: [PATCH] trying to scale the result --- Assets/Scenes/SampleScene.unity | 8 +++-- Assets/Scripts/ComputeController.cs | 46 +++++++++++++++++++++++++--- Assets/Shaders/ColorInverter.compute | 1 + 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 66a9182..4919c1d 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -369,6 +369,10 @@ MonoBehaviour: _computeShader: {fileID: 7200000, guid: 3df3413c2fff3cc41a2eb5505b4c260c, type: 3} _inputTexture: {fileID: 10309, guid: 0000000000000000f000000000000000, type: 0} _resultTexture: {fileID: 0} + _width: 0 + _height: 0 + _scale: 5 + _resize: 0 --- !u!1 &963194225 GameObject: m_ObjectHideFlags: 0 @@ -404,8 +408,8 @@ Camera: m_GameObject: {fileID: 963194225} m_Enabled: 1 serializedVersion: 2 - m_ClearFlags: 1 - m_BackGroundColor: {r: 1, g: 1, b: 1, a: 0} + m_ClearFlags: 2 + m_BackGroundColor: {r: 0.21960786, g: 0.21960786, b: 0.21960786, a: 0} m_projectionMatrixMode: 1 m_GateFitMode: 2 m_FOVAxisMode: 0 diff --git a/Assets/Scripts/ComputeController.cs b/Assets/Scripts/ComputeController.cs index 1e16f0d..1e4c162 100644 --- a/Assets/Scripts/ComputeController.cs +++ b/Assets/Scripts/ComputeController.cs @@ -1,34 +1,70 @@ using UnityEngine; using UnityEngine.Serialization; +using UnityEngine.UIElements; public class ComputeController : MonoBehaviour { + [Header("Conections")] [SerializeField] private ComputeShader _computeShader; [SerializeField] private Texture2D _inputTexture; [SerializeField] private RenderTexture _resultTexture; + [Header("Config")] + [SerializeField] private int _width; + [SerializeField] private int _height; + [SerializeField] private float _scale = 1; + [SerializeField] private bool _resize; + private int _kernelHandle; private static readonly int Result = Shader.PropertyToID("result"); private static readonly int SourceTexture = Shader.PropertyToID("source_texture"); + private static readonly int _threadgroupX = 8; + private static readonly int _threadgroupY = 8; + private void Start() { + SetWidthHeight(); + + _kernelHandle = _computeShader.FindKernel("cs_main"); - _resultTexture = new(512, 512, 24); + RunComputeShader(); + } + + private void RunComputeShader() + { + _resultTexture = new(_width, _height, 24); _resultTexture.name = "ResultTexture"; _resultTexture.enableRandomWrite = true; _resultTexture.Create(); - _computeShader.SetTexture(_kernelHandle, Result, _resultTexture); _computeShader.SetTexture(_kernelHandle, SourceTexture, _inputTexture); + _computeShader.SetTexture(_kernelHandle, Result, _resultTexture); - _computeShader.Dispatch(_kernelHandle, _resultTexture.width / 8, _resultTexture.height / 8, 1); + _computeShader.Dispatch(_kernelHandle, _width / _threadgroupX, _height / _threadgroupY, 1); } private void OnGUI() { - GUI.DrawTexture(new Rect(0, 0, _resultTexture.width, _resultTexture.height), _resultTexture); - GUI.DrawTexture(new Rect(0, 0, _inputTexture.width, _inputTexture.height), _inputTexture); + CheckScale(); + GUI.DrawTexture(new Rect(0, 0, _width, _height), _inputTexture); + GUI.DrawTexture(new Rect(0, _height, _width, _height), _resultTexture); + } + + private void CheckScale() + { + if (_resize) + { + _resize = false; + SetWidthHeight(); + RunComputeShader(); + } + } + + private void SetWidthHeight() + { + _width = Mathf.RoundToInt(_inputTexture.width * _scale); + _height = Mathf.RoundToInt(_inputTexture.height * _scale); } } \ No newline at end of file diff --git a/Assets/Shaders/ColorInverter.compute b/Assets/Shaders/ColorInverter.compute index 24e6371..0883212 100644 --- a/Assets/Shaders/ColorInverter.compute +++ b/Assets/Shaders/ColorInverter.compute @@ -12,5 +12,6 @@ void cs_main(uint3 id : SV_DispatchThreadID) float4 color = source_texture[id.xy]; color.rgb = 1 - color.rgb; //color.rgb = float4(0, 1, 1, 0); + //color.rgb = float4(0, 1, 1, 0); result[id.xy] = color; }