I want to write a GPU shallow water code on Unity3D. For sure, performance matters a lot. I've done this before using DirectX and C++. But for a couple of reasons I want to redo it in Unity3D.

I have some hands on experiences with Unity3D, but I have no idea how I can write GPGPU codes, like what I did in DirectX.

In DirectX I am using textures as my data structure for the properties of water cells (height, x-velocity, y-velocity, etc.) and HLSL shaders as my parallel computational functions (kernels).

Furthermore, I prefer my code to be [near] cross-platform. Is it possible to do something similar in Unity3D using OpenGL?

I am an engineer, and not a game developer, so please keep it simple ...

  • \$\begingroup\$ Can you clarify whether you're looking for help with writing the shaders themselves in Unity's somewhat idiosyncratic format? Or setting up the chain of operations/render passes that you want to perform with those shaders (eg. First use buffer X and Y to compute intermediate result buffer Z, then use Z and X to generate the next frame's Y, then...)? If there are specific aspects of Unity that you're stuck on, letting us know what they are will help get more targeted & relevant answers. \$\endgroup\$ – DMGregory Nov 17 '15 at 13:21

In Unity you can do with shaders pretty much everything you could do with DirectX or other Framework/APIs as long as you use the shader for stuff like surface materials or post processing. Vertex and surface shaders are written in HLSL/Cg but are wrapped in ShaderLab code. I don't know how much that impairs your cross-plattform requirement, but since Unity supports a broad spectrum of target platforms, it should suffice.

Here is some more information on shaders in Unity: http://docs.unity3d.com/Manual/ShadersOverview.html

You store the shaders you write in .shader files. They are recognized by Unity as shader assets and can be chosen in Unity material assetes. Unity shows them automatically in a drop down menu at the very top of each material insptector (see http://docs.unity3d.com/Manual/Materials.html).

Keep in mind that this drop down menu is not structured the same way your .shader files are structured on your file system. They are structured by the path/name you assigned at the beginning of the shader lab code. So if your shader starts with:

Shader "foo/bar" {

your shader will appear accordingly under foo --> bar in the material's shader selection menu. The name of the .shader file is not taken into account.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.