Search Unity

Incoming verts in world matrix as well as additional geometry

Discussion in 'Shaders' started by MESJM1, Aug 20, 2019.

  1. MESJM1

    MESJM1

    Joined:
    Aug 4, 2018
    Posts:
    2
    Is it possible to have the verts in world matrix coordinates as well as the additional geometry? I would like to animate both using world coordinate values. Anything I have tried so far seems to clash with the geometry shader.

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/UnlitShaderHair2"
    4. {
    5.  
    6.  
    7.  
    8.     Properties
    9.     {
    10.    
    11.        
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" "DisableBatching" = "true" }
    16.         LOD 100
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21. // Upgrade NOTE: excluded shader from DX11, OpenGL ES 2.0 because it uses unsized arrays
    22.  
    23. //#pragma exclude_renderers d3d11 gles
    24.             #pragma vertex vert  
    25.             #pragma hull HS
    26.             #pragma domain DS
    27.             #pragma geometry MyGeometryShader
    28.             #pragma fragment frag
    29.        
    30.             // make fog work
    31.             #pragma multi_compile_fog
    32.             #include "UnityCG.cginc"
    33.  
    34.  
    35.  
    36.      struct HairPoints3
    37. {
    38.  
    39.  
    40.  
    41.      float x0; // The base vertex position of the hair
    42.      float y0; // The base vertex position of the hair
    43.      float z0; // The base vertex of position the hair
    44.      float x1; // The Second vertex of the hair
    45.      float y1; // The Second vertex of the hair
    46.      float z1; // The Second vertex of the hair
    47.  
    48.      float x2; // The third vertex of the hair
    49.      float y2; // The third vertex of the hair
    50.      float z2; // The third vertex of the hair
    51.  
    52. };
    53.  
    54.            
    55.     StructuredBuffer<HairPoints3> HairPointsBuffer;
    56.  
    57.  
    58.    
    59.                 struct appdata
    60.             {
    61.                 float4 vertex : POSITION;
    62.                 float2 uv : TEXCOORD0;
    63.                 float4 C : COLOR;
    64.  
    65.             };
    66.  
    67.             struct v2f
    68.             {
    69.                 float2 uv : TEXCOORD0;
    70.                 UNITY_FOG_COORDS(1)
    71.                 float4 vertex : POSITION;
    72.                 float4 C : COLOR;
    73.             };
    74.            
    75.    
    76.     struct HSConstantOutPut
    77.     {
    78.    
    79.    
    80.     float edges[2] : SV_TessFactor;
    81.        
    82.     };
    83.  
    84.     struct HSOutput
    85.  
    86.     {
    87.    
    88.     float3 CPoint : CPOINT;
    89.        
    90.    
    91.     };
    92.  
    93.        
    94.     struct GSOut
    95.     {
    96.             float4 Pos : SV_POSITION;
    97. //            float2 uv : TEXCOORD0;
    98.             float4 C : COLOR;
    99.  
    100.         //    UNITY_FOG_COORDS(1)
    101.     };
    102.  
    103.  
    104.     struct DSOutput
    105.     {
    106.         float4 Position : SV_Position;
    107.    
    108.    
    109.     };
    110.  
    111.  
    112.  
    113.  
    114.  
    115.             sampler2D _MainTex;
    116.             float4 _MainTex_ST;
    117.         //    fixed4 Color;
    118.        
    119.                    
    120.  
    121.  
    122.             ///////////////////////////
    123.             //Vertex Shader//
    124.             //////////////////////////
    125.  
    126.  
    127.             v2f vert (appdata v,uint vid : SV_VertexID)  
    128.             {
    129.                    v2f o;
    130.             o.vertex = v.vertex;
    131.             o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    132.             o.C = float4(1,0,0,1);
    133.        
    134.                                                            
    135.                 return o;
    136.                                
    137.             }
    138.            
    139.             ///////////////////////////
    140.             // My Constant Hull Shader//
    141.             //////////////////////////
    142.  
    143.             HSConstantOutPut HSConst(InputPatch<v2f,4> patch, uint patchID : SV_PrimitiveID)
    144.            
    145.             {
    146.              HSConstantOutPut Output;
    147.  
    148.              Output.edges[0] = 1.0f; // Line amount
    149.              Output.edges[1] = 64.0f; // line tessellation amount
    150.            
    151.             return Output;
    152.            
    153.             }
    154.  
    155.             /////////////////////////////
    156.             // My Hull Shader//
    157.             /////////////////////////////
    158.  
    159.             [domain("isoline")]
    160.             [partitioning("integer")]
    161.             [outputtopology("line")]
    162.             [outputcontrolpoints(4)]
    163.             [patchconstantfunc("HSConst")]
    164.             HSOutput HS(InputPatch<v2f,4> ip, uint id :SV_OutputControlPointID)
    165.             {
    166.            
    167.             HSOutput Output;
    168.             Output.CPoint = ip[id].vertex.xyz;
    169.             return Output;
    170.  
    171.            
    172.             }
    173.  
    174.             ////////////////////////////////////
    175.             // My Domain Shader//
    176.             ////////////////////////////////////
    177.  
    178.             [domain("isoline")]
    179.             DSOutput DS(HSConstantOutPut In, float2 uv : SV_DomainLocation, const OutputPatch<HSOutput,4> Op)
    180.             {
    181.            
    182.             DSOutput Output;
    183.            
    184.             float t = uv.x;
    185.            
    186.             float3 Pos = pow(1.0f-t ,3.0f) * Op[0].CPoint + 3.0f * pow(1.0f - t, 2.0f) * t * Op[1].CPoint + 3.0f* (1-t) * pow(t,2.0f) * Op[2].CPoint + pow(t,3.0f)*Op[3].CPoint;
    187.  
    188.  
    189.  
    190.             Output.Position = float4(Pos,1.0f);
    191.             return Output;
    192.                        
    193.            
    194.             }
    195.  
    196.  
    197.  
    198.  
    199.             //////////////////////////////////////////////////////////////
    200.             //GEOMETRY SHADER SITS HERE!//
    201.             /////////////////////////////////////////////////////////////
    202.  
    203.             [maxvertexcount (3)]
    204.             void MyGeometryShader(line DSOutput GeometryIn[2], inout LineStream<GSOut> TriStream , uint PrimID : SV_PrimitiveID)
    205. {
    206.            
    207.             GSOut Out;
    208.  
    209. // Additional "Hairs" are created at this point......
    210.    
    211.        
    212.    
    213.     float3 POS =     mul(unity_ObjectToWorld, float4(GeometryIn[0].Position.xyz,1) ).xyz;
    214.     POS += float3(HairPointsBuffer[0].x0,  HairPointsBuffer[0].y0,HairPointsBuffer[0].z0) ;
    215.     Out.Pos = mul(UNITY_MATRIX_VP,float4(POS, 1 ) );
    216.  
    217.     Out.C = float4(1,0,1,1);
    218.     TriStream.Append(Out);
    219.                
    220.  
    221.  
    222.  
    223.     float3 POS1 =     mul(unity_ObjectToWorld, float4(GeometryIn[0].Position.xyz,1) ).xyz;
    224.    
    225.     POS1 += float3(HairPointsBuffer[0].x1,  HairPointsBuffer[0].y1,HairPointsBuffer[0].z1) ;
    226.     Out.Pos = mul(UNITY_MATRIX_VP,float4(POS1, 1 ) );
    227.  
    228.     Out.C = float4(1,1,0,1);
    229.     TriStream.Append(Out);
    230.                
    231.  
    232.  
    233.    float3 POS2 =     mul (unity_ObjectToWorld, float4( GeometryIn[0].Position.xyz,1) ).xyz;
    234.      
    235.     POS2 += float3(HairPointsBuffer[0].x2,  HairPointsBuffer[0].y2,HairPointsBuffer[0].z2) ;
    236.     Out.Pos = mul(UNITY_MATRIX_VP, float4(POS2,1));
    237.  
    238.     Out.C = float4(1,0,1,1);
    239.  
    240. TriStream.Append(Out);          
    241.  
    242.  
    243.  
    244.     }
    245.  
    246.  
    247.  
    248.  
    249.             ///////////////////////////////////////////
    250.             // This is our pixel shader.//
    251.             //////////////////////////////////////////
    252.  
    253.  
    254.             fixed4 frag (GSOut i) : SV_Target
    255.             {
    256.        
    257.                                    
    258.  
    259.         return i.C;
    260.  
    261.             //    return Color;
    262.             }
    263.             ENDCG
    264.         }
    265.     }
    266. }
    267.