Unity Community

Register or Sign In:

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 32

  1. Location
    In the line up, catching the big sets.
    Posts
    5,240

    U3: Add 2 sided to AlphaTest-Bumped.shader?

    I need a 2-sided cut-out transparent, diffuse bumped shader. I looked at some of the other 2-sided shaders posted here in the forums, but they don't seem to work in Unity 3.0. Any ideas? Here is the original shader:

    Code:  
    1. Shader "Transparent/Cutout/Bumped Diffuse" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _BumpMap ("Normalmap", 2D) = "bump" {}
    6.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    7. }
    8.  
    9. SubShader {
    10.     Tags {"IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    11.     LOD 300
    12.    
    13. CGPROGRAM
    14. #pragma surface surf Lambert alphatest:_Cutoff
    15.  
    16. sampler2D _MainTex;
    17. sampler2D _BumpMap;
    18. float4 _Color;
    19.  
    20. struct Input {
    21.     float2 uv_MainTex;
    22.     float2 uv_BumpMap;
    23. };
    24.  
    25. void surf (Input IN, inout SurfaceOutput o) {
    26.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    27.     o.Albedo = c.rgb;
    28.     o.Alpha = c.a;
    29.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    30. }
    31. ENDCG
    32. }
    33.  
    34. FallBack "Transparent/Cutout/Diffuse"
    35. }


  2. Location
    GMA950
    Posts
    2,439
    Have you considered doubling your geometry? It's way easier and usually performs better.


  3. Location
    In the line up, catching the big sets.
    Posts
    5,240
    I want to use it on an interactive cloth object.


  4. Location
    GMA950
    Posts
    2,439
    Ah. That's a good reason to do it in a shader.

    Unfortunately, I think might be pretty horrible to do. I would approach it by compiling the above shader with #pragma debug. This will give you the passes generated by the surface shader translator. Then you need to duplicate all of those passes and negate the normals in the duplicates.

    I'm not actually sure how that will work with lighting passes.


  5. Location
    In the line up, catching the big sets.
    Posts
    5,240
    I'm about as far from a shader coder as you can get. I think I modified a grand total of one shader without breaking it. Maybe one of those new visual shader editors might be easier for me to work with...


  6. Location
    GMA950
    Posts
    2,439
    Ok wait I figured out a far easier way to do it. It requires two materials: the built-in one and this, slightly modified one:
    Code:  
    1. Shader "Transparent/Cutout/Bumped Diffuse Backwards" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _BumpMap ("Normalmap", 2D) = "bump" {}
    6.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    7. }
    8.  
    9. SubShader {
    10.     Tags {"IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    11.     LOD 300
    12.     Cull Front
    13.    
    14. CGPROGRAM
    15. #pragma surface surf Lambert alphatest:_Cutoff
    16.  
    17. sampler2D _MainTex;
    18. sampler2D _BumpMap;
    19. float4 _Color;
    20.  
    21. struct Input {
    22.     float2 uv_MainTex;
    23.     float2 uv_BumpMap;
    24. };
    25.  
    26. void surf (Input IN, inout SurfaceOutput o) {
    27.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    28.     o.Albedo = c.rgb;
    29.     o.Alpha = c.a;
    30.     o.Normal = -UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    31. }
    32. ENDCG
    33. }
    34.  
    35. FallBack "Transparent/Cutout/Diffuse"
    36. }
    Then, on your cloth, make the Materials array have a length of 2 instead of 1. Use the built-in shader for the first one, and the fancy new backwards shader for the second one.

    Note that all I did was add Cull Front before the CGPROGRAM block, and a minus sign in front of UnpackNormal().


  7. Location
    In the line up, catching the big sets.
    Posts
    5,240
    Thank you so much Daniel, this is working absolutely perfectly!


  8. Posts
    166
    A simple CULL OFF would disable backface culling the simple way. Daniels solution however has the charme of correctly aligned normals. But wouldn't it be faster to do all the stuff in a second pass as only one draw call would be required?


  9. Location
    GMA950
    Posts
    2,439
    Quote Originally Posted by BIG BUG View Post
    A simple CULL OFF would disable backface culling the simple way. Daniels solution however has the charme of correctly aligned normals.
    Cull Off produces reversed lighting. The second material is the only way to use the normals correctly, although both approaches break shadows in forward rendering.
    But wouldn't it be faster to do all the stuff in a second pass as only one draw call would be required?
    What do you mean by "all the stuff"?


  10. Location
    In the line up, catching the big sets.
    Posts
    5,240
    Actually, I'm quite happy with Daniel's solution. Works great and I haven't seen any appreciable performance lost (although I haven't really done any super accurate performance tests).

  11. SQR SQR is offline

    Posts
    3
    Hi, I'm sorry to jump in like this, I've been in need of a shader like this for cloth precisely and this sounds great, but I have a question, I'm really no programmer so could someone please explain the part where Daniel, says
    Then, on your cloth, make the Materials array have a length of 2 instead of 1. Use the built-in shader for the first one, and the fancy new backwards shader for the second one.
    I would really appreciate it. Thx!

    Ok nevermind that, I figured it out. Still thx for the shader!
    Last edited by SQR; 10-01-2010 at 08:26 AM. Reason: found solution by myself


  12. Location
    United Kingdom
    Posts
    74
    Daniel,

    Thanks so much for your shader solution and help on the forums. It's a shame that the Unity team hasn't posted an AAA tutorial about the Interactive Cloth features and possibilities, but through the community I have been able to figure out most of the details to mae it work. Anyway, your shader works perfectly and looks good. I hope to be able to upload some screenshots of the prototypes I'm working on soon. Thanks again for the help and the shader code.

    Cheers!
    -arbbot
    Anthony Baker [arbbot]
    Solutions Architect / Game Director & Developer
    Blog | Profile


  13. Posts
    3
    I have a problem with above shader. It's working great on meshes, but on Cloth objects it's just flipping normals and front side become invisible.( Any way to fix this?


  14. Location
    GMA950
    Posts
    2,439
    Did you follow the instructions given with the shader?


  15. Posts
    20
    Ok guys,

    Thanks a lot for this very usefull Thread.
    Starting from this, I went a little bit further with a case study (a waving flag shader).

    Full documentation here :
    http://klakos.com/en/advanced-waving...dow-support-3/

    It covers the following points :

    * * Flag vertices animation via a shader (trigo. way)
    * * Advanced ShaderLab surface shading for transparency management
    * * Adding a shadow Pass (with alpha support)
    * * Double-sided surfaces methods (methods 1, 2 and 3)
    * * Provided Double-sided Materials Configuration Wizard (for 3rd method)


    >>> Downloads [from the address bellow]

    * - PDF version of the article : WavingFlag.pdf
    * - Configuration Wizard : BackwardMaterialBuilder.cs
    * - Unity 3.3 Package : WavingFlagDemo.unitypackage | WavingFlagDemo.rar

    Here is my shader code :

    The "FlagWaveCG.cginc" file :
    Code:  
    1. // Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
    2.  
    3. // Original shader by cboe - Mar, 23, 2009
    4. // Enhanced to 3 axis movement by Seon - Jan, 21, 2010
    5. // Added _WaveSpeed by Eric5h5 - Jan, 26, 2010
    6. // CHANGE LOG - Gauthier BOAGLIO (golgauth) / Klakos - May, 07, 2011 :
    7. //      - Added Transparency support
    8. //      - Added Spec and Normal mapping support
    9. //      - Added Shadow casting support (+ Shadow Alpha and Shadow Alpha cutoff support)
    10. //              [Done in the "ShadowCaster" additional Pass]
    11. //      - Added advanced double-sided rendering support
    12. //      - Added _WaveStrength param
    13. //
    14. // Requirements: assumes you are using a subdivided plane created with X (width) * Z (height) where Y is flat.
    15. // Requirements: assumes UV as: left X (U0) is attatched to pole, and Top Z (V1) is at top of pole.  
    16. //
    17. // [url]http://klakos.com/en/advanced-waving-flag-shader-for-unity-double-sided-alpha-shadow-support-3/[/url] ] for
    18. // visuals and more informations
    19.  
    20.  
    21. #include "UnityCG.cginc"
    22.  
    23. float4 _Color;
    24. sampler2D _MainTex;
    25. fixed _Cutoff;
    26. float _WaveSpeed;
    27. float _WaveStrength;
    28.  
    29.  
    30. struct v2f {
    31.     V2F_SHADOW_CASTER;
    32.     float2 uv : TEXCOORD1;
    33. };
    34.  
    35.  
    36. void computeWave (inout appdata_full v, inout v2f o)
    37. {
    38.     float sinOff=(v.vertex.x+v.vertex.y+v.vertex.z) * _WaveStrength;
    39.     float t=-_Time*_WaveSpeed;
    40.     float fx=v.texcoord.x;
    41.     float fy=v.texcoord.x*v.texcoord.y;
    42.  
    43.     v.vertex.x+=sin(t*1.45+sinOff)*fx*0.5;
    44.     v.vertex.y=(sin(t*3.12+sinOff)*fx*0.5-fy*0.9);
    45.     v.vertex.z-=(sin(t*2.2+sinOff)*fx*0.2);
    46.     o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
    47.     o.uv = v.texcoord;
    48. }


    The "FlagWave-Advanced.shader" file :
    Code:  
    1. // Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
    2.  
    3. // Original shader by cboe - Mar, 23, 2009
    4. // Enhanced to 3 axis movement by Seon - Jan, 21, 2010
    5. // Added _WaveSpeed by Eric5h5 - Jan, 26, 2010
    6. // CHANGE LOG - Gauthier BOAGLIO (golgauth) / Klakos - May, 07, 2011 :
    7. //      - Added Transparency support
    8. //      - Added Spec and Normal mapping support
    9. //      - Added Shadow casting support (+ Shadow Alpha and Shadow Alpha cutoff support)
    10. //              [Done in the "ShadowCaster" additional Pass]
    11. //      - Added advanced double-sided rendering support
    12. //      - Added _WaveStrength param
    13. //
    14. // Requirements: assumes you are using a subdivided plane created with X (width) * Z (height) where Y is flat.
    15. // Requirements: assumes UV as: left X (U0) is attatched to pole, and Top Z (V1) is at top of pole.  
    16. //
    17. // See [ http://klakos.com/en/advanced-waving-flag-shader-for-unity-double-sided-alpha-shadow-support-3/ ] for
    18. // visuals and more informations
    19.  
    20.  
    21. Shader "Selfmade/for-2sided/FlagWave Advanced Regular"
    22. {
    23.  
    24. Properties
    25. {
    26.     // Ususal stuffs
    27.     _Color ("Main Color", Color) = (1,1,1,1)
    28.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
    29.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    30.     _MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
    31.  
    32.     // Bump stuffs
    33.     //_Parallax ("Height", Range (0.005, 0.08)) = 0.02
    34.     _BumpMap ("Normalmap", 2D) = "bump" {}
    35.     //_ParallaxMap ("Heightmap (A)", 2D) = "black" {}
    36.    
    37.     // Shadow Stuff
    38.     _Cutoff ("Shadow Alpha cutoff", Range(0.25,0.9)) = 1.0
    39.  
    40.     // Flag Stuffs
    41.     _WaveSpeed ("Wave Speed", Range(0.0, 300.0)) = 50.0
    42.     _WaveStrength ("Wave Strength", Range(0.0, 5.0)) = 1.0
    43. }
    44.  
    45.  
    46. SubShader
    47. {
    48.     Tags {
    49.     "Queue"="Geometry"
    50.     "IgnoreProjector"="True"
    51.     "RenderType"="Transparent"}
    52.  
    53.     LOD 300
    54.  
    55.  
    56.  
    57.     Pass
    58.     {
    59.             Name "ShadowCaster"
    60.             Tags { "LightMode" = "ShadowCaster" }
    61.            
    62.             Fog {Mode Off}
    63.             ZWrite On ZTest Less Cull Off
    64.             Offset 1, 1
    65.  
    66.             CGPROGRAM
    67. // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
    68. #pragma exclude_renderers gles
    69.             #pragma vertex vert
    70.             #pragma fragment frag
    71.             #pragma fragmentoption ARB_precision_hint_fastest
    72.             #pragma multi_compile_shadowcaster
    73.             #include "FlagWaveCG.cginc"
    74.            
    75.            
    76.             v2f vert( appdata_full v )
    77.             {
    78.                 v2f o;
    79.                 computeWave(v, o);
    80.                 TRANSFER_SHADOW_CASTER(o)
    81.    
    82.               return o;
    83.             }
    84.            
    85.             //sampler2D _MainTex;
    86.                    
    87.             float4 frag( v2f i ) : COLOR
    88.             {
    89.                 fixed4 texcol = tex2D( _MainTex, i.uv );
    90.                 clip( texcol.a - _Cutoff );
    91.                 SHADOW_CASTER_FRAGMENT(i)
    92.             }
    93.             ENDCG
    94.  
    95.  
    96.       //SetTexture [_MainTex] {combine texture}
    97.     }
    98.  
    99. //CULL Front
    100.  
    101. CGPROGRAM
    102.         #pragma surface surf BlinnPhong alpha vertex:vert fullforwardshadows approxview
    103.         #include "FlagWaveCG.cginc"
    104.  
    105.  
    106.         half _Shininess;
    107.  
    108.         sampler2D _BumpMap;
    109.         //sampler2D _ParallaxMap;
    110.         float _Parallax;
    111.  
    112.         struct Input {
    113.             float2 uv_MainTex;
    114.             float2 uv_BumpMap;
    115.             //float3 viewDir;
    116.         };
    117.  
    118.         v2f vert (inout appdata_full v) {
    119.             v2f o;
    120.             computeWave(v, o);
    121.             return o;
    122.         }
    123.  
    124.         void surf (Input IN, inout SurfaceOutput o) {
    125.             // Comment the next 4 following lines to get a standard bumped rendering
    126.             // [Without Parallax usage, which can cause strange result on the back side of the plane]
    127.             /*half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
    128.             float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
    129.             IN.uv_MainTex += offset;
    130.             IN.uv_BumpMap += offset;*/
    131.  
    132.             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    133.             o.Albedo = tex.rgb * _Color.rgb;
    134.             o.Gloss = tex.a;
    135.             o.Alpha = tex.a * _Color.a;
    136.             //clip(o.Alpha - _Cutoff);
    137.             o.Specular = _Shininess;
    138.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    139.         }
    140. ENDCG
    141. }
    142.    
    143. Fallback "Transparent/VertexLit"
    144. }


    Hope this will help...
    Last edited by golgauth; 05-14-2011 at 04:08 AM.


  16. Posts
    50
    Hi !

    I try your SHADER "Selfmade/TransparentShadowCaster", but it dosen't work

    Can you help me to see the shadows on my plane with an alpha texture ?

    Many thanks for yout help !
    Last edited by Michael BRICOUT; 10-10-2011 at 11:29 AM.
    Kealkeal


  17. Posts
    20
    Hello,

    I made some tests and it appears that it works with Point and Spot Lights, but no satisfying result with a "Directional Light".
    Does anyone has an idea about this issue ?
    Thanks

    NB : Here is what I get with a "Spot Light" dedicated to the "alpha plane" layer >>

    Last edited by golgauth; 09-19-2011 at 04:46 PM. Reason: Added visual...


  18. Posts
    20
    A workaround to all of this could be :

    Apply 2 materials to the Plane >>

    1. One as described above (TransparentShadowCaster)
    2. A second one to obtain the shadow working with "Directional Light" (using "Transparent/Cutout/Diffuse" built-in shader, which can handle shadows receiving)

    Illustration [Not very satisfying because of the aliasing at the shadow border] :



    Obtained with the following settings :



    But well, I don't have any better simple solution right now... And what about you, guys ?
    Last edited by golgauth; 09-21-2011 at 01:56 AM.


  19. Location
    Germany
    Posts
    333
    Try adding addshadow to the pragma line.
    Chickens Shader Bundle on the Asset Store and on Unitymagic

    Follow me on twitter


  20. Posts
    20
    Already tested with no result
    Thanks