static void ParseVtxFile()
        {
            if (VTX_Header.checkSum != MDL_Header.checksum)
            {
                throw new FileLoadException(String.Format("{0}: Does not match the checksum in the .mdl", ModelObject.name + ".dx90.vtx"));
            }

            mstudiomodel_t pModel = MDL_Models[0];
            mstudiomesh_t  pStudioMesh;

            BoneWeight[] pBoneWeight = new BoneWeight[pModel.numvertices];
            Vector3[]    pVertices   = new Vector3[pModel.numvertices];
            Vector3[]    pNormals    = new Vector3[pModel.numvertices];
            Vector2[]    pUvBuffer   = new Vector2[pModel.numvertices];

            List <Material> pMaterials = new List <Material>();

            ModelFileLoader.ReadType(ref vBodypart, VTX_Header.bodyPartOffset);

            Int32 ModelInputFilePosition = VTX_Header.bodyPartOffset + vBodypart.modelOffset;

            ModelFileLoader.ReadType(ref vModel, ModelInputFilePosition);

            Int32 ModelLODInputFilePosition = ModelInputFilePosition + vModel.lodOffset;

            ModelFileLoader.ReadType(ref vLod, ModelLODInputFilePosition);

            Int32 MeshInputFilePosition = ModelLODInputFilePosition + vLod.meshOffset;

            VTX_Meshes = new MeshHeader_t[vLod.numMeshes];
            ModelFileLoader.ReadArray(ref VTX_Meshes, MeshInputFilePosition);

            for (Int32 i = 0; i < pModel.numvertices; i++)
            {
                pVertices[i] = MathUtils.SwapZY(VVD_Vertexes[pModel.vertexindex + i].m_vecPosition * ConfigLoader.WorldScale);
                pNormals[i]  = MathUtils.SwapZY(VVD_Vertexes[pModel.vertexindex + i].m_vecNormal);
                pUvBuffer[i] = VVD_Vertexes[pModel.vertexindex + i].m_vecTexCoord;
            }

            Mesh pMesh = new Mesh();

            //ModelObject.AddComponent<MeshCollider>().sharedMesh = pMesh;

            pMesh.subMeshCount = vLod.numMeshes;

            pMesh.vertices = pVertices;
            pMesh.normals  = pNormals;
            pMesh.uv       = pUvBuffer;

            if (MDL_Bones.Count > 1)
            {
                for (Int32 i = 0; i < pModel.numvertices; i++)
                {
                    pBoneWeight[i] = GetBoneWeight(VVD_Vertexes[pModel.vertexindex + i].m_BoneWeights);
                }

                SkinnedMeshRenderer smr       = ModelObject.AddComponent <SkinnedMeshRenderer>();
                Matrix4x4[]         bindPoses = new Matrix4x4[MDL_Bones.Count];

                for (Int32 i = 0; i < bindPoses.Length; i++)
                {
                    bindPoses[i] = MDL_Bones[i].worldToLocalMatrix * ModelObject.transform.localToWorldMatrix;
                }

                pMesh.boneWeights = pBoneWeight;
                pMesh.bindposes   = bindPoses;

                smr.sharedMesh = pMesh;

                smr.bones = MDL_Bones.ToArray();
                smr.updateWhenOffscreen = true;
            }
            else
            {
                MeshFilter MeshFilter = ModelObject.AddComponent <MeshFilter>();
                ModelObject.AddComponent <MeshRenderer>();

                MeshFilter.sharedMesh = pMesh;
            }

            for (Int32 i = 0; i < vLod.numMeshes; i++)
            {
                List <Int32> pIndices = new List <Int32>();
                pStudioMesh = MDL_Meshes[i];

                StripGroupHeader_t[] StripGroups = new StripGroupHeader_t[VTX_Meshes[i].numStripGroups];
                Int32 StripGroupFilePosition     = MeshInputFilePosition + (Marshal.SizeOf(typeof(MeshHeader_t)) * i) + VTX_Meshes[i].stripGroupHeaderOffset;
                ModelFileLoader.ReadArray(ref StripGroups, StripGroupFilePosition);

                for (Int32 j = 0; j < VTX_Meshes[i].numStripGroups; j++)
                {
                    Vertex_t[] pVertexBuffer = new Vertex_t[StripGroups[j].numVerts];
                    ModelFileLoader.ReadArray(ref pVertexBuffer, StripGroupFilePosition + (Marshal.SizeOf(typeof(StripGroupHeader_t)) * j) + StripGroups[j].vertOffset);

                    UInt16[] Indices = new UInt16[StripGroups[j].numIndices];
                    ModelFileLoader.ReadArray(ref Indices, StripGroupFilePosition + (Marshal.SizeOf(typeof(StripGroupHeader_t)) * j) + StripGroups[j].indexOffset);

                    for (Int32 n = 0; n < Indices.Length; n++)
                    {
                        pIndices.Add(pVertexBuffer[Indices[n]].origMeshVertID + pStudioMesh.vertexoffset);
                    }
                }

                pMesh.SetTriangles(pIndices.ToArray(), i);
                String MaterialPath = String.Empty;

                for (Int32 j = 0; j < MDL_TDirectories.Length; j++)
                {
                    for (Int32 n = 0; n < ConfigLoader.ModFolders.Length; n++)
                    {
                        if (File.Exists(ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[n] + "/materials/" + MDL_TDirectories[j] + MDL_Textures[pStudioMesh.material] + ".vmt"))
                        {
                            MaterialPath = MDL_TDirectories[j] + MDL_Textures[pStudioMesh.material];
                        }
                    }
                }

                pMaterials.Add(MaterialLoader.Load(MaterialPath));
            }

            ;
            ModelObject.GetComponent <Renderer>().sharedMaterials = pMaterials.ToArray();
        }
        static void CreateModels()
        {
            BSP_Brushes = new List <GameObject>();

            for (Int32 Index = 0; Index < BSP_Models.Length; Index++)
            {
                GameObject Model = new GameObject("*" + Index);
                Model.transform.parent = BSP_WorldSpawn.transform;

                Dictionary <Int32, List <Int32> > MeshInfo = new Dictionary <Int32, List <Int32> >();

                for (Int32 i = BSP_Models[Index].FirstFace; i < BSP_Models[Index].FirstFace + BSP_Models[Index].NumFaces; i++)
                {
                    if (!MeshInfo.ContainsKey(BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID))
                    {
                        MeshInfo.Add(BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID, new List <Int32>());
                    }

                    MeshInfo[BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID].Add(i);
                    MeshInfo[BSP_TexData[BSP_TexInfo[BSP_Faces[i].TexInfo].TexData].NameStringTableID].Add(i);
                }

                for (Int32 i = 0; i < BSP_TextureStringData.Length; i++)
                {
                    if (!MeshInfo.ContainsKey(i))
                    {
                        continue;
                    }

                    List <Face> Faces = new List <Face>();

                    List <Vector3> Vertices  = new List <Vector3>();
                    List <Color32> Colors    = new List <Color32>();
                    List <Int32>   Triangles = new List <Int32>();
                    List <Vector2> UV        = new List <Vector2>();

                    for (Int32 j = 0; j < MeshInfo[i].Count; j++)
                    {
                        dface_t CFace = BSP_Faces[MeshInfo[i][j]];

                        if (CFace.DispInfo == -1)
                        {
                            Faces.Add(BSP_CFaces[MeshInfo[i][j]]);

                            Int32 PointOffset = Vertices.Count;
                            for (Int32 n = 0; n < Faces[j].Triangles.Length; n++)
                            {
                                Triangles.Add(Faces[j].Triangles[n] + PointOffset);
                            }

                            Vertices.AddRange(Faces[j].Vertices);
                            Colors.AddRange(Faces[j].Colors);
                            UV.AddRange(Faces[j].UV);
                        }
                    }

                    GameObject MeshObject = new GameObject(BSP_TextureStringData[i]);
                    MeshObject.transform.parent = Model.transform;
                    MeshObject.isStatic         = true;

                    MeshRenderer MeshRenderer = MeshObject.AddComponent <MeshRenderer>();
                    MeshRenderer.sharedMaterial    = MaterialLoader.Load(BSP_TextureStringData[i]);
                    MeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided;
                    MeshRenderer.lightmapIndex     = ConfigLoader.CurrentLightmap;

                    if (MaterialLoader.HasAnimation)
                    {
                        AnimatedTexture AnimationControlScript = MeshObject.AddComponent <AnimatedTexture>();
                        MaterialLoader.SetupAnimations(ref AnimationControlScript);
                    }

                    Mesh Mesh = MeshObject.AddComponent <MeshFilter>().sharedMesh = new Mesh();
                    Mesh.SetVertices(Vertices);
                    Mesh.SetTriangles(Triangles, 0);
                    Mesh.SetColors(Colors);
                    Mesh.SetUVs(0, UV);

                    if (BSP_TextureStringData[i].Contains("TOOLS/"))
                    {
                        MeshRenderer.enabled = false;
                        BSP_Brushes.Add(MeshObject);
                    }
                    else
                    {
                        MeshObject.AddComponent <MeshCollider>();

                        List <Vector2> UV2          = new List <Vector2>();
                        Texture2D      Lightmap_tex = new Texture2D(1, 1);

                        CreateLightMap(Faces, ref Lightmap_tex, ref UV2);

                        if (ConfigLoader.LoadLightmapsAsTextureShader)
                        {
                            if (MeshRenderer.sharedMaterial != null)
                            {
                                MeshRenderer.sharedMaterial.SetTexture("_LightMap", Lightmap_tex);
                            }
                        }

                        Mesh.SetUVs(1, UV2);
                    }

                    Mesh.RecalculateNormals();
                }
            }
        }
        static void CreateDisplacements()
        {
            Dictionary <Int32, List <Int32> > MeshInfo = new Dictionary <Int32, List <Int32> >();

            for (Int32 i = 0; i < BSP_DispInfo.Length; i++)
            {
                if (!MeshInfo.ContainsKey(BSP_TexData[BSP_TexInfo[BSP_Faces[BSP_DispInfo[i].MapFace].TexInfo].TexData].NameStringTableID))
                {
                    MeshInfo.Add(BSP_TexData[BSP_TexInfo[BSP_Faces[BSP_DispInfo[i].MapFace].TexInfo].TexData].NameStringTableID, new List <Int32>());
                }

                MeshInfo[BSP_TexData[BSP_TexInfo[BSP_Faces[BSP_DispInfo[i].MapFace].TexInfo].TexData].NameStringTableID].Add(BSP_DispInfo[i].MapFace);
            }

            for (Int32 i = 0; i < BSP_TextureStringData.Length; i++)
            {
                if (!MeshInfo.ContainsKey(i))
                {
                    continue;
                }

                List <Face> Faces = new List <Face>();

                List <Vector3> Vertices  = new List <Vector3>();
                List <Color32> Colors    = new List <Color32>();
                List <Int32>   Triangles = new List <Int32>();
                List <Vector2> UV        = new List <Vector2>();

                for (Int32 j = 0; j < MeshInfo[i].Count; j++)
                {
                    if (BSP_Faces[MeshInfo[i][j]].DispInfo != -1)
                    {
                        Faces.Add(BSP_CDisp[BSP_Faces[MeshInfo[i][j]].DispInfo]);

                        Int32 PointOffset = Vertices.Count;
                        for (Int32 n = 0; n < Faces[j].Triangles.Length; n++)
                        {
                            Triangles.Add(Faces[j].Triangles[n] + PointOffset);
                        }

                        Vertices.AddRange(Faces[j].Vertices);
                        Colors.AddRange(Faces[j].Colors);
                        UV.AddRange(Faces[j].UV);
                    }
                }

                GameObject MeshObject = new GameObject(BSP_TextureStringData[i]);
                MeshObject.transform.localScale = new Vector3(1, 1, -1);
                MeshObject.transform.parent     = BSP_WorldSpawn.transform;
                MeshObject.isStatic             = true;

                MeshRenderer MeshRenderer = MeshObject.AddComponent <MeshRenderer>();
                MeshRenderer.sharedMaterial = MaterialLoader.Load(BSP_TextureStringData[i]);
                MeshRenderer.lightmapIndex  = ConfigLoader.CurrentLightmap;

                if (MaterialLoader.HasAnimation)
                {
                    AnimatedTexture AnimationControlScript = MeshObject.AddComponent <AnimatedTexture>();
                    MaterialLoader.SetupAnimations(ref AnimationControlScript);
                }

                Mesh Mesh = MeshObject.AddComponent <MeshFilter>().sharedMesh = new Mesh();
                MeshObject.AddComponent <MeshCollider>();

                Mesh.SetVertices(Vertices);
                Mesh.SetTriangles(Triangles, 0);
                Mesh.SetColors(Colors);
                Mesh.SetUVs(0, UV);

                {
                    List <Vector2> UV2          = new List <Vector2>();
                    Texture2D      Lightmap_tex = new Texture2D(1, 1);

                    CreateLightMap(Faces, ref Lightmap_tex, ref UV2);

                    if (ConfigLoader.LoadLightmapsAsTextureShader)
                    {
                        if (MeshRenderer.sharedMaterial != null)
                        {
                            MeshRenderer.sharedMaterial.SetTexture("_LightMap", Lightmap_tex);
                        }
                    }

                    Mesh.SetUVs(1, UV2);
                }

                Mesh.RecalculateNormals();
            }
        }
Ejemplo n.º 4
0
        public void Configure(List <String> Data)
        {
            this.Data = Data;

            String Classname = Data[Data.FindIndex(n => n == "classname") + 1], Targetname = Data[Data.FindIndex(n => n == "targetname") + 1];

            name = Classname;

            //StudioMDLLoader.Load("editor/axis_helper").SetParent(transform, false);

            if (Data.Contains("origin"))
            {
                String[] Array = Data[Data.FindIndex(n => n == "origin") + 1].Split(' ');

                while (Array.Length != 3)
                {
                    Int32 TempIndex = Data.FindIndex(n => n == "origin") + 1;
                    Array = Data[Data.FindIndex(TempIndex, n => n == "origin") + 1].Split(' ');
                }

                transform.position = new Vector3(-Single.Parse(Array[0]), Single.Parse(Array[2]), -Single.Parse(Array[1])) * ConfigLoader.WorldScale;
            }

            if (Data.Contains("angles"))
            {
                String[] Array = Data[Data.FindIndex(n => n == "angles") + 1].Split(' ');
                if (!Classname.Contains("prop_"))
                {
                    EulerAngles    = new Vector3(Single.Parse(Array[0]), -Single.Parse(Array[1]), -Single.Parse(Array[2]));
                    EulerAngles.y -= 90;
                }
                else
                {
                    EulerAngles = new Vector3(-Single.Parse(Array[2]), Single.Parse(Array[1]), Single.Parse(Array[0]));
                }

                //float y = EulerAngles.y;

                /*if (Mathf.Approximately(y, 90) || Mathf.Approximately(y, 270))
                 *  EulerAngles.y += 90;
                 *
                 * if (Mathf.Approximately(y, 0) || Mathf.Approximately(y, 180))
                 *  EulerAngles.y -= 90;*/

                if (Data.Contains("pitch"))
                {
                    EulerAngles.x = -Single.Parse(Data[Data.FindIndex(n => n == "pitch") + 1]);
                }

                transform.eulerAngles = EulerAngles;
            }

            if (Classname.Contains("trigger"))
            {
                for (Int32 i = 0; i < transform.childCount; i++)
                {
                    GameObject Child = transform.GetChild(i).gameObject;
                    Child.SetActive(false);
                    Child.AddComponent <BoxCollider>().isTrigger = true;
                }
            }

            //if (Classname.Equals("point_viewcontrol"))
            //    gameObject.AddComponent<point_viewcontrol>();

            //3D Skybox
            if (Classname.Equals("sky_camera"))
            {
                //Setup 3DSkybox
                Camera    playerCamera = new GameObject("CameraPlayer").AddComponent <Camera>();
                CameraFly camFly       = playerCamera.gameObject.AddComponent <CameraFly>();
                camFly.skyScale    = float.Parse(Data[Data.FindIndex(n => n == "scale") + 1]);
                camFly.offset3DSky = transform.position;
                if (ConfigLoader.use3DSkybox)
                {
                    Camera skyCamera = gameObject.AddComponent <Camera>();
                    skyCamera.depth        = 0f;
                    skyCamera.farClipPlane = 70f;

                    playerCamera.depth      = 1f;
                    playerCamera.clearFlags = CameraClearFlags.Depth;
                    camFly.skyCamera        = skyCamera.transform;
                }
                //Setup 3DSkybox
            }
            //3D Skybox

            //Simple light

            /*if(Classname.Equals("shadow_control"))
             * {
             *  String[] color = Data[Data.FindIndex(n => n == "color") + 1].Split(' ');
             *  RenderSettings.subtractiveShadowColor = new Color32(Byte.Parse(color[0]), Byte.Parse(color[1]), Byte.Parse(color[2]), 255);
             * }*/

            if (Classname.Contains("light_") || Classname.Equals("light"))
            {
                if (Classname.Equals("light_environment"))
                {
                    if (RenderSettings.sun != null)
                    {
                        return;
                    }

                    String[] _ambient = Data[Data.FindIndex(n => n == "_ambient") + 1].Split(' ');
                    RenderSettings.ambientLight = new Color32(Byte.Parse(_ambient[0]), Byte.Parse(_ambient[1]), Byte.Parse(_ambient[2]), 255);
                }

                Light light = gameObject.AddComponent <Light>();

                if (Classname.Equals("light_spot"))
                {
                    light.type = LightType.Spot;
                }
                else if (Classname.Equals("light_environment"))
                {
                    RenderSettings.sun = light;
                    light.type         = LightType.Directional;
                }

                String[] _light = Data[Data.FindIndex(n => n == "_light") + 1].Split(' ');
                light.color = new Color32(Byte.Parse(_light[0]), Byte.Parse(_light[1]), Byte.Parse(_light[2]), 255);
                if (light.type == LightType.Point || light.type == LightType.Spot)
                {
                    String[] _distance = Data[Data.FindIndex(n => n == "_distance") + 1].Split(' ');

                    Int32 _distance_value = 0;
                    if (Int32.TryParse(_distance[0], out _distance_value))
                    {
                        light.range = _distance_value > 0 ? _distance_value * ConfigLoader.WorldScale : 10;
                    }
                    else
                    {
                        light.range = 10;
                    }

                    if (light.type == LightType.Spot)
                    {
                        String[] _inner_cone = Data[Data.FindIndex(n => n == "_inner_cone") + 1].Split(' ');
                        String[] _cone       = Data[Data.FindIndex(n => n == "_cone") + 1].Split(' ');
                        light.spotAngle = Int32.Parse(_inner_cone[0]) + Int32.Parse(_cone[0]);
                    }
                }

                light.lightmapBakeType = ConfigLoader.DynamicLight ? LightmapBakeType.Mixed : LightmapBakeType.Baked;
                if (ConfigLoader.DynamicLight)
                {
                    light.shadows = LightShadows.Soft;
                    if (light.type == LightType.Directional)
                    {
                        light.shadowBias       = 0.1f;
                        light.shadowNormalBias = 0;
                    }
                    else
                    {
                        light.shadowBias = 0.01f;
                    }
                }
            }
            //Simple light

            if (Classname.Equals("info_player_terrorist"))
            {
                StudioMDLLoader.Load("player/t_phoenix").SetParent(transform, false);
            }

            if (Classname.Equals("info_player_counterterrorist"))
            {
                StudioMDLLoader.Load("player/ct_urban").SetParent(transform, false);
            }

            if (Classname.Equals("info_player_start"))
            {
                StudioMDLLoader.Load("editor/playerstart").SetParent(transform, false);
            }

            if (Classname.Contains("weapon_"))
            {
                StudioMDLLoader.Load("weapons/w_rif_ak47").SetParent(transform, false);
            }

            if (Classname.Equals("hostage_entity"))//hostage_entity
            {
                String[] hostages = new[] { "characters/hostage_01", "characters/hostage_02", "characters/hostage_03", "characters/hostage_04" };
                StudioMDLLoader.Load(hostages[UnityEngine.Random.Range(0, hostages.Length)]).SetParent(transform, false);
            }

            if (Data.Contains("rendermode"))
            {
                if (Data[Data.FindIndex(n => n == "rendermode") + 1] == "10")
                {
                    for (Int32 i = 0; i < transform.childCount; i++)
                    {
                        GameObject Child = transform.GetChild(i).gameObject;
                        Child.GetComponent <Renderer>().enabled = false;
                    }
                }
            }

            if (Classname.Contains("prop_") || Classname.Contains("npc_"))
            {
                string ModelName = Data[Data.FindIndex(n => n == "model") + 1];
                StudioMDLLoader.Load(ModelName).SetParent(transform, false);
            }

            if (Classname.Equals("infodecal"))
            {
                //This is just an example, you need to implement a complete decal system.
                if (ConfigLoader.LoadInfoDecals)
                {
                    String   TextureName = Data[Data.FindIndex(n => n == "texture") + 1];
                    Material DecalMat    = MaterialLoader.Load(TextureName);

                    int x = DecalMat.mainTexture.width;
                    int y = DecalMat.mainTexture.height;

                    float  decalScale = 1;
                    String Value      = MaterialLoader.GetParametr("$decalscale");

                    if (!String.IsNullOrEmpty(Value))
                    {
                        decalScale *= float.Parse(Value);
                    }

                    SpriteRenderer DecalRender = gameObject.AddComponent <SpriteRenderer>();
                    DecalRender.sprite = Sprite.Create((Texture2D)DecalMat.mainTexture, new Rect(0, 0, x, y), new Vector2(0.5f, 0.5f), 1);

                    DecalRender.flipX = true;
                    DecalRender.flipY = true;

                    transform.localScale = new Vector3(decalScale * ConfigLoader.WorldScale, decalScale * ConfigLoader.WorldScale, 1);
                }
            }
        }