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

Newbie GLSL to ShaderLab question

Discussion in 'Shaders' started by neztypezero, May 25, 2013.

  1. neztypezero

    neztypezero

    Joined:
    May 25, 2013
    Posts:
    1
    Hi I am a complete Unity newbie. I just downloaded it today. I wrote a simple game in Objective C for iOS and now I am trying to rewrite it using Unity basically just for fun and see if I can get it to run on my brother's Android phone.

    Anyway I have a texture with some transparent parts and a model with certain vertex colours which I want to blend with the texture. The vertex colours have no alpha. So a place in the texture with 0 alpha would use just the vertex colour and if it was 1 it would use just the texture colour.

    Here is the GLSL fragment shader:

    Code (csharp):
    1. uniform sampler2D u_texUnit;
    2.  
    3. varying mediump vec2 v_uv;
    4. varying mediump vec3 v_color;
    5.  
    6. void main(void) {
    7.     mediump vec4 texturePixel = texture2D(u_texUnit, v_uv);
    8.     mediump vec3 color = (((v_color)*(1.0-texturePixel.a))+(texturePixel.rgb*texturePixel.a));
    9.     gl_FragColor = vec4(color, 1.0);
    10. }
    11.  
    Can anyone tell me the best way to rewrite this in Unity Shading?

    Thanks.
     
    Last edited: May 25, 2013
  2. Elehe

    Elehe

    Joined:
    May 19, 2013
    Posts:
    14
    Code (csharp):
    1. Shader "Custom/NewShader" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags { "RenderType"="Opaque" }
    7.         LOD 200
    8.        
    9.         CGPROGRAM
    10.         #pragma surface surf Lambert
    11.  
    12.         sampler2D _MainTex;
    13.  
    14.         struct Input {
    15.             float2 uv_MainTex;
    16.             float4 color : COLOR;
    17.         };
    18.  
    19.         void surf (Input IN, inout SurfaceOutput o) {
    20.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    21.             o.Albedo = IN.color * (1.0-c.a) + c.rgb * c.a;
    22.         }
    23.         ENDCG
    24.     }
    25.     FallBack "Diffuse"
    26. }
    27.  
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I don't understand why you were using mediump.