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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Need help! trying to rotate normal texture on custom shader! Unity 2017

Discussion in 'Shaders' started by ALGRIFF, Jun 28, 2018.

  1. ALGRIFF

    ALGRIFF

    Joined:
    Nov 16, 2016
    Posts:
    11
    Hi so I'm not ver y good with shaders and i am very new to using them, unfortunately i am still on unity 2017 so i cant use the shader graph yet either. I am trying to rotate the normal texture so that it creates a whirlpool type spiral, i have the normal attached to the object and is appearing fine but I am really struggling to get it to rotate. (I am using the Standard Asset water shader that i want to modify)

    If anyone could help you would be a life saver!!

    *Edit
    Or is there a way i can get the bump map to rotate depending on the objects rotation, like attaching it and then rotating the object through script instead of rotating the texture?
    Instead of the texture being attached to world space it is connected to the local space of the object?

    Code (CSharp):
    1. Shader "FX/SimpleWater4" {
    2. Properties {
    3.     _ReflectionTex ("Internal reflection", 2D) = "white" {}
    4.  
    5.     _MainTex ("Fallback texture", 2D) = "black" {}
    6.     _BumpMap ("Normals ", 2D) = "bump" {}
    7.  
    8.     _DistortParams ("Distortions (Bump waves, Reflection, Fresnel power, Fresnel bias)", Vector) = (1.0 ,1.0, 2.0, 1.15)
    9.     _InvFadeParemeter ("Auto blend parameter (Edge, Shore, Distance scale)", Vector) = (0.15 ,0.15, 0.5, 1.0)
    10.  
    11.     _AnimationTiling ("Animation Tiling (Displacement)", Vector) = (2.2 ,2.2, -1.1, -1.1)
    12.     _AnimationDirection ("Animation Direction (displacement)", Vector) = (1.0 ,1.0, 1.0, 1.0)
    13.  
    14.     _BumpTiling ("Bump Tiling", Vector) = (1.0 ,1.0, -2.0, 3.0)
    15.     _BumpDirection ("Bump Direction & Speed", Vector) = (1.0 ,1.0, -1.0, 1.0)
    16.     _RotationSpeed("Rotation Speed", Float) = 2.0
    17.  
    18.     _FresnelScale ("FresnelScale", Range (0.15, 4.0)) = 0.75
    19.  
    20.     _BaseColor ("Base color", COLOR)  = ( .54, .95, .99, 0.5)
    21.     _ReflectionColor ("Reflection color", COLOR)  = ( .54, .95, .99, 0.5)
    22.     _SpecularColor ("Specular color", COLOR)  = ( .72, .72, .72, 1)
    23.  
    24.     _WorldLightDir ("Specular light direction", Vector) = (0.0, 0.1, -0.5, 0.0)
    25.     _Shininess ("Shininess", Range (2.0, 500.0)) = 200.0
    26.  
    27.     _GerstnerIntensity("Per vertex displacement", Float) = 1.0
    28.     _GAmplitude ("Wave Amplitude", Vector) = (0.3 ,0.35, 0.25, 0.25)
    29.     _GFrequency ("Wave Frequency", Vector) = (1.3, 1.35, 1.25, 1.25)
    30.     _GSteepness ("Wave Steepness", Vector) = (1.0, 1.0, 1.0, 1.0)
    31.     _GSpeed ("Wave Speed", Vector) = (1.2, 1.375, 1.1, 1.5)
    32.     _GDirectionAB ("Wave Direction", Vector) = (0.3 ,0.85, 0.85, 0.25)
    33.     _GDirectionCD ("Wave Direction", Vector) = (0.1 ,0.9, 0.5, 0.5)
    34. }
    35.  
    36.  
    37. CGINCLUDE
    38.  
    39.     #include "UnityCG.cginc"
    40.     #include "WaterInclude.cginc"
    41.  
    42.     struct appdata
    43.     {
    44.         float4 vertex : POSITION;
    45.         float3 normal : NORMAL;
    46.     };
    47.  
    48.     // interpolator structs
    49.  
    50.     struct v2f
    51.     {
    52.         float4 pos : SV_POSITION;
    53.         float4 normalInterpolator : TEXCOORD0;
    54.         float3 viewInterpolator : TEXCOORD1;
    55.         float4 bumpCoords : TEXCOORD2;
    56.         float4 screenPos : TEXCOORD3;
    57.         float4 grabPassPos : TEXCOORD4;
    58.         UNITY_FOG_COORDS(5)
    59.     };
    60.  
    61.     struct v2f_noGrab
    62.     {
    63.         float4 pos : SV_POSITION;
    64.         float4 normalInterpolator : TEXCOORD0;
    65.         float3 viewInterpolator : TEXCOORD1;
    66.         float4 bumpCoords : TEXCOORD2;
    67.         float4 screenPos : TEXCOORD3;
    68.         UNITY_FOG_COORDS(4)
    69.     };
    70.  
    71.     struct v2f_simple
    72.     {
    73.         float4 pos : SV_POSITION;
    74.         float3 viewInterpolator : TEXCOORD0;
    75.         float4 bumpCoords : TEXCOORD1;
    76.         UNITY_FOG_COORDS(2)
    77.     };
    78.  
    79.     // textures
    80.     sampler2D _BumpMap;
    81.     sampler2D _ReflectionTex;
    82.     sampler2D _RefractionTex;
    83.     sampler2D _ShoreTex;
    84.     sampler2D_float _CameraDepthTexture;
    85.  
    86.     // colors in use
    87.     uniform float4 _RefrColorDepth;
    88.     uniform float4 _SpecularColor;
    89.     uniform float4 _BaseColor;
    90.     uniform float4 _ReflectionColor;
    91.  
    92.     // edge & shore fading
    93.     uniform float4 _InvFadeParemeter;
    94.  
    95.     // specularity
    96.     uniform float _Shininess;
    97.     uniform float4 _WorldLightDir;
    98.  
    99.     // fresnel, vertex & bump displacements & strength
    100.     uniform float4 _DistortParams;
    101.     uniform float _FresnelScale;
    102.     uniform float4 _BumpTiling;
    103.     uniform float4 _BumpDirection;
    104.  
    105.     uniform float4 _GAmplitude;
    106.     uniform float4 _GFrequency;
    107.     uniform float4 _GSteepness;
    108.     uniform float4 _GSpeed;
    109.     uniform float4 _GDirectionAB;
    110.     uniform float4 _GDirectionCD;
    111.  
    112.     // shortcuts
    113.     #define PER_PIXEL_DISPLACE _DistortParams.x
    114.     #define REALTIME_DISTORTION _DistortParams.y
    115.     #define FRESNEL_POWER _DistortParams.z
    116.     #define VERTEX_WORLD_NORMAL i.normalInterpolator.xyz
    117.     #define DISTANCE_SCALE _InvFadeParemeter.z
    118.     #define FRESNEL_BIAS _DistortParams.w
    119.  
    120.     //
    121.     // HQ VERSION
    122.     //
    123.  
    124.     v2f vert(appdata_full v)
    125.     {
    126.         v2f o;
    127.    
    128.         half3 worldSpaceVertex = mul(unity_ObjectToWorld,(v.vertex)).xyz;
    129.         half3 vtxForAni = (worldSpaceVertex).xzz;
    130.  
    131.         half3 nrml;
    132.         half3 offsets;
    133.    
    134.         Gerstner (
    135.             offsets, nrml, v.vertex.xyz, vtxForAni,                        // offsets, nrml will be written
    136.             _GAmplitude,                                                // amplitude
    137.             _GFrequency,                                                // frequency
    138.             _GSteepness,                                                // steepness
    139.             _GSpeed,                                                    // speed
    140.             _GDirectionAB,                                                // direction # 1, 2
    141.             _GDirectionCD                                                // direction # 3, 4
    142.         );
    143.    
    144.         v.vertex.xyz += offsets;
    145.    
    146.         half2 tileableUv = worldSpaceVertex.xz;
    147.    
    148.         o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
    149.  
    150.         o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
    151.  
    152.         o.pos = UnityObjectToClipPos(v.vertex);
    153.  
    154.         ComputeScreenAndGrabPassPos(o.pos, o.screenPos, o.grabPassPos);
    155.    
    156.         o.normalInterpolator.xyz = nrml;
    157.    
    158.         o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
    159.    
    160.         UNITY_TRANSFER_FOG(o,o.pos);
    161.         return o;
    162.     }
    163.  
    164.     half4 frag( v2f i ) : SV_Target
    165.     {
    166.         half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
    167.         half3 viewVector = normalize(i.viewInterpolator.xyz);
    168.  
    169.         half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
    170.         half4 screenWithOffset = i.screenPos + distortOffset;
    171.         half4 grabWithOffset = i.grabPassPos + distortOffset;
    172.    
    173.         half4 rtRefractionsNoDistort = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(i.grabPassPos));
    174.         half refrFix = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(grabWithOffset));
    175.         half4 rtRefractions = tex2Dproj(_RefractionTex, UNITY_PROJ_COORD(grabWithOffset));
    176.    
    177.         #ifdef WATER_REFLECTIVE
    178.             half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));
    179.         #endif
    180.  
    181.         #ifdef WATER_EDGEBLEND_ON
    182.         if (LinearEyeDepth(refrFix) < i.screenPos.z)
    183.             rtRefractions = rtRefractionsNoDistort;
    184.         #endif
    185.    
    186.         half3 reflectVector = normalize(reflect(viewVector, worldNormal));
    187.         half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
    188.         float nh = max (0, dot (worldNormal, -h));
    189.         float spec = max(0.0,pow (nh, _Shininess));
    190.    
    191.         half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
    192.    
    193.         #ifdef WATER_EDGEBLEND_ON
    194.             half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
    195.             depth = LinearEyeDepth(depth);
    196.             edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.w));
    197.         #endif
    198.    
    199.         // shading for fresnel term
    200.         worldNormal.xz *= _FresnelScale;
    201.         half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
    202.    
    203.         // base, depth & reflection colors
    204.         half4 baseColor = _BaseColor;
    205.         #ifdef WATER_REFLECTIVE
    206.             half4 reflectionColor = lerp (rtReflections,_ReflectionColor,_ReflectionColor.a);
    207.         #else
    208.             half4 reflectionColor = _ReflectionColor;
    209.         #endif
    210.    
    211.         baseColor = lerp (lerp (rtRefractions, baseColor, baseColor.a), reflectionColor, refl2Refr);
    212.         baseColor = baseColor + spec * _SpecularColor;
    213.    
    214.         baseColor.a = edgeBlendFactors.x;
    215.         UNITY_APPLY_FOG(i.fogCoord, baseColor);
    216.         return baseColor;
    217.     }
    218.  
    219.     //
    220.     // MQ VERSION
    221.     //
    222.  
    223.     v2f_noGrab vert300(appdata_full v)
    224.     {
    225.         v2f_noGrab o;
    226.    
    227.         half3 worldSpaceVertex = mul(unity_ObjectToWorld,(v.vertex)).xyz;
    228.         half3 vtxForAni = (worldSpaceVertex).xzz;
    229.  
    230.         half3 nrml;
    231.         half3 offsets;
    232.         Gerstner (
    233.             offsets, nrml, v.vertex.xyz, vtxForAni,                        // offsets, nrml will be written
    234.             _GAmplitude,                                                // amplitude
    235.             _GFrequency,                                                // frequency
    236.             _GSteepness,                                                // steepness
    237.             _GSpeed,                                                    // speed
    238.             _GDirectionAB,                                                // direction # 1, 2
    239.             _GDirectionCD                                                // direction # 3, 4
    240.         );
    241.    
    242.         v.vertex.xyz += offsets;
    243.    
    244.         half2 tileableUv = worldSpaceVertex.xz;
    245.    
    246.         o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
    247.  
    248.         o.viewInterpolator.xyz = worldSpaceVertex - _WorldSpaceCameraPos;
    249.  
    250.         o.pos = UnityObjectToClipPos(v.vertex);
    251.  
    252.         o.screenPos = ComputeScreenPos(o.pos);
    253.    
    254.         o.normalInterpolator.xyz = nrml;
    255.    
    256.         o.normalInterpolator.w = 1;//GetDistanceFadeout(o.screenPos.w, DISTANCE_SCALE);
    257.    
    258.         UNITY_TRANSFER_FOG(o,o.pos);
    259.         return o;
    260.     }
    261.  
    262.     half4 frag300( v2f_noGrab i ) : SV_Target
    263.     {
    264.         half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, VERTEX_WORLD_NORMAL, PER_PIXEL_DISPLACE);
    265.         half3 viewVector = normalize(i.viewInterpolator.xyz);
    266.  
    267.         half4 distortOffset = half4(worldNormal.xz * REALTIME_DISTORTION * 10.0, 0, 0);
    268.         half4 screenWithOffset = i.screenPos + distortOffset;
    269.    
    270.         #ifdef WATER_REFLECTIVE
    271.             half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(screenWithOffset));
    272.         #endif
    273.    
    274.         half3 reflectVector = normalize(reflect(viewVector, worldNormal));
    275.         half3 h = normalize (_WorldLightDir.xyz + viewVector.xyz);
    276.         float nh = max (0, dot (worldNormal, -h));
    277.         float spec = max(0.0,pow (nh, _Shininess));
    278.    
    279.         half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
    280.    
    281.         #ifdef WATER_EDGEBLEND_ON
    282.             half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos));
    283.             depth = LinearEyeDepth(depth);
    284.             edgeBlendFactors = saturate(_InvFadeParemeter * (depth-i.screenPos.z));
    285.         #endif
    286.    
    287.         worldNormal.xz *= _FresnelScale;
    288.         half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
    289.    
    290.         half4 baseColor = _BaseColor;
    291.         #ifdef WATER_REFLECTIVE
    292.             baseColor = lerp (baseColor, lerp (rtReflections,_ReflectionColor,_ReflectionColor.a), saturate(refl2Refr * 1.0));
    293.         #else
    294.             baseColor = _ReflectionColor;//lerp (baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
    295.         #endif
    296.    
    297.         baseColor = baseColor + spec * _SpecularColor;
    298.    
    299.         baseColor.a = edgeBlendFactors.x * saturate(0.5 + refl2Refr * 1.0);
    300.         UNITY_APPLY_FOG(i.fogCoord, baseColor);
    301.         return baseColor;
    302.     }
    303.  
    304.     //
    305.     // LQ VERSION
    306.     //
    307.  
    308.     v2f_simple vert200(appdata_full v)
    309.     {
    310.         v2f_simple o;
    311.    
    312.         half3 worldSpaceVertex = mul(unity_ObjectToWorld, v.vertex).xyz;
    313.         half2 tileableUv = worldSpaceVertex.xz;
    314.  
    315.         o.bumpCoords.xyzw = (tileableUv.xyxy + _Time.xxxx * _BumpDirection.xyzw) * _BumpTiling.xyzw;
    316.  
    317.         o.viewInterpolator.xyz = worldSpaceVertex-_WorldSpaceCameraPos;
    318.    
    319.         o.pos = UnityObjectToClipPos(v.vertex);
    320.    
    321.         UNITY_TRANSFER_FOG(o,o.pos);
    322.         return o;
    323.  
    324.     }
    325.  
    326.     half4 frag200( v2f_simple i ) : SV_Target
    327.     {
    328.         half3 worldNormal = PerPixelNormal(_BumpMap, i.bumpCoords, half3(0,1,0), PER_PIXEL_DISPLACE);
    329.         half3 viewVector = normalize(i.viewInterpolator.xyz);
    330.  
    331.         half3 reflectVector = normalize(reflect(viewVector, worldNormal));
    332.         half3 h = normalize ((_WorldLightDir.xyz) + viewVector.xyz);
    333.         float nh = max (0, dot (worldNormal, -h));
    334.         float spec = max(0.0,pow (nh, _Shininess));
    335.  
    336.         worldNormal.xz *= _FresnelScale;
    337.         half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER);
    338.  
    339.         half4 baseColor = _BaseColor;
    340.         baseColor = lerp(baseColor, _ReflectionColor, saturate(refl2Refr * 2.0));
    341.         baseColor.a = saturate(2.0 * refl2Refr + 0.5);
    342.  
    343.         baseColor.rgb += spec * _SpecularColor.rgb;
    344.         UNITY_APPLY_FOG(i.fogCoord, baseColor);
    345.         return baseColor;
    346.     }
    347.  
    348. ENDCG
    349.  
    350. Subshader
    351. {
    352.     Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    353.  
    354.     Lod 500
    355.     ColorMask RGB
    356.  
    357.     GrabPass { "_RefractionTex" }
    358.  
    359.     Pass {
    360.             Blend SrcAlpha OneMinusSrcAlpha
    361.             ZTest LEqual
    362.             ZWrite Off
    363.             Cull Off
    364.    
    365.    
    366.             CGPROGRAM
    367.    
    368.             #pragma target 3.0
    369.    
    370.             #pragma vertex vert
    371.             #pragma fragment frag
    372.             #pragma multi_compile_fog
    373.    
    374.             #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
    375.             #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
    376.             #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
    377.  
    378.             ENDCG
    379.  
    380.     }
    381. }
    382.  
    383. Subshader
    384. {
    385.     Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    386.  
    387.     Lod 300
    388.     ColorMask RGB
    389.  
    390.     Pass {
    391.             Blend SrcAlpha OneMinusSrcAlpha
    392.             ZTest LEqual
    393.             ZWrite Off
    394.             Cull Off
    395.    
    396.             CGPROGRAM
    397.    
    398.             #pragma target 3.0
    399.    
    400.             #pragma vertex vert300
    401.             #pragma fragment frag300
    402.             #pragma multi_compile_fog
    403.  
    404.             #pragma multi_compile WATER_VERTEX_DISPLACEMENT_ON WATER_VERTEX_DISPLACEMENT_OFF
    405.             #pragma multi_compile WATER_EDGEBLEND_ON WATER_EDGEBLEND_OFF
    406.             #pragma multi_compile WATER_REFLECTIVE WATER_SIMPLE
    407.  
    408.             ENDCG
    409.     }
    410. }
    411.  
    412. Subshader
    413. {
    414.     Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    415.  
    416.     Lod 200
    417.     ColorMask RGB
    418.  
    419.         Pass{
    420.            
    421.             Blend SrcAlpha OneMinusSrcAlpha
    422.             ZTest LEqual
    423.             ZWrite Off
    424.             Cull Off
    425.  
    426.        
    427.  
    428.             CGPROGRAM
    429.  
    430.             #pragma vertex vert200
    431.             #pragma fragment frag200
    432.             #pragma multi_compile_fog
    433.    
    434.  
    435.             ENDCG
    436.        
    437.  
    438.     }
    439. }
    440.  
    441. Fallback "Transparent/Diffuse"
    442. }
    443.  
     
    Last edited: Jun 28, 2018
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,230
    The shader is making the assumption that the normal map matches world space rather than converting from tangent space to world space. For this shader the solution may be as simple as multiplying the "world normal" by the object to world transform matrix.

    worldNormal = mul((float3x3)unity_ObjectToWorld, worldNormal);

    Or you can look into tangent space normal mapping and try converting the PerPixelNormal function that shader uses over to use that.