Search Unity

Custom V Shader, Manipulate Instance Variable further

Discussion in 'Shaders' started by Desertleaf, Jun 11, 2019.

  1. Desertleaf

    Desertleaf

    Joined:
    Dec 15, 2013
    Posts:
    15
    Unity Question Here:

    When a Variable Property is tagged to be a GPU instancing property, how do i take that variable to manipulate further within a custom vertex shader?

    UNITY_INSTANCING_BUFFER_START(Props)

    UNITY_DEFINE_INSTANCED_PROP(float4, _Color)

    UNITY_INSTANCING_BUFFER_END(Props)

    _____

    So far Props._Color doesnt work because this is not a C# script. its a shader.

    All i am doing is trying to multiply a 2D texture to it, but I get an error saying _Color is undeclared, and this is because its currently considered as a Unity GPU instance variable now.

    Any suggestions, Tips, clues, etc???


    in example:

    float3 diffuseColor = (_MainTex_var.rgb *_Color.rgb);

    How can I use the instance variable instead?
     
  2. Desertleaf

    Desertleaf

    Joined:
    Dec 15, 2013
    Posts:
    15
    i have tried writing

    float3 diffuseColor = _MainTex.var.rgb * UNITY_ACCESS_INSTANCED_PROP(Props, _Color);

    Spits out Error: "undeclared identifier 'Props' "
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
  4. Desertleaf

    Desertleaf

    Joined:
    Dec 15, 2013
    Posts:
    15
    unfortunately I cant post the shader due to an intellectual property agreement, but I can say is that its completely custom without any pre-coded Unity Surfaces.

    It uses Structs for Vertex Inputs, outputs, and Fragment shader. As of now, its completely functional with GPU instancing, but I am trying to make a specific property capable of being instanced. In this case _Color.

    The unity Fragment Example returns their value, but I am returning a Final result instead.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You mean you’re using a surface shader? Again, the documentation has an example of how to use it in a surface shader. And you can use the UNITY_ACCESS_INSTANCED_PROP anywhere to access that value, be it in the surf function, custom vertex function, final function, etc. It makes no difference. If you’re using those macros correctly then when instancing isn’t being used the variable is declared as a normal uniform. If it’s using instancing then the uniform is declared as an array, and the access macro uses the current instance ID as the index into the array. The instance ID is automatically defined at the start of the shader stage, so it can be accessed at any time.


    If you’re using a vertex fragment shader, it doesn’t have a “final result” apart from the fragment’s output.
     
  6. Desertleaf

    Desertleaf

    Joined:
    Dec 15, 2013
    Posts:
    15
    Here are snippets


    CGPROGRAM

    #pragma vertex vert
    #pragma fragment frag
    #define UNITY_PASS_FORWARDBASE
    #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
    #define _GLOSSYENV 1
    #include "UnityCG.cginc"
    #include "AutoLight.cginc"
    #include "Lighting.cginc"
    #include "UnityPBSLighting.cginc"
    #include "UnityStandardBRDF.cginc"
    #include "UnityInstancing.cginc" //ADDED
    #pragma multi_compile_fwdbase_fullshadows
    #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
    #pragma multi_compile_fog
    #pragma multi_compile_instancing // ADDED
    #pragma only_renderers d3d9 d3d11 glcore gles
    #pragma target 3.0


    uniform sampler2D _MainTex;

    UNITY_INSTANCING_BUFFER_START(Props)

    UNITY_DEFINE_INSTANCED_PROP(float4, _Color)

    UNITY_INSTANCING_BUFFER_END(Props)


    struct VertexInput
    {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    float4 tangent : TANGENT;
    float2 texcoord0 : TEXCOORD0;
    float2 texcoord1 : TEXCOORD1;
    float2 texcoord2 : TEXCOORD2;

    UNITY_VERTEX_INPUT_INSTANCE_ID // added
    };

    struct VertexOutput
    {
    float4 pos : SV_POSITION;
    float2 uv0 : TEXCOORD0;
    float2 uv1 : TEXCOORD1;
    float2 uv2 : TEXCOORD2;
    float4 posWorld : TEXCOORD3;
    float3 normalDir : TEXCOORD4;
    float3 tangentDir : TEXCOORD5;
    float3 bitangentDir : TEXCOORD6;
    LIGHTING_COORDS(7,8)
    UNITY_FOG_COORDS(9)
    #if defined(LIGHTMAP_ON) || defined(UNITY_SHOULD_SAMPLE_SH)
    float4 ambientOrLightmapUV : TEXCOORD10;
    #endif

    UNITY_VERTEX_INPUT_INSTANCE_ID //added
    };



    VertexOutput vert (VertexInput v)
    {
    UNITY_SETUP_INSTANCE_ID (v); //added
    VertexOutput o = (VertexOutput)0;
    UNITY_TRANSFER_INSTANCE_ID(v,o); //added
    ///
    ///SomeStuff
    ///

    return o;
    }


    float4 frag(VertexOutput i) : COLOR
    {

    UNITY_SETUP_INSTANCE_ID(i); //add

    ///
    /// LOTS OF STUFF
    ///
    float3 diffuseColor = _MainTex_var.rgb *UNTIY_ACCESS_INSTANCED_PROP(Props, _Color).rgb;

    ///
    /// MORE STUFF
    ///
    return finalRGBA;

    }
    ENDCG

    /// ERROR undeclared Identifier " Props"
     
  7. Desertleaf

    Desertleaf

    Joined:
    Dec 15, 2013
    Posts:
    15
    yes there is a property block before this
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Everything looks correct, except:
    That’s misspelled.
     
    Desertleaf likes this.
  9. Desertleaf

    Desertleaf

    Joined:
    Dec 15, 2013
    Posts:
    15
    :eek:DUDE! you solved the problem!

    I had this up on a facebook group and no one could crack it.

    Your eagle eyes did what many could not!

    any pro tips?? :D
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Always ... no, no ... never forget to check your references.
     
    nikk98 and Desertleaf like this.