private static STGenericMesh LoadMeshData(ColladaScene scene, Node node,
                                                  geometry geom, library_materials materials, controller controller = null)
        {
            mesh daeMesh = geom.Item as mesh;

            STGenericMesh mesh = new STGenericMesh();

            mesh.Vertices = new List <STVertex>();
            mesh.Name     = geom.name;

            var boneWeights = ParseWeightController(controller, scene);

            foreach (var item in daeMesh.Items)
            {
                //Poly lists can control specific amounts of indices for primitive types like quads
                if (item is polylist)
                {
                    var poly = item as polylist;
                    ConvertPolygon(scene, mesh, daeMesh, poly.input,
                                   boneWeights, materials, poly.material, poly.p, (int)poly.count, poly.vcount);
                }
                else if (item is triangles)
                {
                    var triangle = item as triangles;
                    ConvertPolygon(scene, mesh, daeMesh, triangle.input,
                                   boneWeights, materials, triangle.material, triangle.p, (int)triangle.count);
                }
            }

            return(mesh);
        }
Beispiel #2
0
 public static int FindMaterialIndex(library_materials materials, string name)
 {
     if (materials != null)
     {
         for (int i = 0; i < materials.material.Length; i++)
         {
             if (materials.material[i].name == name)
             {
                 return(i);
             }
         }
     }
     return(-1);
 }
Beispiel #3
0
        private static library_materials MakeMaterialsLibrary(ref Model3D mdl)
        {
            library_materials lib_mats = new library_materials();

            int len = mdl.builder.TextureImages.Count;

            material[] materials = new material[len];
            for (ulong i = 0; i < (ulong)materials.LongLength; i++)
            {
                materials[i] = MakeMaterial(i);
            }

            lib_mats.material = materials;
            return(lib_mats);
        }
            public ColladaScene(COLLADA collada, DAE.ImportSettings settings)
            {
                Settings    = settings;
                geometries  = FindLibraryItem <library_geometries>(collada.Items);
                images      = FindLibraryItem <library_images>(collada.Items);
                scenes      = FindLibraryItem <library_visual_scenes>(collada.Items);
                effects     = FindLibraryItem <library_effects>(collada.Items);
                controllers = FindLibraryItem <library_controllers>(collada.Items);
                materials   = FindLibraryItem <library_materials>(collada.Items);

                if (collada.asset != null)
                {
                    UpAxisType = collada.asset.up_axis;
                    UintSize   = collada.asset.unit;
                }
            }
Beispiel #5
0
        private static STGenericMesh LoadMeshData(ColladaScene scene, Node node,
                                                  geometry geom, library_materials materials, controller controller = null)
        {
            mesh daeMesh = geom.Item as mesh;

            STGenericMesh mesh = new STGenericMesh();

            mesh.Vertices = new List <STVertex>();
            mesh.Name     = geom.name;

            var boneWeights = ParseWeightController(controller, scene);

            foreach (var item in daeMesh.Items)
            {
                //Poly lists can control specific amounts of indices for primitive types like quads
                if (item is polylist)
                {
                    var poly = item as polylist;
                    ConvertPolygon(scene, mesh, daeMesh, poly.input,
                                   boneWeights, materials, poly.material, poly.p, (int)poly.count, poly.vcount);
                }
                else if (item is triangles)
                {
                    var triangle = item as triangles;
                    ConvertPolygon(scene, mesh, daeMesh, triangle.input,
                                   boneWeights, materials, triangle.material, triangle.p, (int)triangle.count);
                }
            }

            foreach (var vertex in mesh.Vertices)
            {
                if (scene.Settings.FlipUVsVertical)
                {
                    for (int i = 0; i < vertex.TexCoords.Length; i++)
                    {
                        vertex.TexCoords[i] = new Vector2(vertex.TexCoords[i].X, 1 - vertex.TexCoords[i].Y);
                    }
                }

                vertex.Position = Vector3.TransformPosition(vertex.Position, node.Transform);
                vertex.Position = Vector3.TransformPosition(vertex.Position, Matrix4.CreateScale(0.01f));

                //vertex.Normal = Vector3.TransformNormal(vertex.Normal, node.Transform);
            }

            return(mesh);
        }
Beispiel #6
0
            public ColladaScene(COLLADA collada, DAE.ImportSettings settings)
            {
                Settings    = settings;
                geometries  = FindLibraryItem <library_geometries>(collada.Items);
                images      = FindLibraryItem <library_images>(collada.Items);
                scenes      = FindLibraryItem <library_visual_scenes>(collada.Items);
                effects     = FindLibraryItem <library_effects>(collada.Items);
                controllers = FindLibraryItem <library_controllers>(collada.Items);
                materials   = FindLibraryItem <library_materials>(collada.Items);

                if (effects != null)
                {
                    for (int i = 0; i < effects.effect.Length; i++)
                    {
                        effectLookup.Add(effects.effect[i].id, effects.effect[i]);
                    }
                }

                if (collada.asset != null)
                {
                    UpAxisType = collada.asset.up_axis;
                    UintSize   = collada.asset.unit;
                }
            }
Beispiel #7
0
 private void LoadMaterials(library_materials materials)
 {
 }
Beispiel #8
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"));
        }
 internal void Init(library_materials lib)
 {
     _lib = lib;
 }
        private static void ConvertPolygon(ColladaScene scene, STGenericMesh mesh, mesh daeMesh,
                                           InputLocalOffset[] inputs, List <BoneWeight[]> boneWeights,
                                           library_materials materials, string material, string polys, int polyCount, string vcount = "")
        {
            List <uint> faces = new List <uint>();

            STPolygonGroup group = new STPolygonGroup();

            mesh.PolygonGroups.Add(group);
            group.MaterialIndex = DaeUtility.FindMaterialIndex(materials, material);

            string[] indices     = polys.Trim(' ').Split(' ');
            string[] vertexCount = new string[0];
            if (vcount != string.Empty)
            {
                vertexCount = vcount.Trim(' ').Split(' ');
            }

            int stride = 0;

            for (int i = 0; i < inputs.Length; i++)
            {
                stride = Math.Max(0, (int)inputs[i].offset + 1);
            }

            //Create a current list of all the vertices
            //Use a list to expand duplicate indices
            List <Vertex> vertices     = new List <Vertex>();
            var           vertexSource = DaeUtility.FindSourceFromInput(daeMesh.vertices.input[0], daeMesh.source);
            var           floatArr     = vertexSource.Item as float_array;

            for (int v = 0; v < (int)floatArr.count / 3; v++)
            {
                vertices.Add(new Vertex(vertices.Count, new List <int>()));
            }

            var indexStride = (indices.Length / 3) / polyCount;

            for (int i = 0; i < polyCount; i++)
            {
                int count = 3;
                if (vertexCount.Length > i)
                {
                    count = Convert.ToInt32(vertexCount[i]);
                }

                for (int v = 0; v < count; v++)
                {
                    List <int> semanticIndices = new List <int>();
                    for (int j = 0; j < inputs.Length; j++)
                    {
                        int faceOffset = (indexStride * 3) * i;
                        int index      = Convert.ToInt32(indices[faceOffset + (v * indexStride) + (int)inputs[j].offset]);
                        semanticIndices.Add(index);
                    }

                    BoneWeight[] boneWeightData = new BoneWeight[0];
                    if (boneWeights?.Count > semanticIndices[0])
                    {
                        boneWeightData = boneWeights[semanticIndices[0]];
                    }

                    VertexLoader.LoadVertex(ref faces, ref vertices, semanticIndices, boneWeightData);
                }
            }

            int numTexCoordChannels = 0;
            int numColorChannels    = 0;

            //Find them in both types of inputs
            for (int i = 0; i < inputs.Length; i++)
            {
                if (inputs[i].semantic == "TEXCOORD")
                {
                    numTexCoordChannels++;
                }
                if (inputs[i].semantic == "COLOR")
                {
                    numColorChannels++;
                }
            }

            for (int i = 0; i < daeMesh.vertices.input.Length; i++)
            {
                if (daeMesh.vertices.input[i].semantic == "TEXCOORD")
                {
                    numTexCoordChannels++;
                }
                if (daeMesh.vertices.input[i].semantic == "COLOR")
                {
                    numColorChannels++;
                }
            }


            for (int i = 0; i < vertices.Count; i++)
            {
                if (!vertices[i].IsSet)
                {
                    vertices.Remove(vertices[i]);
                }
            }

            bool hasNormals = false;

            foreach (var daeVertex in vertices)
            {
                if (daeVertex.semanticIndices.Count == 0)
                {
                    continue;
                }

                STVertex vertex = new STVertex();
                vertex.TexCoords   = new Vector2[numTexCoordChannels];
                vertex.Colors      = new Vector4[numColorChannels];
                vertex.BoneWeights = daeVertex.BoneWeights.ToList();
                mesh.Vertices.Add(vertex);

                //DAE has 2 inputs. Vertex and triangle inputs
                //Triangle inputs use indices over vertex inputs which only use one
                //Triangle inputs allow multiple color/uv sets unlike vertex inputs
                //Certain programs ie Noesis DAEs use vertex inputs. Most programs use triangle inputs
                for (int j = 0; j < daeMesh.vertices.input.Length; j++)
                {
                    //Vertex inputs only use the first index
                    var    vertexInput = daeMesh.vertices.input[j];
                    source source      = DaeUtility.FindSourceFromInput(vertexInput, daeMesh.source);
                    if (source == null)
                    {
                        continue;
                    }
                    if (vertexInput.semantic == "NORMAL")
                    {
                        hasNormals = true;
                    }

                    int dataStride = (int)source.technique_common.accessor.stride;
                    int index      = daeVertex.semanticIndices[0] * dataStride;

                    ParseVertexSource(ref vertex, scene, source, numTexCoordChannels, numColorChannels,
                                      dataStride, index, 0, vertexInput.semantic);
                }

                for (int i = 0; i < inputs.Length; i++)
                {
                    var    input  = inputs[i];
                    source source = DaeUtility.FindSourceFromInput(input, daeMesh.source);
                    if (source == null)
                    {
                        continue;
                    }
                    if (input.semantic == "NORMAL")
                    {
                        hasNormals = true;
                    }

                    int dataStride = (int)source.technique_common.accessor.stride;
                    int index      = daeVertex.semanticIndices[i] * dataStride;

                    ParseVertexSource(ref vertex, scene, source, numTexCoordChannels, numColorChannels,
                                      dataStride, index, (int)input.set, input.semantic);
                }
            }

            group.Faces = faces;

            if (!hasNormals)
            {
                CalculateNormals(mesh.Vertices, faces);
            }
            if (scene.Settings.RemoveDuplicateVerts)
            {
                RemoveDuplicateVerts(mesh.Vertices, faces);
            }
        }
Beispiel #11
0
    public ExportToCollada(string workingPath)
    {
        this.workingPath = workingPath;
        collada          = new COLLADA();

        geometries = new List <geometry>();
        nodes      = new List <node>();

        meshLookup = new Dictionary <Mesh, string>();

        ass = new asset();

        ass.up_axis       = UpAxisType.Y_UP;
        ass.unit          = new assetUnit();
        ass.unit.meter    = 1;
        ass.unit.name     = "meter";
        libraryGeometries = new library_geometries();
        // create effects
        libraryEffects = new library_effects();
        effects        = new List <effect>();
        effects.Add(new effect());

        var effectId = CreateId();

        effects[0].id    = effectId;
        effects[0].Items = new effectFx_profile_abstractProfile_COMMON[]
        {
            new effectFx_profile_abstractProfile_COMMON()
        };
        effects[0].Items[0].technique     = new effectFx_profile_abstractProfile_COMMONTechnique();
        effects[0].Items[0].technique.sid = "COMMON";
        var phong = new effectFx_profile_abstractProfile_COMMONTechniquePhong();

        effects[0].Items[0].technique.Item = phong;
        SetPhong(phong, new Vector4(0.5f, 0.5f, 0.5f, 1));


        // create materials
        libraryMaterials = new library_materials();
        materials        = new List <material>();
        materials.Add(new material());

        materialId      = CreateId();
        materials[0].id = materialId;

        materials[0].name                = defaultMaterialName;
        materials[0].instance_effect     = new instance_effect();
        materials[0].instance_effect.url = "#" + effectId;

        // create visual scenes
        visualScenes = new library_visual_scenes();

        visualScene = new visual_scene();
        visualScenes.visual_scene = new visual_scene[]
        {
            visualScene
        };
        var visualSceneId = CreateId();

        visualScenes.visual_scene[0].id = visualSceneId;

        libraryImages = new library_images();
        images        = new List <image>();

        Build();

        collada.scene = new COLLADAScene();
        collada.scene.instance_visual_scene = new InstanceWithExtra();



        collada.scene.instance_visual_scene.url = "#" + visualSceneId;
    }
Beispiel #12
0
        private static List <STGenericMesh> LoadMeshData(ColladaScene scene, Node node,
                                                         geometry geom, library_materials materials, controller controller = null)
        {
            List <STGenericMesh> meshes = new List <STGenericMesh>();

            mesh daeMesh = geom.Item as mesh;

            STGenericMesh mesh = new STGenericMesh();

            mesh.Vertices = new List <STVertex>();
            mesh.Name     = geom.name;
            meshes.Add(mesh);

            var boneWeights = ParseWeightController(controller, scene);

            foreach (var item in daeMesh.Items)
            {
                //Poly lists can control specific amounts of indices for primitive types like quads
                if (item is polylist)
                {
                    var poly = item as polylist;
                    ConvertPolygon(scene, mesh, daeMesh, poly.input,
                                   boneWeights, materials, poly.material, poly.p, (int)poly.count, poly.vcount);
                }
                else if (item is triangles)
                {
                    var triangle = item as triangles;
                    ConvertPolygon(scene, mesh, daeMesh, triangle.input,
                                   boneWeights, materials, triangle.material, triangle.p, (int)triangle.count);
                }
            }

            for (int v = 0; v < mesh.Vertices.Count; v++)
            {
                if (scene.Settings.FlipUVsVertical)
                {
                    for (int i = 0; i < mesh.Vertices[v].TexCoords.Length; i++)
                    {
                        mesh.Vertices[v].TexCoords[i] = new Vector2(mesh.Vertices[v].TexCoords[i].X, 1 - mesh.Vertices[v].TexCoords[i].Y);
                    }
                }

                mesh.Vertices[v].Position = Vector3.TransformPosition(mesh.Vertices[v].Position, node.Transform);
                mesh.Vertices[v].Normal   = Vector3.TransformNormal(mesh.Vertices[v].Normal, node.Transform);
            }

            if (controller != null)
            {
                skin skin       = controller.Item as skin;
                var  bindMatrix = CreateBindMatrix(skin.bind_shape_matrix);
                Console.WriteLine($"bindMatrix {bindMatrix.ExtractScale()}");
                bindMatrix = bindMatrix.ClearScale();
                for (int v = 0; v < mesh.Vertices.Count; v++)
                {
                    mesh.Vertices[v].Position = Vector3.TransformPosition(mesh.Vertices[v].Position, bindMatrix);
                    mesh.Vertices[v].Normal   = Vector3.TransformNormal(mesh.Vertices[v].Normal, bindMatrix);
                }
            }

            //  meshes = STGenericMesh.SeperatePolygonGroups<STGenericMesh>(mesh);

            if (scene.Settings.RemoveDuplicateVerts)
            {
                // mesh.RemoveDuplicateVertices();
            }

            return(meshes);
        }