Unity中怎么表示一个平面

先看一下平面方程的推导:


QQ图片20180816105821.png

由最后的公式可得,A,B,C是法线向量x0,y0,z0是平面上任一点,d常量(匡红的部分)是法线和已知点的点乘取反。所以,设已知平面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);
对比一下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);                  
}

构建一个平面方程就是这么简单。

你可能感兴趣的:(Unity中怎么表示一个平面)