Search Unity

What shaders would you guys want?

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

  1. Rico21745

    Rico21745

    Joined:
    Apr 25, 2012
    Posts:
    409
    I think I saw something similar somewhere Dolkar, though I have no idea where, where they used a C# file and shader combination to do it. I'm still pretty new to shader programming, so I figured I'd throw it out there and see if anyone more experienced had any ideas.
     
  2. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Hi,

    I'm back with my smoke shader ^_^

    Now it looks like this :
    Code (csharp):
    1. Shader "Puffy_Smoke/FakedVolumetric" {
    2.  
    3. Properties {
    4.    
    5.     _ShadowColor ("Shadow Color", Color) = (0.0,0.5,0.5,1)
    6.    
    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.     _Opacity ("Opacity", Range(0.0,1)) = 0.5
    12.    
    13.     _Scattering ("Scattering", Range(0.0,1.0)) = 1
    14.    
    15.     _Density ("Density", Range(0.0,1.0)) = 0
    16.     _Sharpness ("Sharpness", Range(0.0,5.0)) = 0
    17.     _DetailsSpeed ("Details Speed", Range(0.01,1.0)) = 0.2
    18. }
    19.  
    20. Category {
    21.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    22.     Blend SrcAlpha OneMinusSrcAlpha
    23.     AlphaTest Greater .01
    24.     ColorMask RGB
    25.     Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
    26.    
    27.     BindChannels {
    28.         Bind "Color", color
    29.         Bind "Vertex", vertex
    30.         Bind "texcoord", texcoord0
    31.         Bind "texcoord1", texcoord1
    32.     }
    33.    
    34.     // ---- Fragment program cards
    35.     SubShader {
    36.         Pass {
    37.        
    38.             CGPROGRAM
    39.             // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
    40.             #pragma exclude_renderers gles
    41.             // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct appdata_t members worldPos)
    42.             #pragma exclude_renderers xbox360
    43.             #pragma vertex vert
    44.             #pragma fragment frag
    45.             #pragma fragmentoption ARB_precision_hint_fastest
    46.             #pragma multi_compile_particles
    47.             #include "UnityCG.cginc"
    48.  
    49.             sampler2D _MainTex;
    50.             sampler2D _DetailTex;
    51.             sampler2D _CameraDepthTexture;
    52.            
    53.             fixed4 _ShadowColor;
    54.             fixed4 _AmbientColor;
    55.             fixed4 _LightColor;
    56.             float4 _MainTex_ST;
    57.             float4 _DetailTex_ST;
    58.            
    59.             float _Density;
    60.             float _DetailsSpeed;
    61.             float _Sharpness;
    62.             float _Scattering;
    63.             float _Opacity;
    64.             float _details;
    65.             float _LightIntensity;
    66.             float _InvFade;
    67.            
    68.             struct appdata_t {
    69.                 float4 vertex : POSITION;
    70.                 fixed4 color : COLOR;
    71.                 float2 texcoord : TEXCOORD0;
    72.                 float2 texcoord1 : TEXCOORD1;
    73.             };
    74.  
    75.             struct v2f {
    76.                 float4 vertex : POSITION;
    77.                 fixed4 color : COLOR;
    78.                 float2 texcoord : TEXCOORD0;
    79.                 float2 texcoord1 : TEXCOORD1;
    80.                 #ifdef SOFTPARTICLES_ON
    81.                 float4 projPos : TEXCOORD2;
    82.                 #endif
    83.             };
    84.            
    85.            
    86.            
    87.             v2f vert (appdata_t v)
    88.             {
    89.                 //float3 worldPos = mul(_Object2World, v.vertex).xyz;
    90.                 v2f o;
    91.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    92.                 #ifdef SOFTPARTICLES_ON
    93.                 o.projPos = ComputeScreenPos (o.vertex);
    94.                 COMPUTE_EYEDEPTH(o.projPos.z);
    95.                 #endif
    96.                 o.color = v.color;
    97.                 o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    98.                 o.texcoord1 = TRANSFORM_TEX(v.texcoord1,_DetailTex);
    99.                 return o;
    100.             }
    101.  
    102.            
    103.             fixed4 frag (v2f i) : COLOR
    104.             {
    105.                    
    106.                 float _old = i.color.a; // particle age is stored in the color alpha channel
    107.                 float _young = 1f - _old;
    108.                
    109.                 // get main texture color and alpha
    110.                 float4 _sampled = tex2D(_MainTex, i.texcoord);
    111.  
    112.                 float4 _finalcolor = (i.color * _LightColor) * _LightIntensity;
    113.                
    114.                 // mix light color and shadow color
    115.                 _finalcolor = lerp(_ShadowColor , _finalcolor , _sampled.r) + _AmbientColor;
    116.                
    117.                 // older particles gets more light color
    118.                 if(_Scattering > 0) _finalcolor = lerp(_finalcolor , lerp(_finalcolor , _LightColor * i.color * _LightIntensity , _old), _Scattering);
    119.                                
    120.                 if(_Density < 1f){
    121.                     // animated noise
    122.                     _details = tex2D(_DetailTex, i.texcoord1 * (1f + _young * _DetailsSpeed)).r;
    123.                     _details = max(0f,min(1f , lerp((0.5f+_details) * 0.5f, _details , _old*_Sharpness) + _Density));
    124.                     _sampled.a *= _details;
    125.                 }
    126.                
    127.                 // main alpha
    128.                 _finalcolor.a = _Opacity * _sampled.a * _young;
    129.                                
    130.                 #ifdef SOFTPARTICLES_ON
    131.                 float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
    132.                 float partZ = i.projPos.z;
    133.                 float fade = saturate (_InvFade * (sceneZ-partZ));
    134.                 _finalcolor.a *= fade;
    135.                 #endif
    136.                                                    
    137.                 return _finalcolor;
    138.                
    139.             }
    140.            
    141.            
    142.             ENDCG
    143.         }  
    144.     }  
    145.    
    146.     // ---- Dual texture cards
    147.     SubShader {
    148.         Pass {
    149.             SetTexture [_MainTex] {
    150.                 constantColor [_TintColor]
    151.                 Combine texture * constant DOUBLE, texture * primary
    152.             }
    153.             SetTexture [_DetailTex] {
    154.                 Combine previous,previous * texture
    155.             }
    156.         }
    157.     }
    158.    
    159.     // ---- Single texture cards (does not do color tint)
    160.     SubShader {
    161.         Pass {
    162.             SetTexture [_MainTex] {
    163.                 combine texture * primary
    164.             }
    165.         }
    166.     }
    167.  
    168. }
    169.  
    170. }
    171.  
    But I've got 2 problems :

    1) In unity, it shows these warnings :
    And I clearly have no clue on how to fix them...

    2) How to modify it to get it to work on iOS, even with a lower quality, to at least get the alpha fade out

    You can see the current result of this shader here : http://www.alesk.fr/PuffySmoke/
    (the volumetric look is obtained using a texture)

    2b) Do you see anything that could be optimized in the current code ?

    Thanks for any help ^_^ !
     
  3. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Don't write float values as [number]f. Just leave them as a number with a decimal point.

    1.0 rather than 1f
    0.0 rather than 0f
    0.5 rather than 0.5f
    etc...
     
  4. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Aaarrrhh, thanks a lot XD
     
  5. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Hey,
    how would you make a shader so simulate stains on lens? Having an image file with an alpha channel, the RGB would be the color and alpha channel the "mask" and it would only appear when the camera is almost facing light.

    thanks!
     
  6. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    You need two rendertextures for that (to accumulate previously rendered stains) and a little script to compute the stains positions and feed the shader in screen space.
    I'll make a small example later this week. Have been lately very busy with the launch of our upcoming game! :rolleyes:
     
  7. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    222
    Please!! Outlined shader that lit with light and can receive shadows!
     
  8. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Go few pages back, this was already requested (and posted).
     
  9. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    222
    I'v found Some toon shaders here, but they do not receive shadows, only cast, but yes, they are lit. But Im looking for outline shader that receive shadows.
     
  10. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Huh...how did you do the particle lighting??? is it from the code??
     
  11. domkia

    domkia

    Joined:
    Aug 19, 2012
    Posts:
    99
    Hi, is it posible to give an outline for only specific edges/vertex?
     
  12. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    The shader will run on all vertices the material is affecting. They are ways to apply a specific technique based on vertex orientation and position. But really depends how "specific" you are talking about. Define "specific" please. :rolleyes:
     
  13. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Yes, with vertex color + texture ^_^

    By the way, I'm trying to get my particles casting some shadows... I've found this piece of code: http://wiki.unity3d.com/index.php?title=TransparentShadowCaster

    But I don't know how to (and even if it's possible) to merge it with my current shader (here http://forum.unity3d.com/threads/14...-want/page16?p=1347564&viewfull=1#post1347564)

    Any help would be much appreciated !
     
  14. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Change the particle shader tag to this and see if it helps:
    Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
     
  15. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Hi,

    Thanks for the quick reply :)
    Unfortunately, changing to "TransparentCutout" doesn't show any shadow :(

    Just before my first Subshader declaration, I've added this :
    Code (csharp):
    1.  
    2.     Subshader{
    3.         // UsePass "VertexLit/SHADOWCOLLECTOR"
    4.         UsePass "VertexLit/SHADOWCASTER"
    5.     }
    ... and get blocky squares for each billboard, so that's a first step... But I'd rather prefere to get something closer to the alpha applied to my mesh.
    It looks like this :
    $smoke_shadow.png
     
  16. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Just to be sure, change the entire Tags line to:
    Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
     
  17. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    yup, I did it, and still get the result shown in my previous post :/
     
  18. bugoheyer

    bugoheyer

    Joined:
    Jul 23, 2013
    Posts:
    35
    Hey, would be awesome if I could have an unlit texture shader that doesnt use any lighting info, but still receives shadow from a directional light.

    Can I "has" that? ;)
     
  19. kilian277

    kilian277

    Joined:
    Jul 13, 2012
    Posts:
    54
    Hi,

    i just want to recreate the shader shown in this example:

    View attachment 70100

    The images are from the CryEngine SDK 3 for there "common.cloudshader".

    The problem is how to achieve the backlit rim lighting thingy shown on the edges of the cloud in the picture , on the backside where the sun is pointing towards.

    Can maybe someone point me in the right direction or help ?

    Article:
    http://freesdk.crydev.net/display/SDKDOC2/Common.Cloud+Shader

    Thanks !
     
  20. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    334
    Hi,

    I'm trying to make a (static) cubemap shader that also makes use of a cubemapped normal map, means that texture map and normal map must match details (for making planet topo). Actually unpacking the normal map seems to place the normal map correctly but the shading is totally off. There are also some visible seams but that might be caused by Unity's normal map importing, should be able to fix that in xNormal + Farfarer's TS plugin. Cubemaps are also generated via Unity's "advanced texture" import. I spend already some days to figure out the false lighting caused by normal maps. I'm not good at shaders and running out of ideas at the moment. I will appreciate a helping hand here, pointers into the right direction or some lines of code that will fix it. My feeling is that it's not that much code that must go in here...

    I simplified my shader code to make it more readable, please take also a look at the screenshot:
    Code (csharp):
    1. Shader "Custom/cubednormalmap"
    2. {  
    3.     Properties
    4.     {
    5.         _CubeTex("Main Texture Cubemap", CUBE) = "" {}
    6.         _CubeBump("Bump Cubemap", CUBE) = "" {}
    7.     }
    8.    
    9.     SubShader
    10.     {
    11.         Tags { "Queue"="Geometry" "RenderType"="Opaque" }
    12.        
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert vertex:vert
    15.  
    16.         float4 _Color;
    17.         samplerCUBE _CubeTex;
    18.         samplerCUBE _CubeBump;
    19.  
    20.         struct Input
    21.         {
    22.             float3 viewDir;
    23.             float3 CubeTexPos;
    24.         };
    25.        
    26.         void vert(inout appdata_full v, out Input outdata)
    27.         {
    28.             UNITY_INITIALIZE_OUTPUT(Input,outdata);
    29.             outdata.CubeTexPos = v.normal;
    30.         }
    31.        
    32.         void surf (Input IN, inout SurfaceOutput o)
    33.         {
    34.             float3 cubeTexPUV = IN.CubeTexPos.xyz ;
    35.             o.Albedo = texCUBE(_CubeTex, cubeTexPUV).rgb;
    36.             o.Normal = UnpackNormal (texCUBE (_CubeBump, cubeTexPUV));
    37.         }
    38.         ENDCG
    39.     }
    40.     FallBack "Diffuse"
    41. }
    $badlighting.jpg
     
  21. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    I think the problem might actually be with the model... Is it a default Unity sphere?
     
  22. kilian277

    kilian277

    Joined:
    Jul 13, 2012
    Posts:
    54
    I think this will be a simpler method.

    I foudn this article that basically does a smpler method for what i'm trying to achieve http://www.radioactive-software.com/forum/viewtopic.php?t=1442&start=0

    You need to scroll down to the last post to find the technique.

    Now if someone could help assemble this shader in unity that would be great

    Thanks!
     
  23. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    334
    Hi Dolkar,

    The screenshot is not made with a Unity sphere but I tried also with Unity spheres and shading is also wrong. As far as I understand this, it should work with any geometry, as cubemapping doesn't consider unwrapped UVs but generates it's own. I guess that the lighting model needs to be adapted for the unpacked normals.
     
  24. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    I doubt the problem is in wrong texture access. You said the bumps match up with the diffuse texture, which also fits. So the problem must be with the bump mapping itself. One way is that the tangents on your model are wrong... the only other way this could go wrong that comes to my mind is that you didn't generate the bump map correctly. Is it in tangent space? The map should be blue-ish everywhere if so.

    If all else fails, You can always generate a geosphere. That should be easier to seamlessly UV-map with 2d textures
     
  25. WGermany

    WGermany

    Joined:
    Jun 27, 2013
    Posts:
    78
    Anyone tried Screen Space Image Based Lighting? Plug in a filtered cube map and it lights up normals based on screen space normals? Is this possible?
     
  26. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
  27. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    334
    Quite a bunch of time went into investigating this and, yeah, you're right! Tangent space was not suitable and(!) also normal maps have to be generated correctly. Unity's normal map generation doesn't do the job at this point unfortunately.

    While cubemapping doesn't require manually unwrapped UVs, normal mapping does for good tangent space direction flow. So unwrapping the sphere for a nice tangent space is crucial. The previously posted shader works well with my improved spheres and normal maps, now..

    So thanks Dolkar to let me see this thing with another pair of eyes.
     
  28. Ryan-Gatts

    Ryan-Gatts

    Joined:
    Sep 27, 2012
    Posts:
    54
    This is sort of a repost/crosspost from http://forum.unity3d.com/threads/211241-Skybox-cubemap-derived-fog-color
    I've wanted cubemap-shaded, lit-fog forever :) I've attached all the thoughts I have on it. I feel like if the unity community at large had an easy way to implement this effect, it would improve almost any game; so bonus points for free version :D
    $lit_fog_request.jpg
    If I've missed an already existing implementation of this effect, please point me toward it!
     
  29. Arkhivrag

    Arkhivrag

    Joined:
    Apr 25, 2012
    Posts:
    2,984
    Last edited: Dec 30, 2013
  30. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Hi,
    I'm looking for a simple
    2 dimensional color gradient ramp. 4 different colors of a quad. Shader ramp for UI. unlit It do not need light.

     
  31. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Can you provide more information, how you want the colors to be pickedup/behave? You want to provide 4 input colors and the shader must lerp them using the ramp?
     
  32. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    By selecting the material and in the inspector be able to choose [4 colors]
    I'm starting from here.


    Code (CSharp):
    1. Shader "Ramps/Unlit/4CornersGradient" {
    2.  
    3.     Properties{
    4.         _Color("Top Left Color", Color) = (1,1,1)
    5.         _Color2("Top Right Color", Color) = (1,1,1)
    6.         _Color3("Bottom Right Color", Color) = (1,1,1)
    7.         _Color4("Bottom Left Color", Color) = (1,1,1)
    8.     }
    9.  
    10.     SubShader{
    11.         // CODE
    12.     }
    13. }
    14.  
    Yes must lerp them using the 4 colors.

    Code (CSharp):
    1. o.col = lerp(_Color2 ,_Color, v.texcoord.x);
    2. o.col = o.col * lerp(_Color4 ,_Color3, v.texcoord.y) ;
    3.  
     
    Last edited: Apr 20, 2016
    tatoforever likes this.
  33. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    I see yes, gimme some time, I'm a bit busy now but I'll tackle it later in the day.
     
  34. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    OK Thx.

    More info about the progress. I put it in a UI as material but is not working because it creates a strage ramp probably because of the triangles of the mesh used in the UI.


    Code (CSharp):
    1. Shader "Ramps/Unlit/4CornersGradient" {
    2.  
    3.  
    4.     Properties{  
    5.         _Color("Top Color", Color) = (1,1,1)
    6.         _Color2("Bottom Color", Color) = (1,1,1)
    7.         _Color3("Right Color", Color) = (1,1,1)
    8.         _Color4("Left Color", Color) = (1,1,1)
    9.     }
    10.  
    11.     SubShader{
    12.         Tags{ "Queue" = "Background"  "IgnoreProjector" = "True" }
    13.         LOD 100
    14.  
    15.         ZWrite On
    16.  
    17.         Pass{
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #include "UnityCG.cginc"
    22.  
    23.                 fixed4 _Color;
    24.                 fixed4 _Color2;
    25.                 fixed4 _Color3;
    26.                 fixed4 _Color4;
    27.  
    28.  
    29.                 struct v2f {
    30.                     float4 pos : SV_POSITION;
    31.                     fixed4 col : COLOR;
    32.                 };
    33.  
    34.                 v2f vert(appdata_full v)
    35.                 {
    36.                     v2f o;
    37.                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    38.                     o.col = lerp(_Color2,_Color, v.texcoord.y);
    39.                     o.col = o.col * lerp(_Color3, _Color4, v.texcoord.x);
    40.                     return o;
    41.                 }
    42.  
    43.  
    44.                 float4 frag(v2f i) : COLOR{
    45.                     float4 c = i.col;
    46.                     c.a = 1;
    47.                     return c;
    48.                 }
    49.             ENDCG
    50.         }
    51.     }
    52. }
     
  35. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    I'm making two versions, one that blends on the vertex program and the other in the fragment program. You have two problems on the example you provided, first you are blending in the vertex shader, second is your lerping, color1 and color2 gets blendend correctly but 3 and 4 gets multiplied with 1 and 2 which gives an incorrect result. I'll post the solution in a moment.
     
  36. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Yes, Looks like is difficult the second lerp. For debugging how: Here are some new PDF solutions and a starting point for thinking.

     

    Attached Files:

  37. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Here you go (attached).
    Note: I've added a _LerpScale option if you want to linearly scale how much color you want in corners.
     

    Attached Files:

    AlanMattano likes this.
  38. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    CarlSagan_YouAreAwesome.gif
     
    tatoforever likes this.
  39. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    The shaders are also very simple and fast. There's no lerp function between colors, only few subtractions and multiplications. Plus, you got a vertex blend version in case you want superfast rendering.
     
    Last edited: Apr 21, 2016
  40. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Moderators: Forgive the gift

    Ok so I get it right:

    • Vertex blend version is super fast rendering with less color transition quality if a quadrilaterals polygon is not used.
    • Pixel blend version is fast and the transiti in all polygons.
    I'm using it in the UI. I know that you are not a Unity specific expert but I'm getting this error:
    Material doesn't have a texture property '_MainTex'
    UnityEngine.Canvas:SendWillRenderCanvases()

    I like this solution so simple to understand

    Code (CSharp):
    1.     fixed4 col = col1 + col2 + col3 + col4;


    Since I'm learning
    is this faster?

    Code (CSharp):
    1.     fixed4 col =     ((_LerpScale - i.uv.x) * i.uv.y * _Color1) +
    2.             (i.uv.x * i.uv.y * _Color2) +
    3.             ((_LerpScale - i.uv.x) * (_LerpScale - i.uv.y) * _Color3) +
    4.             (i.uv.x * (_LerpScale - i.uv.y) * _Color4);
    5.     return col;

     
    Last edited: Apr 21, 2016
  41. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    For the solution, I like it when it's simple and elegant, even for obscure GPU code. :D

    For the error, you can try adding this line in the property section:
    _MainTex("Main Texture", 2D) = "white" {}

    As for the code you posted, it's hard to say if it will be any faster, probably because the compiler will optimized them the same way due to similarities. It's hard to debug GPU code as tools for it are very scarce. But if you like it that way, go for it. :D
     
  42. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Yes, adding the line the error go away. Probably Unity needs an Attribure to hide _MainTex in the inspector.
    Tato! Thx a lot!

    For who is following and making a color or gray gradients, if the middle gray color is to close to white it is possible to change the fading ramp and moving the middle gray value by adding a pow(value, exp) slider for example to correct the gamma of the monitor:

    Code (CSharp):
    1. _LerpCorrection ("Gamma Correction", Range(0.2, 2.5)) = 0.473
    The range is bette up to 1. If the value is negative then it will make the correction. If the value is positive then it will push the middle gray or color to the border.

    Code (CSharp):
    1. col = lerp(_Color, _Color2 , pow(i.uv.x, _LerpCorrection ));
    2. return col;
     
  43. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    You can hide it already with "HideInInspector" attribute, like so:
    [HideInInspector] _MainTex("Main Texture", 2D) = "white" {}

    As for the pow function, it's possible but this will make a simple UI shader quite expensive as the pow function have to be used twice for every color (8 times total). Probably better do some trickery approximations. I'll see what I can do.
     
  44. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Try this:

    Code (CSharp):
    1.                 //fixed4 blend1 = lerp(_Color1, _Color2 , pow(i.uv.x * _LerpRange, _Intensity));
    2.                 //fixed4 blend2 = lerp(_Color3, _Color4 , pow(1-i.uv.x * _LerpRange, _Intensity));
    3.  
    4.                 fixed4 blend1 = lerp(_Color1, _Color2, i.uv.x * _Intensity);
    5.                 fixed4 blend2 = lerp(_Color3, _Color4, i.uv.x * _Intensity);
    6.  
    7.                 fixed4 col = lerp(blend2, blend1,  i.uv.y);
     
  45. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Just popped by to say tato is totally cool helping people and probably dreams in compute.
     
    MV10 and tatoforever like this.
  46. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    I dreams in a complex world of infinite fractals and colors! :D
     
    hippocoder likes this.
  47. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Yes,Tato inspires and in my case it makes me think and dream about a faster aerodynamic lift Cr compute shader for next Yr sim.
     
    tatoforever likes this.
  48. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    I got some free time to kill now and I'm working on new quadcorner color interpolation. :D
     
  49. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,369
    Try this one, it's a bit more expensive but not by much. Interpolation still linear and ranges from 0 to 1 (to map UVs values).

    Code (CSharp):
    1.  
    2. Shader "Ramps/Unlit/4CornersGradient Pixel Blend"
    3. {
    4.    Properties
    5.    {
    6.      _Intensity ("Intensity", Range(0, 64)) = 4
    7.      _LerpCut ("Lerp Cut", Range(0, 1)) = 0.25
    8.      _Color1("Top Left Color", Color) = (1,0,0,1)
    9.   _Color2("Top Right Color", Color) = (0,1,0,1)
    10.   _Color3("Bottom Left Color", Color) = (0,0,1,1)
    11.   _Color4("Bottom Right Color", Color) = (1,1,0,1)
    12.    }
    13.    SubShader
    14.    {
    15.      Tags {
    16.        "Queue"="Transparent"
    17.        "IgnoreProjector"="True"
    18.        "RenderType"="Transparent"
    19.        "PreviewType"="Plane"
    20.      }
    21.      Lighting Off Cull Off ZTest Always ZWrite Off
    22.      //uncomment this line if you need alpha blending
    23.      //Blend SrcAlpha OneMinusSrcAlpha
    24.  
    25.      Pass
    26.      {
    27.        CGPROGRAM
    28.        #pragma vertex vert
    29.        #pragma fragment frag
    30.        
    31.        // make fog work (uncomment for fog)
    32.        //#pragma multi_compile_fog
    33.        
    34.        #include "UnityCG.cginc"
    35.  
    36.        struct appdata
    37.        {
    38.          float4 vertex : POSITION;
    39.          float2 uv : TEXCOORD0;
    40.        };
    41.  
    42.        struct v2f
    43.        {
    44.          float2 uv : TEXCOORD0;
    45.          //UNITY_FOG_COORDS(1) // uncomment for fog
    46.          float4 vertex : SV_POSITION;
    47.        };
    48.  
    49.        fixed4 _Color1, _Color2, _Color3, _Color4;
    50.        half _Intensity, _LerpCut;
    51.  
    52.        v2f vert (appdata v)
    53.        {
    54.          v2f o;
    55.          o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    56.          o.uv = v.uv;        
    57.  
    58.          //UNITY_TRANSFER_FOG(o,o.vertex); // uncomment for fog
    59.          return o;
    60.        }
    61.        
    62.        fixed4 frag (v2f i) : SV_Target
    63.        {
    64.          fixed4 col1 = saturate(1 - i.uv.x - _LerpCut) * saturate(i.uv.y - _LerpCut) * _Color1 / (1 - _LerpCut);
    65.          fixed4 col2 = saturate(i.uv.x - _LerpCut) * saturate(i.uv.y - _LerpCut) * _Color2 / (1 - _LerpCut);
    66.          fixed4 col3 = saturate(1 - i.uv.x - _LerpCut) * saturate(1 - i.uv.y - _LerpCut) * _Color3 / (1 - _LerpCut);
    67.          fixed4 col4 = saturate(i.uv.x - _LerpCut) * saturate(1 - i.uv.y - _LerpCut) * _Color4 / (1 - _LerpCut);
    68.  
    69.          fixed4 blend1 = lerp(col1, col2, i.uv.x);
    70.          fixed4 blend2 = lerp(col3, col4, i.uv.x);
    71.          fixed4 col = lerp(blend2, blend1,  i.uv.y);
    72.  
    73.          // apply fog
    74.          //UNITY_APPLY_FOG(i.fogCoord, col);// uncomment for fog
    75.          return col * _Intensity;
    76.        }
    77.        ENDCG
    78.      }
    79.    }
    80.    //Fallback "VertexLit"
    81. }
    82.  
     
    Last edited: Apr 23, 2016
    AlanMattano likes this.
  50. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    I'd like to see a Sin City style shader. It would combine a pure black and white threshold with edge detection (the inverted mesh method for outlines is less than ideal IMHO). Additionally, there would be the option to make colors inside the shadows inverted (outlines and main color).