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

Blending/merging normal textures

Discussion in 'Shaders' started by archivision, Feb 28, 2012.

  1. archivision

    archivision

    Joined:
    Jun 17, 2009
    Posts:
    91
    hi,

    is there a way to fix this script ( a standard Reflective/bumped specular shader)
    to use 2 bumpmaps and put it on the o.normal?
    and change the offsets of the 2 bump maps on the run.

    i have searched over the whole interwebzz.. i could not find anything useful :'(

    Code (csharp):
    1.  
    2. Shader "Reflective/Bumped Specular Water" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    6.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    7.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    8.     _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
    9.     _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
    10.     _BumpMap ("Part1Normalmap", 2D) = "bump" {}
    11.     _BumpMap2 ("Part2Normalmap", 2D) = "bump" {}
    12.     _NormalMap ("CompleteNormalmap", 2D) = "bump" {}
    13. }
    14.  
    15. SubShader {
    16.     Tags { "RenderType"="Opaque" }
    17.     LOD 400
    18. CGPROGRAM
    19. #pragma surface surf BlinnPhong
    20. #pragma target 3.0
    21.  
    22. sampler2D _MainTex;
    23. sampler2D _BumpMap;
    24. sampler2D _BumpMap2;
    25. sampler2D _NormalMap;
    26. samplerCUBE _Cube;
    27.  
    28. fixed4 _Color;
    29. fixed4 _ReflectColor;
    30. half _Shininess;
    31.  
    32. struct Input {
    33.     float2 uv_MainTex;
    34.     float2 uv_BumpMap;
    35.     float2 uv_BumpMap2;
    36.     float2 uv_NormalMap;
    37.     float3 worldRefl;
    38.     INTERNAL_DATA
    39. };
    40.  
    41. void surf (Input IN, inout SurfaceOutput o) {
    42.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    43.     fixed4 c = tex * _Color;
    44.     o.Albedo = c.rgb;
    45.     o.Gloss = tex.a;
    46.     o.Specular = _Shininess;
    47.    
    48.     //i gues here.. to calculate the _bumpmap + the _bumpmap2 and put it in the _NormalMap
    49.     o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_NormalMap));
    50.    
    51.     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    52.     fixed4 reflcol = texCUBE (_Cube, worldRefl);
    53.     reflcol *= tex.a;
    54.     o.Emission = reflcol.rgb * _ReflectColor.rgb;
    55.     o.Alpha = reflcol.a * _ReflectColor.a;
    56. }
    57. ENDCG
    58. }
    59.  
    60. FallBack "Reflective/Bumped Diffuse"
    61. }
    62.  
    thanks :)
     
  2. recognize

    recognize

    Joined:
    Mar 26, 2012
    Posts:
    3
    It it was HLSL I would have fixed it for you, Henkie!

    greetings, Arjan
    ;)

    good luck on it though.
     
  3. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    not sure if this is what you are looking for, but this is a very basic example of blending 2 maps and offsetting them.

    Code (csharp):
    1. Shader "Reflective/Bumped Specular Water" {
    2.  
    3.  
    4. Properties {
    5.  
    6.  
    7.     _Color ("Main Color", Color) = (1,1,1,1)
    8.  
    9.  
    10.     _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    11.  
    12.  
    13.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    14.  
    15.  
    16.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    17.  
    18.  
    19.     _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
    20.  
    21.  
    22.     _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
    23.  
    24.  
    25.     _BumpMap ("Part1Normalmap", 2D) = "bump" {}
    26.  
    27.  
    28.     _BumpMap2 ("Part2Normalmap", 2D) = "bump" {}
    29.  
    30. }
    31.  
    32.  
    33.  
    34.  
    35.  
    36. SubShader {
    37.  
    38.  
    39.     Tags { "RenderType"="Opaque" }
    40.  
    41.  
    42.     LOD 400
    43.  
    44.  
    45. CGPROGRAM
    46.  
    47.  
    48. #pragma surface surf BlinnPhong
    49.  
    50.  
    51. #pragma target 3.0
    52.  
    53.  
    54.  
    55.  
    56.  
    57. sampler2D _MainTex;
    58.  
    59.  
    60. sampler2D _BumpMap;
    61.  
    62.  
    63. sampler2D _BumpMap2;
    64.  
    65.  
    66.  
    67. samplerCUBE _Cube;
    68.  
    69.  
    70.  
    71.  
    72.  
    73. fixed4 _Color;
    74.  
    75.  
    76. fixed4 _ReflectColor;
    77.  
    78.  
    79. half _Shininess;
    80.  
    81.  
    82.  
    83.  
    84.  
    85. struct Input {
    86.  
    87.  
    88.     float2 uv_MainTex;
    89.  
    90.  
    91.     float2 uv_BumpMap;
    92.  
    93.  
    94.     float3 worldRefl;
    95.  
    96.  
    97.     INTERNAL_DATA
    98.  
    99.  
    100. };
    101.  
    102.  
    103.  
    104.  
    105.  
    106. void surf (Input IN, inout SurfaceOutput o) {
    107.  
    108.  
    109.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    110.  
    111.  
    112.     fixed4 c = tex * _Color;
    113.  
    114.  
    115.     o.Albedo = c.rgb;
    116.  
    117.  
    118.     o.Gloss = tex.a;
    119.  
    120.  
    121.     o.Specular = _Shininess;
    122.  
    123.  
    124.    
    125.  
    126.  
    127.     //i gues here.. to calculate the _bumpmap + the _bumpmap2 and put it in the _NormalMap
    128.  
    129.     fixed3 normal1=UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap+_Time/10));
    130.     fixed3 normal2=UnpackNormal(tex2D(_BumpMap2, IN.uv_BumpMap*2-_Time/20));
    131.     normal1.xy += normal2.xy;
    132.     normal1.z *= normal2.z;
    133.     o.Normal = normal1;
    134.  
    135.  
    136.    
    137.  
    138.  
    139.     float3 worldRefl = WorldReflectionVector (IN, o.Normal);
    140.  
    141.  
    142.     fixed4 reflcol = texCUBE (_Cube, worldRefl);
    143.  
    144.  
    145.     reflcol *= tex.a;
    146.  
    147.  
    148.     o.Emission = reflcol.rgb * _ReflectColor.rgb;
    149.  
    150.  
    151.     o.Alpha = reflcol.a * _ReflectColor.a;
    152.  
    153.  
    154. }
    155.  
    156.  
    157. ENDCG
    158.  
    159.  
    160. }
    161.  
    162.  
    163.  
    164.  
    165.  
    166. FallBack "Reflective/Bumped Diffuse"
    167.  
    168.  
    169. }
     
  4. archivision

    archivision

    Joined:
    Jun 17, 2009
    Posts:
    91
    Thanks! That did it. many thank.
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    If you want to try out some other methods, Colin Barré-Brisebois and Stephen Hill have a great article here. The code multivac provided uses what they call whiteout blending, but there are a number of other approaches with various benefits and drawbacks.
     
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Good find Daniel, thanks :D