예제 #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"));
        }
예제 #2
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!");
    }