Is it possible to do procedural calculations in shaders?


I’m doing procedural generation for a texture of an earth-like planet. It’s basically simple diamond-square with some perlin for polar caps and a lot of transformations on top of it, generating a 2048x1536px cubemap texture plus a normal map of the same size. I’m happy with the outcome, but not so happy about performance: It’s taking 5-6s per planet, and since i’d like to have more than one, this is a problem. I already did some optimization, bringing calculation time down from about 15s. Some more could be done, but it would only be chipping away tiny fractions.

I’ve done nothing with shaders so far, so my question is on a basic level: Can some or all of that work be done by shaders? If so, will this improve performance (since gpu is doing the work)?

if anybody could point me in the right direction, i’d be really glad. thank you.

You can definitely do this on the GPU and it will be very, very fast. However, you’ll probably want to have Unity Pro so that you can render into and read out of RenderTargets. A high level algorithm would look like this:

Convert your current code into a shader.
Create a dedicated RenderTarget.
Create a dedicated camera and point it at the RenderTarget.
Render a full screen quad with your shader from that camera.
Create a new Texture2D.
Copy the pixels out of your RenderTarget into the new Texture2D.