载入中...
搜索中...
未找到
AmbientOcclusionWgsl.h
浏览该文件的文档.
16fn depth(uv: vec2f) -> f32 { return textureSampleLevel(mainTex, mainSamp, clamp(uv, vec2f(0), vec2f(1)), 0).r; }
17fn normal_load(uv:vec2f)->vec3f{let d=textureDimensions(normalTex);let q=vec2i(clamp(uv,vec2f(0),vec2f(1))*vec2f(d));return textureLoad(normalTex,clamp(q,vec2i(0),vec2i(d)-1),0).xyz;}
42 var nor=normalize(cross(pr-pos,pu-pos)); let eh=m*vec4f(0,0,0,1); let cam=eh.xyz/max(eh.w,.000001);
44 let a=hash12(i.uv*vec2f(1024,768))*6.2831853; let up=select(vec3f(0,1,0),vec3f(1,0,0),abs(nor.y)>=.999);
63 let tw=max(p(24),.00001); let th=max(p(25),.00001); let z=depth(i.uv); if z>=.999||z<=.00001{return vec4f(1,1,1,z);}
64 let pos=world(m,i.uv,z); var n=normalize(cross(world(m,i.uv+vec2f(tw,0),depth(i.uv+vec2f(tw,0)))-pos,world(m,i.uv+vec2f(0,th),depth(i.uv+vec2f(0,th)))-pos));
66 let sr=clamp(r/max(mix(nz,fz,z),.001)*.35,tw*4,.25); let rnd=hash12(i.uv*vec2f(900,700)); var sum=0.0;
67 for(var d=0;d<8;d++){if d>=dirs{break;} let a=(f32(d)+rnd)/f32(dirs)*6.2831853; let dir=vec2f(cos(a),sin(a)); var h=bias;
68 for(var s=1;s<=8;s++){if s>steps{break;} let uv=i.uv+dir*sr*f32(s)/f32(steps); let delta=world(m,uv,depth(uv))-pos; let dist=length(delta);
72 var ao=sum/f32(max(dirs,1)); ao=clamp(pow(max(ao,0),max(p(21),.01)),0,1); ao=mix(1,ao,clamp(p(20),0,2)); return vec4f(ao,ao,ao,z)*i.color;
79 let m=inv_vp();let nz=max(p(16),.001);let fz=max(p(17),nz+.001);let r=max(p(18),.0001);let bias=max(p(19),0);
80 let dirs=i32(clamp(p(22),2,8));let steps=i32(clamp(p(23),2,8));let tw=max(p(24),.00001);let th=max(p(25),.00001);let thick=max(p(26),r*.5);
82 var n=normalize(cross(world(m,i.uv+vec2f(tw,0),depth(i.uv+vec2f(tw,0)))-pos,world(m,i.uv+vec2f(0,th),depth(i.uv+vec2f(0,th)))-pos));
83 let eh=m*vec4f(0,0,0,1);if dot(n,eh.xyz/max(eh.w,.000001)-pos)<0{n=-n;}let sr=clamp(r/max(mix(nz,fz,z),.001)*.4,tw*4,.25);
85 for(var d=0;d<8;d++){if d>=dirs{break;}let a=(f32(d)+rnd)/f32(dirs)*3.14159265;let dir=vec2f(cos(a),sin(a));var h1=hp;var h2=hp;
87 let dp=world(m,i.uv+dir*q,depth(i.uv+dir*q))-pos;let dn=world(m,i.uv-dir*q,depth(i.uv-dir*q))-pos;let lp=length(dp);let ln=length(dn);
90 let c=.5*(integrate(-1.5707963,clamp(h1,-1.5707963,1.5707963))+integrate(-1.5707963,clamp(h2,-1.5707963,1.5707963)));
91 let thin=smoothstep(0,1,(h1+h2+3.14159265)/max(thick/max(r,.001),.001));vis+=mix(c,max(c,.85),.2*(1-thin));}
92 vis/=f32(max(dirs,1));var ao=clamp(pow(max(vis,0),max(p(21),.01)),0,1);ao=mix(1,ao,clamp(p(20),0,2));return vec4f(ao,ao,ao,z)*i.color;
97@fragment fn fs_main(i:FSIn)->@location(0) vec4f{let tw=max(p(0),.00001);let th=max(p(1),.00001);let ds=max(p(2),.0001);let k=clamp(p(3),1,4);
99 for(var y=-2;y<=2;y++){for(var x=-2;x<=2;x++){if x==0&&y==0{continue;}let d=length(vec2f(f32(x),f32(y)));if d>k+.1{continue;}
100 let s=textureSampleLevel(mainTex,mainSamp,clamp(i.uv+vec2f(f32(x)*tw,f32(y)*th),vec2f(0),vec2f(1)),0);let dz=abs(s.a-c.a);
106@fragment fn fs_main(i:FSIn)->@location(0) vec4f{var ao=depth(i.uv);ao=clamp(pow(max(ao,0),max(p(1),.01)),0,1);let d=clamp((1-ao)*max(p(0),0),0,1);return vec4f(0,0,0,d)*i.color;}
113 let m=inv_vp();let nz=max(p(16),.001);let fz=max(p(17),nz+.001);let r=max(p(18),.0001);let bias=max(p(19),0);let count=i32(clamp(p(22),4,24));
114 let tw=max(p(23),.00001);let th=max(p(24),.00001);let z=depth(i.uv);if z>=.9999{return vec4f(0);}let pos=world(m,i.uv,z);
115 var n:vec3f;if p(25)>.5{let q=normal_load(i.uv)*2-1;n=normalize(select(vec3f(0,1,0),q,length(q)>.001));}
116 else{n=normalize(cross(world(m,i.uv+vec2f(tw,0),depth(i.uv+vec2f(tw,0)))-pos,world(m,i.uv+vec2f(0,th),depth(i.uv+vec2f(0,th)))-pos));}
118 for(var s=1;s<=24;s++){if s>count{break;}let a=f32(s)*2.39996323;let q=f32(s)/f32(count);let uv=clamp(i.uv+vec2f(cos(a),sin(a))*q*r/max(mix(nz,fz,z),.001)*.25,vec2f(0),vec2f(1));
119 let sz=depth(uv);let d=world(m,uv,sz)-pos;let l=length(d);if l>.00001&&l<r&&dot(n,d/l)>bias&&sz<z-bias{occ+=1-l/r;}}
constexpr const char * kOverlay
Definition AmbientOcclusionWgsl.h:105
constexpr const char * kAoCommon
Definition AmbientOcclusionWgsl.h:7
constexpr const char * kFromDepth
Definition AmbientOcclusionWgsl.h:111