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

Reflective/Cubemap shader WITH Self Illumination

Discussion in 'Shaders' started by LightningXCE, Dec 8, 2010.

  1. LightningXCE

    LightningXCE

    Joined:
    Sep 21, 2010
    Posts:
    7
    Hello,
    I've been trying to combine the basic concept of having the current reflective/bumped shader and the self-illumination shader and I have come up dry. My current shader gives an error and I was hoping someone might be able to offer a different insight into it.

    Code (csharp):
    1. Shader "SelfIllum/Reflective/Bumped Specular" {
    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.     _Illum ("Illumin (A)", 2D) = "white" {}
    9.     _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
    10.     _BumpMap ("Normalmap", 2D) = "bump" {}
    11. }
    12.  
    13. SubShader {
    14.     Tags { "RenderType"="Opaque" }
    15.     LOD 400
    16. CGPROGRAM
    17. #pragma surface surf BlinnPhong
    18. #pragma target 3.0
    19.  
    20. sampler2D _MainTex;
    21. sampler2D _BumpMap;
    22. sampler2D _Illum;
    23. samplerCUBE _Cube;
    24.  
    25. float4 _Color;
    26. float4 _ReflectColor;
    27. float _Shininess;
    28.  
    29. struct Input {
    30.     float2 uv_MainTex;
    31.     float2 uv_Illum;
    32.     float2 uv_BumpMap;
    33.     float3 worldRefl;
    34.     INTERNAL_DATA
    35. };
    36.  
    37. void surf (Input IN, inout SurfaceOutput o) {
    38.     half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    39.     half4 illum = tex2D(_Illum, IN.uv_Illum);
    40.     half4 c = tex * _Color;
    41.     o.Albedo = c.rgb;
    42.    
    43.     o.Gloss = tex.a;
    44.     o.Specular = _Shininess;
    45.    
    46.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    47.    
    48.     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    49.     half4 reflcol = texCUBE (_Cube, worldRefl);
    50.     reflcol *= tex.a;
    51.     o.Emission = reflcol.rgb *illum.a * _ReflectColor.rgb;
    52.     o.Alpha = reflcol.a * _ReflectColor.a;
    53. }
    54. ENDCG
    55. }
    56.  
    57. FallBack "Reflective/Bumped Diffuse"
    58. }
    Thanks!
     
  2. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    what does the error say?
     
  3. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    comment out
    //float2 uv_Illum;
    and use uv_MainTex instead - then it will compile OK.
     
  4. LightningXCE

    LightningXCE

    Joined:
    Sep 21, 2010
    Posts:
    7
    Ah, thank you! I actually modified it a bit more because that would also modify how the reflection map is drawn. Now, I simply made an additive shader that takes in RGB values from the illumination bitmap and applies those directly to it. Here it is if anyone wants it:

    Code (csharp):
    1. Shader "Self-Illumin/Reflective Bumped Additive" {
    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.     _Illum ("Illumin (RGB)", 2D) = "black" {}
    9.     _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
    10.     _BumpMap ("Normalmap", 2D) = "bump" {}
    11. }
    12.  
    13. SubShader {
    14.     Tags { "RenderType"="Opaque" }
    15.     LOD 400
    16. CGPROGRAM
    17. #pragma surface surf BlinnPhong
    18. #pragma target 3.0
    19.  
    20. sampler2D _MainTex;
    21. sampler2D _BumpMap;
    22. sampler2D _Illum;
    23. samplerCUBE _Cube;
    24.  
    25. float4 _Color;
    26. float4 _ReflectColor;
    27. float _Shininess;
    28.  
    29. struct Input {
    30.     float2 uv_MainTex;
    31.     float2 uv_BumpMap;
    32.     float3 worldRefl;
    33.     INTERNAL_DATA
    34. };
    35.  
    36. void surf (Input IN, inout SurfaceOutput o) {
    37.     half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    38.     half4 illum = tex2D(_Illum, IN.uv_MainTex);
    39.     half4 c = tex * _Color;
    40.     o.Albedo = c.rgb;
    41.    
    42.     o.Gloss = tex.a;
    43.     o.Specular = _Shininess;
    44.    
    45.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    46.    
    47.     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    48.     half4 reflcol = texCUBE (_Cube, worldRefl);
    49.     reflcol *= tex.a;
    50.     o.Emission = reflcol.rgb * _ReflectColor.rgb + illum.rgb ;
    51.     o.Alpha = reflcol.a * _ReflectColor.a;
    52. }
    53. ENDCG
    54. }
    55.  
    56. FallBack "Reflective/Bumped Diffuse"
    57. }
     
  5. UR2

    UR2

    Joined:
    Jun 15, 2010
    Posts:
    11
    Hi LightningXCE,

    I've tried to use your shader on an object that i lightmapped in 3dsmax. It seems that when i try to apply your shader the lightmapping goes nuts on my model. I typically have to use the legacy self illum shader when bringing things over from max otherwise i get the same result when using the updated self illum shader. Not sure if this has something to do with it. Do you know how to fix this?

    Cheers,
    Rob
     
  6. ben8111

    ben8111

    Joined:
    Mar 20, 2009
    Posts:
    5
    Hi Rob LightningXCE,
    I'm also trying to use your shader with a 2nd UV for illumination (legacy shader assign UV2 to illumination map).
    I do not know why it does not work with this shader :

    thank you for your help !
    Cheers
    Ben
     
  7. kaz2057

    kaz2057

    Joined:
    Jan 18, 2011
    Posts:
    326
    Dear Support,

    I need for this shader as soon as possibile.

    I need to use Self Illumanation on 2 UV Channel and, writing same shader as ben8111, shader doesnt' work on 2UV Channel!!! It work just if I use SelfIlll on 1Channel...

    Can you help me please?

    This shader is very important for me ...
    Thanks

     
    Last edited: Oct 19, 2011
  8. kaz2057

    kaz2057

    Joined:
    Jan 18, 2011
    Posts:
    326
    up please
     
  9. kaz2057

    kaz2057

    Joined:
    Jan 18, 2011
    Posts:
    326
    The error is:

    Shader error in '_NEW/TEST2 Lightmapped Reflective Bumped Additive': Program 'frag_surf', cannot locate suitable resource to bind parameter "_ShadowCoord" at line 74

    (Filename: LightmappedSelfIll Line: 74)

    Shader error in '_NEW/TEST2 Lightmapped Reflective Bumped Additive': Program 'frag_surf', input semantic attribute "TEXCOORD" has too big of a numeric index (8) at line 74

    (Filename: LightmappedSelfIll Line: 74)

    Shader error in '_NEW/TEST2 Lightmapped Reflective Bumped Additive': Program 'vert_surf', cannot locate suitable resource to bind parameter "_ShadowCoord" at line 74

    (Filename: LightmappedSelfIll Line: 74)

    Shader error in '_NEW/TEST2 Lightmapped Reflective Bumped Additive': Program 'vert_surf', output semantic attribute "TEXCOORD" has too big of a numeric index (8) at line 74

    (Filename: LightmappedSelfIll Line: 74)

    But I don't understand why!