Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Converting MarchingCubes shader from HLSL to GLSL

Discussion in 'Shaders' started by IkkeKirke, May 23, 2019.

  1. IkkeKirke

    IkkeKirke

    Joined:
    Jun 18, 2018
    Posts:
    3
    I was following a tutorial on the Marching Cubes algorithm that is presented by Sebastian Lague. He shared the project in github but I was unable to get it to work. He is on Windows and no doubt using DirectX11, while I'm on Linux using OpenGLCore.

    In theory this shouldn't be a train-smash because Unity is able to convert from HLSL to GLSL, but in my particular case I'm getting errors with linking the shader, which I'm assuming is due to a failed conversion and/or compilation. I believe there are some conventions to follow when writing a "convertable" HLSL shader (like not using float3), and in this case those precautions weren't deemed necessary by the author.

    I don't want to rule out the fact that it might just be my settings and/or drivers that are causing problems, so I was hoping that someone could try this using OpenGLCore on their machine.

    If it is a matter of the conversion failing, is there any easy way to "force" it to work? I changed the `float3`'s to `float4` and updated the code to handle those, but that alone didn't seem to do the trick. In lieu of getting that to work, is there a way of removing the compute shaders entirely and just having the MeshGenerator generate the mesh?

    Any help would be greatly appreciated here!
     
  2. IkkeKirke

    IkkeKirke

    Joined:
    Jun 18, 2018
    Posts:
    3
    Let me try to be more specific. Is there anything in this script that looks like it will be problematic to convert from HLSL to GLSL?

    Code (CSharp):
    1.  
    2. StructuredBuffer<float3> offsets;
    3. int octaves;
    4. float lacunarity;
    5. float persistence;
    6. float noiseScale;
    7. float noiseWeight;
    8. float floorOffset;
    9. float weightMultiplier;
    10. bool closeEdges;
    11. float hardFloor;
    12. float hardFloorWeight;
    13.  
    14. float4 params;
    15.  
    16. [numthreads(numThreads,numThreads,numThreads)]
    17. void Density (int3 id : SV_DispatchThreadID)
    18. {
    19.     if (id.x >= numPointsPerAxis || id.y >= numPointsPerAxis || id.z >= numPointsPerAxis) {
    20.         return;
    21.     }
    22.  
    23.     float3 pos = centre + id * spacing - boundsSize/2;
    24.     float offsetNoise = 0;
    25.  
    26.     float noise = 0;
    27.  
    28.     float frequency = noiseScale/100;
    29.     float amplitude = 1;
    30.     float weight = 1;
    31.     for (int j =0; j < octaves; j ++) {
    32.         float n = snoise((pos+offsetNoise) * frequency + offsets[j] + offset);
    33.         float v = 1-abs(n);
    34.         v = v*v;
    35.         v *= weight;
    36.         weight = max(min(v*weightMultiplier,1),0);
    37.         noise += v * amplitude;
    38.         amplitude *= persistence;
    39.         frequency *= lacunarity;
    40.     }
    41.    
    42.     float finalVal = -(pos.y + floorOffset) + noise * noiseWeight + (pos.y%params.x) * params.y;
    43.  
    44.     if (pos.y < hardFloor) {
    45.         finalVal += hardFloorWeight;
    46.     }
    47.  
    48.     if (closeEdges) {
    49.         float3 edgeOffset = abs(pos*2)-worldSize + spacing/2;
    50.         float edgeWeight = saturate(sign(max(max(edgeOffset.x,edgeOffset.y),edgeOffset.z)));
    51.         finalVal = finalVal * (1-edgeWeight) - 100 * edgeWeight;
    52.        
    53.     }
    54.  
    55.     int index = indexFromCoord(id.x,id.y,id.z);
    56.     points[index] = float4(pos, finalVal);
    57.    
    58. }
    59.