Search Unity

What shaders would you guys want?

Discussion in 'Shaders' started by tatoforever, Aug 28, 2012.

  1. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Farfarer thanks for shader but it gets errors in compilation.
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Well, what errors?
     
  3. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Hm i get error at 168 line (which actually even doesnt exist in code provided?) about some bolean, sorry i cant open Unity at the momment to get you specific details.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Doh, I misspelled _Fresnel as _Frensel. Changed the code in the original post, works fine now.

    There were no errors about booleans, so that's something in your own code :p
     
  5. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    I still get this:
    Shader error in 'Custom/Bumped Diffuse Fresnel': Program 'SurfShaderInternalFunc', scalar Boolean expression expected at line 168

    and this:

    Shader warning in 'Custom/Bumped Diffuse Fresnel': Program 'SurfShaderInternalFunc', implicit cast from "half2" to "half" at line 168

    I dont get it....there arent even 168 lines of code:=?
     
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Ahh, one of the recent Unity versions expects s.Specular to only have one component and breaks if you add more (like I've done). That was then fixed in the latest release.

    I've updated the shader code to work around that, so it'll be fine in whichever version of Unity you use.



    Oh and the errors from non-existing lines are reported from the compiled versions of the shader (which are far bigger). Makes finding errors a bit of a pain though :p
     
  7. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Hey thanks man that worked well now. One question, am i allowed to use this shader in my dinosaur package for Asset store?
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Sure, do what you like with it :)
     
  9. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Farfarer, is it possible you could do a terrain shader (layered splatmaps) with bumpmaps for each texture, up to four textures, but for general UV Mapped meshes? Or would the terrain shaders work fine :S?
     
  10. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Uhh, this should do it, quick hack of the terrain shader...

    Code (csharp):
    1.  
    2. Shader "Custom/Custom SplatMap" {
    3.     Properties {
    4.         _Control ("Control (RGBA)", 2D) = "red" {}
    5.         _Splat3 ("Layer 3 (A)", 2D) = "white" {}
    6.         _Splat2 ("Layer 2 (B)", 2D) = "white" {}
    7.         _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    8.         _Splat0 ("Layer 0 (R)", 2D) = "white" {}
    9.         _Bump3 ("Bump 3 (A)", 2D) = "bump" {}
    10.         _Bump2 ("Bump 2 (B)", 2D) = "bump" {}
    11.         _Bump1 ("Bump 1 (G)", 2D) = "bump" {}
    12.         _Bump0 ("Bump 0 (R)", 2D) = "bump" {}
    13.         // used in fallback on old cards
    14.         _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    15.         _Color ("Main Color", Color) = (1,1,1,1)
    16.     }
    17.    
    18.     SubShader {
    19.         Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }
    20.         CGPROGRAM
    21.             #pragma surface surf Lambert
    22.             #pragma target 3.0
    23.  
    24.             struct Input {
    25.                 float2 uv_Control : TEXCOORD0;
    26.                 float2 uv_Splat0 : TEXCOORD1;
    27.                 float2 uv_Splat1 : TEXCOORD2;
    28.                 float2 uv_Splat2 : TEXCOORD3;
    29.                 float2 uv_Splat3 : TEXCOORD4;
    30.             };
    31.  
    32.             sampler2D _Control;
    33.             sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    34.             sampler2D _Bump0,_Bump1,_Bump2,_Bump3;
    35.             fixed4 _Color;
    36.  
    37.             void surf (Input IN, inout SurfaceOutput o) {
    38.                 fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    39.                 fixed3 col;
    40.                 col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    41.                 col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
    42.                 col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
    43.                 col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
    44.                 col *= _Color.rgb;
    45.                 fixed3 normal;
    46.                 normal  = splat_control.r * UnpackNormal(tex2D (_Bump0, IN.uv_Splat0));
    47.                 normal += splat_control.g * UnpackNormal(tex2D (_Bump1, IN.uv_Splat1));
    48.                 normal += splat_control.b * UnpackNormal(tex2D (_Bump2, IN.uv_Splat2));
    49.                 normal += splat_control.a * UnpackNormal(tex2D (_Bump3, IN.uv_Splat3));
    50.                 o.Albedo = col;
    51.                 o.Normal = normalize ( normal );
    52.                 o.Alpha = 0.0;
    53.             }
    54.         ENDCG  
    55.     }
    56.  
    57.     // Fallback to Diffuse
    58.     Fallback "Diffuse"
    59. }
    60.  
     
    Last edited: Sep 19, 2012
  11. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Fantastic, I'll test that later. Didn't know it'd be so quick!
     
  12. IndieScapeGames

    IndieScapeGames

    Joined:
    Jul 3, 2012
    Posts:
    80
    I need a shader that draws an outline of the character, similar to the toon shader, but something that is always on-top of the z-buffer. For instance, if the player that has this shader attached goes behind a barrier, the outline of the player can still be seen through the barrier.
     
  13. AMDAndrew

    AMDAndrew

    Joined:
    Aug 10, 2012
    Posts:
    90
  14. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Farfarer, the shader is throwing errors.

    No idea what it means.

    EDIT: Oh dear, loads more.

    That's quite a few.
     
    Last edited: Sep 18, 2012
  15. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Ah, yeah, max of 8 textures on shader model 2. I've edited the original post to make it shader model 3. Or if you want, just add the line...
    #pragma target 3.0
    ...to the file after the other line that starts with #pragma.

    Sorry, couldn't test it before posting it :p
     
  16. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Awesome, works nicely now. One last thing- the main colour property isn't working, so my mesh looks impossibly bright. Would you mind fixing that? Then it will be perfect. I'm going to use this shader alot, if that's okay, so if I release this you'll need some credit too.
     
  17. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    _Color has been added in to the original post.
     
  18. mrpangahas

    mrpangahas

    Joined:
    Nov 17, 2011
    Posts:
    11
    Request for an outline shader ala Borderlands but also overlays a crosshatch texture over your diffuse on the shadow areas of objects and the characters.Is it even possible to do something like that.
     
  19. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Eep, sorry, one more thing- it doesn't look like the mesh is being lit properly anymore. Not sure how to describe it. Might just be my eyes :S.
     
  20. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Pictures would help :p
     
  21. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
  22. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Why you want such effect in a shader? I mean, it would be better in a particle system. You'll have more control and features on it. :rolleyes:

    Note: I'm kind of trying to get some free time to work on your petitions, let's see if i got some next week.
    For those who can contribute with petitions feel free to provide shaders/solutions. :)
     
  23. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    I think wrote something like that for a friend, I'll dig it ou if I get a chance...
     
    Last edited: Sep 20, 2012
  24. IndieScapeGames

    IndieScapeGames

    Joined:
    Jul 3, 2012
    Posts:
    80
    Sounds good, thanks Farfarer!
     
  25. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I'm using larsbertram's awesome ats colormap shader for terrain. It uses a large colormap as a distant terrain overlay rather than using the detail textures, and blends the detail textures to color match the colors of the colormap with 4 texcol variables. I'd really like it to just make the detail maps blend with the color on the colormap at that particular coordinate on the terrain, rather than be limited to 4 solid colors, because my colormap has a lot of color blending. Any ideas?

    Here's the code:

    Code (csharp):
    1.  
    2.  
    3. /* Code provided by Chris Morris of Six Times Nothing (http://www.sixtimesnothing.com) */
    4. /* Free to use and modify  */
    5.  
    6.  
    7. Shader "Hidden/TerrainEngine/Splatmap/Lightmap-FirstPass" {
    8. Properties {
    9.     _Control ("Control (RGBA)", 2D) = "red" {}
    10.     _Splat3 ("Layer 3 (A)", 2D) = "white" {}
    11.     _Splat2 ("Layer 2 (B)", 2D) = "white" {}
    12.     _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    13.     _Splat0 ("Layer 0 (R)", 2D) = "white" {}
    14.     // used in fallback on old cards
    15.     _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    16.     _Color ("Main Color", Color) = (1,1,1,1)
    17.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    18. }
    19.  
    20. SubShader {
    21.     Tags {
    22.         "SplatCount" = "4"
    23.         "Queue" = "Geometry-100"
    24.         "RenderType" = "Opaque"
    25.     }
    26. CGPROGRAM
    27. #pragma surface surf BlinnPhong vertex:vert
    28. #pragma target 3.0
    29. #include "UnityCG.cginc"
    30.  
    31. struct Input {
    32.     float3 worldPos;
    33.     float2 uv_Control : TEXCOORD0;
    34.     float2 uv_Splat0 : TEXCOORD1;
    35.     float2 uv_Splat1 : TEXCOORD2;
    36.     float2 uv_Splat2 : TEXCOORD3;
    37.     float2 uv_Splat3 : TEXCOORD4;
    38.    
    39.     ///
    40.    
    41.     float distance;
    42.    
    43.    
    44. };
    45.  
    46. void vert (inout appdata_full v, out Input o) {
    47.     // Supply the shader with tangents for the terrain
    48.     // A general tangent estimation
    49.     float3 T1 = float3(1, 0, 1);
    50.     float3 Bi = cross(T1, v.normal);
    51.     float3 newTangent = cross(v.normal, Bi);
    52.     normalize(newTangent);
    53.     v.tangent.xyz = newTangent.xyz;
    54.     if (dot(cross(v.normal,newTangent),Bi) < 0)
    55.         v.tangent.w = -1.0f;
    56.     else
    57.         v.tangent.w = 1.0f;
    58.    
    59.     float distanceVar = distance(_WorldSpaceCameraPos, mul(_Object2World, v.vertex));  
    60.     o.distance = distanceVar;
    61.    
    62. }
    63. sampler2D _Control;
    64. sampler2D _CustomColorMap, _TerrainNormalMap;
    65. sampler2D _BumpMap0, _BumpMap1, _BumpMap2, _BumpMap3;
    66. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    67. float _Spec0, _Spec1, _Spec2, _Spec3;
    68. float3 _ColTex0, _ColTex1, _ColTex2, _ColTex3;
    69. float4 _v4CameraPos;
    70. float _SplattingDistance;
    71.  
    72. void surf (Input IN, inout SurfaceOutput o) {
    73.    
    74.     half3 col;
    75.     half3 finalCol;
    76.    
    77.     // do all texture fetches outside the if
    78.     half3 colorMap = tex2D(_CustomColorMap, IN.uv_Control);
    79.     half4 splat_control = tex2D(_Control, IN.uv_Control);
    80.     half3 splatcol0 = tex2D (_Splat0, IN.uv_Splat0).rgb;
    81.     half3 splatcol1 = tex2D (_Splat1, IN.uv_Splat1).rgb;
    82.     half3 splatcol2 = tex2D (_Splat2, IN.uv_Splat2).rgb;
    83.     half3 splatcol2_2nd = tex2D (_Splat2, IN.uv_Splat2* -.5).rgb;
    84.     half3 splatcol3 = tex2D (_Splat3, IN.uv_Splat3).rgb;
    85.     float3 normalsplat0 = UnpackNormal(tex2D(_BumpMap0, IN.uv_Splat0));
    86.     float3 normalsplat1 = UnpackNormal(tex2D(_BumpMap1, IN.uv_Splat1));
    87.     float3 normalsplat2 = UnpackNormal(tex2D(_BumpMap2, IN.uv_Splat2));
    88.     float3 normalsplat2_2nd = UnpackNormal(tex2D(_BumpMap2, IN.uv_Splat2* -.5));
    89.     float3 normalsplat3 = UnpackNormal(tex2D(_BumpMap3, IN.uv_Splat3));
    90.     float3 farnormal = UnpackNormal(tex2D(_TerrainNormalMap, IN.uv_Control));
    91.    
    92.     if (IN.distance < _SplattingDistance)
    93.     {
    94.        
    95.         //// 4 splats, normals, and specular settings
    96.         // use the built in splat map --> make sure you have copied your custom made to the built in via script
    97.        
    98.         //// first detail texture: red = ground
    99.         col = splat_control.r * splatcol0;
    100.         o.Normal = splat_control.r * normalsplat0;
    101.  
    102.         //// second detail texture: green = grass
    103.         col += splat_control.g * splatcol1;
    104.         o.Normal += splat_control.g * normalsplat1;
    105.    
    106.         //// third detail texture rock: blue = rock
    107.         // uses uv mixing
    108.         col += splat_control.b * splatcol2;
    109.         o.Normal += splat_control.b * normalsplat2;
    110.        
    111.         //// forth detail texture: alpha
    112.         col += splat_control.a * splatcol3;
    113.         o.Normal += splat_control.a * normalsplat3;
    114.        
    115.         /// final composition
    116.        
    117.         // fade out detail normal maps
    118.         float fadeout = pow(IN.distance/_SplattingDistance, 4.0);
    119.         o.Normal = lerp(normalize(o.Normal + farnormal), farnormal, fadeout);
    120.        
    121.         // apply colorcorrection to detail maps
    122.         // see: http://blog.wolfire.com/2009/12/Detail-texture-color-matching
    123.         half3 color_correction = splat_control.r*_ColTex0 + splat_control.g*_ColTex1 + splat_control.b*_ColTex2 + splat_control.a*_ColTex3;
    124.         finalCol = col * (colorMap/color_correction);
    125.        
    126.         // fade out detail maps
    127.         finalCol = lerp(finalCol, colorMap, fadeout);
    128.     }
    129.     else {
    130.         finalCol = colorMap;
    131.         o.Normal = farnormal;
    132.     }
    133.        
    134.     o.Albedo = finalCol;
    135.     o.Alpha = 0.0;
    136. }
    137. ENDCG  
    138. }
    139.  
    140. // Fallback to Diffuse
    141. Fallback "Diffuse"
    142. }
    143.  
    144.  
    It's specifically the lines under line 120, the "apply color correction to detail maps" comment. Is there any way I can pull the color at that coordinate from the colormap instead?
     
    Last edited: Sep 21, 2012
  26. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I figured out that if I just remove line 121, and make the next line just:

    finalCol = col*colorMap;

    it pretty much does what I want, but the result is significantly darker than I'd like. I think its just because it's multiplying the detail textures' color by the colormap, so the resulting color is much darker than the distant colormap, depending on how different the detail map's color is than that color on the colormap. Is there anything I can do to brighten it up, besides using desaturated, semi-transparent or overbrightened detail maps?

    You can see what I'm talking about here:

     
  27. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    hi prime,

    have you had a look at the wolfire blog? http://blog.wolfire.com/2009/12/Detail-texture-color-matching
    there you will find some in depth explanation about what happens here and why it is the best way to blend detail maps with the color map.
    another way would be to use only low color detail maps (shown as "greyscale method " in the blog post) – like e.g. cryengine does but that would not allow such colorfull details like the method implemented.

    so: no, i think there is no "better" way to do the blending.

    lars
     
  28. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I did read the article, I think you're misunderstanding what I'm saying. I want my detail textures to blend with the colormap. As far as I can tell, your shader as it exists does not blend with the colormap, it blends the detail textures with the four texcol colors that you select in the editor, which are colors you would likely pull from your colormap... but it relies on your detail maps lining up perfectly with those colors on the colormap. For example, my grassy areas are blended between two colors on my colormap, but I only want one grass texture on those two colors. I couldn't have grass textures blended between two different colors using the texcols without wasting two detail texture slots.

    You can see in the screenshot above that I'm halfway there, those actually are greyscale detail maps with the color correction line removed from the shader, and line 122 made just:

    Code (csharp):
    1. finalCol = col*colorMap;
    So those are greyscale textures picking up color from the color map alone, but they're very dark, that's my only problem.
     
    Last edited: Sep 21, 2012
  29. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    sorry, but that is wrong: the shader first precomputes the detail maps:
    Code (csharp):
    1. line 121: half3 color_correction = splat_control.r*_ColTex0 + splat_control.g*_ColTex1 + splat_control.b*_ColTex2 + splat_control.a*_ColTex3;
    then it blends the result with the colormap:
    Code (csharp):
    1. line 122: finalCol = col * (colorMap/color_correction);
    again: i am sorry but i really do not understand what your problem is:
    the 4 text colors are taken from the detail maps – not from the colormap: they define the average color value of each detail map and are used to generate an intermediate tint map.
    you can then combine this intermediate color with any color map you want and will always get correct shading.
    in my demo terrain the green of the grass detail texture almost fits the green used in the colormap so you might think that there isn’t much happening.
    but that’s not the case.

    just a quick proof with a completely weird colormap (detail textures and tex colors are exactely the same like in the demo. i have just painted some red and blue and green on top of the color map):
    $Bildschirmfoto 2012-09-21 um 15.22.28.png

    and another proof:
    $Bildschirmfoto 2012-09-21 um 15.12.01.png

    as you will note: using completely different color in the colormap and on the detail textures will automatically more or less detint the detail maps. so it is a good idea to use more or less neighbouring colors on both textures.
    i hope this will clarify the things going on in the shader.

    lars
     
    Last edited: Sep 21, 2012
  30. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    Moved to Shader Lab
     
  31. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    PrimeDetective i think that what you are looking for are grayscaled detail maps like those used in Cryengine by default. In that case all color information is passed from main color map, i think that forum user "cod" has done shader with that approach. Not sure if i understand what you mean but probably thats what you are looking for.
     
  32. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Ohh my God, I just read wolfire more carefully and I just figured that out! LOL

    texcol 1-4 is pulled from the textures, not the colormap! Sorry for doubting you lars!
     
  33. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    puhhhhh, i am glad that to read this.
    i was really about getting crazy not understanding your problem!


    lars
     
  34. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Soz for the delay Farfarer. Here's a picture explaining what's happening.



    Thanks.
     
  35. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Hi, sorry for the late answer !
    I'm already using a particle system, and for now, I want to add this worldspace 3D noise on the alpha channel on each particle. I need this to be in world space and in 3D to break the flat looking and rolling effect of billboards while turning the camera around.

    Also, I'm looking for a way to know the current vertex index in a shader, any clue on how to do that ?
     
  36. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Alesk,
    I'm not sure if Unity by default feeds vertex index as semantics in the shaders but you can always pass the vertex index with a script onto your shader (as a uniform).
    The effect you are looking for can be done. Can you post your particle shader you want to have the noise?
    Cheers,
     
    Last edited: Oct 9, 2012
  37. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    I'm curious to see how to do that... In fact I'm looking for a way to move the UV generation from my unity script to the shader, and I need the vertex index for that...

    Here it is.
    I'm a total noob on shaders, so I've tweaked an existing one. I have 2 uv coodinates for each vertex one for the MainTex wich and another for the _DetailTex wich is my current 2d noise map used as alpha only, this is this one I want to be replaced by a 3D noise in worldspace

    Note : I'm using the tangent to pass extra data, here it's the age of the particle.
    If you have any better solution for that, I'm interested.

    Code (csharp):
    1.  
    2. Shader "Smoke/Volumetric" {
    3.  
    4. Properties {
    5.     _TintColor ("Tint Color", Color) = (0.5,0.5,0.0,0.5)
    6.     _ShadowColor ("Shadow Color", Color) = (0.0,0.5,0.5,0.5)
    7.     _MainTex ("Particle Texture", 2D) = "white" {}
    8.     _DetailTex ("Particle Details Texture", 2D) = "white" {}
    9.     _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    10. }
    11.  
    12. Category {
    13.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    14.     Blend SrcAlpha OneMinusSrcAlpha
    15.     AlphaTest Greater .01
    16.     ColorMask RGB
    17.     Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
    18.    
    19.     BindChannels {
    20.         Bind "Color", color
    21.         Bind "Vertex", vertex
    22.         Bind "texcoord", texcoord0
    23.         Bind "texcoord1", texcoord1
    24.     }
    25.    
    26.     // ---- Fragment program cards
    27.     SubShader {
    28.         Pass {
    29.        
    30.             CGPROGRAM
    31. // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct appdata_t members worldPos)
    32. #pragma exclude_renderers xbox360
    33.             #pragma vertex vert
    34.             #pragma fragment frag
    35.             #pragma fragmentoption ARB_precision_hint_fastest
    36.             #pragma multi_compile_particles
    37.             #include "UnityCG.cginc"
    38.  
    39.             sampler2D _MainTex;
    40.             sampler2D _DetailTex;
    41.             fixed4 _TintColor;
    42.             fixed4 _ShadowColor;
    43.            
    44.             struct appdata_t {
    45.                 float4 vertex : POSITION;
    46.                 fixed4 color : COLOR;
    47.                 float2 texcoord : TEXCOORD0;
    48.                 float2 texcoord1 : TEXCOORD1;
    49.                 float4 tangent : TANGENT;
    50.             };
    51.  
    52.             struct v2f {
    53.                 float4 vertex : POSITION;
    54.                 fixed4 color : COLOR;
    55.                 float2 texcoord : TEXCOORD0;
    56.                 float2 texcoord1 : TEXCOORD1;
    57.                 float4 extra : TEXCOORD3;
    58.                 #ifdef SOFTPARTICLES_ON
    59.                 float4 projPos : TEXCOORD2;
    60.                 #endif
    61.             };
    62.            
    63.             float4 _MainTex_ST;
    64.             float4 _DetailTex_ST;
    65.            
    66.             v2f vert (appdata_t v)
    67.             {
    68.                 //float3 worldPos = mul(_Object2World, v.vertex).xyz;
    69.                 v2f o;
    70.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    71.                 #ifdef SOFTPARTICLES_ON
    72.                 o.projPos = ComputeScreenPos (o.vertex);
    73.                 COMPUTE_EYEDEPTH(o.projPos.z);
    74.                 #endif
    75.                 o.color = v.color;
    76.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    77.                 o.texcoord1 = TRANSFORM_TEX(v.texcoord1,_DetailTex);
    78.                 o.extra = v.tangent;
    79.                 return o;
    80.             }
    81.  
    82.             sampler2D _CameraDepthTexture;
    83.             float _InvFade;
    84.            
    85.             fixed4 frag (v2f i) : COLOR
    86.             {
    87.                 float opacity = i.color.a;
    88.                
    89.                 #ifdef SOFTPARTICLES_ON
    90.                 float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
    91.                 float partZ = i.projPos.z;
    92.                 float fade = saturate (_InvFade * (sceneZ-partZ));
    93.                 opacity *= fade;
    94.                 #endif
    95.                
    96.                 float _young = 1f-i.extra.x;
    97.                 float _old = i.extra.x;
    98.  
    99.                 // get main texture color and alpha
    100.                 float4 _sampled = tex2D(_MainTex, i.texcoord);
    101.  
    102.                 // mix light color and shadow color
    103.                 float4 _finalcolor = lerp(_ShadowColor,_TintColor,_sampled.r);
    104.                
    105.                 // more light color while spreading
    106.                 _finalcolor = _TintColor * _old + _finalcolor * _young;
    107.                
    108.                 // alpha from details textures
    109.                 float _details = 1f;
    110.                                
    111.                 _details = _young * tex2D(_DetailTex, i.texcoord1).r;
    112.                
    113.                 // main alpha
    114.                 _finalcolor.a = i.color.a *_sampled.a * _details * opacity;
    115.                
    116.                 return _finalcolor * 2f;
    117.             }
    118.            
    119.            
    120.             ENDCG
    121.         }  
    122.     }  
    123.    
    124.     // ---- Dual texture cards
    125.     SubShader {
    126.         Pass {
    127.             SetTexture [_MainTex] {
    128.                 constantColor [_TintColor]
    129.                 Combine texture * constant DOUBLE, texture * primary
    130.             }
    131.             SetTexture [_DetailTex] {
    132.                 Combine previous,previous * texture
    133.             }
    134.         }
    135.     }
    136.    
    137.     // ---- Single texture cards (does not do color tint)
    138.     SubShader {
    139.         Pass {
    140.             SetTexture [_MainTex] {
    141.                 combine texture * primary
    142.             }
    143.         }
    144.     }
    145.  
    146. }
    147.  
    148. }
    149.  
    150.  
     
    Last edited: Oct 9, 2012
  38. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Look at Material functions (SetColor, SetFloat, SetTexture, etc).
    http://docs.unity3d.com/Documentation/ScriptReference/Material.html

    But I'm still not sure why you need the vertex index? For what?
    Anyway, you can get the vertex index using scripts, compute some-non heavy calculations there and pass the rest to your shader.
    I'm going to take a look at your shader later.
     
  39. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    This will only send a unique value shared by all vertices :/

    I need it to compute the new uv for each vertex of my billboards.
    Since they are all made of quads, with the same vertex ordering, I would know wich vertex I'm dealing with to compute the UV coordinates accordingly directly into the shader.
    Anyway, since then I've splitted the computing on multiple threads, so it's not so heavy now.

    Thanks ^^
     
  40. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    If you want to manipulate UVs, you can do it directly on the shader. What kind of UVs manipulation you want to do (i still don't get why you need the vertex index)? :rolleyes:
     
  41. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Each particle is a billboard made of 4 vertices and 2 triangles
    so, for each vertex I need to know its position on the billboard (top left, top right, bottom right, bottom left) to generate the right UV coordinates... and to know this I need the vertex index, with a modulo 4 on the index, I can know the vertex position on the quad since they are all ordered in the same way for all billboards.

    So here I want to generate UV1 and UV2 coordinates in the shader for each billboard.
     
  42. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    You need 4 floats in your shader that will represent each vertex UV coordinates and then use SetFloat from a script to feed the UVs. You can you get the vertex index/positions easily from a script but again, i don't think you have to do it that way. You can get the vertex position directly in the shader and do your UVs manipulation there (instead of having one script per billboard plane).
    Now the question is, do you know the kind of UV manipulation you want to do?
    Do you have all you need (input textures/values)?
     
    Last edited: Oct 10, 2012
  43. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Hi,

    I'm doing almost everything by script. I populate my mesh with the vertices positions, uvs1 and 2, colors, etc... Then I reuse these data in the shader.
    I don't have a script per billboard, I have a script that generate a single mesh with all billboards inside.
    For now, my script generates UV1 for each billboard, depending on the camera and light position, and UV2 are updated all along each particle life span.
    All is done in script and so I'm forced to send all the data to the gpu, wich takes time. I would like to try to move the computing as much as possible in the shader to prevent the sending of uvs data to the gpu.
    So by knowing the current vertex index while the shader is processing, I could deduct from that index wich corner of wich billboard it represents, and compute the UVs accordingly.
    If I have to send as much data as I have while computing in the script... then I don't think it's worth it.

    Right now, I have a working demo here : http://www.alesk.fr/demo/smoke
    All is done with script, except what's in the shader I've posted before.
     
    Last edited: Oct 11, 2012
  44. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    @Alesk,
    Sorry for my late reply, I'm quite busy these days. I just saw your web demo but seriously, you can already do that with a shuriken particle emitter. You can't for example generate all the triangles in a shader (unless you are doing some crazy stuff with 3D textures in DX11). Well yes, you can generate more triangles and offset their positions but it will require several passes (one pass per billboard plane) and of course that's not what you want. You are looking for performance right? You'll have more performance an great artistic control in a shuriken particle system (with only 1 draw call).
     
    Last edited: Oct 17, 2012
  45. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Hi,

    No problem, for the late reply, I'm quite busy too, so I understand very well ;)

    My demo has also already 1 draw call, and no you can't do the exact same shading effect with shuriken. Here I'm simulating a volume effect on the particle, and it reacts to the main light in the scene.
    If the light rotates, the shading off the particles will change accordingly.
    For this I need to set different UV coordinates for each particle, wich is not possible with shuriken, if I'm not mistaken.

    Anyway, I've planned to see if it's possible to plug my code on top of shuriken to get the best of both... Until then I just wanted to speed things up on my side and unload the cpu as much as possible.


    EDIT : here is a new demo where the smoke is static and create a cloud, you can move around like in the previous demo with the arrow keys+mouse or orbit the light with U and I keys, you'll see that the smoke is lit according to the light position. (I've added a lens flare to clearly see the light source)

    => http://www.alesk.fr/demo/smoke/static/

    (if you d'ont see the smoke at the beginning, just left click to get the focus on it)
     
    Last edited: Oct 18, 2012
  46. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Would anybody mind modifying the Reflective Bumped Specular shader to include detail maps, like the Diffuse- Detail shader? Thanks.
     
  47. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Here is, you'll find it under Reflective section named "Bumped Specular Detail"
    View attachment $Reflect-BumpSpecDetail.shader
     
  48. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Hi there tato, thanks for that. My problem is that the detail texture isn't tiling like in the standard diffuse_detail shader. Do you know why that could be?
     
  49. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Select your detail texture in the project view and change it's "Wrap Mode" to repeat.
     
  50. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Done, and no change. Same problem.
     
    Last edited: Oct 27, 2012