mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 16:52:43 +08:00
65 lines
2.5 KiB
Plaintext
65 lines
2.5 KiB
Plaintext
// helper functions for lighting model-functions
|
|
|
|
// return unnormalized vector, support oppo-side
|
|
// V from pixel to camera
|
|
vec3 CalculateRefractDirection(vec3 N, vec3 V, float NoV, float ior)
|
|
{
|
|
float NoVAbs = abs(NoV);
|
|
// two sided
|
|
float sideSign = NoV < 0.0 ? -1.0 : 1.0;
|
|
N *= sideSign;
|
|
NoV *= sideSign;
|
|
float sinB = sqrt(1.0 - NoVAbs*NoVAbs) / ior;
|
|
vec3 X = normalize(-V + N * NoVAbs);
|
|
vec3 R = -N + X * sinB;
|
|
return R;
|
|
}
|
|
|
|
vec3 CalculateReflectDirection(vec3 N, vec3 V, float NoV)
|
|
{
|
|
// two sided
|
|
float sideSign = NoV < 0.0 ? -1.0 : 1.0;
|
|
N *= sideSign;
|
|
return reflect(-V, N);
|
|
}
|
|
|
|
// for bumped planar reflection
|
|
vec3 CalculatePlanarReflectPositionOnPlane(vec3 N, vec3 V, vec3 worldPos, vec4 plane, vec3 cameraPos, float probeReflectedDepth)
|
|
{
|
|
float distPixelToPlane = -dot(plane, vec4(worldPos, 1.0));
|
|
// bring plane to worldPos, avoid artifacts when reflected point away from plane (do not bring worldPos to plane)
|
|
plane.w += distPixelToPlane;
|
|
float distCameraToPlane = abs(-dot(plane, vec4(cameraPos, 1.0)));
|
|
vec3 planeN = plane.xyz;
|
|
vec3 virtualCameraPos = cameraPos - 2.0 * distCameraToPlane * planeN;
|
|
/*support for two-sided reflections
|
|
float sideSignPlaneN = dot(planeN, V) < 0.0 ? -1.0 : 1.0;
|
|
float sideSignN = dot(N, V) < 0.0 ? -1.0 : 1.0;
|
|
planeN *= sideSignPlaneN;
|
|
N *= sideSignN;*/
|
|
vec3 bumpedR = normalize(reflect(-V, N)); //R'
|
|
|
|
// actually reflected pos alone bumpedR direction, avoid tracing by specified a fake depth
|
|
vec3 reflectedPointPos = worldPos + probeReflectedDepth * bumpedR;
|
|
vec3 virtualCameraToReflectedPoint = normalize(reflectedPointPos - virtualCameraPos);
|
|
|
|
// the ray from virtual camera to reflected point, will intersect with plane on P'
|
|
float y = distCameraToPlane / max(EPSILON_LOWP, dot(planeN, virtualCameraToReflectedPoint));
|
|
return virtualCameraPos + y * virtualCameraToReflectedPoint;
|
|
}
|
|
|
|
|
|
|
|
// fix cubemap direction with box projection
|
|
// return unnormalized vector and weight for exceeding
|
|
vec4 CalculateBoxProjectedDirection(vec3 R, vec3 worldPos, vec3 cubeCenterPos, vec3 cubeBoxHalfSize)
|
|
{
|
|
// point W is the worldPos in the space origin align with cube center
|
|
vec3 W = worldPos - cubeCenterPos;
|
|
// find point P which intersected with cube box border from W alone R
|
|
vec3 projectedLength = (sign(R) * cubeBoxHalfSize - W) / (R + vec3(EPSILON));
|
|
float len = min(min(projectedLength.x, projectedLength.y), projectedLength.z);
|
|
vec3 P = W + len * R;
|
|
float weight = len < 0.0 ? 0.0 : 1.0;
|
|
return vec4(P, weight);
|
|
} |