private void CalculateNormals(List <DxsPoly> polyList, NormalGeneration normalOptions, double creaseAngle) { if (normalOptions == NormalGeneration.None) { return; } double cosAngle = System.Math.Cos(MathHelper.DegreesToRadians * creaseAngle); //Loop through each polygon for (int i = 0; i < polyList.Count; i++) { DxsPoly poly = polyList[i]; //For each vertex for (int j = 0; j < 3; j++) { DxsVertex vert = poly.Vertices[j]; int vid = vert.ID; Vector3 smoothNormal = poly.FaceNormal; if (normalOptions != NormalGeneration.Face) { //Find every poly that shares this vertex for (int k = 0; k < polyList.Count; k++) { if (i != k) { DxsPoly otherPoly = polyList[k]; for (int m = 0; m < 3; m++) { DxsVertex otherVertex = otherPoly.Vertices[m]; int otherVid = otherVertex.ID; //Other poly uses this vertex position, then take their normal into account if (vid == otherVid) { if (normalOptions == NormalGeneration.Crease) { if (Vector3.Dot(poly.FaceNormal, otherPoly.FaceNormal) > cosAngle) { smoothNormal += otherPoly.FaceNormal; } } else { smoothNormal += otherPoly.FaceNormal; } } } } } } smoothNormal.Normalize(); vert.Normal = smoothNormal; poly.Vertices[j] = vert; } } }
public void CompileMeshes(NormalGeneration normalOptions, double creaseAngle, bool swapWinding) { foreach (KeyValuePair <int, List <DxsPoly> > kv in _polys) { List <DxsPoly> polyList = kv.Value; if (polyList.Count > 0) { DxsMesh mesh = new DxsMesh(polyList, normalOptions, creaseAngle, swapWinding); _compiledMeshes.Add(mesh); } } }
public DxsMesh(List <DxsPoly> polyList, NormalGeneration normalOptions, double creaseAngle, bool swapWinding) { _matID = polyList[0].MaterialID; _indices = new DataBuffer <int>(polyList.Count * 3); _vertList = new List <DxsVertex>(); if (swapWinding) { foreach (DxsPoly poly in polyList) { DxsVertex temp = poly.Vertices[0]; poly.Vertices[0] = poly.Vertices[2]; poly.Vertices[2] = temp; } } CalculateNormals(polyList, normalOptions, creaseAngle); //Get all the unique vertices and build the index list foreach (DxsPoly poly in polyList) { for (int i = 0; i < 3; i++) { DxsVertex vertex = poly.Vertices[i]; int index; if (IsUniqueVertex(_vertList, vertex, out index)) { _vertList.Add(vertex); index = _vertList.Count - 1; } _indices.Set(index); } } Vector3 center = GetCenter(); for (int i = 0; i < _vertList.Count; i++) { DxsVertex v = _vertList[i]; v.Position = v.Position - center; _vertList[i] = v; } _translation = center; }
/// <summary> /// Deserializes the object and populates it from the input. /// </summary> /// <param name="input">Savable input</param> public override void Read(ISavableReader input) { base.Read(input); _normalGen = input.ReadEnum <NormalGeneration>(); _creaseAngle = input.ReadSingle(); _generateTangentBasis = input.ReadBoolean(); _swapWindingOrder = input.ReadBoolean(); _flipUVs = input.ReadBoolean(); _scale = input.ReadSingle(); _xAngle = input.ReadSingle(); _yAngle = input.ReadSingle(); _zAngle = input.ReadSingle(); _userMaterialFile = input.ReadString(); _materialNamesCorrespondToGeometry = input.ReadBoolean(); _preferLitMaterials = input.ReadBoolean(); _importLights = input.ReadBoolean(); _imageParameters = input.ReadSavable <ImageLoaderParameters>(); _texturePath = input.ReadString(); }
/// <summary> /// Initializes a new ModelLoaderParameter class set to parameter defaults. /// </summary> public ModelLoaderParameters() { _swapWindingOrder = false; _flipUVs = false; _xAngle = 0; _yAngle = 0; _zAngle = 0; _normalGen = NormalGeneration.None; _creaseAngle = 66.0f; _generateTangentBasis = false; _imageParameters = new ImageLoaderParameters(); _preferLitMaterials = true; _texturePath = null; _userMaterialFile = null; _materialNamesCorrespondToGeometry = false; _importLights = false; }