Hi there,
I’m a bit new to unity shader programming, currently, I want to maintain a realtime Global Distance Field which should be calculated out from all local signed distance field(SDF) data (you can search “SDFr” on Github for the tool). I’ve already had some baked Texture3D of those local SDF and pass each of them into the shader, now the shader has a number of Texture3D’s.
Since they are local SDF and the global SDF should convert them to the world space, here also pass a StructuredBuffer which contains a sequence of bounding boxes and transformations. Now the method to calculate the global distance at any sample point (regarding one Texture3D) is like:
Texture3D _VolumeATex;
Texture3D _VolumeBTex;
...
struct SDFrVolumeData
{
float4x4 WorldToLocal;
float3 Extents;
};
...
StructuredBuffer<SDFrVolumeData> _VolumeBuffer;
...
fixed4 frag(...){
float temp = 0;
float minDistance = LARGE_DISTANCE;
// globalPos is current sample position
// check if the global position is inside the bounding box
if(InsideAABB(globalPos, _VolumeBuffer[0])){
// sample the distance from local SDF
temp = DistanceFunctionTex3DFast(globalPos, _VolumeBuffer[0], _VolumeATex);
distance1 = min(distance1, temp);
} else {
// otherwise return an approximate distance
temp = GlobalDistanceToAABBPlus(globalPos, _VolumeBuffer[0], _VolumeATex);
distance1 = min(distance1, temp);
}
...
// something similar to above for _VolumeBuffer[i] and _Volume[BCD]Tex
}
As you could see I have to match up the _VolumeBuffer element with _Volume[×]Tex manually in the shader code, since there isn’t something like Texture3Darray currently. I could bear this as now there are only 3 textures and volumes, but it’s going to be more and I hope there is a matching method so that I can use for loop and index the textures. Besides, I’m not sure if I can put a Texture3D in the component of StructuredBuffer? Should this be done like storing a pointer to the texture?
Glad if someone can help with this, many thanks.