Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Having textures generate alpha from Grayscale via scripting

Discussion in 'Editor & General Support' started by Tofugames, Jan 16, 2011.

  1. Tofugames

    Tofugames

    Joined:
    Nov 29, 2010
    Posts:
    37
    Hello, I have been trying to figure this out for some time now and have yet to find anything that can help me with this.. I am procedurally generating noise textures at runtime to act as cloud textures and I am trying to get them to work correctly in my shader, however in order to do so they need to have "Generate alpha from Grayscale" enabled or else they are not transparent where they need to be.. I was wondering if there was a way to access that property or achieve a similar result via scripting?? Any help would be much appreciated! :)

    ~~Tofugames
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  3. Tofugames

    Tofugames

    Joined:
    Nov 29, 2010
    Posts:
    37
    This is the shader I'm using (a slightly modified version of pixelstudio's skydome shader):

    Code (csharp):
    1. // Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
    2.  
    3. ///Skydome shader by Martijn Dekker aka Pixelstudio
    4. ///if you have questions just drop me a mail at martijn.pixelstudio@gmail.com
    5. ///Version 1.0
    6.  
    7. ///Color correction by Jon Apgar jon.apgar@hotmail.com
    8.  
    9. Shader "aScattering 2.1" {
    10.     Properties {
    11.         DirectionalityFactor("DirectionalityFactor",float) = 0.50468
    12.         SunColorIntensity("SunColorIntensity",float) = 1.468
    13.         tint("tint",float) = 1
    14.         fade("Cloud fade height",float) = 0
    15.         cloudSpeed1("cloudSpeed1",float)=1
    16.         cloudSpeed2("cloudSpeed2",float)=1.5
    17.         plane_height1("cloud plane height 1",float)=12
    18.         plane_height2("cloud plane height 2",float)=13
    19.         noisetex ("noise texture", 2D) = "white" {}
    20.         starTexture ("star Texture", 2D) = "white" {}
    21.         curveTexture ("curveTexture", 2D) = "white" {}
    22.         LightDir("LightDir",Vector) = (-0.657,-0.024,0.7758)
    23.         vBetaRayleigh("vBetaRayleigh",Vector) = (0.0008,0.0014,0.0029)
    24.         BetaRayTheta("BetaRayTheta",Vector) = (0.0001,0.0002,0.005)
    25.         vBetaMie("vBetaMie",Vector) = (0.0012,0.0016,0.0023)
    26.         BetaMieTheta("BetaMieTheta",Vector) = (0.0009,0.0012,0.0017)
    27.         g_vEyePt("g_vEyePt",Vector) = (0,13.397,0)
    28.         g_vSunColor("g_vSunColor",Vector) = (0.6878,0.5951,0.4217)
    29.         wind_direction("winddirection",Vector) = (0.8736,1.2048,1.2365,0.3)
    30.         hueShift("hueShift",float) =0
    31.         satM("saturationMultiplier",float) =1
    32.         satT("saturationTranspose",float) =0
    33.         briM("brightnessMultiplier",float) =1
    34.         briT("brightnessTranspose",float) =0
    35.     }
    36.  
    37.     SubShader {
    38.     Pass {
    39.         Cull Front
    40.         CGPROGRAM
    41.         #pragma vertex vert
    42.         #pragma fragment frag
    43.         #pragma target 3.0
    44.         #include "UnityCG.cginc"
    45.         float3 g_vEyePt;
    46.         float3 vBetaRayleigh;
    47.         float3 BetaRayTheta;
    48.         float3 vBetaMie;
    49.         float3 BetaMieTheta;
    50.         float3 LightDir;
    51.         float3 g_vSunColor;
    52.         float DirectionalityFactor;
    53.         float SunColorIntensity;
    54.         float cloudSpeed1;
    55.         float cloudSpeed2;
    56.         float tint;
    57.         float plane_height1;
    58.         float plane_height2;
    59.         float fade;
    60.         float4 wind_direction;
    61.         float4 cloudProperties;
    62.         float satM;
    63.         float satT;
    64.         float briM;
    65.         float briT;
    66.         float hueShift;
    67.  
    68.  
    69.         sampler2D noisetex;
    70.         sampler2D starTexture;
    71.         sampler2D curveTexture;
    72.  
    73.         struct vertex_output {
    74.             float4 position         : POSITION;
    75.             float4 color            : COLOR;
    76.             float2 uvcoords1        : TEXCOORD0;// Cloud layer01
    77.             float intensity         : TEXCOORD1;// Cloud intensity
    78.             float2 uvcoords2        : TEXCOORD2;// Cloud layer02
    79.             float2 skydomecoluv     : TEXCOORD3;// UV for the cloud layer
    80.             float orgposz           : TEXCOORD4;
    81.             float2 starUV           : TEXCOORD5;
    82.         };
    83.  
    84.         float3 BetaR(float Theta){
    85.             return BetaRayTheta*(3.0f+0.5f*Theta*Theta);
    86.         }
    87.  
    88.         float3 BetaM(float Theta){
    89.             float g =DirectionalityFactor;
    90.             return(BetaMieTheta*pow(1.0f-g,2.0f))/(pow(1+g*g-2*g*Theta,1.5f));
    91.         }
    92.  
    93.         float3 Lin(float Theta,float SR,float SM){
    94.             return ((BetaR(Theta)+BetaM(Theta))*(1.0f-exp(-(vBetaRayleigh*SR+vBetaMie*SM))))/(vBetaRayleigh + vBetaMie );
    95.         }
    96.  
    97.         float3 RGBtoHSV(in float3 RGB)
    98.         {
    99.             float3 HSV = float3(0,0,0);
    100.    
    101.             HSV.z = max(RGB.r, max(RGB.g, RGB.b));
    102.             float M = min(RGB.r, min(RGB.g, RGB.b));
    103.             float C = HSV.z - M;
    104.        
    105.             if (C != 0)
    106.             {
    107.                 HSV.y = C / HSV.z;
    108.                 float3 Delta = (HSV.z - RGB) / C;
    109.                 Delta.rgb -= Delta.brg;
    110.                 Delta.rg += float2(2,4);
    111.                 if (RGB.r >= HSV.z)
    112.                     HSV.x = Delta.b;
    113.                 else if (RGB.g >= HSV.z)
    114.                     HSV.x = Delta.r;
    115.                 else
    116.                     HSV.x = Delta.g;
    117.                 HSV.x = frac(HSV.x / 6);
    118.             }
    119.             return HSV;
    120.         }
    121.  
    122.         float3 Hue(float H)
    123.         {
    124.             float R = abs(H * 6 - 3) - 1;
    125.             float G = 2 - abs(H * 6 - 2);
    126.             float B = 2 - abs(H * 6 - 4);
    127.             return saturate(float3(R,G,B));
    128.         }
    129.  
    130.         float3 HSVtoRGB(float3 HSV)
    131.         {
    132.             return ((Hue(HSV.x) - 1) * HSV.y + 1) * HSV.z;
    133.         }
    134.  
    135.         float3 Saturate(float3 rgb, float h, float s, float b,float st, float bt) {
    136.             float3 hsv = RGBtoHSV(rgb);
    137.             hsv.x = fmod(hsv.x+h,360);
    138.             hsv.y = clamp(hsv.y*s +st,0,1);
    139.             hsv.z = clamp(hsv.z*b +bt,0,1);
    140.             return (HSVtoRGB(hsv));
    141.             return rgb;
    142.         }
    143.        
    144.         float3 Curve(float3 rgb) {
    145.        
    146.             #if !defined(SHADER_API_OPENGL)
    147.             rgb.r = tex2D(curveTexture, float2(rgb.r,0)).r;
    148.             rgb.g = tex2D(curveTexture, float2(rgb.g,0)).g;
    149.             rgb.b = tex2D(curveTexture, float2(rgb.b,0)).b;
    150.             #endif
    151.             return rgb;
    152.         }
    153.        
    154.         vertex_output vert(appdata_base Input) {
    155.             vertex_output OUT;
    156.            
    157.             //mul( iPos, g_mWorld );
    158.             //float3 vPosWorld = Input.vertex;
    159.             float3 vPosWorld = mul(UNITY_MATRIX_MV,Input.vertex);
    160.            
    161.             //float3 ray = vPosWorld - g_vEyePt;
    162.             float3 ray =  ObjSpaceViewDir(Input.vertex);
    163.             float far = length(ray) ;
    164.             ray = normalize(ray);
    165.             float Theta = dot(ray, LightDir);
    166.             float SR =(1.05f-pow(ray.y,0.3f)) * 2000;
    167.             float SM=far*0.05f;
    168.             float3 L=Lin(Theta, SR, SM );
    169.  
    170.             //cloud stuff
    171.             float3 normVect=normalize(Input.vertex)/100;
    172.             OUT.orgposz=abs(Input.vertex.y);
    173.             float2 vectLength1=float2(normVect.z,normVect.x)*plane_height1;
    174.             float2 vectLength2=float2(normVect.z,normVect.x)*plane_height2;
    175.             float t1=_Time*cloudSpeed1;
    176.             float t2=_Time*cloudSpeed2;
    177.             OUT.uvcoords1.xy=0.9*vectLength1+t1/10*wind_direction.xy*OUT.orgposz;
    178.             OUT.uvcoords2.xy=0.4*vectLength2+t2/10*wind_direction.zw*OUT.orgposz;
    179.             float fadeheight=fade/64;
    180.             OUT.starUV=Input.texcoord * 20;
    181.             OUT.position=mul(UNITY_MATRIX_MVP,Input.vertex);
    182.             OUT.intensity=max(normVect.y-fadeheight,0);
    183.             OUT.color.rgb=Saturate(Curve(L*(g_vSunColor)*SunColorIntensity),hueShift,satM,briM,satT,briT);
    184.             OUT.color.a=1.0f;
    185.             return OUT;
    186.         }
    187.         float4 frag (vertex_output IN): COLOR {
    188.             float4 color:COLOR;
    189.             float4 noise1=tex2D(noisetex,IN.uvcoords1.xy/IN.orgposz);
    190.             float4 noise2=tex2D(noisetex,IN.uvcoords2.xy/IN.orgposz);
    191.             float4 stars=tex2D(starTexture,IN.starUV.xy);
    192.             float4 cloud_color=(noise1*noise2);
    193.             float intensity=1-exp(-512*pow(IN.intensity,1));
    194.             stars*=1-saturate(g_vSunColor.z* 4 + cloud_color.a*2);
    195.             float cloud_alpha = max(noise1.a, noise2.a);
    196.             stars*= cloud_alpha *2;
    197.             color=stars;
    198.             //color+=(g_vSunColor.z+tint)*cloud_color.z*(intensity)*cloud_color;
    199.             color+= tint*cloud_color.a*(intensity)*float4(Saturate(g_vSunColor,0,1.5,1,0,0),1);
    200.             color+=IN.color;
    201.             color.a=1.0;
    202.             return color;
    203.         }
    204.         ENDCG
    205.         }
    206.     }
    207. FallBack "None"
    208. }
    And thanks! I'll look into that.
     
unityunity