Looking to apply a RenderText to an in game object and running into troubles

I have created a compute shader that processes and sucessfully produces a RenderTexture. That part of this is complete and I have checked its output as good.

How do I take this texture and apply it to a an object in a scene? Below is the logic that I have been attempting to use, and I feel like I am missing something fundamental, even if I have done the hardest part of this (writing the actual shader). The cube that I’m using in the example is just a completely base object I added through adding 3D Object → Cube in the right click menu of the hierarchy and hand to my script.

public GameObject cube;

...

layerShader = (ComputeShader)Resources.Load("Shaders/LayeredImageShader");
kernelHandle = layerShader.FindKernel("CSMain");
shaderOutput = new RenderTexture(width, height, 0, RenderTextureFormat.ARGBFloat);
cube.GetComponent<MeshRenderer>().material.mainTexture = shaderOutput;
layerShader.SetTexture(kernelHandle, "result", shaderOutput);

// set up the details of my shader here...

layerShader.Dispatch(kernelHandle, xGroups, yGroups, zGroups);

After the dispatch is run, I can see the texture on the object change, but it is messed up and chopped into odd pieces. I feel like there’s something I’m missing about the very last step here. As I said, I have verified that the texture being produced by the shader is correct. If I copy it back to CPU memory and display it as a sprite, it looks exactly as I would expect. I have both run the dispach in the Update() method of my MonoScript and as a one time thing beneath a button click with the same results.

Any advice appreciated!

It’s probably just because this:

is returning a copy of the Material, which you then change, and then abandon in place.

Either way, try it with a known camera output on a texture, get that working first before you try any crazy shader tricks: divide and conquer FTW!

MORE:

Generally accessing .material / .materials is going to give you a fresh copy. (See docs for why and when)

Thank you for these links! They will help me as a move forward! Unfortunately, I don’t think they solve my specific problem. I have updated my code so that the dispatch runs off of a button click and the update simply applies the resulting texture to my cube within the Update() method, ensuring that its texture is always the output that was created from the shader.

Here is a shot of what my material looks like in the inspector before I click the button:

And here is what it looks like after:

As you can see, the image is torn into strips for some reason. This is reflected in the object itself, as well. Which just looks weird overall.

Game view:

Scene View: