0
$\begingroup$

I am currently working on implementing parametric curve based dynamic tessellation using GLSL. I have scanned the entire internet and I cannot find even a single good example of a GLSL shader which uses compute buffers from Unity I need help develop a basic GLSL Unity shader with compute buffers to send an array of floats from Unity C# to GLSL domain shader I would really appreciate all the help possible.

I have tried using Material.SetFloatArray(), however, with not much success. I would appreciate any help. I have attached both my C# script and the HLSL shader scripts for reference.

private RosConnector rosConnector;
private Texture2D dtex;
//private GameObject pointMap;
public MeshRenderer renderer;

private float[] paramData; 
public ComputeBuffer cb_params;
private MaterialPropertyBlock matBlock;


void Start()
{
    rosConnector = GetComponent<RosConnector>();
    string subscription_id = rosConnector.RosSocket.Subscribe<map_sense.PlanarRegions>("/map/regions", RegionMsgHandler);
    matBlock = new MaterialPropertyBlock();
    paramData = new float[192];
    renderer = GameObject.FindWithTag("QuadMap").GetComponent<MeshRenderer>();

} 

private void RegionMsgHandler(map_sense.PlanarRegions message)
{
    Debug.Log(message.data.Length);
    for(int i = 0; i<192; i++){
        paramData[i] = (float)i;
    }

    renderer.material.SetFloatArray("params", paramData);


}
Shader "Custom/QuadTessellationHLSL" 
{
    SubShader 
    {
        Pass 
        { 
            Cull Off
            CGPROGRAM
            #pragma vertex TessellationVertexProgram
            #pragma hull HullProgram
            #pragma domain DomainProgram
            #pragma fragment FragmentProgram
            #pragma target 4.6

// ---------------------------------------------------------------- 

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            struct ControlPoint
            {
                float4 vertex : INTERNALTESSPOS;
                float2 uv : TEXCOORD0;
            };
            struct hsConstOut
            {
                float Edges[4] : SV_TessFactor;
                float Inside[2] : SV_InsideTessFactor;
            }; 
            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
            }; 

            float params[192];
// ---------------------------------------------------------------


            v2f VertexProgram(appdata v) // Not the primary vertex program.
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            ControlPoint TessellationVertexProgram(appdata v){
                ControlPoint p;
                p.vertex = v.vertex;
                p.uv = v.uv;
                return p;           
            };


            hsConstOut hull_constant_function(InputPatch<ControlPoint, 4> patch)
            {
                hsConstOut output;
                output.Edges[0] = output.Edges[1] = output.Edges[2] = output.Edges[3] = output.Inside[0] = output.Inside[1] = 16;  
                return output;
            }

                [domain("quad")]
                [partitioning("integer")]
                [outputtopology("triangle_cw")]
                [outputcontrolpoints(4)]
                [patchconstantfunc("hull_constant_function")]           
            ControlPoint HullProgram(InputPatch<ControlPoint, 4>patch, uint id: SV_OutputControlPointID)
            {
                return patch[id];
            }

                [domain("quad")]
            v2f DomainProgram(hsConstOut factors, 
                                const OutputPatch<ControlPoint, 4>patch,
                                float2 UV:SV_DomainLocation)
            {
                float4 a = patch[0].vertex;
                float4 b = patch[1].vertex;
                float4 c = patch[2].vertex;
                float4 d = patch[3].vertex;

                float4 v0 = lerp(a,b,UV.x);
                float4 v1 = lerp(d,c,UV.x);
                float4 vFinal = lerp(v0,v1,UV.y);

                float2 uv0 = lerp(a,b,UV.x);
                float2 uv1 = lerp(d,c,UV.x);
                float2 uvFinal = lerp(uv0,uv1,UV.y);

                float3 n0 = cross(a.xyz-b.xyz,a.xyz-d.xyz);
                float4 normal = float4(normalize(n0),1);

                float scale = 0.00001;

                float x = UV.x*10 - 5;
                float y = UV.y*10 - 5;

                //float height = scale * (pow(x,3) + pow(y,3));

                int i = int(a.x);
                int j = int(a.y);



                float height = scale * (2*pow(x,2) + 0.5*pow(y,2)) + (a.x + a.y);






                //float height = params[1];

                appdata data; 
                data.vertex = vFinal + height * normal;
                data.uv = uvFinal;

                return VertexProgram(data);
            }


            fixed4 FragmentProgram(v2f i) : SV_TARGET
            {  
                //float4 col = tex2D(_MainTex, i.uv);
                fixed4 col = fixed4(0.5,0.8,0.4,1.0);
                return col;
            }

            ENDCG
        }
    }
}
$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.