Using same procedural code in shader and script

Does anyone know of a relatively painless way to coordinate shader-based procedural code (for a map in this case) with script-based code (to check points on the map)? Obviously I could translate the shader code into C# but that means maintaining two pieces of code to the produce the same map. I’ve been toying with the idea of having the shader write to a rendertexture which can then be accessed by a script, which might work but it seems a bit sticky. Anyone have a better idea?

Generate procedural maps or whatever is needed in C#. You can use DOTS to assist with performance. Then pass results to shader. No need for duplication of code.

it would be easier to do this in C++.

Hlsl is relatively close to C/C++ and in unity it supports includes. Which means you would be able to use shared include files between script and native code and reduce number of duplications this way.

In C#… I don’t believe there’s a sane way to do it, although someone definitely tried to write a source translator.

C++ is only available in Unity Pro though, right?

Unity Pro doesn’t have exclusive features like that anymore unless you’re working with a version pre-5.x

You can use C++ as a native plugin in all tiers. Been this way since the very first release of unity 5.
However, you can’t really drive monobehaviors from C++ directly.

I’m using 5.0.1, so I guess I can use C++ ?

Yes, but I can’t really recommend using 5.0.1 even within the 5.x cycle at this point. Even 5.4.6 is a better option than that at this point unless you’re mired in tech debt locking you to that early a version.

Yes, you can. Or, at least you SHOULD be able to, because i don’t recall state of C++ native plugin support in 5.0.1.

You’ll still need to write some glue to interface with C++ dll, though.

Like a webassembly for shaders :smile: