/// <summary> /// Creates mesh and material for this enemy. /// </summary> /// <param name="dfUnity">DaggerfallUnity singleton. Required for content readers and settings.</param> /// <param name="archive">Texture archive index derived from type and gender.</param> private void AssignMeshAndMaterial(DaggerfallUnity dfUnity, int archive) { // Get mesh filter if (meshFilter == null) { meshFilter = GetComponent <MeshFilter>(); } // Vertices for a 1x1 unit quad // This is scaled to correct size depending on facing and orientation float hx = 0.5f, hy = 0.5f; Vector3[] vertices = new Vector3[4]; vertices[0] = new Vector3(hx, hy, 0); vertices[1] = new Vector3(-hx, hy, 0); vertices[2] = new Vector3(hx, -hy, 0); vertices[3] = new Vector3(-hx, -hy, 0); // Indices int[] indices = new int[6] { 0, 1, 2, 3, 2, 1, }; // Normals Vector3 normal = Vector3.Normalize(Vector3.up + Vector3.forward); Vector3[] normals = new Vector3[4]; normals[0] = normal; normals[1] = normal; normals[2] = normal; normals[3] = normal; // Create mesh Mesh mesh = new Mesh(); mesh.name = string.Format("MobileEnemyMesh"); mesh.vertices = vertices; mesh.triangles = indices; mesh.normals = normals; // Assign mesh meshFilter.sharedMesh = mesh; // Create material Material material = TextureReplacement.GetMobileBillboardMaterial(archive, GetComponent <MeshFilter>(), ref summary.ImportedTextures) ?? dfUnity.MaterialReader.GetMaterialAtlas( archive, 0, 4, 1024, out summary.AtlasRects, out summary.AtlasIndices, 4, true, 0, false, true); // Set new enemy material GetComponent <MeshRenderer>().sharedMaterial = material; }
/// <summary> /// Creates mesh and material for this enemy. /// </summary> /// <param name="dfUnity">DaggerfallUnity singleton. Required for content readers and settings.</param> /// <param name="archive">Texture archive index derived from type and gender.</param> private void AssignMeshAndMaterial(DaggerfallUnity dfUnity, int archive) { // Get mesh filter if (meshFilter == null) { meshFilter = GetComponent <MeshFilter>(); } // Vertices for a 1x1 unit quad // This is scaled to correct size depending on facing and orientation float hx = 0.5f, hy = 0.5f; Vector3[] vertices = new Vector3[4]; vertices[0] = new Vector3(hx, hy, 0); vertices[1] = new Vector3(-hx, hy, 0); vertices[2] = new Vector3(hx, -hy, 0); vertices[3] = new Vector3(-hx, -hy, 0); // Indices int[] indices = new int[6] { 0, 1, 2, 3, 2, 1, }; // Normals Vector3 normal = Vector3.Normalize(Vector3.up + Vector3.forward); Vector3[] normals = new Vector3[4]; normals[0] = normal; normals[1] = normal; normals[2] = normal; normals[3] = normal; // Create mesh Mesh mesh = new Mesh(); mesh.name = string.Format("MobileEnemyMesh"); mesh.vertices = vertices; mesh.triangles = indices; mesh.normals = normals; // Assign mesh meshFilter.sharedMesh = mesh; // Create material Material material = TextureReplacement.GetMobileBillboardMaterial(archive, GetComponent <MeshFilter>(), ref summary.ImportedTextures) ?? dfUnity.MaterialReader.GetMaterialAtlas( archive, 0, 4, 1024, out summary.AtlasRects, out summary.AtlasIndices, 4, true, 0, false, true); // Update cached record values in case of non-classic texture if (summary.RecordSizes == null || summary.RecordSizes.Length == 0) { if (summary.ImportedTextures.Albedo != null && summary.ImportedTextures.Albedo.Length > 0) { int recordCount = summary.ImportedTextures.Albedo.Length; // Cache size and scale for each record summary.RecordSizes = new Vector2[recordCount]; summary.RecordFrames = new int[recordCount]; for (int i = 0; i < recordCount; i++) { // Get size and scale of this texture Texture2D firstFrame = summary.ImportedTextures.Albedo[i][0]; Vector2 size = new Vector2(firstFrame.width, firstFrame.height); // Set optional scale TextureReplacement.SetBillboardScale(archive, i, ref size); // Store final size and frame count summary.RecordSizes[i] = size * MeshReader.GlobalScale; summary.RecordFrames[i] = summary.ImportedTextures.Albedo[i].Length; } } else { Debug.LogError($"Texture archive {archive} has no valid records"); } } // Set new enemy material GetComponent <MeshRenderer>().sharedMaterial = material; }