Beispiel #1
0
        public void ExportToCollada(string outputPath)
        {
            var collada     = new COLLADA();
            var asset       = new asset();
            var contributor = new assetContributor();

            if (ArtToolInfo != null)
            {
                contributor.authoring_tool = ArtToolInfo.FromArtToolName;
            }
            else
            {
                contributor.authoring_tool = "LSLib COLLADA Exporter v" + Common.LibraryVersion();
            }
            asset.contributor = new assetContributor[] { contributor };
            asset.created     = DateTime.Now;
            asset.modified    = DateTime.Now;
            asset.unit        = new assetUnit();
            asset.unit.name   = "meter";
            // TODO: Handle up vector, etc. properly?
            if (ArtToolInfo != null)
            {
                asset.unit.meter = ArtToolInfo.UnitsPerMeter;
            }
            else
            {
                asset.unit.meter = 1;
            }
            asset.up_axis = UpAxisType.Y_UP;
            collada.asset = asset;

            var geometries  = new List <geometry>();
            var controllers = new List <controller>();
            var geomNodes   = new List <node>();

            ExportModels(geometries, controllers, geomNodes);

            var animations     = new List <animation>();
            var animationClips = new List <animation_clip>();

            if (Animations != null)
            {
                foreach (var anim in Animations)
                {
                    var anims = anim.ExportAnimations();
                    animations.AddRange(anims);
                    var clip = new animation_clip();
                    clip.id           = anim.Name + "_Animation";
                    clip.name         = anim.Name;
                    clip.start        = 0.0;
                    clip.end          = anim.Duration;
                    clip.endSpecified = true;

                    var animInstances = new List <InstanceWithExtra>();
                    foreach (var animChannel in anims)
                    {
                        var instance = new InstanceWithExtra();
                        instance.url = "#" + animChannel.id;
                        animInstances.Add(instance);
                    }

                    clip.instance_animation = animInstances.ToArray();
                    animationClips.Add(clip);
                }
            }

            var rootElements = new List <object>();

            if (animations.Count > 0)
            {
                var animationLib = new library_animations();
                animationLib.animation = animations.ToArray();
                rootElements.Add(animationLib);
            }

            if (animationClips.Count > 0)
            {
                var animationClipLib = new library_animation_clips();
                animationClipLib.animation_clip = animationClips.ToArray();
                rootElements.Add(animationClipLib);
            }

            if (geometries.Count > 0)
            {
                var geometryLib = new library_geometries();
                geometryLib.geometry = geometries.ToArray();
                rootElements.Add(geometryLib);
            }

            if (controllers.Count > 0)
            {
                var controllerLib = new library_controllers();
                controllerLib.controller = controllers.ToArray();
                rootElements.Add(controllerLib);
            }

            var visualScenes = new library_visual_scenes();
            var visualScene  = new visual_scene();

            visualScene.id   = "DefaultVisualScene";
            visualScene.name = "unnamed";

            visualScene.node          = geomNodes.ToArray();
            visualScenes.visual_scene = new visual_scene[] { visualScene };

            var visualSceneInstance = new InstanceWithExtra();

            visualSceneInstance.url = "#DefaultVisualScene";
            rootElements.Add(visualScenes);

            var scene = new COLLADAScene();

            scene.instance_visual_scene = visualSceneInstance;
            collada.scene = scene;

            collada.Items = rootElements.ToArray();

            collada.Save(outputPath);
        }
Beispiel #2
0
        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"));
        }