• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by danipren · Apr 28, 2016 at 01:33 AM · importfileconversion

problem with dff importer script

ive been going through some dff files lately and converting them is becoming a pain. ive searched the internet for an alternative such as batch converting but have come up with nothing.

so i found this script online i have tried contactin the dev for some help but i cant get hold of him so i was wondering if someone could help shed some light on this for me

im bringing in the dff files but its still not doin anything and as soon as the file is out of view in the project view it gets deleted

any help would really really be appreciated as ive spent days pulling out my hair but its way above my level

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by danipren · Apr 28, 2016 at 01:33 AM

 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[i].EndsWith("dff")) return;
 
             int progress = 0;
             EditorUtility.DisplayProgressBar("Importing DFF File...", "Processing: " + importedassets[i], progress / 100);
 
             LoadDFF(importedassets[i]);
 
             progress = 100;
             EditorUtility.DisplayProgressBar("Importing DFF File...", "Processing: " + importedassets[i], progress / 100);
 
             EditorUtility.ClearProgressBar();
         }
     }
 
     public static BinaryReader br;
 
     static void LoadDFF(string path)
     {
         List<Frame> frames = new List<Frame>();
         List<Color> vertexColors = new List<Color>();
         List<Vector2> uvCoordinates = new List<Vector2>();
         List<IndicesAtt> facesInfo = new List<IndicesAtt>();
         List<Vector3> verticies = new List<Vector3>();
         List<Vector3> normals = new List<Vector3>();
         List<Material> materials = new List<Material>();
 
         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<char> n = new List<char>();
                 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<char> n = new List<char>();
                     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[i].name.Length <= 2)
                     return;
 
                 Mesh m = new Mesh();
 
                 m.vertices = verticies.ToArray();
                 //m.colors = vertexColors.ToArray();
 
                 List<List<int>> triangles = new List<List<int>>();
 
                 foreach (Material ma in materials)
                 {
                     List<int> tris = new List<int>();
                     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<int> 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[i].name) + si + ".mat");
                         AssetDatabase.SaveAssets();
                         mr.sharedMaterials[mi] = AssetDatabase.LoadAssetAtPath(path.Remove(path.Length - 3, 3) + "/Materials/" + RemoveSpecialCharacters(frames[i].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[i] >= '0' && str[i] <= '9')
                 || (str[i] >= 'A' && str[i] <= 'z'
                     || (str[i] == '.' || str[i] == '_')))
             {
                 sb.Append(str[i]);
             }
         }
 
         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<Frame> 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[i] + " , " + y[i] + " , " + z[i] + " )";
         }
         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 + " )";
     }
 }
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

61 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity spontaneously moves image assets into a parent folder minutes after import 1 Answer

Unity uses all memory when converting an hdr file into a cubemap 0 Answers

How to import .dae file runtime with more than 65k vertices 0 Answers

Help! All BMP files with transparency can not be read anymore ! 0 Answers

Best way to export data and be able to import it in another PC. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges