From 27339bdda46f4f826b4c5e2e0d471941221accf7 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sun, 8 Oct 2023 00:56:53 +0200 Subject: [PATCH] colors are inverted on the result texture --- Assets/Scenes/SampleScene.unity | 2 +- Assets/Scripts/ComputeController.cs | 13 +++++++------ Assets/Shaders/ColorInverter.compute | 13 ++++++------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 140cca6..66a9182 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -367,7 +367,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: _computeShader: {fileID: 7200000, guid: 3df3413c2fff3cc41a2eb5505b4c260c, type: 3} - _inputTexture: {fileID: 10305, guid: 0000000000000000f000000000000000, type: 0} + _inputTexture: {fileID: 10309, guid: 0000000000000000f000000000000000, type: 0} _resultTexture: {fileID: 0} --- !u!1 &963194225 GameObject: diff --git a/Assets/Scripts/ComputeController.cs b/Assets/Scripts/ComputeController.cs index e24ad10..1e16f0d 100644 --- a/Assets/Scripts/ComputeController.cs +++ b/Assets/Scripts/ComputeController.cs @@ -1,10 +1,12 @@ using UnityEngine; +using UnityEngine.Serialization; public class ComputeController : MonoBehaviour { [SerializeField] private ComputeShader _computeShader; [SerializeField] private Texture2D _inputTexture; [SerializeField] private RenderTexture _resultTexture; + private int _kernelHandle; private static readonly int Result = Shader.PropertyToID("result"); private static readonly int SourceTexture = Shader.PropertyToID("source_texture"); @@ -13,11 +15,9 @@ public class ComputeController : MonoBehaviour { _kernelHandle = _computeShader.FindKernel("cs_main"); - _resultTexture = new(100, 100, 24) - { - enableRandomWrite = true, - name = "Test" - }; + _resultTexture = new(512, 512, 24); + _resultTexture.name = "ResultTexture"; + _resultTexture.enableRandomWrite = true; _resultTexture.Create(); _computeShader.SetTexture(_kernelHandle, Result, _resultTexture); @@ -28,6 +28,7 @@ public class ComputeController : MonoBehaviour private void OnGUI() { - GUI.DrawTexture(new(0, 0, _resultTexture.width, _resultTexture.height), _resultTexture); + GUI.DrawTexture(new Rect(0, 0, _resultTexture.width, _resultTexture.height), _resultTexture); + GUI.DrawTexture(new Rect(0, 0, _inputTexture.width, _inputTexture.height), _inputTexture); } } \ No newline at end of file diff --git a/Assets/Shaders/ColorInverter.compute b/Assets/Shaders/ColorInverter.compute index c850f9c..24e6371 100644 --- a/Assets/Shaders/ColorInverter.compute +++ b/Assets/Shaders/ColorInverter.compute @@ -1,17 +1,16 @@ -// Each #kernel tells which function to compile; you can have many kernels #pragma kernel cs_main -// Create a RenderTexture with enableRandomWrite flag and set it -// with cs.SetTexture +// The output texture RWTexture2D result; // Input texture Texture2D source_texture; -[numthreads(8,8,1)] -void cs_main (uint3 id : SV_DispatchThreadID) +[numthreads(8, 8, 1)] +void cs_main(uint3 id : SV_DispatchThreadID) { - float4 color = result[id.xy]; - color.rgb = 1 - color.rgb; // invert the color + float4 color = source_texture[id.xy]; + color.rgb = 1 - color.rgb; + //color.rgb = float4(0, 1, 1, 0); result[id.xy] = color; }