def TransformPlane(plane, mat): from Vector3Math import Vector3 from Vector4Math import Vector4 from MatrixMath import Matrix o = Vector4(plane.a * plane.d, plane.b * plane.d, plane.c * plane.d, 1.0) n = (plane.a, plane.b, plane.c, 0.0) o.Transform(mat) mati = Matrix(mat) mati.Inverse() mat.Transpose() n.Transform(mati) on = Vector3(o.x, o.y, o.z) nn = Vector3(n.x, n.y, n.z) return Plane(n.x, n.y, n.z, on.Dot(nn))
def __init__(self, *args): from Vector3Math import Vector3 from MatrixMath import Matrix self.UseTransform = False self.TransformMatrix = Matrix.New() self.TransformMatrixInverse = Matrix.New() if len(args) == 6: self.Vertex = [Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5])] elif len(args) == 1: if isinstance(args[0], BoundingBox): self.Vertex = [Vector3(args[0].Vertex[0]), Vector3(args[0].Vertex[1])] self.UseTransform = args[0].UseTransform self.TransformMatrix = args[0].TransformMatrix self.TransformMatrixInverse = args[0].TransformMatrixInverse elif len(args) == 2: if isinstance(args[0], Vector3) and isinstance(args[1], Vector3): self.Vertex = [args[0], args[1]] else: self.Vertex = [Vector3(1.7976931348623157e+308, 1.7976931348623157e+308, 1.7976931348623157e+308), Vector3(-1.7976931348623157e+308, -1.7976931348623157e+308, -1.7976931348623157e+308)]
def Clear(): from Vector3Math import Vector3 from MatrixMath import Matrix from PlaneMath import Plane self.ViewProjMatrixCacheEnabled = False self.FarNearDisabled = False self.ViewProjMatrixCache = Matrix() self.HexahedronPlane = [ Plane.New(), Plane.New(), Plane.New(), Plane.New(), Plane.New(), Plane.New() ] self.FrustumVertex = [Vector3.New(), Vector3.New(), Vector3.New(), Vector3.New(),\ Vector3.New(), Vector3.New(), Vector3.New(), Vector3.New()] self.FrustumVertexsource = [Vector3(1,1,0), Vector3(-1,1,0), Vector3(-1,-1,0), Vector3(1,-1,0),\ Vector3(1,1,1), Vector3(-1,1,1), Vector3(-1,-1,1), Vector3(1,-1,1)] self.FrustumID = 0
def Zero(self): self.Vertex = [Vector3.New(), Vector3.New()] self.UseTransform = False self.TransformMatrix = Matrix.New() self.TransformMatrixInverse = Matrix.New()
def ExtractFromProjectMatrix(self, viewProj, viewProjInverse): from MatrixMath import Matrix if ViewProjMatrixCacheEnabled: if ViewProjMatrixCache == viewProj: return self.FrustumID += 1 self.ViewProjMatrixCacheEnabled = True selfViewProjMatrixCache = Matrix(viewProj) for i in range(0, 8): self.FrustumVertex[i] = Vector3.TransformCoord( self.FrustumVertexsource[i], viewProjInverse) self.HexahedronPlane[0] = Plane(viewProj.m[0][3] + viewProj.m[0][0],\ viewProj.m[1][3] + viewProj.m[1][0],\ viewProj.m[2][3] + viewProj.m[2][0],\ viewProj.m[3][3] + viewProj.m[3][0]) self.HexahedronPlane[0].Normalize() self.HexahedronPlane[1] = Plane(viewProj.m[0][3] - viewProj.m[0][0],\ viewProj.m[1][3] - viewProj.m[1][0],\ viewProj.m[2][3] - viewProj.m[2][0],\ viewProj.m[3][3] - viewProj.m[3][0]) self.HexahedronPlane[1].Normalize() #// index bug (code mistake) fix 2015.09.03 self.HexahedronPlane[2] = Plane( viewProj.m[0][3] - viewProj.m[0][1],\ viewProj.m[1][3] - viewProj.m[1][1],\ viewProj.m[2][3] - viewProj.m[2][1],\ viewProj.m[3][3] - viewProj.m[3][1]) self.HexahedronPlane[2].Normalize() self.HexahedronPlane[3] = Plane(viewProj.m[0][3] + viewProj.m[0][1],\ viewProj.m[1][3] + viewProj.m[1][1],\ viewProj.m[2][3] + viewProj.m[2][1],\ viewProj.m[3][3] + viewProj.m[3][1]) self.HexahedronPlane[3].Normalize() self.HexahedronPlane[4] = Plane(viewProj.m[0][2], viewProj.m[1][2], viewProj.m[2][2], viewProj.m[3][2]) self.HexahedronPlane[4].Normalize() self.HexahedronPlane[5] = Plane( viewProj.m[0][3] - viewProj.m[0][2],\ viewProj.m[1][3] - viewProj.m[1][2],\ viewProj.m[2][3] - viewProj.m[2][2],\ viewProj.m[3][3] - viewProj.m[3][2]) self.HexahedronPlane[5].Normalize() def IsPointInbound(self, x, y, z): from Vector3Math import Vector3 from Util import MathUtil p = Vector3(x, y, z) if self.FarNearDisabled: if MathUtil.PlanePointDistance(self.HexahedronPlane[0], p) < 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[1], p) < 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[2], p) < 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[3], p) < 0: return True return False else: if (MathUtil.PlanePointDistance(self.HexahedronPlane[0], p) < 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[1], p) < 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[2], p) < 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[3], p) < 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[4], p)< 0 and\ MathUtil.PlanePointDistance(self.HexahedronPlane[5], p) < 0): return True return False
def MakeObjectFrustum(self, viewPos, objectBox): from Vector3Math import Vector3 from BoundingBoxMath import BoundingBox from Util import MathUtil from MatrixMath import Matrix pos = objectBox.GetCenter() dist = pos - viewPos distance = dist.Length() ydir = Vector3(0, 1, 0) posTop = pos + distance * Vector3(0, 1, 0) #//Vector3 posBottom = pos + distance * Vector3(0,-1,0); leftdir = Vector3.New() leftdir.Cross(dist) #//NXVec3Cross(&leftdir, &dist, &ydir); // right handed posLeft = pos + leftdir #//Vector3 posRight = pos - leftdir; viewMatrix = Matrix.New() #//NXMatrixLookAtLH(&viewMatrix, &viewPos, &pos, &ydir); viewMatrix = Matrix.LookAtLH(viewPos, pos, ydir) sumBox = BoundingBox.New() #//objectbox.Transform(buffer8, viewMatrix, NULL); sumBox, tv0, tv1, tv2, tv3, tv4, tv5, tv6, tv7 = objectBox.Transform( viewMatrix) buffer8 = [tv0, tv1, tv2, tv3, tv4, tv5, tv6, tv7] bv0 = Vector3(objectBox.Vertex[0]) bv1 = Vector3(objectBox.Vertex[1]) #// original reffere v0 = [Vector3( bv0.x, bv0.y, bv0.z ),\ Vector3( bv1.x, bv0.y, bv0.z ),\ Vector3( bv0.x, bv1.y, bv0.z ),\ Vector3( bv1.x, bv1.y, bv0.z ),\ Vector3( bv0.x, bv0.y, bv1.z ),\ Vector3 (bv1.x, bv0.y, bv1.z ),\ Vector3( bv0.x, bv1.y, bv1.z ),\ Vector3 (bv1.x, bv1.y, bv1.z )] topIndex = 0 max = -1.7976931348623157e+308 for i in range(0, 8): if buffer8[i].y > max: max = buffer8[i].y topIndex = i bottomIndex = 0 max = 1.7976931348623157e+308 for i in range(0, 8): if buffer8[i].y < max: max = buffer8[i].y bottomIndex = i leftIndex = 0 #//## 확인 사항 max = 1.7976931348623157e+308 for i in range(0, 8): if buffer8[i].x < max: max = buffer8[i].x leftIndex = i rightIndex = 0 #//## 확인 사항 max = -1.7976931348623157e+308 for i in range(0, 8): if buffer8[i].x > max: max = buffer8[i].x rightIndex = i self.HexahedronPlane[2] = MathUtil.GetPlaneFromPolygon( viewPos, posLeft, v0[topIndex]) self.HexahedronPlane[3] = MathUtil.GetPlaneFromPolygon( viewPos, posLeft, v0[bottomIndex]) self.HexahedronPlane[0] = MathUtil.GetPlaneFromPolygon( viewPos, posTop, v0[leftIndex]) self.HexahedronPlane[1] = MathUtil.GetPlaneFromPolygon( viewPos, posTop, v0[rightIndex]) self.FarNearDisabled = False return True