Intermediate Shader Tutorial 13 Starry Sky Rendering

1. Implementation principle

1. Use spherical coordinates to divide the space
2. Generate hash for each space divided grid
3. Define the size of the star, flickering period, light and shade, etc. according to the hashID
4. Use smoothstep to control the size of the circle when drawing a circle on the grid

2. Source code

1. Shift of moving speed between different layers in a single-layer FBM with time

// 通过rd 来进行空间划分 这样在根据相机进行改变
float3 Stars(in float3 rd,float den,float tileNum)
{
    float3 c = float3(0.,0.,0.);
    float3 p = rd*tileNum;//空间划分
    float SIZE = 0.5;
    //分多层
    for (float i=0.;i<3.;i++)
    {
        float3 q = frac(p)-0.5;
        float3 id = floor(p);
        float2 rn = hash33(id).xy;

        float size = (hash13(id)*0.2+0.8)*SIZE; 
        float demp = pow(1.-size/SIZE,.8)*0.45;
        float val = (sin(_Time.y*31.*size)*demp+1.-demp) * size;
        float c2 = 1.-smoothstep(0.,val,length(q));//画圆
        //随机显示 随着深度的层数的增加添加更多的星星 增加每个grid 出现星星的概率
        c2 *= step(rn.x,(.0005+i*i*0.001)*den);
        c += c2*(lerp(float3(1.0,0.49,0.1),float3(0.75,0.9,1.),rn.y)*0.25+0.75);//不同的亮度
        p *= 1.4;//增加grid密度
    }
    return c*c*.7;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208756&siteId=291194637
Recommended