Ejemplo n.º 1
0
        List<ModelPrim> Parse()
        {
            var Prims = new List<ModelPrim>();

            float DEG_TO_RAD = 0.017453292519943295769236907684886f;

            if (Model == null) return Prims;

            Matrix4 transform = Matrix4.Identity;

            UpAxisType upAxis = UpAxisType.Y_UP;

            var asset = Model.asset;
            if (asset != null)
            {
                upAxis = asset.up_axis;
                if (asset.unit != null)
                {
                    float meter = (float)asset.unit.meter;
                    transform[0, 0] = meter;
                    transform[1, 1] = meter;
                    transform[2, 2] = meter;
                }
            }

            Matrix4 rotation = Matrix4.Identity;

            if (upAxis == UpAxisType.X_UP)
            {
                rotation = Matrix4.CreateFromEulers(0.0f, 90.0f * DEG_TO_RAD, 0.0f);
            }
            else if (upAxis == UpAxisType.Y_UP)
            {
                rotation = Matrix4.CreateFromEulers(90.0f * DEG_TO_RAD, 0.0f, 0.0f);
            }

            rotation = rotation * transform;
            transform = rotation;

            ParseVisualScene();
            ParseMaterials();

            foreach (var item in Model.Items)
            {
                if (item is library_geometries)
                {
                    var geometries = (library_geometries)item;
                    foreach (var geo in geometries.geometry)
                    {
                        var mesh = geo.Item as mesh;
                        if (mesh == null) continue;

                        var nodes = Nodes.FindAll(n => n.MeshID == geo.id);
                        if (nodes != null)
                        {
                            byte[] mesh_asset = null;
                            foreach (var node in nodes)
                            {
                                var prim = new ModelPrim();
                                prim.ID = node.ID;
                                Prims.Add(prim);
                                Matrix4 primTransform = transform;
                                primTransform = primTransform * node.Transform;

                                AddPositions(mesh, prim, primTransform);

                                foreach (var mitem in mesh.Items)
                                {
                                    if (mitem is triangles)
                                    {
                                        AddFacesFromPolyList(Triangles2Polylist((triangles)mitem), mesh, prim, primTransform);
                                    }
                                    if (mitem is polylist)
                                    {
                                        AddFacesFromPolyList((polylist)mitem, mesh, prim, primTransform);
                                    }
                                }

                                if (mesh_asset == null)
                                {
                                    prim.CreateAsset(UUID.Zero);
                                    mesh_asset = prim.Asset;
                                }
                                else
                                    prim.Asset = mesh_asset;
                            }
                        }
                    }
                }
            }

            return Prims;
        }
Ejemplo n.º 2
0
        List <ModelPrim> Parse()
        {
            var Prims = new List <ModelPrim>();

            float DEG_TO_RAD = 0.017453292519943295769236907684886f;

            if (Model == null)
            {
                return(Prims);
            }

            Matrix4 transform = Matrix4.Identity;

            UpAxisType upAxis = UpAxisType.Y_UP;

            var asset = Model.asset;

            if (asset != null)
            {
                upAxis = asset.up_axis;
                if (asset.unit != null)
                {
                    float meter = (float)asset.unit.meter;
                    transform[0, 0] = meter;
                    transform[1, 1] = meter;
                    transform[2, 2] = meter;
                }
            }

            Matrix4 rotation = Matrix4.Identity;

            if (upAxis == UpAxisType.X_UP)
            {
                rotation = Matrix4.CreateFromEulers(0.0f, 90.0f * DEG_TO_RAD, 0.0f);
            }
            else if (upAxis == UpAxisType.Y_UP)
            {
                rotation = Matrix4.CreateFromEulers(90.0f * DEG_TO_RAD, 0.0f, 0.0f);
            }

            rotation  = rotation * transform;
            transform = rotation;

            ParseVisualScene();
            ParseMaterials();

            foreach (var item in Model.Items)
            {
                if (item is library_geometries)
                {
                    var geometries = (library_geometries)item;
                    foreach (var geo in geometries.geometry)
                    {
                        var mesh = geo.Item as mesh;
                        if (mesh == null)
                        {
                            continue;
                        }

                        var nodes = Nodes.FindAll(n => n.MeshID == geo.id);     // Find all instances of this geometry
                        if (nodes != null)
                        {
                            ModelPrim firstPrim = null;         // The first prim is actually calculated, the others are just copied from it.

                            Vector3 asset_scale  = new Vector3(1, 1, 1);
                            Vector3 asset_offset = new Vector3(0, 0, 0);            // Scale and offset between Collada and OS asset (Which is always in a unit cube)

                            foreach (var node in nodes)
                            {
                                var prim = new ModelPrim();
                                prim.ID = node.ID;
                                Prims.Add(prim);

                                // First node is used to create the asset. This is as the code to crate the byte array is somewhat
                                // erroneously placed in the ModelPrim class.
                                if (firstPrim == null)
                                {
                                    firstPrim = prim;
                                    AddPositions(out asset_scale, out asset_offset, mesh, prim, transform);     // transform is used only for inch -> meter and up axis transform.

                                    foreach (var mitem in mesh.Items)
                                    {
                                        if (mitem is triangles)
                                        {
                                            AddFacesFromPolyList(Triangles2Polylist((triangles)mitem), mesh, prim, transform);  // Transform is used to turn normals according to up axis
                                        }
                                        if (mitem is polylist)
                                        {
                                            AddFacesFromPolyList((polylist)mitem, mesh, prim, transform);
                                        }
                                    }

                                    prim.CreateAsset(UUID.Zero);
                                }
                                else
                                {
                                    // Copy the values set by Addpositions and AddFacesFromPolyList as these are the same as long as the mesh is the same
                                    prim.Asset     = firstPrim.Asset;
                                    prim.BoundMin  = firstPrim.BoundMin;
                                    prim.BoundMax  = firstPrim.BoundMax;
                                    prim.Positions = firstPrim.Positions;
                                    prim.Faces     = firstPrim.Faces;
                                }

                                // Note: This ignores any shear or similar non-linear effects. This can cause some problems but it
                                // is unlikely that authoring software can generate such matrices.
                                node.Transform.Decompose(out prim.Scale, out prim.Rotation, out prim.Position);
                                float roll, pitch, yaw;
                                node.Transform.GetEulerAngles(out roll, out pitch, out yaw);

                                // The offset created when normalizing the mesh vertices into the OS unit cube must be rotated
                                // before being added to the position part of the Collada transform.
                                Matrix4 rot    = Matrix4.CreateFromQuaternion(prim.Rotation);           // Convert rotation to matrix for for Transform
                                Vector3 offset = Vector3.Transform(asset_offset * prim.Scale, rot);     // The offset must be rotated and mutiplied by the Collada file's scale as the offset is added during rendering with the unit cube mesh already multiplied by the compound scale.
                                prim.Position += offset;
                                prim.Scale    *= asset_scale;                                           // Modify scale from Collada instance by the rescaling done in AddPositions()
                            }
                        }
                    }
                }
            }

            return(Prims);
        }
Ejemplo n.º 3
0
        List <ModelPrim> Parse()
        {
            var Prims = new List <ModelPrim>();

            float DEG_TO_RAD = 0.017453292519943295769236907684886f;

            if (Model == null)
            {
                return(Prims);
            }

            Matrix4 transform = Matrix4.Identity;

            UpAxisType upAxis = UpAxisType.Y_UP;

            var asset = Model.asset;

            if (asset != null)
            {
                upAxis = asset.up_axis;
                if (asset.unit != null)
                {
                    float meter = (float)asset.unit.meter;
                    transform[0, 0] = meter;
                    transform[1, 1] = meter;
                    transform[2, 2] = meter;
                }
            }

            Matrix4 rotation = Matrix4.Identity;

            if (upAxis == UpAxisType.X_UP)
            {
                rotation = Matrix4.CreateFromEulers(0.0f, 90.0f * DEG_TO_RAD, 0.0f);
            }
            else if (upAxis == UpAxisType.Y_UP)
            {
                rotation = Matrix4.CreateFromEulers(90.0f * DEG_TO_RAD, 0.0f, 0.0f);
            }

            rotation  = rotation * transform;
            transform = rotation;

            ParseVisualScene();
            ParseMaterials();

            foreach (var item in Model.Items)
            {
                if (item is library_geometries)
                {
                    var geometries = (library_geometries)item;
                    foreach (var geo in geometries.geometry)
                    {
                        var mesh = geo.Item as mesh;
                        if (mesh == null)
                        {
                            continue;
                        }

                        var nodes = Nodes.FindAll(n => n.MeshID == geo.id);
                        if (nodes != null)
                        {
                            byte[] mesh_asset = null;
                            foreach (var node in nodes)
                            {
                                var prim = new ModelPrim();
                                prim.ID = node.ID;
                                Prims.Add(prim);
                                Matrix4 primTransform = transform;
                                primTransform = primTransform * node.Transform;

                                AddPositions(mesh, prim, primTransform);

                                foreach (var mitem in mesh.Items)
                                {
                                    if (mitem is triangles)
                                    {
                                        AddFacesFromPolyList(Triangles2Polylist((triangles)mitem), mesh, prim, primTransform);
                                    }
                                    if (mitem is polylist)
                                    {
                                        AddFacesFromPolyList((polylist)mitem, mesh, prim, primTransform);
                                    }
                                }

                                if (mesh_asset == null)
                                {
                                    prim.CreateAsset(UUID.Zero);
                                    mesh_asset = prim.Asset;
                                }
                                else
                                {
                                    prim.Asset = mesh_asset;
                                }
                            }
                        }
                    }
                }
            }

            return(Prims);
        }