Search Unity

newbie questions

Discussion in 'Shaders' started by Boonalot, Sep 8, 2017.

  1. Boonalot

    Boonalot

    Joined:
    Sep 8, 2017
    Posts:
    3
    Hey :)
    I watched some Turorials about making shaders, because i want some fancy effects on an Avatar model i made for VRChat...i especially liked
    , where some guy makes an animated UI shader. Now as soon as i start modifing shader examples i get stuck everywhere, like an unlit shader doesnt cast shadows and such, i digged a little more into and the more i do the more i get confused, im totally new to the subject of scripting and even though i already have some basic knowledge from looking up documentations and other tutorials , i dont really know where to start experimenting and coding.
    What i want to achieve is a shader that makes the texture of the model move randomly a little ( i understood the concept of what the guy did in the Video) and if thats possible i would like to move the skin randomly a little over the vertex function as well, but of course i want it to cast and receive shadows too just like in the normal surface shader.Anyway ...even one of those functions would be great to have.
    Now first of all i want to know if its possible to make such shader, then i would like to know from where to start, like what kind of basic script to pick to start working on it, then of course any further advise would be helpfull, like what are the things i will need to set up/implement to have the basic structure that can make it work, i looked up the examples in the Manual and even though it explains how to get shadows cast on a vertex fragment shader, the result doesnt quite look like what i can take to start with, or maybe a little , you could tell me if im on the right track with that shader type, i already compared the structure of shader examples to get a clue, but i also got very confused, while the surface default shader looks so simple , the way to get to have an unlit shader example cast and receive shadows looked complicated and gave me the opinion that what i want to do is way more complicated then what the guy in the video did with the UI shader and will contain steps and phrases and other stuff i cant set up right yet, even though i already understood the basic concept of what goes where and such.
    So ALL kind of help would be useful. I first thought hey this looks pretty easy and is understandable i can do that, but like i said when i started out i got stuck and the more i looked up the more confused i got.If its necessary im willing to learn a lot about scripting , if you could point me to what i have to know to make something like i want, even though i was hoping i could just do what the guy did, and what was pretty easy to understand, but now looks as if its only easy because its a script that is simple by itself , and the way the UI element is definded makes it easy to be modified without adding complicted stuff. So PLEASE help me in any direction as im out of ideas what to do next.Im very thankfull for every answer i could get, so please leave me a comment.
     
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Shader programming is not a profession, which can be mastered in one day.
    So first, you should learn basics, step by step.

    Here you are, basic texture distortion shader. Assign the shader to material, then set material to your game object. Feel free to experiment with variables A , B, and the most important 39th line of code.


    Source:
    https://github.com/przemyslawzaworski/Unity3D-CG-programming/blob/master/distortion.shader

    Code (CSharp):
    1. Shader "Distortion"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         A("A",Range(1.0,20.0)) = 5.0  
    7.         B("B",Range(1.0,20.0)) = 5.0          
    8.     }
    9.     Subshader
    10.     {
    11.         Pass
    12.         {
    13.             Cull off
    14.             CGPROGRAM
    15.             #pragma vertex vertex_shader
    16.             #pragma fragment pixel_shader
    17.             #pragma target 2.0
    18.  
    19.             sampler2D _MainTex;
    20.             float A,B;
    21.          
    22.             struct custom_type
    23.             {
    24.                 float4 vertex : SV_POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.          
    28.             custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
    29.             {
    30.                 custom_type vs;
    31.                 vs.vertex = UnityObjectToClipPos (vertex);
    32.                 vs.uv = uv;
    33.                 return vs;
    34.             }
    35.  
    36.             float4 pixel_shader (custom_type ps) : COLOR
    37.             {
    38.                 float2 uv = ps.uv.xy;
    39.                 uv.y+=cos(uv.x*A+_Time.g)/B;
    40.                 return tex2D(_MainTex,uv);  
    41.             }
    42.             ENDCG
    43.         }
    44.     }
    45. }
     
  3. bgolus

    bgolus

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

    Boonalot

    Joined:
    Sep 8, 2017
    Posts:
    3
    Last edited: Sep 8, 2017