载入中...
搜索中...
未找到
OceanWgsl.h
浏览该文件的文档.
1#pragma once
2// Generated by scripts/compile_ocean_shaders.py.
3namespace eve::fluids::shaders {
4inline constexpr char oceanVertex[] = R"wgsl(struct Light3D{posRadius:vec4f,color:vec4f};
5struct Frame{mvp:mat4x4f,model:mat4x4f,lightDir:vec4f,lightColor:vec4f,tint:vec4f,cameraPos:vec4f,ambient:vec4f,lights:array<Light3D,8>,texBomb:vec4f,parallax:vec4f,surface:vec4f,view:mat4x4f,clipInfo:vec4f,cloud:vec4f,cloudWind:vec4f,envProbeCenter:vec4f,envProbeExtent:vec4f,skinInfo:vec4f,reflectionProbeCenter:array<vec4f,2>,reflectionProbeExtent:array<vec4f,2>};
6@group(0) @binding(0) var<uniform> frame:Frame;
7struct Varying {
8 @builtin(position) pos:vec4f,
9 @location(0) normal:vec3f,
10 @location(1) surface:vec2f,
11 @location(2) world:vec3f,
12};
13struct Vertex {@location(0) pos:vec3f,@location(1) normal:vec3f,@location(2) uv:vec2f};
14@vertex fn vs_main(input:Vertex)->Varying {
15 var out:Varying;
16 out.pos=frame.mvp*vec4f(input.pos,1.0);out.pos.y=-out.pos.y;
17 out.world=(frame.model*vec4f(input.pos,1.0)).xyz;
18 out.normal=normalize(mat3x3f(frame.model[0].xyz,frame.model[1].xyz,frame.model[2].xyz)*input.normal);
19 out.surface=input.uv;return out;
20}
21)wgsl";
22inline constexpr char oceanFragment[] = R"wgsl(struct Light3D{posRadius:vec4f,color:vec4f};
23struct Frame{mvp:mat4x4f,model:mat4x4f,lightDir:vec4f,lightColor:vec4f,tint:vec4f,cameraPos:vec4f,ambient:vec4f,lights:array<Light3D,8>,texBomb:vec4f,parallax:vec4f,surface:vec4f,view:mat4x4f,clipInfo:vec4f,cloud:vec4f,cloudWind:vec4f,envProbeCenter:vec4f,envProbeExtent:vec4f,skinInfo:vec4f,reflectionProbeCenter:array<vec4f,2>,reflectionProbeExtent:array<vec4f,2>};
24@group(0) @binding(0) var<uniform> frame:Frame;
25struct Varying {
26 @builtin(position) pos:vec4f,
27 @location(0) normal:vec3f,
28 @location(1) surface:vec2f,
29 @location(2) world:vec3f,
30};
31@group(0) @binding(3) var environment:texture_cube<f32>;
32@group(0) @binding(7) var samp:sampler;
33@group(0) @binding(2) var hullMask:texture_2d<f32>;
34fn hullValue(column:i32,row:i32)->f32 {
35 let bytes=vec4u(round(textureLoad(hullMask,vec2i(column,row),0)*255.0));
36 return bitcast<f32>(bytes.x | (bytes.y<<8u) | (bytes.z<<16u) | (bytes.w<<24u));
37}
38fn dryInterior(point:vec3f)->bool {
39 if (any(textureDimensions(hullMask)!=vec2u(4u,545u))) {return false;}
40 let count=clamp(i32(hullValue(0,0)),0,32);
41 for(var volume=0;volume<count;volume++) {
42 let row=1+volume*17;let planes=clamp(i32(hullValue(0,row)),0,16);
43 var inside=planes>0;
44 for(var plane=0;plane<planes;plane++) {
45 let r=row+1+plane;
46 let n=vec3f(hullValue(0,r),hullValue(1,r),hullValue(2,r));
47 if(dot(n,point)>hullValue(3,r)) {inside=false;break;}
48 }
49 if(inside) {return true;}
50 }
51 return false;
52}
53fn foamNoise(p:vec2f)->f32 {
54 let cell=floor(p);let fraction=fract(p);let f=fraction*fraction*(3.0-2.0*fraction);
55 let h=fract(sin(vec4f(dot(cell,vec2f(127.1,311.7)),
56 dot(cell+vec2f(1,0),vec2f(127.1,311.7)),
57 dot(cell+vec2f(0,1),vec2f(127.1,311.7)),
58 dot(cell+vec2f(1,1),vec2f(127.1,311.7))))*43758.5453);
59 return mix(mix(h.x,h.y,f.x),mix(h.z,h.w,f.x),f.y);
60}
61@fragment fn fs_main(input:Varying,@builtin(front_facing) front:bool)->@location(0) vec4f {
62 let N=normalize(select(-input.normal,input.normal,front));
63 let variance=dot(dpdx(N),dpdx(N))+dot(dpdy(N),dpdy(N));
64 if(dryInterior(input.world)) {discard;}
65 let V=normalize(frame.cameraPos.xyz-input.world);
66 let L=normalize(frame.lightDir.xyz);let R=reflect(-V,N);
67 let fresnel=0.02037+0.97963*pow(1.0-max(dot(N,V),0.0),5.0);
68 let coverage=clamp(max(input.surface.x,(0.72-input.surface.y)*2.5),0.0,1.0);
69 let threshold=0.18+0.55*foamNoise(input.world.xz*0.4);
70 let foam=smoothstep(threshold-0.15,threshold+0.15,coverage)*(0.55+0.45*foamNoise(input.world.xz*1.2));
71 let roughness=mix(0.12,0.65,foam);
72 let reflected=textureSampleLevel(environment,samp,R,roughness*f32(textureNumLevels(environment)-1u)).rgb*frame.lightColor.w;
73 // Keep the water-body scattering and filtered sun lobe identical to Vulkan.
74 let water=vec3f(0.008,0.075,0.095)*(0.35+0.65*max(dot(N,L),0.0));
75 let exponent=mix(160.0,24.0,foam);
76 let filteredExponent=exponent/(1.0+exponent*variance);
77 let sun=(filteredExponent/exponent)*pow(max(dot(N,normalize(V+L)),0.0),filteredExponent);
78 let lit=mix(water,reflected,vec3f(fresnel))+frame.lightColor.rgb*sun*0.15*max(dot(N,L),0.0);
79 let foamLight=vec3f(0.83,0.91,0.91)*(0.4+0.6*max(dot(N,L),0.0));
80 return vec4f(mix(lit,foamLight,vec3f(foam))*frame.tint.rgb,1.0);
81}
82)wgsl";
83}
constexpr char oceanVertex[]
Definition OceanWgsl.h:4
constexpr char oceanFragment[]
Definition OceanWgsl.h:22