Esempio n. 1
1
        public void Export(string filePath)
        {
            PalletProperties palletProperties = _palletSolution.Analysis.PalletProperties;

            COLLADA model = new COLLADA();
            // asset
            model.asset = new asset()
            {
                created = DateTime.Now,
                modified = DateTime.Now
            };
            model.asset.keywords = "StackBuilder Pallet Case";
            model.asset.title = _palletSolution.Title;
            model.asset.unit = new assetUnit() { name = "millimeters", meter = 0.001 };
            model.asset.up_axis = UpAxisType.Z_UP;

            library_images images = new library_images();
            library_materials materials = new library_materials();
            library_effects effects = new library_effects();
            library_geometries geometries = new library_geometries();
            library_nodes nodes = new library_nodes();
            library_cameras cameras = new library_cameras();
            library_animations animations = new library_animations();
            library_visual_scenes scenes = new library_visual_scenes();

            COLLADAScene colladaScene = new COLLADAScene();

            model.Items = new Object[] { images, materials, effects, geometries, nodes, cameras, animations, scenes };
            model.scene = colladaScene;

            // colors and materials
            List<effect> listEffects = new List<effect>();
            List<material> listMaterials = new List<material>();
            List<image> listImages = new List<image>();

            // effects
            effect effectPallet;
            material materialPallet;
            CreateMaterial(palletProperties.Color, null, null, "Pallet", out effectPallet, out materialPallet);
            listEffects.Add(effectPallet);
            listMaterials.Add(materialPallet);

            Box box = new Box(0, _palletSolution.Analysis.BProperties);

            // build list of effects / materials / images
            uint faceIndex = 0;
            foreach (Face face in box.Faces)
            {
                // build texture image if any
                string textureName = null;
                if (face.HasBitmap)
                {
                    textureName = string.Format("textureFace_{0}", faceIndex);
                    string texturePath = System.IO.Path.Combine(
                        System.IO.Path.GetDirectoryName(filePath)
                        , textureName + ".jpg");

                    double dimX = 0.0, dimY = 0.0;

                    switch (faceIndex)
                    {
                        case 0: dimX = box.Width; dimY = box.Height; break;
                        case 1: dimX = box.Width; dimY = box.Height; break;
                        case 2: dimX = box.Length; dimY = box.Height; break;
                        case 3: dimX = box.Length; dimY = box.Height; break;
                        case 4: dimX = box.Length; dimY = box.Width; break;
                        case 5: dimX = box.Length; dimY = box.Width; break;
                        default: break;
                    }
                    face.ExtractFaceBitmap(dimX, dimY, _bmpWidth, texturePath);
                    // create image
                    listImages.Add(
                        new image()
                        {
                            id = textureName + ".jpg",
                            name = textureName + ".jpg",
                            Item = @".\" + textureName + @".jpg"
                        }
                    );
                }
                material materialCase;
                effect effectCase;
                CreateMaterial(face.ColorFill, textureName, "0", string.Format("Case{0}", faceIndex), out effectCase, out materialCase);
                listEffects.Add(effectCase);
                listMaterials.Add(materialCase);

                ++faceIndex;
            }

            // add to image list
            images.image = listImages.ToArray();

            // case lines material
            effect effectCaseLines;
            material materialCaseLines;
            CreateMaterial(Color.Black, null, null, "CaseLines", out effectCaseLines, out materialCaseLines);
            listEffects.Add(effectCaseLines);
            listMaterials.Add(materialCaseLines);
            effects.effect = listEffects.ToArray();
            materials.material = listMaterials.ToArray();

            // geometries
            geometry geomPallet = new geometry() { id = "palletGeometry", name = "palletGeometry" };
            geometry geomCase = new geometry() { id = "caseGeometry", name = "caseGeometry" };
            geometries.geometry = new geometry[] { geomPallet, geomCase };
            // pallet
            mesh meshPallet = CreatePalletMesh(palletProperties);
            geomPallet.Item = meshPallet;
            // case
            mesh meshCase = CreateCaseMesh(_palletSolution.Analysis.BProperties as BoxProperties);
            geomCase.Item = meshCase;
            // library_animations
            animation animationMain = new animation() { id = "animationMain_ID", name = "animationMain" };
            animations.animation = new animation[] { animationMain };

            List<object> listAnimationSource = new List<object>();

            // library_visual_scenes
            visual_scene mainScene = new visual_scene() { id = "MainScene", name = "MainScene" };
            scenes.visual_scene = new visual_scene[] { mainScene };

            List<node> sceneNodes = new List<node>();
            sceneNodes.Add(new node()
            {
                id = "PalletNode",
                name = "PalletNode",
                instance_geometry = new instance_geometry[]
                {
                    new instance_geometry()
                    {
                        url = "#palletGeometry",
                        bind_material = new bind_material()
                        {
                            technique_common = new instance_material[]
                            {
                                new instance_material()
                                {
                                    symbol="materialPallet",
                                    target=string.Format("#{0}", materialPallet.id)
                                }
                            }
                        }
                    }
                }
            });
            uint caseIndex = 0;
            foreach (ILayer layer in _palletSolution)
            {
                BoxLayer bLayer = layer as BoxLayer;
                if (null == bLayer) continue;

                foreach (BoxPosition bp in bLayer)
                {
                    Vector3D translation = bp.Position;
                    Vector3D rotations = bp.Transformation.Rotations;

                    node caseNode = new node()
                    {
                        id = string.Format("CaseNode_{0}_ID", caseIndex),
                        name = string.Format("CaseNode_{0}", caseIndex),
                        ItemsElementName = new ItemsChoiceType2[]
                        {
                            ItemsChoiceType2.translate,
                            ItemsChoiceType2.rotate,
                            ItemsChoiceType2.rotate,
                            ItemsChoiceType2.rotate
                        },
                        Items = new object[]
                        {
                            new TargetableFloat3()
                            {
                                Values = new double[] { translation.X, translation.Y, translation.Z },
                                sid = "t",
                            },
                            new rotate()
                            {
                                Values = new double[] { 1.0, 0.0, 0.0, rotations.X },
                                sid = "rx"
                            },
                            new rotate()
                            {
                                Values = new double[] { 0.0, 1.0, 0.0, rotations.Y },
                                sid = "ry"
                            },
                            new rotate()
                            {
                                Values = new double[] { 0.0, 0.0, 1.0, rotations.Z },
                                sid = "rz"
                            } 
                        },

                        instance_geometry = new instance_geometry[]
                        {
                            new instance_geometry()
                            {
                                url="#caseGeometry",
                                bind_material = new bind_material()
                                {
                                    technique_common = new instance_material[]
                                    {
                                        new instance_material() { symbol="materialCase0", target="#material_Case0_ID" },
                                        new instance_material() { symbol="materialCase1", target="#material_Case1_ID" },
                                        new instance_material() { symbol="materialCase2", target="#material_Case2_ID" },
                                        new instance_material() { symbol="materialCase3", target="#material_Case3_ID" },
                                        new instance_material() { symbol="materialCase4", target="#material_Case4_ID" },
                                        new instance_material() { symbol="materialCase5", target="#material_Case5_ID" },
                                        new instance_material() { symbol="materialCaseLines", target="#material_CaseLines_ID"}
                                    }
                                }
                            }
                        }
                    };
                    sceneNodes.Add(caseNode);

                    // animations
                    CreateAnimation(caseIndex, (uint)_palletSolution.CaseCount, listAnimationSource, bp);

                    // increment case index
                    ++caseIndex;
                }
            }

            // add nodes
            mainScene.node = sceneNodes.ToArray();

            animationMain.Items = listAnimationSource.ToArray();

            // library_cameras
            camera cameraCamera = new camera() { id = "Camera-Camera", name = "Camera-Camera" };
            cameraOpticsTechnique_commonPerspective cameraPerspective = new cameraOpticsTechnique_commonPerspective()
            {
                znear = new TargetableFloat() { sid = "znear", Value = 1.0 },
                zfar = new TargetableFloat() { sid = "zfar", Value = 10000.0 }
            };
            cameraCamera.optics = new cameraOptics() { technique_common = new cameraOpticsTechnique_common() { Item = cameraPerspective } };
            cameras.camera = new camera[] { cameraCamera };

            // colladaScene
            colladaScene.instance_visual_scene = new InstanceWithExtra() { url = "#MainScene" };

            model.Save(filePath);
            model.Save(System.IO.Path.ChangeExtension(filePath, "xml"));
        }
Esempio n. 2
0
        public override ModelBase LoadModel(Vector3 scale)
        {
            foreach (var item in m_COLLADAModel.Items)
            {
                if (item.GetType().Equals(typeof(library_images))) this.library_images = item as library_images;
                if (item.GetType().Equals(typeof(library_effects))) this.library_effects = item as library_effects;
                if (item.GetType().Equals(typeof(library_materials))) this.library_materials = item as library_materials;
                if (item.GetType().Equals(typeof(library_geometries))) this.library_geometries = item as library_geometries;
                if (item.GetType().Equals(typeof(library_controllers))) this.library_controllers = item as library_controllers;
                if (item.GetType().Equals(typeof(library_visual_scenes))) this.library_visual_scenes = item as library_visual_scenes;
                if (item.GetType().Equals(typeof(library_animations))) this.library_animations = item as library_animations;
            }

            ReadMaterials();

            ReadVisualScenes();

            ReadAnimations();

            m_Model.ScaleModel(scale);
            m_Model.ScaleAnimations(scale);

            return m_Model;
        }
Esempio n. 3
0
        public static void ExportCollada(CmpFile cmp, ResourceManager resources, string output)
        {
            //Build tree
            InputModel        rootModel    = null;
            List <InputModel> parentModels = new List <InputModel>();

            foreach (var p in cmp.Parts)
            {
                if (p.Construct == null)
                {
                    rootModel = new InputModel()
                    {
                        Transform = Matrix4x4.Identity,
                        Model     = p.Model,
                        Con       = "Root"
                    };
                    break;
                }
            }
            parentModels.Add(rootModel);
            var q = new Queue <Part>(cmp.Parts);
            int infiniteDetect = 0;

            while (q.Count > 0)
            {
                var part = q.Dequeue();
                if (part.Construct == null)
                {
                    continue;
                }
                bool enqueue = true;
                foreach (var mdl in parentModels)
                {
                    if (part.Construct.ParentName == mdl.Con)
                    {
                        var child = new InputModel()
                        {
                            Transform = part.Construct.Rotation * Matrix4x4.CreateTranslation(part.Construct.Origin),
                            Model     = part.Model,
                            Con       = part.Construct.ChildName
                        };
                        mdl.Children.Add(child);
                        parentModels.Add(child);
                        enqueue = false;
                        break;
                    }
                }
                if (enqueue)
                {
                    q.Enqueue(part);
                }
                infiniteDetect++;
                if (infiniteDetect > 200000000)
                {
                    throw new Exception("Infinite cmp loop detected");
                }
            }

            //Build collada
            var dae    = NewCollada();
            var efx    = new CL.library_effects();
            var mats   = new CL.library_materials();
            var geos   = new CL.library_geometries();
            var scenes = new CL.library_visual_scenes();
            var vscene = new CL.visual_scene();

            vscene.name                     = vscene.id = "main-scene";
            scenes.visual_scene             = new CL.visual_scene[] { vscene };
            dae.scene                       = new CL.COLLADAScene();
            dae.scene.instance_visual_scene = new CL.InstanceWithExtra()
            {
                url = "#main-scene"
            };
            var glist   = new List <CL.geometry>();
            var mlist   = new List <string>();
            var matlist = new List <ExportMaterial>();

            BuildModel(resources, rootModel, glist, mlist, matlist);
            geos.geometry = glist.ToArray();
            mats.material = mlist.Select((x) => new CL.material()
            {
                name            = x,
                id              = x + "-material",
                instance_effect = new CL.instance_effect()
                {
                    url = "#" + x + "-effect"
                }
            }).ToArray();
            efx.effect = matlist.Select((x) => new CL.effect()
            {
                id    = x.Name + "-effect",
                Items = new[]
                {
                    new CL.effectFx_profile_abstractProfile_COMMON()
                    {
                        technique = new CL.effectFx_profile_abstractProfile_COMMONTechnique()
                        {
                            id   = "common",
                            Item = new CL.effectFx_profile_abstractProfile_COMMONTechniquePhong()
                            {
                                ambient             = ColladaColor("ambient", Color4.Black),
                                emission            = ColladaColor("emmision", Color4.Black),
                                diffuse             = ColladaColor("diffuse", x.Dc),
                                specular            = ColladaColor("specular", new Color4(0.25f, 0.25f, 0.25f, 1f)),
                                shininess           = ColladaFloat("shininess", 50),
                                index_of_refraction = ColladaFloat("index_of_refraction", 1)
                            }
                        }
                    }
                }
            }).ToArray();
            var rootNodes = new List <CL.node>();

            BuildNodes(rootModel, rootNodes);
            vscene.node = rootNodes.ToArray();
            dae.Items   = new object[] { efx, mats, geos, scenes };
            using (var stream = File.Create(output))
                ColladaSupport.XML.Serialize(stream, dae);
        }
Esempio n. 4
0
    private void SaveMeshes()
    {
        COLLADA exportScene = new COLLADA();

        List<geometry> geometryList = new List<geometry>();

        //if(testMesh!= null)
        //{
        //    geometry geo = COLLADA.MeshToGeometry(testMesh);
        //    if (geo != null)
        //        geometryList.Add(geo);
        //}

        foreach (Mesh mesh in blocks)
        {
            if (mesh != null)
            {
                geometry geo = (COLLADA.MeshToGeometry(mesh));
                if (geo != null)
                    geometryList.Add(geo);
            }
        }
        foreach (Mesh mesh in stencilBlocks)
        {
            if (mesh != null)
            {
                geometry geo = (COLLADA.MeshToGeometry(mesh));
                if (geo != null)
                    geometryList.Add(geo);
            }
        }
        foreach (Mesh mesh in transparentBlocks)
        {
            if (mesh != null)
            {
                geometry geo = (COLLADA.MeshToGeometry(mesh));
                if (geo != null)
                    geometryList.Add(geo);
            }
        }
        foreach (Mesh mesh in liquidBlocks)
        {
            if (mesh != null)
            {
                geometry geo = (COLLADA.MeshToGeometry(mesh));
                if (geo != null)
                    geometryList.Add(geo);
            }
        }

        library_geometries geometryLib = new library_geometries();
        geometryLib.geometry = geometryList.ToArray();

        library_visual_scenes visualSceneLib = new library_visual_scenes();
        visual_scene visualScene = new visual_scene();

        visualSceneLib.visual_scene = new visual_scene[1];
        visualSceneLib.visual_scene[0] = visualScene;

        visualScene.id = "Map";
        visualScene.name = "Map";
        visualScene.node = new node[geometryList.Count];
        for (int i = 0; i < geometryList.Count; i++)
        {
            node thisNode = new node();
            visualScene.node[i] = thisNode;
            geometry thisGeometry = geometryList[i];
            thisNode.id = thisGeometry.id.Remove(thisGeometry.id.Length - 4);
            thisNode.name = thisGeometry.name.Remove(thisGeometry.name.Length - 6);
            thisNode.sid = thisNode.id;

            thisNode.Items = new object[1];
            thisNode.Items[0] = COLLADA.ConvertMatrix(Matrix4x4.identity);

            thisNode.instance_geometry = new instance_geometry[1];
            thisNode.instance_geometry[0] = new instance_geometry();
            thisNode.instance_geometry[0].url = "#" + thisGeometry.id;
            thisNode.ItemsElementName = new ItemsChoiceType2[1];
            thisNode.ItemsElementName[0] = ItemsChoiceType2.matrix;
        }

        COLLADAScene sceneInstance = new COLLADAScene();
        sceneInstance.instance_visual_scene = new InstanceWithExtra();
        sceneInstance.instance_visual_scene.url = "#" + visualScene.id;

        exportScene.scene = sceneInstance;

        exportScene.Items = new object[2];
        exportScene.Items[0] = geometryLib;
        exportScene.Items[1] = visualSceneLib;

        asset assetHeader = new asset();
        assetHeader.unit = new assetUnit();
        assetHeader.unit.meter = 1;
        assetHeader.unit.name = "meter";
        assetHeader.up_axis = UpAxisType.Y_UP;

        exportScene.asset = assetHeader;

        if (File.Exists("Map.dae"))
            File.Delete("Map.dae");
        exportScene.Save("Map.dae");

        Texture2D mainTex = (Texture2D)basicTerrainMaterial.GetTexture("_MainTex");

        Color[] mainTexPixels = mainTex.GetPixels();
        Color[] diffusePixels = new Color[mainTexPixels.Length];
        Color[] roughnessPixels = new Color[mainTexPixels.Length];

        for(int i = 0; i < mainTexPixels.Length; i++)
        {
            diffusePixels[i] = new Color(mainTexPixels[i].r, mainTexPixels[i].g, mainTexPixels[i].b, 1.0f);
            roughnessPixels[i] = new Color(mainTexPixels[i].a, mainTexPixels[i].a, mainTexPixels[i].a, 1.0f);
        }

        Texture2D diffuseTex = new Texture2D(mainTex.width, mainTex.height);
        Texture2D roughnessTex = new Texture2D(mainTex.width, mainTex.height);

        diffuseTex.SetPixels(diffusePixels);
        roughnessTex.SetPixels(roughnessPixels);

        diffuseTex.Apply();
        roughnessTex.Apply();

        byte[] diffuseBytes = diffuseTex.EncodeToPNG();
        byte[] roughnessBytes = roughnessTex.EncodeToPNG();

        File.WriteAllBytes("pattern.png", diffuseBytes);
        File.WriteAllBytes("specular.png", roughnessBytes);

        Texture2D bumpMap = (Texture2D)basicTerrainMaterial.GetTexture("_BumpMap");

        Color[] bumpMapPixels = bumpMap.GetPixels();
        Color[] normalMapPixels = new Color[bumpMapPixels.Length];
        Color[] ambientMapPixels = new Color[bumpMapPixels.Length];
        Color[] alphaMapPixels = new Color[bumpMapPixels.Length];

        for (int i = 0; i < bumpMapPixels.Length; i++)
        {
            normalMapPixels[i] = new Color(bumpMapPixels[i].a, bumpMapPixels[i].g, Mathf.Sqrt(1 - ((bumpMapPixels[i].a * 2 - 1) * (bumpMapPixels[i].a * 2 - 1)) + ((bumpMapPixels[i].g * 2 - 1) * (bumpMapPixels[i].g * 2 - 1))));
            ambientMapPixels[i] = new Color(bumpMapPixels[i].r, bumpMapPixels[i].r, bumpMapPixels[i].r, 1.0f);
            alphaMapPixels[i] = new Color(bumpMapPixels[i].b, bumpMapPixels[i].b, bumpMapPixels[i].b, 1.0f);
        }

        Texture2D normalTex = new Texture2D(bumpMap.width, bumpMap.height);
        Texture2D ambientTex = new Texture2D(bumpMap.width, bumpMap.height);
        Texture2D alphaTex = new Texture2D(bumpMap.width, bumpMap.height);

        normalTex.SetPixels(normalMapPixels);
        ambientTex.SetPixels(ambientMapPixels);
        alphaTex.SetPixels(alphaMapPixels);

        normalTex.Apply();
        ambientTex.Apply();
        alphaTex.Apply();

        byte[] normalBytes = normalTex.EncodeToPNG();
        byte[] ambientBytes = ambientTex.EncodeToPNG();
        byte[] alphaBytes = alphaTex.EncodeToPNG();

        File.WriteAllBytes("normal.png", normalBytes);
        File.WriteAllBytes("occlusion.png", ambientBytes);
        File.WriteAllBytes("alpha.png", alphaBytes);

        Debug.Log("Saved map!");
    }
Esempio n. 5
0
        public static void ExportCollada(ModelFile mdl, ResourceManager resources, string output)
        {
            var dae    = NewCollada();
            var mats   = new CL.library_materials();
            var efx    = new CL.library_effects();
            var geos   = new CL.library_geometries();
            var scenes = new CL.library_visual_scenes();
            var vscene = new CL.visual_scene();

            vscene.name                     = vscene.id = "main-scene";
            scenes.visual_scene             = new CL.visual_scene[] { vscene };
            dae.scene                       = new CL.COLLADAScene();
            dae.scene.instance_visual_scene = new CL.InstanceWithExtra()
            {
                url = "#main-scene"
            };
            var exported = ProcessModel(mdl, resources);

            geos.geometry = exported.Geometries.ToArray();
            mats.material = exported.Materials.Select((x) => new CL.material()
            {
                name            = x.Name,
                id              = x.Name + "-material",
                instance_effect = new CL.instance_effect()
                {
                    url = "#" + x.Name + "-effect"
                }
            }).ToArray();
            efx.effect = exported.Materials.Select((x) => new CL.effect()
            {
                id    = x.Name + "-effect",
                Items = new[]
                {
                    new CL.effectFx_profile_abstractProfile_COMMON()
                    {
                        technique = new CL.effectFx_profile_abstractProfile_COMMONTechnique()
                        {
                            id   = "common",
                            Item = new CL.effectFx_profile_abstractProfile_COMMONTechniquePhong()
                            {
                                ambient             = ColladaColor("ambient", Color4.Black),
                                emission            = ColladaColor("emmision", Color4.Black),
                                diffuse             = ColladaColor("diffuse", x.Dc),
                                specular            = ColladaColor("specular", new Color4(0.25f, 0.25f, 0.25f, 1f)),
                                shininess           = ColladaFloat("shininess", 50),
                                index_of_refraction = ColladaFloat("index_of_refraction", 1)
                            }
                        }
                    }
                }
            }).ToArray();
            var nodes = new List <CL.node>();

            for (int i = 0; i < exported.Geometries.Count; i++)
            {
                nodes.Add(exported.GetNode(i, Matrix4x4.Identity, mdl.Path));
            }
            vscene.node = nodes.ToArray();
            dae.Items   = new object[] { efx, mats, geos, scenes };
            using (var stream = File.Create(output))
                ColladaSupport.XML.Serialize(stream, dae);
        }