Exemple #1
0
 private static void Update(CSharpBoxClass cSharpBox)
 {
     InfoStore.Add("UnityEngine Time : " + Time.time);
 }
Exemple #2
0
    private static void Start(CSharpBoxClass cSharpBox)
    {
        cSharpBox.ClearLogs();

        MainConstruct mainConstruct = cSharpBox.MainConstruct as MainConstruct;

        string outPutFolderPath = Get.ProfilePaths.ProfileRootDir().Append(mainConstruct.GetBlueprintName()).ToString() + string.Format("-{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
        string texFolderPath    = Path.Combine(outPutFolderPath, "Textures");

        Directory.CreateDirectory(outPutFolderPath);
        Directory.CreateDirectory(texFolderPath);

        //TextureDefinition getTexDef(MaterialDefinition I) => Configured.i.Textures.Find(I.ColorTextureReference.Reference.Guid);
        IEnumerable <TextureDefinition> textureDefinitionList = Configured.i.Materials.Components
                                                                .Select(I => Configured.i.Textures.Find(I.ColorTextureReference.Reference.Guid))
                                                                .Where(I => I != null)
                                                                .Distinct();

        StringBuilder sb = new StringBuilder();

        foreach (TextureDefinition textureDefinition in textureDefinitionList)
        {
            ModSource modSource = textureDefinition.Source;

            if (modSource != ModSource.File && modSource != ModSource.Resources)
            {
                continue;
            }

            byte[] encodeResult = null;

            try
            {
                encodeResult = ForcedEncodeToJPG(textureDefinition.Texture.GetTexture());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                continue;
            }

            if (encodeResult != null)
            {
                string texName = TexNameGenerate(textureDefinition);

                File.WriteAllBytes(Path.Combine(texFolderPath, texName + ".jpg"), encodeResult);

                cSharpBox.Log("newmtl " + texName);
                cSharpBox.Log("map_Kd " + Path.Combine("Textures", texName + ".jpg"));
                cSharpBox.Log(string.Empty);

                sb.Append("newmtl " + texName + "\n");
                sb.Append("map_Kd " + Path.Combine("Textures", texName + ".jpg") + "\n");
                sb.Append("\n");
            }
        }

        using (StreamWriter sw = new StreamWriter(Path.Combine(outPutFolderPath, "Test.mtl")))
        {
            sw.Write(sb.ToString());
        }



        List <AllConstruct> allConstructList = new List <AllConstruct>();

        mainConstruct.AllBasicsRestricted.GetAllConstructsBelowUsAndIncludingUs(allConstructList);

        foreach (AllConstruct allConstruct in allConstructList)
        {
            if (!(allConstruct.Chunks is ConstructableMeshMerger))
            {
                continue;
            }

            Dictionary <MaterialDefinition, List <Mesh> > meshListDictionary = new Dictionary <MaterialDefinition, List <Mesh> >();

            foreach (ICarriedObjectReference iCOR in allConstruct.CarriedObjects.Objects)
            {
                MeshRenderer meshRenderer = iCOR.ObjectItself.GetComponent <MeshRenderer>();
                MeshFilter   meshFilter   = iCOR.ObjectItself.GetComponent <MeshFilter>();

                if (meshRenderer == null || meshFilter == null)
                {
                    continue;
                }

                KeyValuePair <Guid, MaterialDefinition> item = Configured.i.Materials.DictionaryOfComponents.FirstOrDefault(d => d.Value.Material == meshRenderer.sharedMaterial);

                if (item.Equals(default(KeyValuePair <Guid, MaterialDefinition>)))
                {
                    continue;
                }

                if (!meshListDictionary.ContainsKey(item.Value))
                {
                    meshListDictionary.Add(item.Value, new List <Mesh>());
                }

                Mesh newMesh = UnityEngine.Object.Instantiate(meshFilter.sharedMesh);
                IEnumerable <Vector3> newVertexList = newMesh.vertices.Select(d => meshFilter.transform.localToWorldMatrix.MultiplyPoint(d));
                newVertexList = newVertexList.Select(d => allConstruct.myTransform.worldToLocalMatrix.MultiplyPoint(d));
                newMesh.SetVertices(newVertexList.ToList());

                meshListDictionary[item.Value].Add(newMesh);
            }

            ConstructableMeshMerger meshMerger = allConstruct.Chunks as ConstructableMeshMerger;
            cSharpBox.Log("AllBlockVertex Count : " + meshMerger.VertexCount);

            foreach (KeyValuePair <int, List <ChunkMesh> > chunkMeshListDictionary in meshMerger.D)
            {
                bool found;
                MaterialDefinition materialDefinition = Configured.i.Materials.FindUsingTheRuntimeId(chunkMeshListDictionary.Key, out found);

                bool flag_0 = chunkMeshListDictionary.Value.Count == 0;
                bool flag_1 = chunkMeshListDictionary.Value.All(d => d.VertCount == 0);

                if (!found || flag_0 || flag_1)
                {
                    continue;
                }

                if (!meshListDictionary.ContainsKey(materialDefinition))
                {
                    meshListDictionary.Add(materialDefinition, new List <Mesh>());
                }

                meshListDictionary[materialDefinition].AddRange(chunkMeshListDictionary.Value.Select(d => d.GetMesh()));
            }



            int  subConstructIndex = allConstruct.PersistentSubConstructIndex;
            bool isSubConstruct    = subConstructIndex != -1;

            Vector3    localPosition = Vector3.zero;
            Quaternion localRotation = Quaternion.identity;

            if (isSubConstruct)
            {
                MainConstruct mc = allConstruct.Main;
                localPosition = mc.SafeGlobalToLocal(allConstruct.SafePosition);
                localRotation = mc.SafeGlobalRotationToLocalRotation(allConstruct.SafeRotation);
            }

            List <Mesh> meshList = new List <Mesh>();

            foreach (KeyValuePair <MaterialDefinition, List <Mesh> > meshListDictionaryData in meshListDictionary)
            {
                List <Vector3> vertices        = new List <Vector3>();
                List <Vector3> normals         = new List <Vector3>();
                List <Vector2> uv              = new List <Vector2>();
                List <int>     triangles       = new List <int>();
                int            loadVertexCount = 0;

                foreach (Mesh mesh in meshListDictionaryData.Value)
                {
                    vertices.AddRange(mesh.vertices);
                    normals.AddRange(mesh.normals);
                    uv.AddRange(mesh.uv);
                    triangles.AddRange(mesh.triangles.Select(d => d + loadVertexCount));
                    loadVertexCount += mesh.vertexCount;
                }

                if (isSubConstruct)
                {
                    vertices = vertices.Select(I => localRotation * I + localPosition).ToList();
                }

                TextureDefinition textureDefinition = Configured.i.Textures.Find(meshListDictionaryData.Key.ColorTextureReference.Reference.Guid);

                Mesh newMesh = new Mesh
                {
                    indexFormat = UnityEngine.Rendering.IndexFormat.UInt32,
                    name        = TexNameGenerate(textureDefinition)
                };

                newMesh.SetVertices(vertices);
                newMesh.SetNormals(normals);
                newMesh.SetUVs(0, uv);
                newMesh.SetTriangles(triangles, 0);

                FlipHorizontal(newMesh);
                meshList.Add(newMesh);

                cSharpBox.Log("vertexCount : " + newMesh.vertices.Length);
            }



            string fileName = $"Test ({OutputCount++})";

            if (subConstructIndex == -1)
            {
                fileName = "MainConstruct";
            }
            else
            {
                fileName = $"SubConstruct_{subConstructIndex}";
            }

            MeshToFile(meshList, Path.Combine(outPutFolderPath, fileName + ".obj"));
        }
    }