先看一下平面方程的推導(dǎo):
QQ圖片20180816105821.png
由最后的公式可得混埠,A,B诗轻,C是法線向量x0钳宪,y0,z0是平面上任一點(diǎn)扳炬,d常量(匡紅的部分)是法線和已知點(diǎn)的點(diǎn)乘取反使套。所以,設(shè)已知平面Transform為surface鞠柄,表示該平面方程的方法為:
Vector3 pos = surface.position;
Vector3 normal = surface.up;
float d = -Vector3.Dot(normal, pos) ;
Vector4 plane = new Vector4(normal.x, normal.y, normal.z, d);
對(duì)比一下AngryBots中地面反射的平面方程:
private void RenderReflectionFor (Camera cam, Camera reflectCamera)
{
if(!reflectCamera)
return;
SaneCameraSettings(reflectCamera);
reflectCamera.backgroundColor = clearColor;
GL.SetRevertBackfacing(true);
Transform reflectiveSurface = reflectiveSurfaceHeight;
Vector3 eulerA = cam.transform.eulerAngles;
reflectCamera.transform.eulerAngles = new Vector3(-eulerA.x, eulerA.y, eulerA.z);
reflectCamera.transform.position = cam.transform.position;
Vector3 pos = reflectiveSurface.position;
pos.y = reflectiveSurface.position.y;
Vector3 normal = reflectiveSurface.up;
float d = -Vector3.Dot(normal, pos) - clipPlaneOffset;
Vector4 reflectionPlane = new Vector4(normal.x, normal.y, normal.z, d);
Matrix4x4 reflection = Matrix4x4.zero;
reflection = CalculateReflectionMatrix(reflection, reflectionPlane);
oldpos = cam.transform.position;
Vector3 newpos = reflection.MultiplyPoint (oldpos);
reflectCamera.worldToCameraMatrix = cam.worldToCameraMatrix * reflection;
Vector4 clipPlane = CameraSpacePlane(reflectCamera, pos, normal, 1.0f);
Matrix4x4 projection = cam.projectionMatrix;
projection = CalculateObliqueMatrix(projection, clipPlane);
reflectCamera.projectionMatrix = projection;
reflectCamera.transform.position = newpos;
Vector3 euler = cam.transform.eulerAngles;
reflectCamera.transform.eulerAngles = new Vector3(-euler.x, euler.y, euler.z);
reflectCamera.RenderWithShader (replacementShader, "Reflection");
GL.SetRevertBackfacing(false);
}
構(gòu)建一個(gè)平面方程就是這么簡(jiǎn)單。