Search Unity

Reflective Material Only Covers A Bit Of The Model

Discussion in 'Editor & General Support' started by yair1221, Jan 18, 2013.

  1. yair1221

    yair1221

    Joined:
    Sep 28, 2011
    Posts:
    39
    Yeah, so...its kind of a wierd problem, heres the deal:

    i have a texture, that marks some areas as changeable colors, there is probably a name for that, i just dont know it.
    anyhow, when i change the Specular Color option, only the areas marked by the texture change their colors
    I actually thought of it as a good thing(marking teams, for instance, each team with its own colors)

    but today, i was trying to put a reflective material on the model - and only the areas mentioned before were reflective, everything else was dry as if it was a simple diffuse shader

    my question is: can i have the entire model reflective, and the selected area team-colored?
    actually, using the specular color doesn't exactly get me the wanted result, the team colors show only when looked upon from certain angles... is there a way to make the team colors permanent, and make the entire model reflective?
     
  2. yair1221

    yair1221

    Joined:
    Sep 28, 2011
    Posts:
    39
  3. yair1221

    yair1221

    Joined:
    Sep 28, 2011
    Posts:
    39
    uhm...bump?
     
  4. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    With Unity's built-in reflective shader reflectivity is controlled by the diffuse textures alpha channel.
     
  5. yair1221

    yair1221

    Joined:
    Sep 28, 2011
    Posts:
    39
    oh sh*t...so, is there a way to have both team colors and reflectivity? will it require a custom shader or is it simply not possible?
     
  6. Molt

    Molt

    Joined:
    Sep 6, 2011
    Posts:
    103
    It'll be doable with a custom shader. I'd recommend taking the source to the shader you're currently using and just tweaking it a little for this, adding a new reflectivity float parameter.
     
  7. yair1221

    yair1221

    Joined:
    Sep 28, 2011
    Posts:
    39
    damn, well i tried mixing the diffuse shader with a reflective shader and a bumped shader...all i got was tons of errors :p
     
  8. Molt

    Molt

    Joined:
    Sep 6, 2011
    Posts:
    103
    Decided it was too long since I've played with textures and so I've written something I think will meet your needs, although it is more complex and more flexible than you asked for. It's not yet perfect but I don't have more time to spend on it at the moment.

    It supports three different team colours for each material, with a single texture map controlling the mixing between them by using the R, G, and B channels. The alpha of the team colour map controls the overall contribution of the texture map. The actual team colours themselves are parameters at the material level so you can use the same maps to control

    If you just want to just do a single team colour then draw it in pure opaque red, on a transparent background, and set the "Team Color 1" parameter to the colour you want to use.. for the other two colours use pure green and blue. The colours can be mixed but to be honest I can't think of many times you'd need that.

    The shader is here:
    Code (csharp):
    1. Shader "Custom/Team Color Shader" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    5.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    6.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    7.     _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
    8.     _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
    9.     _BumpMap ("Normalmap", 2D) = "bump" {}
    10.     _TeamColorMap("Team Color Map", 2D) = "black" {}
    11.     _TeamColor1("Team Color 1", Color) = (1, 0.5, 0.5, 1)
    12.     _TeamColor2("Team Color 2", Color) = (0.5, 1, 0.5, 1)
    13.     _TeamColor3("Team Color 3", Color) = (0.5, 0.5, 1, 1)
    14. }
    15.  
    16. SubShader {
    17.     Tags { "RenderType"="Opaque" }
    18.     LOD 400
    19. CGPROGRAM
    20. #pragma surface surf BlinnPhong
    21. #pragma target 3.0
    22. //input limit (8) exceeded, shader uses 9
    23. #pragma exclude_renderers d3d11_9x
    24.  
    25. sampler2D _MainTex;
    26. sampler2D _BumpMap;
    27. sampler2D _TeamColorMap;
    28. samplerCUBE _Cube;
    29. float3 _TeamColor1;
    30. float3 _TeamColor2;
    31. float3 _TeamColor3;
    32.  
    33. fixed4 _Color;
    34. fixed4 _ReflectColor;
    35. half _Shininess;
    36.  
    37. struct Input {
    38.     float2 uv_MainTex;
    39.     float2 uv_BumpMap;
    40.     float3 worldRefl;
    41.     INTERNAL_DATA
    42. };
    43.  
    44. void surf (Input IN, inout SurfaceOutput o) {
    45.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    46.     fixed4 teamColorLookup = tex2D(_TeamColorMap, IN.uv_MainTex);
    47.    
    48.     fixed3 c = tex.rgb * _Color.rgb;
    49.     fixed3 combinedTeamColor = (
    50.         _TeamColor1 * teamColorLookup.r +
    51.         _TeamColor2 * teamColorLookup.g +
    52.         _TeamColor3 * teamColorLookup.b
    53.     ) / (teamColorLookup.r + teamColorLookup.g + teamColorLookup.b + 0.001);
    54.     c = lerp(c, combinedTeamColor, teamColorLookup.a);
    55.  
    56.     o.Albedo = c.rgb;
    57.    
    58.     o.Gloss = tex.a;
    59.     o.Specular = _Shininess;
    60.    
    61.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    62.    
    63.     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    64.     fixed4 reflcol = texCUBE (_Cube, worldRefl);
    65.     reflcol *= tex.a;
    66.     o.Emission = reflcol.rgb * _ReflectColor.rgb;
    67.     o.Alpha = reflcol.a * _ReflectColor.a;
    68. }
    69. ENDCG
    70. }
    71.  
    72. FallBack "Reflective/Bumped Diffuse"
    73. }
    74.  
    ...and a test project, including the shader, is attached here- View attachment $teamcolor_texture_test.zip
     
  9. yair1221

    yair1221

    Joined:
    Sep 28, 2011
    Posts:
    39
    hey thanks :)
    the team colors work like charm, the reflection however doesn't work, it just seems to make the team colors stronger...
    my guess is that the reflection data is taken from the texture's alpha, so basically it only reflects the location of the team colors, ill try and upload a picture in a few minutes

    EDIT::
    without reflection map:
    $noreflect.JPG
    with reflection map:
    $withreflect.JPG
     
    Last edited: Feb 22, 2013
  10. Molt

    Molt

    Joined:
    Sep 6, 2011
    Posts:
    103
    The reflection's glossiness isn't in the Team Color Map, it's the alpha of the main colour texture- the one controlled by 'Base (RGB) RefStrGloss (A)'. I'm currently reinstalling my machine so I can't check it at the moment but I'll have a look later. It is the same code as the original Unity shader I based this all on though so I think it should work unless I've gone and broken it by accident.
     
  11. Molt

    Molt

    Joined:
    Sep 6, 2011
    Posts:
    103
    Okay, had time to have a look at this and now have a version which seems to work. This version allows you to control the reflection by using the alpha in the diffuse map (The letter 'R' in the demo scene), and also the alpha of the reflectivity colour. There's a new version of the demo scene too: View attachment $Teamcolor Texture Test.7z
    $SShot.png

    Code (csharp):
    1. Shader "Custom/Team Color Shader" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    5.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    6.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    7.     _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
    8.     _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
    9.     _BumpMap ("Normalmap", 2D) = "bump" {}
    10.     _TeamColorMap("Team Color Map", 2D) = "black" {}
    11.     _TeamColor1("Team Color 1", Color) = (1, 0.5, 0.5, 1)
    12.     _TeamColor2("Team Color 2", Color) = (0.5, 1, 0.5, 1)
    13.     _TeamColor3("Team Color 3", Color) = (0.5, 0.5, 1, 1)
    14. }
    15.  
    16. SubShader {
    17.     Tags { "RenderType"="Opaque" }
    18.     LOD 400
    19. CGPROGRAM
    20. #pragma surface surf BlinnPhong
    21. #pragma target 3.0
    22. //input limit (8) exceeded, shader uses 9
    23. #pragma exclude_renderers d3d11_9x
    24.  
    25. sampler2D _MainTex;
    26. sampler2D _BumpMap;
    27. sampler2D _TeamColorMap;
    28. samplerCUBE _Cube;
    29. float3 _TeamColor1;
    30. float3 _TeamColor2;
    31. float3 _TeamColor3;
    32.  
    33. fixed4 _Color;
    34. fixed4 _ReflectColor;
    35. half _Shininess;
    36.  
    37. struct Input {
    38.     float2 uv_MainTex;
    39.     float2 uv_BumpMap;
    40.     float3 worldRefl;
    41.     INTERNAL_DATA
    42. };
    43.  
    44. void surf (Input IN, inout SurfaceOutput o) {
    45.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    46.     fixed4 teamColorLookup = tex2D(_TeamColorMap, IN.uv_MainTex);
    47.    
    48.     fixed3 c = tex.rgb * _Color.rgb;
    49.     fixed3 combinedTeamColor = (
    50.         _TeamColor1 * teamColorLookup.r +
    51.         _TeamColor2 * teamColorLookup.g +
    52.         _TeamColor3 * teamColorLookup.b
    53.     ) / (teamColorLookup.r + teamColorLookup.g + teamColorLookup.b + 0.001);
    54.     c = lerp(c, combinedTeamColor, teamColorLookup.a);
    55.  
    56.     o.Albedo = c.rgb;
    57.    
    58.     o.Gloss = 0.5; // tex.a;
    59.     o.Specular = _Shininess;
    60.    
    61.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    62.    
    63.     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    64.    
    65.     fixed4 reflcol = texCUBE (_Cube, worldRefl);
    66.     reflcol *= tex.a * _ReflectColor.a;
    67.     o.Emission = reflcol.rgb * _ReflectColor.rgb;
    68.  
    69.     o.Alpha = reflcol.a * _ReflectColor.a;
    70. }
    71. ENDCG
    72. }
    73.  
    74. FallBack "Reflective/Bumped Diffuse"
    75. }
     
  12. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    So, this could be used to make only some parts of a texture reflective using the alpha channel correct? Does unity4 not have something that does this built in?
     
  13. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    The built-in Reflective shaders already do that. The thing that yair1221 needed was something that supported that and recoloring, which is what Molt has written here.
     
  14. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Ah, I see now. Thanks for clarifying. So, the built in shader for using alpha channel for reflective, is the bumped diffuse specular or something else?
     
  15. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    I think all the Reflective shaders do it. For example, Reflective/Diffuse's first texture slot says "Base (RGB) RefStrength (A)" meaning that the RGB channels of that texture will be used for color, while the alpha channel will be used for reflection strength.