using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System;
using System.IO;
using System.Text;
public class DFFExtention : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedassets, string[] deletedasssets, string[] movedAssets, string[] movedFromAssetPaths)
{
if (importedassets.Length == 0) return;
for(int i = 0; i < importedassets.Length; i++)
{
if (!importedassets*.EndsWith("dff")) return;*
int progress = 0;
EditorUtility.DisplayProgressBar(“Importing DFF File…”, "Processing: " + importedassets*, progress / 100);*
LoadDFF(importedassets*);*
progress = 100;
EditorUtility.DisplayProgressBar(“Importing DFF File…”, "Processing: " + importedassets*, progress / 100);*
EditorUtility.ClearProgressBar();
}
}
public static BinaryReader br;
static void LoadDFF(string path)
{
List frames = new List();
List vertexColors = new List();
List uvCoordinates = new List();
List facesInfo = new List();
List verticies = new List();
List normals = new List();
List materials = new List();
FileStream binaryFile = null;
try
{
binaryFile = new FileStream(path, FileMode.Open);
}
catch
{
return;
}
if (!binaryFile.Name.EndsWith(“dff”))
{
return;
}
br = new BinaryReader(binaryFile);
bool hasNormals_16 = false;
br.BaseStream.Position = 0x18;
int meshCount = 0;
//Object count
{
br.ReadInt32();
br.ReadInt32();
br.ReadInt32();
}
br.BaseStream.Position = 0x3C;
//Add frames
{
AddFrames(frames);
}
DebugLog(br.BaseStream.Position.ToString(“x”));
br.BaseStream.Position += 24;
DebugLog(br.BaseStream.Position.ToString(“x”));
//Frame name
{
//string filen = oFile.SafeFileName.Remove(oFile.SafeFileName.Length - 4, 4);
for (int x = 0; x < frames.Count; x++)
{
List n = new List();
for (int i = 0; i < 100; i++)
{
if (br.PeekChar() == 0 && n.Count >= 1)
{
break;
}
n.Add(br.ReadChar());
}
string frameName = new string(n.ToArray());
frames[x].name = frameName;
br.BaseStream.Position += 24;
}
}
br.BaseStream.Position–;
//Mesh count
{
meshCount = br.ReadInt32();
DebugLog("/y Mesh count: " + meshCount);
}
br.BaseStream.Position += 24;
int triangleC = 0;
int vertexC = 0;
try
{
//Mesh info
{
int i = br.ReadInt16();
string s = Convert(i);
if (s[5] == 1)
{
//isTriangleStrip_1 = true;
}
if (s[4] == 1)
{
//vertexTranslation_2 = true;
}
if (s[3] == 1)
{
//textures_4 = true;
}
if (s[2] == 1)
{
//vertexColors_8 = true;
}
if (s[1] == 1)
{
hasNormals_16 = true;
}
if (s[0] == 1)
{
//modulateMaterialColor_64 = true;
}
br.ReadByte();
br.ReadByte();
triangleC = br.ReadInt32();
vertexC = br.ReadInt32();
br.ReadInt32();
//Vertex colors!
try
{
for (int l = 0; l < vertexC; l++)
{
Color c = HexToRGB(“” + br.ReadUInt32().ToString(“x”));
vertexColors.Add(c);
}
}
catch
{
binaryFile.Close();
File.Delete(path);
return;
}
DebugLog(“/g Added " + vertexColors.Count + " colors to list.”);
//Uvs!
try
{
for (int l = 0; l < vertexC; l++)
{
Vector2 uv = new Vector2();
uv.x = br.ReadSingle();
uv.y = br.ReadSingle();
uvCoordinates.Add(uv);
}
}
catch
{
binaryFile.Close();
File.Delete(path);
return;
}
DebugLog(“/g Added " + uvCoordinates.Count + " uv coordinates.”);
//Faces!
try
{
for (int l = 0; l < triangleC; l++)
{
facesInfo.Add(new IndicesAtt(br.ReadUInt16(), br.ReadUInt16(), br.ReadUInt16(), br.ReadUInt16()));
}
}
catch
{
binaryFile.Close();
File.Delete(path);
return;
}
DebugLog(“/g Added " + facesInfo.Count + " faces.”);
Vector3 boundingSphere = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
float radius = br.ReadSingle();
br.ReadInt32(); //has normals
br.ReadInt32(); //has
//Vertex!
try
{
for (int l = 0; l < vertexC; l++)
{
Vector3 pos = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
verticies.Add(pos);
}
DebugLog(“/g Added " + verticies.Count + " vertex!”);
DebugLog(boundingSphere.ToString() + " radius of " + radius);
if (hasNormals_16)
{
for (int l = 0; l < vertexC; l++)
{
Vector3 pos = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
normals.Add(pos);
}
}
DebugLog(“/g Added " + normals.Count + " normals!”);
}
catch
{
binaryFile.Close();
File.Delete(path);
return;
}
}
br.BaseStream.Position += 24;
//Materials
{
int mc = br.ReadInt32();
DebugLog("/y Material count " + mc);
for (int i = 0; i < mc; i++)
{
try
{
if (br.ReadInt32() == -1)
{
materials.Add(new Material(Shader.Find(“Bumped Diffuse”)));
}
else
{
materials.Add(null);
}
}
catch
{
binaryFile.Close();
File.Delete(path);
return;
}
}
}
}
catch
{
binaryFile.Close();
File.Delete(path);
return;
}
string textureName = “”;
br.BaseStream.Position += 24;
try
{
//Material proprieties
{
for (int x = 0; x < materials.ToArray().Length; x++)
{
br.ReadInt32();
string s = br.ReadUInt32().ToString(“x”);
foreach (Material m in materials)
{
if (m != null)
{
Color c = HexToRGB(“” + s);
//m.color = c;
}
}
bool textured = false;
br.ReadUInt32();
if (br.ReadUInt32() >= 1)
{
textured = true;
}
br.ReadSingle(); // ambient
br.ReadSingle(); // specular
br.ReadSingle(); // diffuse
br.BaseStream.Position += 40;
List n = new List();
for (int i = 0; i < 100; i++)
{
if (br.PeekChar() == 0 && n.Count >= 1)
{
break;
}
n.Add(br.ReadChar());
}
textureName = new string(n.ToArray());
textureName = RemoveSpecialCharacters(textureName);
/*System.Drawing.Bitmap b = new System.Drawing.Bitmap(System.Drawing.Image.FromFile(“C:/Users/Hal/Desktop/GTA Models/tgas/” + textureName + “.tga”));
Texture2D texture = new Texture2D(b.Width, b.Height);
for(int xx = 0; xx < b.Width; xx++)
{
for (int yy = 0; yy < b.Height; yy++)
{
texture.SetPixel(xx, yy, new Color(b.GetPixel(xx, yy).R, b.GetPixel(xx, yy).G, b.GetPixel(xx, yy).B, b.GetPixel(xx, yy).A));
}
}*/
Texture2D texture = /AssetDatabase.LoadAssetAtPath(“Assets/Textures/” + textureName + “.tga”, typeof(Texture2D)) as Texture2D/ null;
//Texture texture = Resources.Load(textureName) as Texture;
try
{
materials[x].SetTexture(0, texture);
}
catch
{
}
br.BaseStream.Position += 2 + 62;
//br.BaseStream.Position += 52;
}
}
br.BaseStream.Position += 24;
br.ReadInt32().ToString(“x”); // filter flags
for (int i = 0; i < meshCount; i++)
{
if (frames*.name.Length <= 2)*
return;
Mesh m = new Mesh();
m.vertices = verticies.ToArray();
//m.colors = vertexColors.ToArray();
List<List> triangles = new List<List>();
foreach (Material ma in materials)
{
List tris = new List();
foreach (IndicesAtt f in facesInfo)
{
if (f.attribute == materials.IndexOf(ma))
{
tris.Add(f.index3);
tris.Add(f.index2);
tris.Add(f.index1);
}
}
triangles.Add(tris);
}
try
{
//m.SetIndices(indecies.ToArray(), MeshTopology.Triangles, 3);
foreach (List e in triangles)
{
m.subMeshCount++;
m.SetTriangles(e.ToArray(), triangles.IndexOf(e));
}
}
catch { }
m.uv = uvCoordinates.ToArray();
m.RecalculateBounds();
m.RecalculateNormals();
/*int si = 0;
for (int mi = 0; mi < mr.sharedMaterials.Length; mi++)
{
mr.sharedMaterials[mi].SetTextureScale(“_MainTex”, new Vector2(-1, -1));
try
{
AssetDatabase.CreateAsset(mr.sharedMaterials[mi], path.Remove(path.Length - 3, 3) +“/Materials/” + RemoveSpecialCharacters(frames*.name) + si + “.mat”);*
AssetDatabase.SaveAssets();
mr.sharedMaterials[mi] = AssetDatabase.LoadAssetAtPath(path.Remove(path.Length - 3, 3) + “/Materials/” + RemoveSpecialCharacters(frames*.name) + si + “.mat”, typeof(Material)) as Material;*
}
catch
{
}
si++;
}
go.transform.Rotate(new Vector3(-90, 0, 0));*/
AssetDatabase.CreateAsset(m, path.Remove(path.Length - 3, 3) + “.asset”);
AssetDatabase.SaveAssets();
binaryFile.Close();
AssetDatabase.Refresh();
}
}
catch
{
binaryFile.Close();
File.Delete(path);
AssetDatabase.Refresh();
return;
}
binaryFile.Close();
File.Delete(path);
AssetDatabase.Refresh();
}
public static string RemoveSpecialCharacters(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if ((str >= ‘0’ && str <= ‘9’)
|| (str >= ‘A’ && str <= ‘z’
|| (str == ‘.’ || str == ‘_’)))
{
sb.Append(str*);*
}
}
return sb.ToString();
}
public static Color HexToRGB(string color)
{
var finalColor = new Color();
try
{
var red = (HexToInt(color[1]) + HexToInt(color[0]) * 16.000) / 255;
var green = (HexToInt(color[3]) + HexToInt(color[2]) * 16.000) / 255;
var blue = (HexToInt(color[5]) + HexToInt(color[4]) * 16.000) / 255;
finalColor.r = (float)red;
finalColor.g = (float)green;
finalColor.b = (float)blue;
finalColor.a = 1;
}
catch
{
finalColor = Color.white;
}
return finalColor;
}
public static int HexToInt(char hexChar)
{
string hex = “” + hexChar;
switch (hex)
{
case “0”: return 0;
case “1”: return 1;
case “2”: return 2;
case “3”: return 3;
case “4”: return 4;
case “5”: return 5;
case “6”: return 6;
case “7”: return 7;
case “8”: return 8;
case “9”: return 9;
case “A”: return 10;
case “B”: return 11;
case “C”: return 12;
case “D”: return 13;
case “E”: return 14;
case “F”: return 15;
}
return 0;
}
public static string Convert(int x)
{
char[] bits = new char[32];
int i = 0;
try
{
while (x != 0)
{
bits[i++] = (x & 1) == 1 ? ‘1’ : ‘0’;
x >>= 1;
}
}
catch
{
}
Array.Reverse(bits, 0, i);
return new string(bits);
}
public static void AddFrames(List frames)
{
int framesc = br.ReadInt32();
for (int i = 0; i < framesc; i++)
{
Frame frame = new Frame();
for (int x = 0; x < 3; x++)
{
frame.rotationMatrix.x[x] = br.ReadSingle();
}
for (int x = 0; x < 3; x++)
{
frame.rotationMatrix.y[x] = br.ReadSingle();
}
for (int x = 0; x < 3; x++)
{
frame.rotationMatrix.z[x] = br.ReadSingle();
}
frame.position.x = br.ReadSingle();
frame.position.y = br.ReadSingle();
frame.position.z = br.ReadSingle();
frame.currentFrameIndex = br.ReadInt32();
frame.matrixCreatingFlags = br.ReadInt32();
frames.Add(new Frame());
}
}
public static void DebugLog(string s)
{
// Debug.Log(s);
}
}
public class IndicesAtt
{
public int index1;
public int index2;
public int attribute;
public int index3;
public IndicesAtt(int i1, int i2, int att, int i3)
{
index1 = i1;
index2 = i2;
index3 = i3;
attribute = att;
}
}
public class Frame
{
public RotationMatrix rotationMatrix = new RotationMatrix();
public Position position = new Position();
public int currentFrameIndex;
public int matrixCreatingFlags;
public string name { get; set; }
}
public class RotationMatrix
{
public Single[] x = new Single[3];
public Single[] y = new Single[3];
public Single[] z = new Single[3];
public RotationMatrix()
{
x = new Single[3] { 0, 0, 0 };
y = new Single[3] { 0, 0, 0 };
z = new Single[3] { 0, 0, 0 };
}
public RotationMatrix(Single[] x, Single[] y, Single[] z)
{
this.x = x;
this.y = y;
this.z = z;
}
public override string ToString()
{
string finals = “”;
for (int i = 0; i < 3; i++)
{
finals += "( " + x + " , " + y + " , " + z + " )";
}
return finals;
}
}
public class Position
{
public Single x;
public Single y;
public Single z;
public Position()
{
x = 0;
y = 0;
z = 0;
}
public Position(Single x, Single y, Single z)
{
this.x = x;
this.y = y;
this.z = z;
}
public override string ToString()
{
return “( " + x + " , " + y + " , " + z + " )”;
}
}