2019-06-09

微信截图_20190609152628.png
#ifndef RAYMARCHING_INCLUDED
#define RAYMARCHING_INCLUDED

#define MIXSTEP 2
#define ONESTEPDISTANCE 4

#include"CalCulateNoise.cginc"

sampler2D _noiseTex;
float _rainSpeed;
float _chaosRate;
int _rainNumber;
float _density;

//founction
float2 Unity_Rotate_Radians_float(float2 UV, float2 Center, float Rotation)
{
    UV -= Center;
    float s = sin(Rotation);
    float c = cos(Rotation);
    float2x2 rMatrix = float2x2(c, -s, s, c);
    rMatrix *= 0.5;
    rMatrix += 0.5;
    rMatrix = rMatrix * 2 - 1;
    UV.xy = mul(UV.xy, rMatrix);
    UV += Center;
    float2 Out=float2(0,0);
    return Out;
}

float angleBetween(float3 vector1, float3 vector2) {
     return acos(dot(vector1, vector2)/(length(vector1)*length(vector2)));
 }

float GetNoiseTexValue(float2 uv,float rainSpeed,float chaosRate ,int rainNumber ){
    uv*=pow(1.5,rainNumber);
    uv.y*=rainSpeed;
    uv.y+=_Time.y*40;
    //float2 uv_R=Unity_Rotate_Radians_float(uv,float2(0.5,0.5),chaosRate*15*sin(_Time.y/15 + rainNumber*2));
    return smoothstep(0.65,0.85,FBMR(uv,3));
}

fixed4 rayMarching(float3 ro ,float3 rd,fixed3 sceneCol,float rz ,float2 uv){
    

    float3 rayPos=ro+rd*MIXSTEP;
    float rainAlpha=0;
    for(int ix=0;ix<_rainNumber;ix++){
        if(length(rayPos-ro)>=rz){
            break;
        }

        float alpha=1.0;
        if(2>=rz-length(rayPos-ro)){
            alpha = smoothstep(0,2,rz-length(rayPos-ro));
        }
        
        float2 UV=float2(angleBetween(float3(0,0,1),float3(rd.x,0,rd.z))*_density, rayPos.y/300);
    
        rainAlpha+=GetNoiseTexValue(UV,_rainSpeed,_chaosRate,ix+1)*alpha;

        
        rayPos+=rd*ONESTEPDISTANCE;

    }
    float3 lightCol=float3(0.2,0.2,0.2);
    fixed3 finalCol=sceneCol+rainAlpha*lightCol;

    return  fixed4(finalCol,1.0);
    //return fixed4(rainAlpha,rainAlpha,rainAlpha,1.0);
}

#endif

你可能感兴趣的:(2019-06-09)