colors are inverted on the result texture

This commit is contained in:
Stedd 2023-10-08 00:56:53 +02:00
parent 9782c69826
commit 27339bdda4
3 changed files with 14 additions and 14 deletions

View File

@ -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:

View File

@ -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);
}
}

View File

@ -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<float4> result;
// Input texture
Texture2D<float4> 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;
}