Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Move Vertex of Cube (Vertex - Frag) Program

Discussion in 'Shaders' started by m-y, Mar 16, 2020.

  1. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    Hello All
    i am still working on learning writing shaders
    i have some questions confusing me .
    Are shaders always need script C# To make it life ?
    How to get a specific vertex using shader code ? like in the below image i want to play with this vertex only ? upload_2020-3-17_1-26-42.png
    i thought that i can write shader that can move or rotate or change colors without any code !
    second point i am trying to play with Cuber Vertex i want to tie it or move it or make it stretch
    as i know i can access model vertices
    every vertex is point between two Edges
    so what i am doing wrong here
    that is a simple cube i want to stretch or playing with it's corner vertex marked by a circle
    and that is my shader code

    what is wrong ? what i need more ?

    Code (CSharp):
    1. Shader "Custom/MostafaYahia2"
    2. {
    3.    Properties {
    4.  
    5.    _MainTexture("Main Texture",2D) = "White"{}
    6.       _SecondTexture("Second Texture",2D) = "White"{}
    7.       _ThirdTexture("Third Texture",2D) = "White"{}
    8.  
    9.    _Color("Main Color",COLOR) = (1,1,1)
    10.    _Amount ("Amount",Range(0,10)) =1
    11.    }
    12.    Category
    13.    {
    14.    Tags { "RenderType"="Opaque" }
    15.    SubShader
    16.    {
    17.    Pass
    18.    {
    19.    CGPROGRAM
    20.    #pragma vertex vertFunc ;
    21.    #pragma fragment fragFunc ;
    22.    #include "UnityCG.cginc"
    23.  
    24.    sampler2D _MainTexture ;
    25.    sampler2D _SecondTexture ;
    26.    sampler2D _ThirdTexture ;
    27.    float4 _Color ;
    28.    float4 _Amount ;
    29.    struct LocalVertex
    30.    {
    31.    fixed4 vertex : POSITION ;
    32.    fixed2 uvmain : TEXCOORD0 ;
    33.  
    34.  
    35.    };
    36.  
    37.    struct GlobalVertex
    38.    {
    39.    fixed4 vertex : SV_POSITION ;
    40.    fixed2 uv : TEXCOORD0 ;
    41.    fixed2 uvsecond :TEXCOORD1 ;
    42.       fixed2 uvthird :TEXCOORD2 ;
    43.  
    44.    };
    45.  
    46.  
    47.  
    48.       fixed4 _MainTexture_ST ;
    49.             fixed4 _SecondTexture_ST ;
    50.             fixed4 _ThirdTexture_ST;
    51.  
    52.   GlobalVertex vertFunc ( appdata_full local)
    53.    {
    54.    GlobalVertex global ;
    55.    global.vertex = UnityObjectToClipPos(local.vertex);
    56. // global.uv = TRANSFORM_TEX(local.uvmain,_MainTexture);
    57. //  global.uvsecond = TRANSFORM_TEX(local.uvmain,_SecondTexture) ;
    58.     //  global.uvthird = TRANSFORM_TEX(local.uvmain,_ThirdTexture) ;
    59.     local.vertex.x += _Amount ;
    60.  
    61.  
    62.    return global;
    63.    }
    64.  
    65.  
    66. fixed4  fragFunc (GlobalVertex global) : COLOR
    67.    {
    68.    fixed4 x  = tex2D(_MainTexture,global.uv);
    69.    fixed4 y = tex2D(_SecondTexture,global.uvsecond);
    70.    fixed4 z = tex2D(_ThirdTexture,global.uvthird);
    71.   x = lerp(y,x,z);
    72.    return  x*_Color;
    73.    }
    74.  
    75.  
    76.    ENDCG
    77.  
    78.  
    79.    }
    80.    }
    81. }
    82. }
    83.  
    upload_2020-3-17_1-26-42.png
     
  2. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    ???
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    A vertex function acts on a single vertex at a time with no knowledge of anything but that vertex’s data and the material properties. If you want to move a specific vertex, you can use the
    SV_VertexID
    semantic to know which vertex you’re currently processing to do something unique to it. You could use the vertex ID to check against a hard coded value or a material property and then do the offset, or pass the material a vector array with unique offsets per vertex.

    You then need to do those offsets prior to setting the vertex shader’s output value.