Esempio n. 1
0
        private MeshData ConvertRipToMeshData(RipData ripData)
        {
            MeshData mesh = new MeshData(ripData.meshName);

            for (int i = 0; i < ripData.indexCount; ++i)
            {
                mesh.Trangles.Add((int)ripData.indexData[i]);
            }

            var buffers = mesh.Buffers;

            for (int i = 0; i < ripData.elements.Length; ++i)
            {
                RipAttributeElement ele = ripData.elements[i];
                if (ele.sematic == ESemantic.UnKnown)
                {
                    continue;
                }
                MeshData.VBuffer buff;
                if (!buffers.TryGetValue(ele.sematic, out buff))
                {
                    buff = new MeshData.VBuffer();
                    buffers.Add(ele.sematic, buff);

                    buff.mDimension = (int)ele.dimension;

                    for (int j = 0; j < ripData.vertexCount; ++j)
                    {
                        var data = new Vector4();
                        for (int k = 0; k < ele.dimension; ++k)
                        {
                            float val = mRipData.vertexData[j * mRipData.dimensionPerVertex + k + ele.bytesOffset / 4];
                            if (ele.sematic == ESemantic.Position)
                            {
                                //val *= mMdlscaler;
                            }
                            data[k] = val;
                        }
                        buff.mData.Add(data);
                    }
                }
            }
            return(mesh);
        }
Esempio n. 2
0
        public RipData Load(string path)
        {
            BinaryReader reader  = null;
            RipData      ripData = null;

            try {
                if (string.IsNullOrEmpty(path))
                {
                    return(null);
                }
                reader = new BinaryReader(File.Open(path, FileMode.Open));
            } catch (Exception e) {
                if (null != reader)
                {
                    reader.Close();
                }
                Debug.LogError("打开文件失败:" + path + "\n" + e.Message);
                return(null);
            }

            int    id   = path.LastIndexOf("/");
            string name = path.Substring(id + 1);

            reader.ReadUInt32(); //signature
            uint version = reader.ReadUInt32();

            if (version == RipFileVersion)
            {
                uint          dwFacesCnt             = reader.ReadUInt32();
                uint          dwVertexesCnt          = reader.ReadUInt32();
                uint          vertexSize             = reader.ReadUInt32();
                uint          textureFilesCnt        = reader.ReadUInt32();
                uint          shaderFilesCnt         = reader.ReadUInt32();
                uint          vertexAttributesCnt    = reader.ReadUInt32();
                List <uint>   vertexAttribTypesArray = new List <uint>();
                List <string> textureFiles           = new List <string>();
                List <string> shaderFiles            = new List <string>();

                //attributes
                List <float> vertexArray            = new List <float>();
                List <uint>  faceArray              = new List <uint>();
                List <RipAttributeElement> elements = new List <RipAttributeElement>();

                for (int i = 0; i < vertexAttributesCnt; ++i)
                {
                    RipAttributeElement elem = new RipAttributeElement();
                    string semantic          = ReadStr(reader);
                    uint   semanticIndex     = reader.ReadUInt32();
                    uint   offset            = reader.ReadUInt32();
                    uint   size            = reader.ReadUInt32();
                    uint   typeMapElements = reader.ReadUInt32();
                    for (int j = 0; j < typeMapElements; ++j)
                    {
                        uint typeElement = reader.ReadUInt32();
                        vertexAttribTypesArray.Add(typeElement);
                    }

                    elem.name        = semantic + semanticIndex;
                    elem.bytesOffset = offset;
                    elem.dimension   = size / 4;
                    elem.sematic     = ESemantic.UnKnown;
                    elements.Add(elem);
                }

                //read textures
                for (int i = 0; i < textureFilesCnt; ++i)
                {
                    textureFiles.Add(ReadStr(reader));
                }

                //read Shaders
                for (int i = 0; i < shaderFilesCnt; ++i)
                {
                    shaderFiles.Add(ReadStr(reader));
                }

                //read indices
                for (int i = 0; i < dwFacesCnt; ++i)
                {
                    faceArray.Add(reader.ReadUInt32());
                    faceArray.Add(reader.ReadUInt32());
                    faceArray.Add(reader.ReadUInt32());
                }

                //read vertexes
                for (int i = 0; i < dwVertexesCnt; ++i)
                {
                    for (int j = 0; j < vertexAttribTypesArray.Count; ++j)
                    {
                        uint  elementType = vertexAttribTypesArray[j];
                        float z           = 0.0f;
                        if (elementType == 0)    //float
                        {
                            z = reader.ReadSingle();
                        }
                        else if (elementType == 1)     //uint
                        {
                            z = reader.ReadUInt32();
                        }
                        else if (elementType == 2)     //int
                        {
                            z = reader.ReadInt32();
                        }
                        else
                        {
                            z = reader.ReadUInt32();
                        }
                        vertexArray.Add(Convert.ToSingle(z));
                    }
                }

                //构造rip文件
                ripData                         = new RipData();
                ripData.meshName                = path.Substring(path.LastIndexOf("/") + 1);
                ripData.vertexData              = vertexArray.ToArray();
                ripData.indexData               = faceArray.ToArray();
                ripData.vertexBytesSize         = vertexSize;
                ripData.vertexCount             = dwVertexesCnt;
                ripData.indexCount              = dwFacesCnt * 3;
                ripData.attributeCountPerVertex = vertexAttributesCnt;
                ripData.dimensionPerVertex      = Convert.ToUInt32(vertexAttribTypesArray.Count);
                ripData.elements                = elements.ToArray();
                ripData.textureFiles            = textureFiles.ToArray();
                ripData.shaderFiles             = shaderFiles.ToArray();
            }
            else
            {
                Debug.LogError("Rip文件:" + name + "版本错误");
            }
            return(ripData);
        }
Esempio n. 3
0
        void OnGUI()
        {
            if (null == mRipData)
            {
                return;
            }
            if (null == mStyles)
            {
                mStyles = new Styles();
            }
            int   vertexNum   = (int)mRipData.vertexCount;
            float height      = this.position.height - 20;
            float width       = 40;
            int   viewItemNum = Mathf.FloorToInt(height / 18 - 1);

            current = (int)GUI.VerticalScrollbar(new Rect(0, 0, 20, height + 3), current, viewItemNum, 0, vertexNum);

            int end   = Mathf.Min(current + viewItemNum, vertexNum);
            int start = Mathf.Max(0, end - viewItemNum);

            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.Space(20);
                //draw id
                EditorGUILayout.BeginVertical(mStyles.mPreviewBox, GUILayout.Width(width), GUILayout.Height(height));
                {
                    EditorGUILayout.BeginHorizontal(mStyles.mOLTitle);
                    {
                        EditorGUILayout.LabelField(" id", EditorStyles.boldLabel, GUILayout.Width(width));
                    }
                    EditorGUILayout.EndHorizontal();
                    for (int i = start; i < end; ++i)
                    {
                        EditorGUILayout.LabelField(i.ToString(), EditorStyles.boldLabel, GUILayout.Width(width));
                    }
                }
                EditorGUILayout.EndVertical();
                GUILayout.Space(1);

                //data
                DataPanelScroll = EditorGUILayout.BeginScrollView(DataPanelScroll);
                EditorGUILayout.BeginHorizontal();
                {
                    for (int i = 0; i < mRipData.elements.Length; ++i)
                    {
                        RipAttributeElement ele = mRipData.elements[i];
                        width = ele.dimension * 100;
                        EditorGUILayout.BeginVertical(mStyles.mPreviewBox, GUILayout.Width(width), GUILayout.Height(height));
                        {
                            EditorGUILayout.BeginHorizontal(mStyles.mOLTitle);
                            {
                                ele.sematic = (ESemantic)EditorGUILayout.EnumPopup(ele.sematic, GUILayout.Width(width));
                            }
                            EditorGUILayout.EndHorizontal();
                            for (int j = start; j < end; ++j)
                            {
                                EditorGUILayout.BeginHorizontal();
                                for (int k = 0; k < ele.dimension; ++k)
                                {
                                    EditorGUILayout.LabelField(mRipData.vertexData[j * mRipData.dimensionPerVertex + k + ele.bytesOffset / 4].ToString(), GUILayout.Width(90));
                                }
                                EditorGUILayout.EndHorizontal();
                            }
                        }
                        EditorGUILayout.EndVertical();
                    }
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndScrollView();
            }

            EditorGUILayout.BeginVertical();
            {
                GUILayout.Label(mRipData.GetInfo());
                mMdlscaler = EditorGUILayout.FloatField("Scale:", mMdlscaler);
                if (GUILayout.Button("确认导入"))
                {
                    onMeshDataPrepared(ConvertRipToMeshData(mRipData));
                    this.Close();
                }
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
        }