I'm working on a tool for the editor in which you can take a sprite and it blurs it for you. I'm using a compute shader with buffer approach to blur it but blur requires multiple passes. After using Shader.Dispatch(), I'm trying to use Buffer.GetData() but this returns a black/empty texture because (I think) the buffer will only be done after a frame update which only happens in editor when you change something. I tried using corourines in the editor but that does not work. Using a non-buffer approach (RenderTexture & ReadPixels) only allows me to do one blur pass. Is there another way to Dispatch() and use the result of a compute shader in a loop in the editor?
Here's the piece of code I written so far:
SpriteRenderer sr = GetComponent();
reftex = sr.sprite.texture;
Color[] outputData = new Color[reftex.width * reftex.height];
ComputeBuffer buffer = new ComputeBuffer(reftex.width * reftex.height, 16);
Color[] bufferData = outputData;
buffer.SetData(bufferData);
int kernel = shader.FindKernel("CSMain");
shader.SetTexture(kernel, "Input", reftex);
shader.SetVector("TexelSize", new Vector4(1f / reftex.width, 1f / reftex.height, reftex.width, reftex.height));
shader.SetBuffer(kernel, "Output", buffer);
shader.Dispatch(kernel, buffer.count, 1, 1);
//yield return new WaitForEndOfFrame();
buffer.GetData(outputData);
resultTexture.SetPixels(outputData);
resultTexture.Apply();
↧