private void BackfaceCulling() { foreach (Triangle triangle in Triangles) { Vector3 n = triangle.SurfaceNormal().Normalize(); triangle.IsBackFaced = Math.Round(n.Z, 1) >= 0; } //Logger.Value("culled triangles", Triangles.Count(x => x.IsBackFaced)); Logger.Value("valid triangles", Triangles.Count(x => !x.IsBackFaced)); }
private void InitLazyProps() { this._ModelData = new Lazy <ModelBase>(() => _ModelDataInitializer()); this._Vertices = new Lazy <Vertex[]>(() => Triangles.SelectMany(t => t).Union(new Vertex[0]).ToArray()); this._Textures = new Lazy <Texture[]>(() => Triangles.Select(t => t.Texture).Union(new Texture[0]).ToArray()); this._Joints = new Lazy <Joint[]>(() => RootJoint == null ? new Joint[0] : RootJoint.GetSelfAndDescendents().ToArray()); this._Animations = new Lazy <AnimationSequence[]>(() => Joints.Select(j => j.Animation).ToArray()); this._IsVisible = new Lazy <bool>(() => Textures.Where(t => ExcludeTexturePrefixes.Count(v => t.Name.StartsWith(v)) == 0).Count() != 0); this._IsAnimated = new Lazy <bool>(() => Animations.Count() != 0 && NumAnimationKeyFrames != 0); this._HasMultipleTextures = new Lazy <bool>(() => Textures.Count() > 1); this._HasGeometry = new Lazy <bool>(() => Triangles.Count() != 0); this._HasNormals = new Lazy <bool>(() => HasGeometry && Vertices.First().Normal != Vector4.Zero); this._HasTangents = new Lazy <bool>(() => HasGeometry && Vertices.First().Tangent != Vector4.Zero); this._HasBinormals = new Lazy <bool>(() => HasGeometry && Vertices.First().Binormal != Vector4.Zero); this._HasSkeleton = new Lazy <bool>(() => RootJoint != null); this._NumJoints = new Lazy <int>(() => Joints.Count()); this._NumAnimationFrames = new Lazy <int>(() => Animations.Select(a => a.NumFrames).Union(new[] { 0 }).Max()); this._NumAnimationKeyFrames = new Lazy <int>(() => Animations.Select(anim => anim.Frames.Select(f => f.FrameNum)).Aggregate((a, v) => a.Union(v)).Count()); }
public void Rebuild(float min) { float minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew; // 1, 2, 3, 4 oct Vector3 center = new Vector3((SpaceCube.x1.X + SpaceCube.x2.X) * 0.5f, (SpaceCube.x1.Y + SpaceCube.x4.Y) * 0.5f, (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f); minXnew = (SpaceCube.x1.X + SpaceCube.x2.X) * 0.5f; maxXnew = SpaceCube.x1.X; minYnew = (SpaceCube.x1.Y + SpaceCube.x4.Y) * 0.5f; maxYnew = SpaceCube.x1.Y; minZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; maxZnew = SpaceCube.x1.Z; if (Triangles.Count() < 100 || (maxXnew - minXnew) < min) { return; } DaughterNodes[0] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); maxXnew = (SpaceCube.x1.X + SpaceCube.x2.X) * 0.5f; minXnew = SpaceCube.x2.X; minYnew = (SpaceCube.x1.Y + SpaceCube.x4.Y) * 0.5f; maxYnew = SpaceCube.x1.Y; minZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; maxZnew = SpaceCube.x1.Z; DaughterNodes[1] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); maxXnew = (SpaceCube.x1.X + SpaceCube.x2.X) * 0.5f; minXnew = SpaceCube.x2.X; maxYnew = (SpaceCube.x2.Y + SpaceCube.x3.Y) * 0.5f; minYnew = SpaceCube.x3.Y; minZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; maxZnew = SpaceCube.x1.Z; DaughterNodes[2] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); minXnew = (SpaceCube.x3.X + SpaceCube.x4.X) * 0.5f; maxXnew = SpaceCube.x4.X; maxYnew = (SpaceCube.x1.Y + SpaceCube.x4.Y) * 0.5f; minYnew = SpaceCube.x4.Y; minZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; maxZnew = SpaceCube.x1.Z; DaughterNodes[3] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); // 5, 6, 7, 8 oct minXnew = (SpaceCube.x1.X + SpaceCube.x2.X) * 0.5f; maxXnew = SpaceCube.x1.X; minYnew = (SpaceCube.x1.Y + SpaceCube.x4.Y) * 0.5f; maxYnew = SpaceCube.x1.Y; maxZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; minZnew = SpaceCube.x5.Z; DaughterNodes[4] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); maxXnew = (SpaceCube.x1.X + SpaceCube.x2.X) * 0.5f; minXnew = SpaceCube.x2.X; minYnew = (SpaceCube.x1.Y + SpaceCube.x4.Y) * 0.5f; maxYnew = SpaceCube.x1.Y; maxZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; minZnew = SpaceCube.x5.Z; DaughterNodes[5] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); maxXnew = (SpaceCube.x1.X + SpaceCube.x2.X) * 0.5f; minXnew = SpaceCube.x2.X; maxYnew = (SpaceCube.x2.Y + SpaceCube.x3.Y) * 0.5f; minYnew = SpaceCube.x3.Y; maxZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; minZnew = SpaceCube.x5.Z; DaughterNodes[6] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); minXnew = (SpaceCube.x3.X + SpaceCube.x4.X) * 0.5f; maxXnew = SpaceCube.x4.X; maxYnew = (SpaceCube.x1.Y + SpaceCube.x4.Y) * 0.5f; minYnew = SpaceCube.x4.Y; maxZnew = (SpaceCube.x1.Z + SpaceCube.x5.Z) * 0.5f; minZnew = SpaceCube.x5.Z; DaughterNodes[7] = new TreeNode(minXnew, maxXnew, minYnew, maxYnew, minZnew, maxZnew); List <Triangle> newVectors = new List <Triangle>(); while (Triangles.Count() != 0) { int counter1 = -1; int counter2 = -1; int counter3 = -1; Triangle i = Triangles.Last(); Triangles.RemoveAt(Triangles.Count() - 1); counter1 = PositionFinder(i.N0, center); counter2 = PositionFinder(i.N1, center); counter3 = PositionFinder(i.N2, center); if (counter1 == counter2 && counter2 == counter3) { DaughterNodes[counter1].Triangles.Add(i); } else { newVectors.Add(i); } } Triangles = newVectors; List = false; DaughterNodes[0].List = true; DaughterNodes[0].Rebuild(min); DaughterNodes[1].List = true; DaughterNodes[1].Rebuild(min); DaughterNodes[2].List = true; DaughterNodes[2].Rebuild(min); DaughterNodes[3].List = true; DaughterNodes[3].Rebuild(min); DaughterNodes[4].List = true; DaughterNodes[4].Rebuild(min); DaughterNodes[5].List = true; DaughterNodes[5].Rebuild(min); DaughterNodes[6].List = true; DaughterNodes[6].Rebuild(min); DaughterNodes[7].List = true; DaughterNodes[7].Rebuild(min); }