【TA】低成本点光和体积光

低成本点光和体积光。

低成本点光和体积光

求射线和几何体交点的函数 ray-surface intersectors for some common primitive types

image-20240711194732210

圆锥光

基本就是求交点那套。重点在于拿到交点后该如何做出边缘柔和的边缘过度。

不严谨地考虑光束的衰减的时候(除非写实向项目,不需要做得非常真实),其实和光源点的垂直距离(z值),即光束的长度能代表衰减的程度,还有观测点是否处在光束的侧面边缘。

垂直距离用近点交点计算,边缘衰减用中点计算,取两个值计算出的Max值。

1
2
3
4
5
6
float3 mid = ro + rd * (sphere.x + sphere.y) * 0.5;
float3 near = ro + rd*min(sphere.x , sphere.y);
float mid_h = saturate((mid.z - pb.z) / (pa.z - pb.z));
float mid_r = mid_h * (ra - rb) + rb;
float alpha = smoothstep(0,1,(sphere.y - sphere.x) / (ra*2));
alpha *= max(smoothstep(0,_LightLengthFade,mid.z + pa0),smoothstep(0,_LightLengthFade,near.z + pa0));

20240513205200_rec_

矩形光

因为面光源也是使用很多的光源,所以搓一个矩形光。

因为矩形光所有面都是平面,暂时没想到什么用解析几何来做的好方案,直接写一个求直线和平面交点并判断该交点是否在几何体内的函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 求射线与四棱柱的交点
float CustomFakeVoxelPyramidLight(float3 origin,float3 direction, float d, float r0, float r1) {

float3 normals[6] = {
float3(0, 0, 1), // 顶面
float3(0, 0, -1), // 底面
float3(1, 0, (r1 - r0)/(2.0f * d)), // 两侧面
float3(0, 1, (r1 - r0)/(2.0f * d)),
float3(-1, 0, (r1 - r0)/(2.0f * d)), // 对应相反方向的两侧面
float3(0, -1, (r1 - r0)/(2.0f * d))
};
float3 pPoints[6] = {
float3(0, 0, d / 2.0), // 顶面
float3(0, 0, -d / 2.0), // 底面
float3(r0 / 4.0 + r1 /4.0, 0, 0),
float3(0, r0 /4.0 + r1 / 4.0, 0),
float3(-r1 / 4.0 - r0 / 4.0, 0, 0),
float3(0, -r1 / 4.0 - r0/4.0, 0)
};


float t = FLT_MAX;
float2 minLen = float2(FLT_MAX,FLT_MAX);
float flag = false;
float mid = 0;
float z_length = 0;
float test =0;
for (int i = 0; i <6; ++i) {
float tempT;
if (IntersectRayPlane(normals[i], pPoints[i], origin,direction, tempT)) {
float3 tempPoint = origin + tempT * direction;
float2 len = IsPointInPrism(tempPoint, r0, r1, d,i);
}
}

float3 hitPoint = origin + t * direction;
float3 hitPoint_mid = origin + z_length * direction/2;
return smoothstep(0,0.3,mid) * smoothstep(0,_LightLengthFade,1-(d/2 -hitPoint_mid.z)/d);
}

用这种方案计算的话需要考虑精度问题,在比较交点是否落在几何体范围内的时候可以加上1e-6

image-20240711204026496

结果:

20240513-205909

参考

一个接近无偏的低成本体积光