Search Unity

Advanced Shaders for realtime lighted terrains [update_04]

Discussion in 'Made With Unity' started by larsbertram1, Mar 2, 2010.

  1. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    this thread is about unity 2.6. in case you are using unity 3.x please look for the advanced terrain shaders:

    http://forum.unity3d.com/threads/119127-Coming-soon-Advanced-terrain-shaders-%96-Volume-2




    after a long time of working on hacking the built in terrain shaders i come up with the first version to be published to the community.
    although all my changes are pretty far away from being a proper solution i think they really improve the overall look.
    maybe someone more familiar with shaderlab and cg will continue to work on those.


    what happened so far?

    - hacked bark shader for trees: now receiving shadows
    - hacked vertexlit shader for detail meshes placed on the terrain (foliage): now casting and receiving shadows
    - hacked grass shader (detail meshes): now waving a bit more accuratley
    - hacked terrain shader: added some kind of bumpmapping

    updates
    webplayer is online
    full package of the project is online
    more information is online [see page 2 of this thread]

    lars

    p.s.: i would be pleased to see some pictures of your terrains – in case you decide to use the shaders. that would be interesting.
     

    Attached Files:

    Last edited: Mar 21, 2012
  2. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,790
    whaaa... this sounds awesome... :eek:
    One of the MOST NEEDED updates to unity imho...
     
  3. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    Looks great! Looking forward to seeing a webplayer!
     
  4. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Nice work! :)
     
  6. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    Now the unitypackage is online

    it contains all shaders, scripts, textures and models that you find in the webplayer.
    just download and import it into a new project.
    doing so will cause some errors as the locomotion system needs some more input axises to be defined. but you can easily do this in --> edit --> project setting --> input [the console will log all missing input axises]
    maybe you will have to save and restart unity to have the new shaders take effect.

    http://dl.dropbox.com/u/2322017/advancedTerrainShaders.unitypackage – [133MB]
     
  7. dxpsite

    dxpsite

    Joined:
    Mar 3, 2010
    Posts:
    2
    awesome!!!! like as Unreal)))) :eek:
     
  8. Pixelstudio_nl

    Pixelstudio_nl

    Joined:
    Jun 22, 2009
    Posts:
    179
    looks nice! how did you get those trees ? modeled then your self ?
     
  9. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    no, most of them are from the unitree project, the standard terrain asset or a mod pack...
     
  10. Fourthings

    Fourthings

    Joined:
    Feb 15, 2010
    Posts:
    313
    This is looking verrrrrry nice
     
  11. Pixero

    Pixero

    Joined:
    Dec 10, 2009
    Posts:
    130
    Wow, thanks alot. Just what I was wanting.
     
  12. Essal

    Essal

    Joined:
    Feb 16, 2010
    Posts:
    107
    Looks great,
    I assume I wont be able to use this with Unity Indie? (since it seems like realtime lighting)
     
  13. GusM

    GusM

    Joined:
    Aug 27, 2005
    Posts:
    585
    Really nice, thank you so much for sharing.
     
  14. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Thank you! :D

    How did you implement the fog?
     
  15. KarelA

    KarelA

    Joined:
    Dec 30, 2008
    Posts:
    422
    Fantastic work!!!!

    I love showcase section. Every now and then a genius comes up and shows their work. Are these dynamic shadows or is it lightmap?
     
  16. KaelisAsur

    KaelisAsur

    Joined:
    Apr 9, 2009
    Posts:
    361
    Spectacular. Thank you!
     
  17. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    What scripts is missed on the camera ?
     

    Attached Files:

  18. liverolA

    liverolA

    Joined:
    Feb 10, 2009
    Posts:
    347
    looks nice,would somebody upload this to another host? can't download it!!thanks a lot!
     
  19. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    nothing important – just remove it!
     
  20. FunnyRobot

    FunnyRobot

    Joined:
    Dec 18, 2009
    Posts:
    26
    This is simply great! Makes ground look so much more natural. Trees look great too with all those shadows. Thank you.
     
  21. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you mean the fog on the river / in the forest?
    it is done using soft particles.
     
  22. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    some documentation

    -----------------------------
    special notes on the shipped shaders


    in order to get weed drawn correctely is if parts of it were under water i had to change the render queue values of – i can’t remember – some of the shaders – maybe billboard, tree leaves, water and/or vertelit shader… unfortunately this leads to billboards drawn on top of tree leaves as far as their semitranparent parts are concerned. if you want to get rid of this you will have to rework the render queue either by editing the shaders or dynamically via scripting depending on the camera position.

    -----------------------------
    tree bark shader


    the tree bark shader equals more or less the common normal-diffuse shader.

    limits:
    it does not support the train engines feature’s bending, scaling and different coloring of trees.
    ---> set the tree’s bend factor to a very low value (e.g. 0.025). doing so will give you slightly bending leaves which does not look too strange.
    ---> on painting trees to the terrain choose the following value:
    - color variation: 0
    - tree height / tree width variation: 0 – 10

    bugs:
    during the fading from billboard to mesh tree the bark changes its color and gets pretty dark.
    ---> so i hide the fading in the fog by adjusting fog density [render settings] and billboard start.
    whenever you make changes to the terrain’s lightmap unity updates the tree’s coloring which will lead to some color flickering of the tree barks.
    ---> go to the project folder --> lightmap_lowres and choose "white" then update the tree lightmap color [menu --> terrain --> update tree lightmap color].

    Code (csharp):
    1.  
    2. Shader "Nature/Soft Occlusion Bark" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,0)
    5.     _MainTex ("Base (RGB)", 2D) = "white" {}
    6. }
    7.  
    8. Category {
    9.     Blend AppSrcAdd AppDstAdd
    10.     Fog { Color [_AddFog] }
    11.    
    12.     // ------------------------------------------------------------------
    13.     // ARB fragment program
    14.    
    15.     SubShader {
    16.         // Ambient pass
    17.        
    18.         Tags {
    19.             "BillboardShader" = "Hidden/TerrainEngine/Soft Occlusion Bark rendertex"
    20.             "TerrainCustomLit" = "Hidden/TerrainEngine/Soft Occlusion Bark customLit"
    21.         }
    22.        
    23.         Pass {
    24.             Name "BASE"
    25.             Tags {"LightMode" = "PixelOrNone"}
    26.             Color [_PPLAmbient]
    27.             SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    28.         }
    29.         // Vertex lights
    30.         Pass {
    31.             Name "BASE"
    32.             Tags {"LightMode" = "Vertex"}
    33.             Lighting On
    34.             Material {
    35.                 Diffuse [_Color]
    36.                 Emission [_PPLAmbient]
    37.             }
    38.             SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    39.         }
    40.         // Pixel lights
    41.         Pass {
    42.             Name "PPL"
    43.             Tags { "LightMode" = "Pixel" }
    44. CGPROGRAM
    45. #pragma vertex vert
    46. #pragma fragment frag
    47. #pragma multi_compile_builtin
    48. #pragma fragmentoption ARB_fog_exp2
    49. #pragma fragmentoption ARB_precision_hint_fastest
    50. #include "UnityCG.cginc"
    51. #include "AutoLight.cginc"
    52.  
    53. struct v2f {
    54.     V2F_POS_FOG;
    55.     LIGHTING_COORDS
    56.     float2  uv;
    57.     float3  normal;
    58.     float3  lightDir;
    59. };
    60.  
    61. uniform float4 _MainTex_ST;
    62.  
    63.  
    64. v2f vert (appdata_base v)
    65. {
    66.     v2f o;
    67.    
    68.     PositionFog( v.vertex, o.pos, o.fog );
    69.     o.normal = v.normal;
    70.     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    71.     o.lightDir = ObjSpaceLightDir( v.vertex );
    72.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    73.     return o;
    74. }
    75.  
    76. uniform sampler2D _MainTex;
    77.  
    78. float4 frag (v2f i) : COLOR
    79. {
    80.     // The eternal tradeoff: do we normalize the normal?
    81.     //float3 normal = normalize(i.normal);
    82.     float3 normal = i.normal;
    83.     half4 texcol = tex2D( _MainTex, i.uv );
    84.     return DiffuseLight( i.lightDir, normal, texcol, LIGHT_ATTENUATION(i) );
    85. }
    86. ENDCG
    87.         }
    88.     }
    89.    
    90.    
    91.    
    92.    
    93.     // -----
    94.    
    95.    
    96.     SubShader {
    97.         Tags {
    98.             "BillboardShader" = "Hidden/TerrainEngine/Soft Occlusion Bark rendertex"
    99.             "TerrainCustomLit" = "Hidden/TerrainEngine/Soft Occlusion Bark customLit"
    100.         }
    101.         Pass {
    102.             Lighting On
    103.             Material {
    104.                 Diffuse [_Color]
    105.                 Ambient [_Color]
    106.             }
    107.             SetTexture [_MainTex] { combine primary * texture DOUBLE, constant }
    108.         }      
    109.     }
    110.    
    111.    
    112.    
    113.    
    114.    
    115.    
    116.    
    117.     // ------------------------------------------------------------------
    118.     // Radeon 9000
    119.  
    120.     SubShader {
    121.         // Ambient pass
    122.         Pass {
    123.             Name "BASE"
    124.             Tags {"LightMode" = "PixelOrNone"}
    125.             Color [_PPLAmbient]
    126.             SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    127.         }
    128.         // Vertex lights
    129.         Pass {
    130.             Name "BASE"
    131.             Tags {"LightMode" = "Vertex"}
    132.             Lighting On
    133.             Material {
    134.                 Diffuse [_Color]
    135.                 Emission [_PPLAmbient]
    136.             }
    137.             SetTexture [_MainTex] { constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    138.         }
    139.        
    140.         // Pixel lights with 0 light textures
    141.         Pass {
    142.             Name "PPL"
    143.             Tags {
    144.                 "LightMode" = "Pixel"
    145.                 "LightTexCount" = "0"
    146.             }
    147.  
    148. CGPROGRAM
    149. #pragma vertex vert
    150. #include "UnityCG.cginc"
    151.  
    152. struct v2f {
    153.     V2F_POS_FOG;
    154.     float2 uv       : TEXCOORD0;
    155.     float3 normal   : TEXCOORD1;
    156.     float3 lightDir : TEXCOORD2;
    157. };
    158.  
    159. uniform float4 _MainTex_ST;
    160.  
    161. v2f vert(appdata_base v)
    162. {
    163.     v2f o;
    164.     PositionFog( v.vertex, o.pos, o.fog );
    165.     o.normal = v.normal;
    166.     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    167.     o.lightDir = ObjSpaceLightDir( v.vertex );
    168.     return o;
    169. }
    170. ENDCG
    171.             Program "" {
    172.                 SubProgram {
    173.                     Local 0, [_ModelLightColor0]
    174.                     Local 1, (0,0,0,0)
    175.  
    176. "!!ATIfs1.0
    177. StartConstants;
    178.     CONSTANT c0 = program.local[0];
    179.     CONSTANT c1 = program.local[1];
    180. EndConstants;
    181.  
    182. StartOutputPass;
    183.     SampleMap r0, t0.str;           # main texture
    184.     SampleMap r1, t2.str;           # normalized light dir
    185.     PassTexCoord r2, t1.str;        # normal
    186.    
    187.     DOT3 r5.sat, r2, r1.2x.bias;    # R5 = diffuse (N.L)
    188.    
    189.     MUL r0, r0, r5;
    190.     MUL r0.rgb.2x, r0, c0;
    191.     MOV r0.a, c1;
    192. EndPass;
    193. "
    194.                 }
    195.             }
    196.             SetTexture[_MainTex] {combine texture}
    197.             SetTexture[_CubeNormalize] {combine texture}
    198.         }
    199.        
    200.         // Pixel lights with 1 light texture
    201.         Pass {
    202.             Name "PPL"
    203.             Tags {
    204.                 "LightMode" = "Pixel"
    205.                 "LightTexCount" = "1"
    206.             }
    207.  
    208. CGPROGRAM
    209. #pragma vertex vert
    210. #include "UnityCG.cginc"
    211.  
    212. uniform float4 _MainTex_ST;
    213. uniform float4x4 _SpotlightProjectionMatrix0;
    214.  
    215. struct v2f {
    216.     V2F_POS_FOG;
    217.     float2 uv       : TEXCOORD0;
    218.     float3 normal   : TEXCOORD1;
    219.     float3 lightDir : TEXCOORD2;
    220.     float4 LightCoord0 : TEXCOORD3;
    221. };
    222.  
    223. v2f vert(appdata_tan v)
    224. {
    225.     v2f o;
    226.     PositionFog( v.vertex, o.pos, o.fog );
    227.     o.normal = v.normal;
    228.     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    229.     o.lightDir = ObjSpaceLightDir( v.vertex );
    230.    
    231.     o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
    232.    
    233.     return o;
    234. }
    235. ENDCG
    236.             Program "" {
    237.                 SubProgram {
    238.                     Local 0, [_ModelLightColor0]
    239.                     Local 1, (0,0,0,0)
    240.  
    241. "!!ATIfs1.0
    242. StartConstants;
    243.     CONSTANT c0 = program.local[0];
    244.     CONSTANT c1 = program.local[1];
    245. EndConstants;
    246.  
    247. StartOutputPass;
    248.     SampleMap r0, t0.str;           # main texture
    249.     SampleMap r1, t2.str;           # normalized light dir
    250.     PassTexCoord r4, t1.str;        # normal
    251.     SampleMap r2, t3.str;           # a = attenuation
    252.    
    253.     DOT3 r5.sat, r4, r1.2x.bias;    # R5 = diffuse (N.L)
    254.    
    255.     MUL r0, r0, r5;
    256.     MUL r0.rgb.2x, r0, c0;
    257.     MUL r0.rgb, r0, r2.a;           # attenuate
    258.     MOV r0.a, c1;
    259. EndPass;
    260. "
    261.                 }
    262.             }
    263.             SetTexture[_MainTex] {combine texture}
    264.             SetTexture[_CubeNormalize] {combine texture}
    265.             SetTexture[_LightTexture0] {combine texture}
    266.         }
    267.        
    268.         // Pixel lights with 2 light textures
    269.         Pass {
    270.             Name "PPL"
    271.             Tags {
    272.                 "LightMode" = "Pixel"
    273.                 "LightTexCount" = "2"
    274.             }
    275. CGPROGRAM
    276. #pragma vertex vert
    277. #include "UnityCG.cginc"
    278.  
    279. uniform float4 _MainTex_ST;
    280. uniform float4x4 _SpotlightProjectionMatrix0;
    281. uniform float4x4 _SpotlightProjectionMatrixB0;
    282.  
    283. struct v2f {
    284.     V2F_POS_FOG;
    285.     float2 uv       : TEXCOORD0;
    286.     float3 normal   : TEXCOORD1;
    287.     float3 lightDir : TEXCOORD2;
    288.     float4 LightCoord0 : TEXCOORD3;
    289.     float4 LightCoordB0 : TEXCOORD4;
    290. };
    291.  
    292. v2f vert(appdata_tan v)
    293. {
    294.     v2f o;
    295.     PositionFog( v.vertex, o.pos, o.fog );
    296.     o.normal = v.normal;
    297.     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    298.     o.lightDir = ObjSpaceLightDir( v.vertex );
    299.    
    300.     o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
    301.     o.LightCoordB0 = mul(_SpotlightProjectionMatrixB0, v.vertex);
    302.    
    303.     return o;
    304. }
    305. ENDCG
    306.             Program "" {
    307.                 SubProgram {
    308.                     Local 0, [_ModelLightColor0]
    309.                     Local 1, (0,0,0,0)
    310.  
    311. "!!ATIfs1.0
    312. StartConstants;
    313.     CONSTANT c0 = program.local[0];
    314.     CONSTANT c1 = program.local[1];
    315. EndConstants;
    316.  
    317. StartOutputPass;
    318.     SampleMap r0, t0.str;           # main texture
    319.     SampleMap r1, t2.str;           # normalized light dir
    320.     PassTexCoord r4, t1.str;        # normal
    321.     SampleMap r2, t3.stq_dq;        # a = attenuation 1
    322.     SampleMap r3, t4.stq_dq;        # a = attenuation 2
    323.    
    324.     DOT3 r5.sat, r4, r1.2x.bias;    # R5 = diffuse (N.L)
    325.    
    326.     MUL r0, r0, r5;
    327.     MUL r0.rgb.2x, r0, c0;
    328.     MUL r0.rgb, r0, r2.a;           # attenuate
    329.     MUL r0.rgb, r0, r3.a;
    330.     MOV r0.a, c1;
    331. EndPass;
    332. "
    333.                 }
    334.             }
    335.             SetTexture[_MainTex] {combine texture}
    336.             SetTexture[_CubeNormalize] {combine texture}
    337.             SetTexture[_LightTexture0] {combine texture}
    338.             SetTexture[_LightTextureB0] {combine texture}
    339.         }
    340.     }
    341.    
    342.     // ------------------------------------------------------------------
    343.     // Radeon 7000
    344.    
    345.     Category {
    346.         Material {
    347.             Diffuse [_Color]
    348.             Emission [_PPLAmbient]
    349.         }
    350.         Lighting On
    351.         SubShader {
    352.             // Ambient pass
    353.             Pass {
    354.                 Name "BASE"
    355.                 Tags {"LightMode" = "PixelOrNone"}
    356.                 Color [_PPLAmbient]
    357.                 Lighting Off
    358.                 SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
    359.             }
    360.             // Vertex lights
    361.             Pass {
    362.                 Name "BASE"
    363.                 Tags {"LightMode" = "Vertex"}
    364.                 Lighting On
    365.                 Material {
    366.                     Diffuse [_Color]
    367.                     Emission [_PPLAmbient]
    368.                 }
    369.                 SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
    370.             }
    371.             // Pixel lights with 2 light textures
    372.             Pass {
    373.                 Name "PPL"
    374.                 Tags {
    375.                     "LightMode" = "Pixel"
    376.                     "LightTexCount"  = "2"
    377.                 }
    378.                 ColorMask RGB
    379.                 SetTexture [_LightTexture0]  { combine previous * texture alpha, previous }
    380.                 SetTexture [_LightTextureB0] { combine previous * texture alpha, previous }
    381.                 SetTexture [_MainTex] { combine previous * texture DOUBLE }
    382.             }
    383.             // Pixel lights with 1 light texture
    384.             Pass {
    385.                 Name "PPL"
    386.                 Tags {
    387.                     "LightMode" = "Pixel"
    388.                     "LightTexCount"  = "1"
    389.                 }
    390.                 ColorMask RGB
    391.                 SetTexture [_LightTexture0] { combine previous * texture alpha, previous }
    392.                 SetTexture [_MainTex] { combine previous * texture DOUBLE }
    393.             }
    394.             // Pixel lights with 0 light textures
    395.             Pass {
    396.                 Name "PPL"
    397.                 Tags {
    398.                     "LightMode" = "Pixel"
    399.                     "LightTexCount" = "0"
    400.                 }
    401.                 ColorMask RGB
    402.                 SetTexture[_MainTex] { combine previous * texture DOUBLE }
    403.             }
    404.         }
    405.     }
    406. }
    407.  
    408. Fallback "VertexLit", 2
    409.  
    410. }
    411.  

    -----------------------------
    vertexlit shader

    the vertexlit shader is based on the common alphatest-diffuse shader.

    it comes in two versions: one for single sided geometry [no culling] and the other one for double sided geometry.

    VertexLit_1sided.shader
    this should be the less expensive version of the shader as the number of triangles which have to be pushed to the gpu is just half as big compared to two sided version.
    but automatically flipping the normals causes some flickering in the lighting and as backfaces do not get written the the shadow map’s depth buffer correctly shadows of objects which are in the background will look through [ see: http://forum.unity3d.com/viewtopic.php?p=249794&highlight=#249794].
    well, actually it does not look too bad as leaves usually are some kind of semi transparent…

    in case you don’t want this you will have to use the second version: VertexLit_2sided.shader.

    in order to switch between the shaders open them in unitron and change their names so that the one you want is called:
    - Shader "Hidden/TerrainEngine/Details/Vertexlit" {

    then you will have to assign the proper meshes to the terrain’s details. most detail meshes have two different versions: 1 and 2 sided.

    limits:
    it does not support bending.
    in order to get nicer results [less brightness and darkness] compared to diffuse lighting i use a special lighting term:

    Code (csharp):
    1.  
    2. /// lighting hack: some baselight + diffuse lighting * x   
    3. diffuse = 0.4 + diffuse * 0.35;
    4.  
    you can adjust the 2 constants to fit your lighting.

    bugs:
    both versions of the shader produce artifacts: some kind of bright glow around the texture edge where the geometry receives shadows. i guess it is some kind of alpha blending issue… ion can’t figure out a solution – maybe somebody else.

    vertexlit 1sided
    Code (csharp):
    1.  
    2. Shader "Hidden/TerrainEngine/Details/Vertexlit" {
    3.    Properties {
    4.    // we get no color?
    5.    _Color ("Main Color", Color) = (1,1,1,1)
    6.    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    7.    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    8. }
    9.  
    10. Category {
    11.    Tags {"RenderType"="TransparentCutout"}
    12.    LOD 200
    13.    Alphatest Greater [_Cutoff]
    14.    AlphaToMask True
    15.    ColorMask RGB
    16.    Blend AppSrcAdd AppDstAdd
    17.    Cull Off
    18.    
    19.    
    20.    
    21.    // ------------------------------------------------------------------
    22.    // ARB fragment program
    23.    
    24.    SubShader {
    25.       // Ambient pass
    26.       Pass {
    27.          Name "BASE"
    28.          Tags {"LightMode" = "PixelOrNone"}
    29.          Color [_PPLAmbient]
    30.          SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * primary}
    31.       }
    32.        
    33.       // Vertex lights
    34.       // not supported?
    35.       /*
    36.       Pass {
    37.          Name "BASE"
    38.          Tags {"LightMode" = "Vertex"}
    39.          Lighting On
    40.          Material {
    41.             Diffuse [_Color]
    42.             Emission [_PPLAmbient]
    43.          }
    44.          SetTexture [_MainTex] {combine texture * primary DOUBLE, texture * primary}
    45.       }
    46.       */
    47.      
    48.       // Pixel lights
    49.       Pass {    
    50.          Name "PPL"
    51.          Tags { "LightMode" = "Pixel" }
    52.          Fog { Color [_AddFog] }
    53.  
    54. CGPROGRAM
    55. #pragma fragment frag
    56. #pragma vertex vert
    57. #pragma multi_compile_builtin
    58. #pragma fragmentoption ARB_fog_exp2
    59. #pragma fragmentoption ARB_precision_hint_fastest
    60. #include "UnityCG.cginc"
    61. #include "AutoLight.cginc"
    62.  
    63. struct v2f {
    64.    V2F_POS_FOG;
    65.    LIGHTING_COORDS
    66.    float2   uv;
    67.    float3   normal;
    68.    float3   lightDir;
    69.    float3   vertex;
    70. };
    71.  
    72. uniform float4 _MainTex_ST;
    73.  
    74. v2f vert (appdata_base v)
    75. {
    76.    v2f o;
    77.    PositionFog( v.vertex, o.pos, o.fog );
    78.    
    79.     //Viewdir in object space
    80.     // see: [url]http://forum.unity3d.com/viewtopic.php?t=12015[/url]
    81.     float3 ovd = v.vertex.xyz - _ObjectSpaceCameraPos;
    82.     float dp = dot(ovd, v.normal);    
    83.     //If viewdir is same direction-ish as normal, flip it.
    84.     o.normal = v.normal * -sign(dp);
    85.  
    86.     o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    87.     o.lightDir = ObjSpaceLightDir( v.vertex );
    88.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    89.     return o;
    90. }
    91.  
    92. uniform sampler2D _MainTex;
    93. uniform float4 _Color;
    94.  
    95. float4 frag (v2f i) : COLOR
    96. {
    97.    half4 texcol = tex2D( _MainTex, i.uv );    
    98.     // its is not written to the shadowmap correctely...
    99.  
    100.     #ifndef USING_DIRECTIONAL_LIGHT
    101.     i.lightDir = normalize(i.lightDir);
    102.     #endif
    103.    
    104.     // original lighting
    105.     //half c = DiffuseLight( i.lightDir, i.normal, texcol, LIGHT_ATTENUATION(i) );
    106.    
    107.     half diffuse = dot( i.normal, i.lightDir );
    108.     /// lighting hack: some baselight + diffuse lighting * x   
    109.     diffuse = 0.4 + diffuse * 0.35;
    110.     half4 c;
    111.     c.rgb = texcol.rgb * _ModelLightColor0.rgb * (diffuse * LIGHT_ATTENUATION(i) * 2);
    112.    
    113.     //c.a = 0; // diffuse passes by default don't contribute to overbright  
    114.     c.a = texcol.a * _Color.a;
    115.     //c.a = texcol.a;
    116.     return c;
    117. }
    118. ENDCG
    119.          SetTexture [_MainTex] {combine texture}
    120.          SetTexture [_LightTexture0] {combine texture}
    121.          SetTexture [_LightTextureB0] {combine texture}
    122.          
    123.       }
    124.     /// end 1st pass
    125.    
    126.     /// 2nd pass
    127.       Pass {
    128.          Name "PPL2"
    129.          Tags { "LightMode" = "Pixel" }
    130.          // Only render pixels less or equal to the value
    131.          AlphaTest LEqual [_Cutoff]
    132.                  
    133.          // Set up alpha blending
    134.          Blend SrcAlpha OneMinusSrcAlpha
    135.          // Dont write to the depth buffer
    136.          ZWrite Off
    137.          SetTexture [_MainTex] { constantColor [_Color] Combine texture * constant, texture * constant}
    138.       }
    139.     /// end 2nd pass
    140.    
    141.    }    
    142.    
    143.    
    144.    
    145.    
    146.     // ------------------------------------------------------------------
    147.    // Radeon 9000
    148.  
    149.    SubShader {
    150.       // Ambient pass
    151.       Pass {
    152.          Name "BASE"
    153.          Tags {"LightMode" = "PixelOrNone"}
    154.          Color [_PPLAmbient]
    155.          SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    156.       }
    157.       // Vertex lights
    158.       Pass {
    159.          Name "BASE"
    160.          Tags {"LightMode" = "Vertex"}
    161.          Lighting On
    162.          Material {
    163.             Diffuse [_Color]
    164.             Emission [_PPLAmbient]
    165.          }
    166.          SetTexture [_MainTex] {Combine texture * primary DOUBLE, texture * primary}
    167.       }
    168.        
    169.       // Pixel lights with 0 light textures
    170.       Pass {
    171.          Name "PPL"
    172.          Tags {
    173.             "LightMode" = "Pixel"
    174.             "LightTexCount" = "0"
    175.          }
    176.  
    177. CGPROGRAM
    178. #pragma vertex vert
    179. #include "UnityCG.cginc"
    180.  
    181. struct v2f {
    182.    V2F_POS_FOG;
    183.    float2 uv      : TEXCOORD0;
    184.    float3 normal   : TEXCOORD1;
    185.    float3 lightDir   : TEXCOORD2;
    186. };
    187.  
    188. uniform float4 _MainTex_ST;
    189.  
    190. v2f vert(appdata_base v)
    191. {
    192.    v2f o;
    193.    PositionFog( v.vertex, o.pos, o.fog );
    194.    o.normal = v.normal;
    195.    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    196.    o.lightDir = ObjSpaceLightDir( v.vertex );
    197.    return o;
    198. }
    199. ENDCG
    200.          Program "" {
    201.             SubProgram {
    202.                Local 0, [_ModelLightColor0]
    203.                Local 1, [_Color]
    204.  
    205. "!!ATIfs1.0
    206. StartConstants;
    207.   CONSTANT c0 = program.local[0];
    208.   CONSTANT c1 = program.local[1];
    209. EndConstants;
    210.  
    211. StartOutputPass;
    212.   SampleMap r0, t0.str;         # main texture
    213.   SampleMap r1, t2.str;         # normalized light dir
    214.   PassTexCoord r2, t1.str;      # normal
    215.    
    216.   DOT3 r5.sat, r2, r1.2x.bias;   # R5 = diffuse (N.L)
    217.    
    218.   MUL r0.rgb, r0, r5;
    219.   MUL r0.rgb.2x, r0, c0;
    220.   MUL r0.a, r0, c1;
    221. EndPass;
    222. "
    223.             }
    224.          }
    225.          SetTexture[_MainTex] {combine texture}
    226.          SetTexture[_CubeNormalize] {combine texture}
    227.       }
    228.        
    229.       // Pixel lights with 1 light texture
    230.       Pass {
    231.          Name "PPL"
    232.          Tags {
    233.             "LightMode" = "Pixel"
    234.             "LightTexCount" = "1"
    235.          }
    236.  
    237. CGPROGRAM
    238. #pragma vertex vert
    239. #include "UnityCG.cginc"
    240.  
    241. uniform float4 _MainTex_ST;
    242. uniform float4x4 _SpotlightProjectionMatrix0;
    243.  
    244. struct v2f {
    245.    V2F_POS_FOG;
    246.    float2 uv      : TEXCOORD0;
    247.    float3 normal   : TEXCOORD1;
    248.    float3 lightDir   : TEXCOORD2;
    249.    float4 LightCoord0 : TEXCOORD3;
    250. };
    251.  
    252. v2f vert(appdata_tan v)
    253. {
    254.    v2f o;
    255.    PositionFog( v.vertex, o.pos, o.fog );
    256.    o.normal = v.normal;
    257.    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    258.    o.lightDir = ObjSpaceLightDir( v.vertex );
    259.    
    260.    o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
    261.    
    262.    return o;
    263. }
    264. ENDCG
    265.          Program "" {
    266.             SubProgram {
    267.                Local 0, [_ModelLightColor0]
    268.                Local 1, [_Color]
    269.  
    270. "!!ATIfs1.0
    271. StartConstants;
    272.   CONSTANT c0 = program.local[0];
    273.   CONSTANT c1 = program.local[1];
    274. EndConstants;
    275.  
    276. StartOutputPass;
    277.   SampleMap r0, t0.str;         # main texture
    278.   SampleMap r1, t2.str;         # normalized light dir
    279.   PassTexCoord r4, t1.str;      # normal
    280.   SampleMap r2, t3.str;         # a = attenuation
    281.    
    282.   DOT3 r5.sat, r4, r1.2x.bias;   # R5 = diffuse (N.L)
    283.    
    284.   MUL r0.rgb, r0, r5;
    285.   MUL r0.rgb.2x, r0, c0;
    286.   MUL r0.rgb, r0, r2.a;         # attenuate
    287.   MUL r0.a, r0, c1;
    288. EndPass;
    289. "
    290.             }
    291.          }
    292.          SetTexture[_MainTex] {combine texture}
    293.          SetTexture[_CubeNormalize] {combine texture}
    294.          SetTexture[_LightTexture0] {combine texture}
    295.       }
    296.        
    297.       // Pixel lights with 2 light textures
    298.       Pass {
    299.          Name "PPL"
    300.          Tags {
    301.             "LightMode" = "Pixel"
    302.             "LightTexCount" = "2"
    303.          }
    304. CGPROGRAM
    305. #pragma vertex vert
    306. #include "UnityCG.cginc"
    307.  
    308. uniform float4 _MainTex_ST;
    309. uniform float4x4 _SpotlightProjectionMatrix0;
    310. uniform float4x4 _SpotlightProjectionMatrixB0;
    311.  
    312. struct v2f {
    313.    V2F_POS_FOG;
    314.    float2 uv      : TEXCOORD0;
    315.    float3 normal   : TEXCOORD1;
    316.    float3 lightDir   : TEXCOORD2;
    317.    float4 LightCoord0 : TEXCOORD3;
    318.    float4 LightCoordB0 : TEXCOORD4;
    319. };
    320.  
    321. v2f vert(appdata_tan v)
    322. {
    323.    v2f o;
    324.    PositionFog( v.vertex, o.pos, o.fog );
    325.    o.normal = v.normal;
    326.    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    327.    o.lightDir = ObjSpaceLightDir( v.vertex );
    328.    
    329.    o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
    330.    o.LightCoordB0 = mul(_SpotlightProjectionMatrixB0, v.vertex);
    331.    
    332.    return o;
    333. }
    334. ENDCG
    335.          Program "" {
    336.             SubProgram {
    337.                Local 0, [_ModelLightColor0]
    338.                Local 1, [_Color]
    339.  
    340. "!!ATIfs1.0
    341. StartConstants;
    342.   CONSTANT c0 = program.local[0];
    343.   CONSTANT c1 = program.local[1];
    344. EndConstants;
    345.  
    346. StartOutputPass;
    347.   SampleMap r0, t0.str;         # main texture
    348.   SampleMap r1, t2.str;         # normalized light dir
    349.   PassTexCoord r4, t1.str;      # normal
    350.   SampleMap r2, t3.stq_dq;      # a = attenuation 1
    351.   SampleMap r3, t4.stq_dq;      # a = attenuation 2
    352.    
    353.   DOT3 r5.sat, r4, r1.2x.bias;   # R5 = diffuse (N.L)
    354.    
    355.   MUL r0.rgb, r0, r5;
    356.   MUL r0.rgb.2x, r0, c0;
    357.   MUL r0.rgb, r0, r2.a;         # attenuate
    358.   MUL r0.rgb, r0, r3.a;
    359.   MUL r0.a, r0, c1;
    360. EndPass;
    361. "
    362.             }
    363.          }
    364.          SetTexture[_MainTex] {combine texture}
    365.          SetTexture[_CubeNormalize] {combine texture}
    366.          SetTexture[_LightTexture0] {combine texture}
    367.          SetTexture[_LightTextureB0] {combine texture}
    368.       }
    369.    }
    370.    
    371.    // ------------------------------------------------------------------
    372.    // Radeon 7000
    373.    
    374.    Category {
    375.       Material {
    376.          Diffuse [_Color]
    377.          Emission [_PPLAmbient]
    378.       }
    379.       Lighting On
    380.       Cull Off
    381.       SubShader {
    382.          Pass {
    383.             Name "BASE"
    384.             Tags {"LightMode" = "PixelOrNone"}
    385.             Color [_PPLAmbient]
    386.             Lighting Off
    387.             SetTexture [_MainTex] {Combine texture * primary DOUBLE}
    388.             SetTexture [_MainTex] {Combine texture * primary DOUBLE}
    389.             SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
    390.          }
    391.          Pass {    
    392.             Name "BASE"
    393.             Tags {"LightMode" = "Vertex"}
    394.             SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
    395.          }
    396.          Pass {
    397.             Name "PPL"
    398.             Tags {
    399.                "LightMode" = "Pixel"
    400.                "LightTexCount" = "2"
    401.             }
    402.             SetTexture [_LightTexture0]    { combine previous * texture alpha, previous }
    403.             SetTexture [_LightTextureB0]   {
    404.                combine previous * texture alpha + constant, previous
    405.                constantColor [_PPLAmbient]
    406.             }
    407.             SetTexture [_MainTex]    { combine previous * texture DOUBLE, primary * texture}
    408.          }
    409.          Pass {
    410.             Name "PPL"
    411.             Tags {
    412.                "LightMode" = "Pixel"
    413.                "LightTexCount"  = "1"
    414.             }
    415.             SetTexture [_LightTexture0] {
    416.                combine previous * texture alpha + constant, previous
    417.                constantColor [_PPLAmbient]
    418.             }
    419.             SetTexture [_MainTex]    { combine previous * texture DOUBLE, primary * texture}
    420.          }
    421.          Pass {
    422.             Name "PPL"
    423.             Tags {
    424.                "LightMode" = "Pixel"
    425.                "LightTexCount"  = "0"
    426.             }
    427.             SetTexture [_MainTex]    { combine previous * texture DOUBLE, primary * texture}
    428.          }
    429.       }
    430.    }
    431. }
    432.  
    433. Fallback "Transparent/Cutout/VertexLit", 2
    434.  
    435. }
    436.  
    vertexlit 2 sided

    Code (csharp):
    1.  
    2. Shader "Hidden/TerrainEngine/Details/VertexlitTwosided" {
    3.    Properties {
    4.    _Color ("Main Color", Color) = (1,1,1,1)
    5.    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    7. }
    8.  
    9. Category {
    10.    Tags {"RenderType"="TransparentCutout"}
    11.    LOD 200
    12.    Alphatest Greater [_Cutoff]
    13.    AlphaToMask True
    14.    ColorMask RGB
    15.    Blend AppSrcAdd AppDstAdd
    16.    
    17.    
    18.    
    19.    // ------------------------------------------------------------------
    20.    // ARB fragment program
    21.    
    22.    SubShader {
    23.       // Ambient pass
    24.        
    25.       Pass {
    26.          Name "BASE"
    27.          Tags {"LightMode" = "PixelOrNone"}
    28.          Color [_PPLAmbient]
    29.          SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * primary}
    30.       }
    31.        
    32.       // Vertex lights
    33.       Pass {
    34.          Name "BASE"
    35.          Tags {"LightMode" = "Vertex"}
    36.          Lighting On
    37.          Material {
    38.             Diffuse [_Color]
    39.             Emission [_PPLAmbient]
    40.          }
    41.          SetTexture [_MainTex] {combine texture * primary DOUBLE, texture * primary}
    42.       }
    43.       // Pixel lights
    44.      
    45.       Pass {    
    46.          Name "PPL"
    47.          Tags { "LightMode" = "Pixel" }
    48.          Fog { Color [_AddFog] }
    49.          
    50. CGPROGRAM
    51. #pragma fragment frag
    52. #pragma vertex vert
    53. #pragma multi_compile_builtin
    54. #pragma fragmentoption ARB_fog_exp2
    55. #pragma fragmentoption ARB_precision_hint_fastest
    56. #include "UnityCG.cginc"
    57. #include "AutoLight.cginc"
    58.  
    59. struct v2f {
    60.    V2F_POS_FOG;
    61.    LIGHTING_COORDS
    62.    float2   uv;
    63.    float3   normal;
    64.    float3   lightDir;
    65. };
    66.  
    67. uniform float4 _MainTex_ST;
    68.  
    69. v2f vert (appdata_base v)
    70. {
    71.    v2f o;
    72.    PositionFog( v.vertex, o.pos, o.fog );
    73.    o.normal = v.normal;
    74.    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    75.    o.lightDir = ObjSpaceLightDir( v.vertex );
    76.    TRANSFER_VERTEX_TO_FRAGMENT(o);
    77.    return o;
    78. }
    79.  
    80. uniform sampler2D _MainTex;
    81. uniform float4 _Color;
    82.  
    83. float4 frag (v2f i) : COLOR
    84. {
    85.    half4 texcol = tex2D( _MainTex, i.uv );    
    86.    // half4 c = DiffuseLight( i.lightDir, i.normal, texcol, LIGHT_ATTENUATION(i) );
    87.    
    88.  
    89.     #ifndef USING_DIRECTIONAL_LIGHT
    90.     i.lightDir = normalize(i.lightDir);
    91.     #endif
    92.    
    93.     half diffuse = dot( i.normal, i.lightDir );
    94.     /// lighting hack: some baselight + diffuse lighting * x   
    95.     diffuse = 0.4 + diffuse * 0.35;
    96.    
    97.     half4 c;
    98.     c.rgb = texcol.rgb * _ModelLightColor0.rgb * (diffuse * LIGHT_ATTENUATION(i) * 2);
    99.     c.a = 0; // diffuse passes by default don't contribute to overbright  
    100.     c.a = texcol.a * _Color.a;
    101.     return c;
    102. }
    103. ENDCG
    104.          SetTexture [_MainTex] {combine texture}
    105.          SetTexture [_LightTexture0] {combine texture}
    106.          SetTexture [_LightTextureB0] {combine texture}
    107.          
    108.       }
    109.     /// end 1st pass
    110.      
    111.      
    112.     ///2pass
    113.       Pass {    
    114.          Name "PPL2"
    115.          Tags { "LightMode" = "Pixel" }
    116.          // Only render pixels less or equal to the value
    117.          AlphaTest LEqual [_Cutoff]
    118.                  
    119.          // Set up alpha blending
    120.          Blend SrcAlpha OneMinusSrcAlpha
    121.          // Dont write to the depth buffer
    122.          ZWrite Off
    123.          SetTexture [_MainTex] { constantColor [_Color] Combine texture * constant, texture * constant}
    124.       }
    125.     ///2pass
    126.        
    127.    }
    128.    
    129.    
    130.    
    131.    
    132.    
    133.     // ------------------------------------------------------------------
    134.    // Radeon 9000
    135.  
    136.    SubShader {
    137.       // Ambient pass
    138.       Pass {
    139.          Name "BASE"
    140.          Tags {"LightMode" = "PixelOrNone"}
    141.          Color [_PPLAmbient]
    142.          SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
    143.       }
    144.       // Vertex lights
    145.       Pass {
    146.          Name "BASE"
    147.          Tags {"LightMode" = "Vertex"}
    148.          Lighting On
    149.          Material {
    150.             Diffuse [_Color]
    151.             Emission [_PPLAmbient]
    152.          }
    153.          SetTexture [_MainTex] {Combine texture * primary DOUBLE, texture * primary}
    154.       }
    155.        
    156.       // Pixel lights with 0 light textures
    157.       Pass {
    158.          Name "PPL"
    159.          Tags {
    160.             "LightMode" = "Pixel"
    161.             "LightTexCount" = "0"
    162.          }
    163.  
    164. CGPROGRAM
    165. #pragma vertex vert
    166. #include "UnityCG.cginc"
    167.  
    168. struct v2f {
    169.    V2F_POS_FOG;
    170.    float2 uv      : TEXCOORD0;
    171.    float3 normal   : TEXCOORD1;
    172.    float3 lightDir   : TEXCOORD2;
    173. };
    174.  
    175. uniform float4 _MainTex_ST;
    176.  
    177. v2f vert(appdata_base v)
    178. {
    179.    v2f o;
    180.    PositionFog( v.vertex, o.pos, o.fog );
    181.    o.normal = v.normal;
    182.    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    183.    o.lightDir = ObjSpaceLightDir( v.vertex );
    184.    return o;
    185. }
    186. ENDCG
    187.          Program "" {
    188.             SubProgram {
    189.                Local 0, [_ModelLightColor0]
    190.                Local 1, [_Color]
    191.  
    192. "!!ATIfs1.0
    193. StartConstants;
    194.   CONSTANT c0 = program.local[0];
    195.   CONSTANT c1 = program.local[1];
    196. EndConstants;
    197.  
    198. StartOutputPass;
    199.   SampleMap r0, t0.str;         # main texture
    200.   SampleMap r1, t2.str;         # normalized light dir
    201.   PassTexCoord r2, t1.str;      # normal
    202.    
    203.   DOT3 r5.sat, r2, r1.2x.bias;   # R5 = diffuse (N.L)
    204.    
    205.   MUL r0.rgb, r0, r5;
    206.   MUL r0.rgb.2x, r0, c0;
    207.   MUL r0.a, r0, c1;
    208. EndPass;
    209. "
    210.             }
    211.          }
    212.          SetTexture[_MainTex] {combine texture}
    213.          SetTexture[_CubeNormalize] {combine texture}
    214.       }
    215.        
    216.       // Pixel lights with 1 light texture
    217.       Pass {
    218.          Name "PPL"
    219.          Tags {
    220.             "LightMode" = "Pixel"
    221.             "LightTexCount" = "1"
    222.          }
    223.  
    224. CGPROGRAM
    225. #pragma vertex vert
    226. #include "UnityCG.cginc"
    227.  
    228. uniform float4 _MainTex_ST;
    229. uniform float4x4 _SpotlightProjectionMatrix0;
    230.  
    231. struct v2f {
    232.    V2F_POS_FOG;
    233.    float2 uv      : TEXCOORD0;
    234.    float3 normal   : TEXCOORD1;
    235.    float3 lightDir   : TEXCOORD2;
    236.    float4 LightCoord0 : TEXCOORD3;
    237. };
    238.  
    239. v2f vert(appdata_tan v)
    240. {
    241.    v2f o;
    242.    PositionFog( v.vertex, o.pos, o.fog );
    243.    o.normal = v.normal;
    244.    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    245.    o.lightDir = ObjSpaceLightDir( v.vertex );
    246.    
    247.    o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
    248.    
    249.    return o;
    250. }
    251. ENDCG
    252.          Program "" {
    253.             SubProgram {
    254.                Local 0, [_ModelLightColor0]
    255.                Local 1, [_Color]
    256.  
    257. "!!ATIfs1.0
    258. StartConstants;
    259.   CONSTANT c0 = program.local[0];
    260.   CONSTANT c1 = program.local[1];
    261. EndConstants;
    262.  
    263. StartOutputPass;
    264.   SampleMap r0, t0.str;         # main texture
    265.   SampleMap r1, t2.str;         # normalized light dir
    266.   PassTexCoord r4, t1.str;      # normal
    267.   SampleMap r2, t3.str;         # a = attenuation
    268.    
    269.   DOT3 r5.sat, r4, r1.2x.bias;   # R5 = diffuse (N.L)
    270.    
    271.   MUL r0.rgb, r0, r5;
    272.   MUL r0.rgb.2x, r0, c0;
    273.   MUL r0.rgb, r0, r2.a;         # attenuate
    274.   MUL r0.a, r0, c1;
    275. EndPass;
    276. "
    277.             }
    278.          }
    279.          SetTexture[_MainTex] {combine texture}
    280.          SetTexture[_CubeNormalize] {combine texture}
    281.          SetTexture[_LightTexture0] {combine texture}
    282.       }
    283.        
    284.       // Pixel lights with 2 light textures
    285.       Pass {
    286.          Name "PPL"
    287.          Tags {
    288.             "LightMode" = "Pixel"
    289.             "LightTexCount" = "2"
    290.          }
    291. CGPROGRAM
    292. #pragma vertex vert
    293. #include "UnityCG.cginc"
    294.  
    295. uniform float4 _MainTex_ST;
    296. uniform float4x4 _SpotlightProjectionMatrix0;
    297. uniform float4x4 _SpotlightProjectionMatrixB0;
    298.  
    299. struct v2f {
    300.    V2F_POS_FOG;
    301.    float2 uv      : TEXCOORD0;
    302.    float3 normal   : TEXCOORD1;
    303.    float3 lightDir   : TEXCOORD2;
    304.    float4 LightCoord0 : TEXCOORD3;
    305.    float4 LightCoordB0 : TEXCOORD4;
    306. };
    307.  
    308. v2f vert(appdata_tan v)
    309. {
    310.    v2f o;
    311.    PositionFog( v.vertex, o.pos, o.fog );
    312.    o.normal = v.normal;
    313.    o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    314.    o.lightDir = ObjSpaceLightDir( v.vertex );
    315.    
    316.    o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
    317.    o.LightCoordB0 = mul(_SpotlightProjectionMatrixB0, v.vertex);
    318.    
    319.    return o;
    320. }
    321. ENDCG
    322.          Program "" {
    323.             SubProgram {
    324.                Local 0, [_ModelLightColor0]
    325.                Local 1, [_Color]
    326.  
    327. "!!ATIfs1.0
    328. StartConstants;
    329.   CONSTANT c0 = program.local[0];
    330.   CONSTANT c1 = program.local[1];
    331. EndConstants;
    332.  
    333. StartOutputPass;
    334.   SampleMap r0, t0.str;         # main texture
    335.   SampleMap r1, t2.str;         # normalized light dir
    336.   PassTexCoord r4, t1.str;      # normal
    337.   SampleMap r2, t3.stq_dq;      # a = attenuation 1
    338.   SampleMap r3, t4.stq_dq;      # a = attenuation 2
    339.    
    340.   DOT3 r5.sat, r4, r1.2x.bias;   # R5 = diffuse (N.L)
    341.    
    342.   MUL r0.rgb, r0, r5;
    343.   MUL r0.rgb.2x, r0, c0;
    344.   MUL r0.rgb, r0, r2.a;         # attenuate
    345.   MUL r0.rgb, r0, r3.a;
    346.   MUL r0.a, r0, c1;
    347. EndPass;
    348. "
    349.             }
    350.          }
    351.          SetTexture[_MainTex] {combine texture}
    352.          SetTexture[_CubeNormalize] {combine texture}
    353.          SetTexture[_LightTexture0] {combine texture}
    354.          SetTexture[_LightTextureB0] {combine texture}
    355.       }
    356.    }
    357.    
    358.    // ------------------------------------------------------------------
    359.    // Radeon 7000
    360.    
    361.    Category {
    362.       Material {
    363.          Diffuse [_Color]
    364.          Emission [_PPLAmbient]
    365.       }
    366.       Lighting On
    367.       Cull Off
    368.       SubShader {
    369.          Pass {
    370.             Name "BASE"
    371.             Tags {"LightMode" = "PixelOrNone"}
    372.             Color [_PPLAmbient]
    373.             Lighting Off
    374.             SetTexture [_MainTex] {Combine texture * primary DOUBLE}
    375.             SetTexture [_MainTex] {Combine texture * primary DOUBLE}
    376.             SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
    377.          }
    378.          Pass {    
    379.             Name "BASE"
    380.             Tags {"LightMode" = "Vertex"}
    381.             SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
    382.          }
    383.          Pass {
    384.             Name "PPL"
    385.             Tags {
    386.                "LightMode" = "Pixel"
    387.                "LightTexCount" = "2"
    388.             }
    389.             SetTexture [_LightTexture0]    { combine previous * texture alpha, previous }
    390.             SetTexture [_LightTextureB0]   {
    391.                combine previous * texture alpha + constant, previous
    392.                constantColor [_PPLAmbient]
    393.             }
    394.             SetTexture [_MainTex]    { combine previous * texture DOUBLE, primary * texture}
    395.          }
    396.          Pass {
    397.             Name "PPL"
    398.             Tags {
    399.                "LightMode" = "Pixel"
    400.                "LightTexCount"  = "1"
    401.             }
    402.             SetTexture [_LightTexture0] {
    403.                combine previous * texture alpha + constant, previous
    404.                constantColor [_PPLAmbient]
    405.             }
    406.             SetTexture [_MainTex]    { combine previous * texture DOUBLE, primary * texture}
    407.          }
    408.          Pass {
    409.             Name "PPL"
    410.             Tags {
    411.                "LightMode" = "Pixel"
    412.                "LightTexCount"  = "0"
    413.             }
    414.             SetTexture [_MainTex]    { combine previous * texture DOUBLE, primary * texture}
    415.          }
    416.       }
    417.    }
    418. }
    419.  
    420. Fallback "Transparent/Cutout/VertexLit", 2
    421.  
    422. }
    423.  
    -----------------------------
    grass shader

    not much to say about this. it just comes with some nicer bending…

    grass shader

    Code (csharp):
    1.  
    2. Shader "Hidden/TerrainEngine/Details/WavingDoublePass" {
    3.     Properties {
    4.         _WavingTint ("Fade Color", Color) = (.7,.6,.5, 0)
    5.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    6.         _WaveAndDistance ("Wave and distance", Vector) = (12, 3.6, 1, 1)
    7.         _Cutoff ("Cutoff", float) = 0.5
    8.         _WindSpeed ("WaveSpeed", range (1, 10)) = 4        
    9.         _WaveScale ("WaveScale", range (1, 40)) = 5
    10.     }
    11.     SubShader {
    12.         Tags {
    13.             "Queue" = "Transparent-101"
    14.             "IgnoreProjector"="True"
    15.             "RenderType"="Grass"
    16.         }
    17.  
    18.         ColorMask RGB
    19.         Cull Off
    20.        
    21.         Pass {
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma multi_compile NO_INTEL_GMA_X3100_WORKAROUND INTEL_GMA_X3100_WORKAROUND
    25.             #include "MyWavingGrass.cginc"
    26.             ENDCG          
    27.  
    28.             AlphaTest Greater [_Cutoff]
    29.  
    30.             SetTexture [_MainTex] { combine texture * primary DOUBLE, texture * primary}
    31.         }
    32.         Pass {
    33.             Tags { "RequireOptions" = "SoftVegetation" }
    34.             CGPROGRAM
    35.             #pragma vertex vert
    36.             #pragma multi_compile NO_INTEL_GMA_X3100_WORKAROUND INTEL_GMA_X3100_WORKAROUND
    37.             #include "MyWavingGrass.cginc"
    38.             ENDCG          
    39.  
    40.             // Dont write to the depth buffer
    41.             ZWrite off
    42.            
    43.             // Only render pixels less or equal to the value
    44.             AlphaTest LEqual [_Cutoff]
    45.            
    46.             // Set up alpha blending
    47.             Blend SrcAlpha OneMinusSrcAlpha
    48.  
    49.             SetTexture [_MainTex] { combine texture * primary DOUBLE, texture * primary }
    50.          }
    51.     }
    52.     SubShader {
    53.         Tags {
    54.             "Queue" = "Transparent-101"
    55.             "IgnoreProjector"="True"
    56.             "RenderType"="Grass"
    57.         }
    58.  
    59.         ColorMask RGB
    60.         Cull Off
    61.        
    62.         Pass {
    63.             BindChannels {
    64.                 Bind "vertex", vertex
    65.                 Bind "color", color
    66.                 Bind "texcoord", texcoord
    67.             }
    68.             AlphaTest Greater [_Cutoff]
    69.             SetTexture [_MainTex] { combine texture * primary DOUBLE, texture }
    70.         }
    71.     }
    72.    
    73.     Fallback Off
    74.    
    75. }
    76.  
    77.  
    MyWavingGrass.cg

    Code (csharp):
    1.  
    2. #include "UnityCG.cginc"
    3. #include "TerrainEngine.cginc"
    4.  
    5.  
    6.  
    7. struct v2f {
    8.     float4 pos : POSITION;
    9.     float4 color : COLOR;
    10.     float fog : FOGC;
    11.     float4 uv : TEXCOORD0;
    12. };
    13.  
    14. int _WindSpeed, _WaveScale;
    15.  
    16. v2f vert (appdata_grass v) {
    17.     v2f o;
    18.  
    19.     //float waveAmount = v.color.a * _WaveAndDistance.z;   
    20.     float waveAmountmy = 0;
    21.     // neede to get the coloring from the lightmap...
    22.     TerrainWaveGrass (v.vertex, waveAmountmy, v.color.rgb, o.color);
    23.    
    24.     // start new bending
    25.    
    26.     //const float4 _waveXSize = float4(0.012, 0.02, -0.06, 0.048) * 2;
    27.     //const float4 _waveZSize = float4 (0.006, .02, -0.02, 0.1) * 2;
    28.    
    29.     // float ( ????, verdrehung, ????, ????)
    30.    
    31.     const float4 _waveXSize = float4 (0.001, 0.091, 0.02, 0.1) * 4;
    32.     const float4 _waveZSize = float4 (0.005, 0.051, 0.02, 0.1) * 4;
    33.    
    34.     const float4 waveSpeed = float4 (0.3, .3, .08, .07) * 4;
    35.  
    36.    float4 _waveXmove = _waveXSize * waveSpeed * _WaveScale;
    37.    float4 _waveZmove = _waveZSize * waveSpeed * _WaveScale;
    38.  
    39.    // We model the wind as basic waves...
    40.  
    41.    // Calculate the wind input to leaves from their vertex positions...
    42.    // for now, we transform them into world-space x/z positions...
    43.    // Later on, we should actually be able to do the whole calc's in post-projective space
    44.    float3 worldPos = mul ((float3x4)_Object2World, v.vertex);
    45.    
    46.    // This is the input to the sinusiodal warp
    47.    float4 waves;
    48.    waves = (worldPos.x ) * _waveXSize;
    49.    waves += (worldPos.z ) * _waveZSize;
    50.  
    51.    // Add in time to model them over time
    52.    waves += _Time.x * waveSpeed *_WindSpeed;
    53.  
    54.    float4 s, c;
    55.    waves = frac (waves);
    56.    FastSinCos (waves, s,c);
    57.  
    58.    float waveAmount = v.texcoord.y;
    59.    s *= waveAmount;
    60.  
    61.    // Faster winds move the grass more than slow winds
    62.    s *= normalize (waveSpeed);
    63.  
    64.    s = s * s;
    65.    float fade = dot (s, 1.3);
    66.    s = s * s;
    67.    float3 waveMove = float3 (0,0,0);
    68.    waveMove.x = dot (s, _waveXmove);
    69.    waveMove.z = dot (s, _waveZmove);
    70.  
    71.    v.vertex.xz -= mul ((float3x3)_World2Object, waveMove).xz;
    72.    
    73. ////////// end new bending
    74.  
    75.     o.pos = mul (glstate.matrix.mvp, v.vertex);
    76.     o.fog = o.pos.z;
    77.     o.uv = v.texcoord;
    78.  
    79.     // Saturate because Radeon HD drivers on OS X 10.4.10 don't saturate vertex colors properly
    80.     float3 offset = v.vertex.xyz - _CameraPosition.xyz;
    81.     o.color.a = saturate( _WaveAndDistance.w - dot(offset, offset) );
    82.  
    83.     return o;
    84. }
    85.  
    86.  
    87. v2f BillboardVert (appdata_grass v) {
    88.     v2f o;
    89.     TerrainBillboardGrass (v.vertex, v.texcoord1.xy);
    90.     float waveAmount = v.texcoord1.y;
    91.     TerrainWaveGrass (v.vertex, waveAmount, v.color.rgb, o.color);
    92.        
    93.     float4 pos = mul (glstate.matrix.mvp, v.vertex);   
    94.     o.pos = mul (glstate.matrix.mvp, v.vertex);
    95.     o.fog = o.pos.z;
    96.     o.uv = v.texcoord;
    97.     return o;
    98. }
    99.  
    100.  

    -----------------------------
    hacked terrain shader

    it is based on an idea by Kragh [http://forum.unity3d.com/viewtopic.php?t=34949&start=15]: using unused alpha channel of the terrain textures to store some kind of normal map as you can’t add new textures.
    first of all i thought about compressing a 24 bit bump map to the 8 bit alpha channel in some way like this: BBGGGRRR. so red would have 8 different greys, green also and blue just 4. not a fancy bump map but more than just nothing. anyway – i did not find a way to do this in photoshop and did not want to try to script such a compression. decompression would have to be done on the gpu.
    now i just create some kind of greyscale image, copy it to the alpha channel and split it up in the shader somehow… just trail and error.

    limits:
    it just supports pixellit terrains with max 4. textures. but i guess it would not be a big deal to extend it to 8 textures.

    FirstPassRealtime.shader

    Code (csharp):
    1.  
    2. Shader "Hidden/TerrainEngine/Splatmap/Realtime-FirstPass" {
    3. Properties {
    4.     _Control ("Control (RGBA)", 2D) = "red" {}
    5.     _LightMap ("LightMap (RGB)", 2D) = "white" {}
    6.     _Splat3 ("Layer 3 (R)", 2D) = "white" {}
    7.     _Splat2 ("Layer 2 (B)", 2D) = "white" {}
    8.     _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    9.     _Splat0 ("Layer 0 (A)", 2D) = "white" {}
    10.     _BaseMap ("BaseMap (RGB)", 2D) = "white" {}
    11. }
    12.    
    13. SubShader {    
    14.     Tags {
    15.         "SplatCount" = "4"
    16.         "Queue" = "Geometry-100"
    17.         "RenderType" = "Opaque"
    18.     }
    19.    
    20.     Blend AppSrcAdd AppDstAdd
    21.     Fog { Color [_AddFog] }
    22.    
    23.     // Ambient pass
    24.     Pass {
    25.         Tags { "LightMode" = "PixelOrNone" }
    26.        
    27.         CGPROGRAM
    28.         #pragma vertex AmbientSplatVertex
    29.         #pragma fragment AmbientSplatFragment
    30.         #pragma fragmentoption ARB_fog_exp2
    31.         #pragma fragmentoption ARB_precision_hint_fastest
    32.    
    33.         #define USE_LIGHTMAP
    34.         #include "splatting.cginc"
    35.         ENDCG
    36.     }
    37.     // Vertex lights
    38.     Pass {
    39.         Tags { "LightMode" = "Vertex" }
    40.        
    41.         CGPROGRAM
    42.         #pragma vertex VertexlitSplatVertex
    43.         #pragma fragment VertexlitSplatFragment
    44.         #pragma fragmentoption ARB_fog_exp2
    45.         #pragma fragmentoption ARB_precision_hint_fastest
    46.         #define USE_LIGHTMAP
    47.         #include "splatting.cginc"
    48.         ENDCG
    49.     }
    50.     // Pixel lights
    51.     Pass {
    52.         Tags { "LightMode" = "Pixel" }
    53.        
    54.         CGPROGRAM
    55.         #pragma vertex PixellitSplatVertex
    56.         #pragma fragment PixellitSplatFragment
    57.         #pragma fragmentoption ARB_fog_exp2
    58.         #pragma fragmentoption ARB_precision_hint_fastest
    59.         #pragma multi_compile_builtin
    60.         #include "UnityCG.cginc"
    61.         #include "AutoLight.cginc"
    62.        
    63.         #define INCLUDE_PIXEL
    64.         // #include "splatting.cginc"
    65.         ///
    66. struct v2f_ambient {
    67.     V2F_POS_FOG;
    68.     float4 uv[3] : TEXCOORD0;
    69.     float fade : TEXCOORD3;
    70. };
    71. struct v2f_vertex {
    72.     V2F_POS_FOG;
    73.     float4 uv[3] : TEXCOORD0;
    74.     float4 color : COLOR;
    75.     #ifdef USE_LIGHTMAP
    76.     float fade : TEXCOORD3;
    77.     #endif
    78. };
    79.  
    80. #ifdef INCLUDE_PIXEL
    81. struct v2f_pixel {
    82.     V2F_POS_FOG;
    83.     LIGHTING_COORDS
    84.     float3 normal;
    85.     float4 lightDirFade;
    86.     float4 uv[3];
    87. };
    88. #endif
    89.  
    90.  
    91. uniform sampler2D _Control;
    92. uniform sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    93. uniform float4 _Splat0_ST,_Splat1_ST,_Splat2_ST,_Splat3_ST,_Splat4_ST;
    94.  
    95. uniform sampler2D _LightMap;
    96.  
    97. uniform float4 _RealtimeFade;
    98.  
    99. float4 CalculateVertexLights (float3 viewPos, float3 objSpaceNormal)
    100. {
    101.     float3 normal = mul (objSpaceNormal, (float3x3)glstate.matrix.transpose.modelview[0]);
    102.    
    103.     // Do vertex light calculation: up to four lights,
    104.     // treat spot lights as point lights.
    105.     // TODO: optimize/vectorize me!
    106.     float4 lightColor = glstate.lightmodel.ambient;
    107.     for (int i = 0; i < 4; i++) {
    108.         float3 toLight = glstate.light[i].position.xyz - viewPos.xyz * glstate.light[i].position.w;
    109.         float lengthSq = dot(toLight, toLight);
    110.         float atten = 1.0 / (1.0 + lengthSq * glstate.light[i].attenuation.z);
    111.         float lightAmt = max( 0, dot (normal, normalize(toLight)) * atten );
    112.         lightColor += glstate.light[i].diffuse * lightAmt;
    113.     }
    114.  
    115.     return lightColor;
    116. }
    117.  
    118. #define CALC_SPLAT_UV(baseUV) \
    119.     o.uv[0].xy = baseUV; \
    120.     o.uv[1].xy = TRANSFORM_TEX (baseUV, _Splat0); \
    121.     o.uv[1].zw = TRANSFORM_TEX (baseUV, _Splat1); \
    122.     o.uv[2].xy = TRANSFORM_TEX (baseUV, _Splat2); \
    123.     o.uv[2].zw = TRANSFORM_TEX (baseUV, _Splat3);
    124.  
    125. #define SAMPLE_SPLAT(i,splat_color) \
    126.     half4 splat_control = tex2D (_Control, i.uv[0].xy); \
    127.     splat_color = splat_control.r * tex2D (_Splat0, i.uv[1].xy); \
    128.     splat_color += splat_control.g * tex2D (_Splat1, i.uv[1].zw); \
    129.     splat_color += splat_control.b * tex2D (_Splat2, i.uv[2].xy); \
    130.     splat_color += splat_control.a * tex2D (_Splat3, i.uv[2].zw);
    131.  
    132. float4 VertexlitSplatFragment (v2f_vertex i) : COLOR {
    133.     half4 splat;
    134.     SAMPLE_SPLAT(i,splat);
    135.     #ifdef USE_LIGHTMAP
    136.     half4 lightmap = tex2D (_LightMap, i.uv[0].xy);
    137.     i.color = lerp( lightmap, i.color, saturate(i.fade) );
    138.     #endif
    139.     half4 col = splat * i.color;
    140.     col *= float4 (2,2,2,0);
    141.     return col;
    142. }
    143.  
    144. v2f_vertex VertexlitSplatVertex (appdata_base v) {
    145.     v2f_vertex o;
    146.     PositionFog( v.vertex, o.pos, o.fog );
    147.     float3 viewpos = mul(glstate.matrix.modelview[0], v.vertex).xyz;
    148.     o.color = CalculateVertexLights (viewpos, v.normal);
    149.     CALC_SPLAT_UV(v.texcoord.xy);
    150.     #ifdef USE_LIGHTMAP
    151.     o.fade = 1.0 - (o.fog * _RealtimeFade.z + _RealtimeFade.w);
    152.     #endif 
    153.     return o;
    154. }
    155.  
    156.  
    157. float4 AmbientSplatFragment (v2f_ambient i) : COLOR {
    158.     half4 splat;
    159.     SAMPLE_SPLAT(i,splat);
    160.     half4 lightcolor = tex2D (_LightMap, i.uv[0].xy);  
    161.     lightcolor = lerp( _PPLAmbient, lightcolor, saturate(i.fade) );
    162.     half4 col = splat * lightcolor;
    163.     col *= float4 (2,2,2,1);
    164.     return col;
    165. }
    166.  
    167. v2f_ambient AmbientSplatVertex (appdata_base v) {
    168.     v2f_ambient o;
    169.     PositionFog( v.vertex, o.pos, o.fog );
    170.     CALC_SPLAT_UV(v.texcoord.xy);
    171.     o.fade = o.fog * _RealtimeFade.z + _RealtimeFade.w;
    172.     return o;
    173. }
    174.  
    175.  
    176. #ifdef INCLUDE_PIXEL
    177.  
    178. float4 PixellitSplatFragment (v2f_pixel i) : COLOR {
    179.     float4 splat;
    180.     SAMPLE_SPLAT(i,splat);
    181.     float atten = LIGHT_ATTENUATION(i);
    182.    
    183. // get normal from the normal map
    184. // if a bumpmap is assigned
    185. // it would be just one bumpmap for all textures...
    186. // and you would have to make it not to be drawn to the terrain in #define SAMPLE_SPLAT -> last line
    187.     float3 normal = tex2D(_Splat3, i.uv[2].zw) * 2;
    188.    
    189. // or let us create a normal from a greyscale image v1 
    190.     //float3 normal = float3(0.15, 1.25 * splat.a, 0.15) * 2;
    191.    
    192.    
    193.    
    194. // or let us create a normal from a greyscale image v2 
    195.     float rvalue = clamp( splat.a , 0.0 , 0.33 );
    196.     float gvalue = clamp( splat.a , 0.34 , 0.66 );
    197.     float bvalue = clamp( splat.a , 0.67 , 1.0);
    198.    
    199.     normal = float3(gvalue*2, rvalue*2, bvalue*2) *2-0.25;
    200.    
    201.     normal = i.normal + normal;
    202.    
    203.     // origiginal ? ends up being to bright
    204.     //return DiffuseLight( i.lightDirFade.xyz, i.normal, splat, atten * saturate(i.lightDirFade.w) );
    205.    
    206.     return DiffuseLight( i.lightDirFade.xyz, normal, splat, atten * saturate(i.lightDirFade.w) ) * 0.5;
    207. }
    208.  
    209. v2f_pixel PixellitSplatVertex (appdata_base v) {
    210.     v2f_pixel o;
    211.     PositionFog( v.vertex, o.pos, o.fog );
    212.     CALC_SPLAT_UV(v.texcoord.xy);
    213.     o.normal = v.normal;
    214.     o.lightDirFade.xyz = ObjSpaceLightDir( v.vertex );
    215.     o.lightDirFade.w = 1.0 - (o.fog * _RealtimeFade.z + _RealtimeFade.w);
    216.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    217.     return o;
    218. }
    219.  
    220. #endif
    221.  
    222.  
    223. float4 LightmapSplatFragment (v2f_vertex i) : COLOR {
    224.     half4 splat;
    225.     SAMPLE_SPLAT(i,splat);
    226.     half4 col = splat * tex2D (_LightMap, i.uv[0].xy);
    227.     col *= float4 (2,2,2,1);
    228.     return col;
    229. }
    230.  
    231. v2f_vertex LightmapSplatVertex (appdata_base v) {
    232.     v2f_vertex o;
    233.     PositionFog( v.vertex, o.pos, o.fog );
    234.     CALC_SPLAT_UV(v.texcoord.xy);
    235.     return o;
    236. }      
    237.        
    238.        
    239.        
    240.         ///
    241.         ENDCG
    242.     }
    243.    
    244.     UsePass "VertexLit/SHADOWCOLLECTOR"
    245. }
    246.    
    247. // Fallback to Lightmap
    248. Fallback "Hidden/TerrainEngine/Splatmap/Lightmap-FirstPass"
    249. }
    250.  
    251.  


    -----------------------------
    making sure that the modified shaders will get exported

    read: http://forum.unity3d.com/viewtopic.php?t=28053
    it is important to assign the shaders to some materials and map those to a game object placed in the scene.
     
  23. mRadek

    mRadek

    Joined:
    Feb 17, 2009
    Posts:
    25
    How to use Lightmaps with bumpmaps together?
     
  24. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    just as a proof of concept of using a "compressed" bumpmap on the terrain i took a screenshoot of the bumpmap generated by unity, opened it in photoshop, exported the channels as gif (red using 8 colors = 3bit, green using 8 colors = 3bit and blue using 4 colors = 2bit) and pasted these gifs back to the related channels...
    it looks a bit "sharper" and very bumpy but alright to me.
     

    Attached Files:

  25. Zeusk

    Zeusk

    Joined:
    Mar 9, 2010
    Posts:
    9
    verry nice i love the ultra glow effect
     
  26. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    there have been a lot of requests on the support of day and night cycles as far as the terrain engine is concerned. right now it is not possible as the only possibility to do so is using vertex light on the terrain which corrupts the lighting of grass. it also would be nice to be able to use pixel lighting on the terrain in combination with a dynamic directional light.


    modifications to support day and night cycles
    -----------------------------------

    terrain lighting
    - possibility to choose between lightmap or vertex lighting as fall back for pixel lightened terrain at the given shadow distance.

    grass shader
    as the lighting of grass should not be calculated using the normal of the grass mesh but the terrain's normal we need the terrain's normal in the grass shader.
    i can think of two ways of passing it:
    1 ) normals of terrain passed to shader as float if possible
    2 ) instead of passing the lightmap pass a "bumpmap" of the terrain and use it as lookup table for the normals within the shader
     
  27. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    I've taken this project and added shadows of moving clouds. Here's the current status:

    http://web.lemuria.org/~tom/unity/DriftingClouds.html
    package:
    http://web.lemuria.org/~tom/unity/MovingClouds.unitypackage

    As you can see in the webplayer, the cloud shadows are broken, but only in the webplayer. Load the package into any scene (e.g. the one Lars posted) and drag the prefab into your scene (may have to adjust ortho size, etc.) and you'll (hopefully) notice that it works fine in the editor.

    There is also some negative interaction with the vegetation, where when the projector is enabled, the transparent parts of grass, etc. become visible.

    For both of these problems, someone who understands more about shaders may want to take a look and find out why that is. I have no idea.


    The script and shader are based on what was posted in this thread: http://forum.unity3d.com/viewtopic.php?t=32807
     
  28. ThorsHand11

    ThorsHand11

    Joined:
    Apr 17, 2010
    Posts:
    13
    Sorry to sound like an idiot but I can't seem to get it to work. Help!
     
  29. SomeRandomGuy18

    SomeRandomGuy18

    Joined:
    Jun 1, 2010
    Posts:
    1
    My Unity can't decompress the file
     
  30. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    An addition i'd love to see would be horizontal texture mapping for terrain, like Grome :)
     
  31. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    This is very nice looking, although the shadows occasionally flicker a bit. I am most impressed with your main character though, the doggy is really nice.
     
  32. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    The scene looks stunning. One of the most impressive i've seen in the showcase yet.



    The poor doggie however, his legs tend to do the wacky locomotion dance. As if upon close inspection he's really one of the dogs from the remake of "The Thing" back in the 80's.
     
  33. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    I saw you were (at some point) using my OatHDR shader though it is "Missing" from the package. I've since updated it (and finally got it working with 2.6 as it was supposed to), you can grab it here. I'd give you an updated version of the godrays too, but I've been having build problems with it still.
     
  34. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    I think it looks more like a fox.
     
  35. caesargao

    caesargao

    Joined:
    Dec 16, 2009
    Posts:
    19
    the download link on first page is not good anymore. anyone has a new working link? :D
     
  36. platonia96

    platonia96

    Joined:
    Mar 10, 2010
    Posts:
    225
    worked fine for me- using this?

     
  37. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    … i did some rework on my terrain textures, changed lighting and the terrain heightmap. although it is still only faked bumpmapping on the terrain just abusing the texture’s alpha channel i am more and more convinced by its impact.

    lars
     

    Attached Files:

  38. platonia96

    platonia96

    Joined:
    Mar 10, 2010
    Posts:
    225
    Care to share the hacked shader?

    Looks really awesome!
     
  39. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
  40. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    The normal mapping alone is incredible. Do the textures behave just like in the unhacked version though, or are there oddities?
     
  41. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    just compare the images showing hacked and original shader: both shader use the same textures… but using a standard rgb (instead of argb) texture within the hacked shader leads to odd results.
     
  42. platonia96

    platonia96

    Joined:
    Mar 10, 2010
    Posts:
    225
  43. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    it contains everything: shaders, hacked shaders, textures, models … just everything you see in the webplayer. in fact it is just my whole project exported.
     
  44. platonia96

    platonia96

    Joined:
    Mar 10, 2010
    Posts:
    225
    :eek: This is truly amazing.
     
  45. grugin

    grugin

    Joined:
    Jul 28, 2010
    Posts:
    12
    the link to the package is dead. There is an another way to get this wonderful package ??? Thanks in advance
     
  46. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    here comes the shaders:
     

    Attached Files:

  47. 3dDude

    3dDude

    Joined:
    Jul 4, 2010
    Posts:
    1,067
    how does this work?
     
  48. Julian-Glenn

    Julian-Glenn

    Joined:
    Jul 23, 2010
    Posts:
    97
    Brilliant work.
    Very impressive.
    Kudos

    /julian
     
  49. afalk

    afalk

    Joined:
    Jun 21, 2010
    Posts:
    164
    Looks utterly gorgeous :) Downloading now to take a look!
     
  50. timbo3185

    timbo3185

    Joined:
    Aug 2, 2010
    Posts:
    5
    Does anyone else have this package available from another service? the dropbox maxes out and i can't download it, I would love to have this for my project :)