开发中遇到float精度不够
Vector3 worldPos = mMapCamera.ScreenToWorldPoint(screenPos);
Vector3 tempPos = mMapCamera.WorldToScreenPoint(worldPos);
上面代码执行后screenPos不等于tempPos ,找了半天发现是float精度不够,最后自己重写ScreenToWorldPoint来解决
public Vector3 ScreenToWorldPoint(Vector3 screenPos) {
double screenX = screenPos.x;
double screenY = screenPos.y;
double screenZ = screenPos.z;
Matrix4x4 pMatrix = mMapCamera.projectionMatrix;
double px = screenX / Screen.width;
px = (px - 0.5f) * 2f;
double py = screenY / Screen.height;
py = (py - 0.5f) * 2f;
double pz = (-screenZ - pMatrix.m23) / pMatrix.m22;
double pw = screenZ;
px *= pw;
py *= pw;
Matrix4x4 pInverseMatrix = mMapCamera.projectionMatrix.inverse;
double vx = (pInverseMatrix.m00 * px + pInverseMatrix.m01 * py + pInverseMatrix.m02 * pz + pInverseMatrix.m03 * pw);
double vy = (pInverseMatrix.m10 * px + pInverseMatrix.m11 * py + pInverseMatrix.m12 * pz + pInverseMatrix.m13 * pw);
double vz = (pInverseMatrix.m20 * px + pInverseMatrix.m21 * py + pInverseMatrix.m22 * pz + pInverseMatrix.m23 * pw);
Matrix4x4 vInverseMatrix = mMapCamera.worldToCameraMatrix.inverse;
double x = (vInverseMatrix.m00 * vx + vInverseMatrix.m01 * vy + vInverseMatrix.m02 * vz + vInverseMatrix.m03 * 1);
double y = (vInverseMatrix.m10 * vx + vInverseMatrix.m11 * vy + vInverseMatrix.m12 * vz + vInverseMatrix.m13 * 1);
double z = (vInverseMatrix.m20 * vx + vInverseMatrix.m21 * vy + vInverseMatrix.m22 * vz + vInverseMatrix.m23 * 1);
return new Vector3((float)(x), (float)(y), (float)(z));
}
public Vector3 WorldToScreenPoint(Vector3 worldPos) {
double worldX = worldPos.x;
double worldY = worldPos.y;
double worldZ = worldPos.z;
Matrix4x4 vMatrix = mMapCamera.worldToCameraMatrix;
double vx = (vMatrix.m00 * worldX + vMatrix.m01 * worldY + vMatrix.m02 * worldZ + vMatrix.m03 * 1);
double vy = (vMatrix.m10 * worldX + vMatrix.m11 * worldY + vMatrix.m12 * worldZ + vMatrix.m13 * 1);
double vz = (vMatrix.m20 * worldX + vMatrix.m21 * worldY + vMatrix.m22 * worldZ + vMatrix.m23 * 1);
Matrix4x4 pMatrix = mMapCamera.projectionMatrix;
double px = (pMatrix.m00 * vx + pMatrix.m01 * vy + pMatrix.m02 * vz + pMatrix.m03 * 1);
double py = (pMatrix.m10 * vx + pMatrix.m11 * vy + pMatrix.m12 * vz + pMatrix.m13 * 1);
double pz = (pMatrix.m20 * vx + pMatrix.m21 * vy + pMatrix.m22 * vz + pMatrix.m23 * 1);
double pw = (pMatrix.m30 * vx + pMatrix.m31 * vy + pMatrix.m32 * vz + pMatrix.m33 * 1);
double x = px / pw;
double y = py / pw;
x = (x * 0.5) + 0.5;
y = (y * 0.5) + 0.5;
return new Vector3((float)(x * Screen.width), (float)(y * Screen.height), (float)(-vz));
}
|