Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Basic questions about shader!! (+ How to study)

Discussion in 'Shaders' started by kimhwinyen, Jul 9, 2022.

  1. kimhwinyen

    kimhwinyen

    Joined:
    Mar 11, 2022
    Posts:
    3
    Hello everyone!
    I just started learning Unity Shader this time, and I have a big question about the CG part of Shader Asset!

    Look at the pictures!
    Isn't float just a variable? But it behaves as a different value at every point on the object's surface.
    Also in o.Emission = ??.grb (using swizzling?), the value changes according to the position of each point.
    Especially when I was learning uv, I was curious why it appeared differently.

    If my thinking is correct, does the function run repeatedly at each position, at each point, and so on? If there is a material that uses that shader, I wonder if this shader will run this function over and over again for all points when the object is viewed through the camera!
    This may be a very easy or stupid question!

    I am watching through Korean lectures, and I feel the need for quality Unity Shader lectures!
    If there is a method you studied, please let me know!

    Thank you!!!!!!!
     

    Attached Files:

  2. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    637
    Disclaimer: I'm not a shader expert, feel free to correct me if there's any mistake!

    To answer your questions, let's focus on how the mesh is rendered.

    A mesh has many vertices, and each of them contains some information.
    (e.g. position of each vertex)
    Vertices-Data.jpg

    These information will be passed to the shader.

    There are two kinds of (basic) shaders:
    [Unity's surface shader surf() is like a pre-finished shader that you can add/change some steps ]
    • Vertex shader (3 times in the picture)
    • Fragment shader (15 times in the picture)
    Rasterization.jpg

    The vertex shader will convert vertices' 3D position to a "2D" position (clip space), so that fragment shader will know how many pixels it should draw (and where to draw).

    Note: the information that fragment shader requires is interpolated from nearby vertices.


    Go back to the questions!

    Yes, do you mean the "uv_MainTex"?

    UV ("uv_MainTex") is a coordinate (for each vertex). Shaders need this coordinate to "paste" textures onto the mesh correctly.

    Each fragment shader outputs a color.
    In your pictures, the shader finds a Color at the coordinate (UV*) in the Texture provided.
    tex2D()


    UV*: the UV that fragment shader requires is interpolated from nearby vertices' UV.

    For example, the color changes if we move the UV of one vertex:
    Mesh-UV1.jpg


    I guess you already know now.

    The "float4 MainTex" has different sampling result for different pixels, because their UVs are different.
     
    kimhwinyen likes this.
  3. kimhwinyen

    kimhwinyen

    Joined:
    Mar 11, 2022
    Posts:
    3
    Kudos to your answer! Thank you for your Answer!! I've been trying to learn about shader since the date of that question, and I've gotten somewhat familiar with how to use it and the syntax of CG and HLSL. And this answer helped me get my cornerstones right!

    may I ask how you learned your shader knowledge?
    Anyway, thank you so much!
     
  4. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    637
    Hi, I recommend catlike coding's rendering tutorials. (you can choose some of the parts that you are interested in)

    There're detailed explanations right after each part, so usually no need to google the reasons again.

    Also, I think it can be a good exercise to try to understand other people's shaders.
     
    kimhwinyen likes this.
  5. kimhwinyen

    kimhwinyen

    Joined:
    Mar 11, 2022
    Posts:
    3
    Thank you so much sir! I will study hard about It and I will refer that site!
     
    wwWwwwW1 likes this.