Search Unity

Shader Creation Help

Discussion in 'Shaders' started by 1druid1, Jul 14, 2016.

  1. 1druid1

    1druid1

    Joined:
    Dec 7, 2015
    Posts:
    12
    Hi All

    I currently use Blender to create fan art for EVE Online and I am looking into the realm of doing fan videos. Now I know I could do this all in Blender but I want to use Unity as its much easier and faster to script ships, weapons, turrets and visual effects to make battle videos.

    Now I have gotten most of it down apart from shaders for textures, which I just cant get the hang of and have no idea on how to program them, what I am looking for is any help with creating a shader that allows me to take an image with lets say, Black, White, Light Grey and Dark Grey and apply a colour individually to one of these, this would save me having to create images for every object via gimp.

    Is this possible?

    Cheers

    DJ
     
  2. BrandonFogerty

    BrandonFogerty

    Joined:
    Jan 29, 2016
    Posts:
    83
    This isn't the most optimized shader but it should get you started.

    Here is an image I made with photoshop.
    TestImage.png

    Here are the results when the shader is applied with the following material settings

    Screen Shot 2016-07-14 at 10.29.13 PM.png

    The colors are hardcoded in the shader. You can tune them to whatever you want. Then specify what those colors should be mapped to in the material settings.

    The following is the shader code.

    Code (CSharp):
    1. Shader "Unlit/ColorMapper"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _WhiteColor ("White Color", COLOR) = (1.0, 1.0, 1.0, 1.0)
    7.         _BlackColor ("Black Color", COLOR) = (0.0, 0.0, 0.0, 1.0)
    8.         _LightGreyColor ("Light Grey Color", COLOR) = (0.5, 0.5, 0.5, 1.0)
    9.         _DarkGreyColor ("Dark Grey Color", COLOR) = (0.3, 0.3, 0.3, 1.0)
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.  
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.            
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float4 vertex : SV_POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.  
    38.             float4 _WhiteColor;
    39.             float4 _LightGreyColor;
    40.             float4 _DarkGreyColor;
    41.             float4 _BlackColor;
    42.            
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    47.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    48.  
    49.                 return o;
    50.             }
    51.            
    52.             float3 frag (v2f i) : SV_Target
    53.             {
    54.                 // sample the texture
    55.                 float3 col = tex2D(_MainTex, i.uv);
    56.  
    57.                 float cl = length(col);
    58.                 float wl = length(float3(1.0, 1.0, 1.0));
    59.                 float lgl = length(float3(0.5, 0.5, 0.5));
    60.                 float dgl = length(float3(0.3, 0.3, 0.3));
    61.                 float bl = length(float3( 0.0, 0.0, 0.0 ));
    62.  
    63.  
    64.                 if( cl >= wl )
    65.                 {
    66.                     return _WhiteColor.rgb;
    67.                 }
    68.  
    69.                 if( cl >= lgl )
    70.                 {
    71.                     return _LightGreyColor.rgb;
    72.                 }
    73.  
    74.                 if( cl >= dgl )
    75.                 {
    76.                     return _DarkGreyColor.rgb;
    77.                 }
    78.  
    79.                 return _BlackColor.rgb;
    80.  
    81.             }
    82.             ENDCG
    83.         }
    84.     }
    85. }
    86.  
     
  3. 1druid1

    1druid1

    Joined:
    Dec 7, 2015
    Posts:
    12
    Hi, thanks for your shader example, it works as prescribed, unfortunately it doesn't do what I need, having pulled one of the images from a DDS file I checked out the colours and although it looks like 4 solid colours it isn't, they all have varying ranges within there colours although not easy to see, so instead of 4 colours there are many.

    Cheers

    DJ
     
  4. 1druid1

    1druid1

    Joined:
    Dec 7, 2015
    Posts:
    12
    Hi All

    On a side note what I am trying to achieve is to create a shader that is similar to the Blend File created by this user for Blender.

    https://forums.eveonline.com/default.aspx?g=posts&t=435376

    If there are any Blender / Unity 3D shader gurus, maybe you could have a look, it may be impossible or there may already be a shader in Unity for purchase that does the same that I am not aware off.

    Cheers

    DJ
     
  5. 1druid1

    1druid1

    Joined:
    Dec 7, 2015
    Posts:
    12
    Hi All

    I have spent sometime with my limited knowledge on the Blender Cycles shader and pulled the material apart.

    The material does the following.

    Takes in 3 dds files, now the issue with these files is to save texture space each dds file contains up to 4 separate textures stored in the R,G,B and Alpha channel.

    So dds 1 texture might contain the Albedo stored on R,G,B and roughness on the Alpha channel.
    dds 2 texture contains the Normal map on the Green and Alpha channel and the occlusion on the Blue
    dds 3 texture contains Paint on the Red, Material on the Green, Dirt on the Blue and Glow on the Alpha.

    Now the thing with all the images are none of them are colour, all images are greyscale images.

    The Albedo, Material, Dirt and Paint images can be individually coloured then these are layered to produce what we would class as a final diffuse image for the model.

    Now I think the way it works is the Albedo is the base layer, then material, Paint then dirt, each one of them can be coloured based on a range of greyscale, but there is also Alphas for transparency for the layering and opacity levels for each image.

    So from my basic description above is it possible to create a shader that can do all of the above obviously still containg the basics like Glow, Spec, Normal, Roughness ect.

    Cheers

    DJ