private Geometry(RenderWareStream.Geometry geom, Mesh mesh, TextureDictionary[] textureDictionaries) { Mesh = mesh; if (geom.Skinning != null) { Mesh.boneWeights = Types.Convert(geom.Skinning.VertexBoneIndices, geom.Skinning.VertexBoneWeights); SkinToBoneMatrices = Types.Convert(geom.Skinning.SkinToBoneMatrices); } _geom = geom; _textureDictionaries = textureDictionaries; _materials = new Dictionary<MaterialFlags, UnityEngine.Material[]>(); }
private TextureDictionary(RenderWareStream.TextureDictionary txd) { _diffuse = new Dictionary<string, Texture>(StringComparer.InvariantCultureIgnoreCase); _alpha = new Dictionary<string, Texture>(StringComparer.InvariantCultureIgnoreCase); foreach (var native in txd.Textures) { var tex = new Texture(native); if (tex.IsDiffuse) { _diffuse.Add(tex.DiffuseName, tex); } if (tex.IsAlpha) { if (_alpha.ContainsKey(tex.AlphaName)) { Debug.LogWarningFormat("Tried to re-add {0} (diffuse {1} vs {2})", tex.AlphaName, tex.DiffuseName, _alpha[tex.AlphaName].DiffuseName); continue; } _alpha.Add(tex.AlphaName, tex); } } }
public GeometryFrame(RenderWareStream.Frame src, RenderWareStream.Atomic atomic) { Source = src; Name = src.Name != null ? src.Name.Value : DefaultName; ParentIndex = src.ParentIndex; GeometryIndex = atomic == null ? -1 : (int)atomic.GeometryIndex; Position = Types.Convert(src.Position); Rotation = UnityEngine.Quaternion.LookRotation(Types.Convert(src.MatrixForward), Types.Convert(src.MatrixUp)); }
private static GeometryFrame Convert(RenderWareStream.Frame src, IEnumerable<Atomic> atomics) { var atomic = atomics.FirstOrDefault(x => x.FrameIndex == src.Index); return new GeometryFrame(src, atomic); }
private static Mesh Convert(RenderWareStream.Geometry src) { var mesh = new Mesh(); // ReSharper disable ConvertClosureToMethodGroup mesh.vertices = src.Vertices.Select(x => Types.Convert(x)).ToArray(); if (src.Normals != null) { mesh.normals = src.Normals.Select(x => Types.Convert(x)).ToArray(); } if (src.Colours != null) { mesh.colors32 = src.Colours.Select(x => Types.Convert(x)).ToArray(); } if (src.TexCoords != null && src.TexCoords.Length > 0) { mesh.uv = src.TexCoords[0].Select(x => Types.Convert(x)).ToArray(); } // ReSharper restore ConvertClosureToMethodGroup if (src.Normals == null) { mesh.normals = CalculateNormals(src, mesh.vertices); } mesh.subMeshCount = src.MaterialSplits.Length; var isTriangleStrip = (src.Flags & GeometryFlag.TriangleStrips) == GeometryFlag.TriangleStrips; var subMesh = 0; foreach (var split in src.MaterialSplits) { var indices = isTriangleStrip ? FromTriangleStrip(split.FaceIndices) : split.FaceIndices; mesh.SetIndices(indices, MeshTopology.Triangles, subMesh++); } mesh.RecalculateBounds(); return mesh; }
private static UnityEngine.Material Convert(RenderWareStream.Material src, TextureDictionary[] txds, MaterialFlags flags) { LoadedTexture diffuse; LoadedTexture mask = null; var overrideAlpha = (flags & MaterialFlags.OverrideAlpha) == MaterialFlags.OverrideAlpha; var vehicle = (flags & MaterialFlags.Vehicle) == MaterialFlags.Vehicle; if (!overrideAlpha && src.Colour.A != 255) { flags |= MaterialFlags.Alpha; } if (src.TextureCount > 0) { var tex = src.Textures[0]; diffuse = txds.GetDiffuse(tex.TextureName); if (src.TextureCount > 1) { Debug.LogFormat("Something has {0} textures!", src.TextureCount); } if (diffuse == null) { Debug.LogWarningFormat("Unable to find texture {0}", tex.TextureName); } if (!string.IsNullOrEmpty(tex.MaskName)) { mask = txds.GetAlpha(tex.MaskName) ?? diffuse; } else if (vehicle) { mask = diffuse; } if (!overrideAlpha && mask != null && mask.HasAlpha) { flags |= MaterialFlags.Alpha; } } else { diffuse = WhiteTex; } var shader = GetShader(flags); var mat = new UnityEngine.Material(shader); var clr = Types.Convert(src.Colour); if (vehicle) { var found = false; for (var i = 1; i < _sKeyColors.Length; ++i) { var key = _sKeyColors[i]; if (key.r != clr.r || key.g != clr.g || key.b != clr.b) continue; mat.SetInt(CarColorIndexId, i); found = true; break; } if (found) { mat.color = Color.white; } else { mat.color = clr; } } else { mat.color = clr; } if (diffuse != null) mat.SetTexture(MainTexId, diffuse.Texture); if (mask != null) mat.SetTexture(MaskTexId, mask.Texture); mat.SetFloat(MetallicId, src.Specular); mat.SetFloat(SmoothnessId, src.Smoothness); return mat; }
private static UnityEngine.Vector3[] CalculateNormals(RenderWareStream.Geometry src, UnityEngine.Vector3[] verts) { var norms = new UnityEngine.Vector3[src.VertexCount]; for (var i = 0; i < src.FaceCount; ++i) { var face = src.Faces[i]; var a = verts[face.Vertex0]; var b = verts[face.Vertex1]; var c = verts[face.Vertex2]; var v = b - a; var w = c - b; var norm = new UnityEngine.Vector3( v.y * w.z - v.z * w.y, v.z * w.x - v.x * w.z, v.x * w.y - v.y * w.x).normalized; norms[face.Vertex0] -= norm; norms[face.Vertex1] -= norm; norms[face.Vertex2] -= norm; } for (var i = 0; i < src.VertexCount; ++i) { if (norms[i].sqrMagnitude <= 0f) { norms[i] = UnityEngine.Vector3.up; } else { norms[i] = norms[i].normalized; } } return norms; }