Ejemplo n.º 1
0
        internal void ProcessChildren(Object3D obj = null)
        {
            var children = new List <IElement>();

            if (obj == null)
            {
                children = Children;
            }
            else
            {
                children = obj.Children;

                if (obj is Group)
                {
                    if (!(obj.Parent is Group) && obj.Parent.Parent == null)
                    {
                        SerializationAdaptor.Object.Children.Add(obj);
                    }
                    else
                    if (obj.Parent.Parent == null)
                    {
                        SerializationAdaptor.Object.Children.Add(obj);
                    }
                }
            }

            foreach (var child in children)
            {
                Debug.WriteLine((child as Element).Type, "ThreeLib");

                if (child.GetType() == typeof(Group))
                {
                    ProcessChildren(child as Group);
                }

                else
                {
                    if (child.GetType().GetProperty("Geometry") != null)
                    {
                        var geoId      = Guid.Empty;
                        var currentGeo = child.GetType().GetProperty("Geometry").GetValue(child, null);

                        switch (currentGeo.GetType().Name)
                        {
                        case "BufferGeometry":
                            (currentGeo as BufferGeometry).Uuid = SerializationAdaptor.BufferGeometries.AddIfNew(currentGeo as BufferGeometry);
                            break;

                        case "Geometry":
                            (currentGeo as Geometry).Uuid = SerializationAdaptor.Geometries.AddIfNew(currentGeo as Geometry);
                            break;

                        case "SphereBufferGeometry":
                            (currentGeo as SphereBufferGeometry).Uuid = SerializationAdaptor.Geometries.AddIfNew(currentGeo as SphereBufferGeometry);
                            break;

                        case "TextBufferGeometry":
                            var font = SerializationAdaptor.Fonts.AddIfNew((currentGeo as TextBufferGeometry).Parameters.Font.FontData);

                            (currentGeo as TextBufferGeometry).Parameters.Font.Data = font;
                            (currentGeo as TextBufferGeometry).Uuid = SerializationAdaptor.Geometries.AddIfNew(currentGeo as TextBufferGeometry);
                            break;

                        default:
                            //other derivatives of Geometry
                            //geoId = SerializationAdaptor.Geometries.AddIfNew(currentGeo as Geometry);
                            break;
                        }
                    }

                    if (child.GetType().GetProperty("Children").GetValue(child, null) is List <IElement> objChildren && objChildren.Count > 0)
                    {
                        ProcessChildren(child as Object3D);
                    }

                    switch ((child as Element).Type)
                    {
                    case "Mesh":

                        var mesh = child as Mesh;

                        dynamic material = new Material();

                        if (mesh.Material is MeshStandardMaterial)
                        {
                            material = mesh.Material as MeshStandardMaterial;
                        }
                        else if (mesh.Material is MeshBasicMaterial)
                        {
                            material = mesh.Material as MeshBasicMaterial;
                        }

                        foreach (var kvp in material.GetTextures())
                        {
                            if (kvp.Value != null)
                            {
                                kvp.Value.Image.Uuid = SerializationAdaptor.Images.AddIfNew(kvp.Value.Image);
                                kvp.Value.Uuid       = SerializationAdaptor.Textures.AddIfNew(kvp.Value);
                            }
                        }

                        material.Uuid = SerializationAdaptor.Materials.AddIfNew(material);

                        if (obj == null)
                        {
                            SerializationAdaptor.Object.Children.Add(mesh);
                        }

                        break;

                    case "Line":

                        var line = child as Line;
                        (line.Material as LineBasicMaterial).Uuid = SerializationAdaptor.Materials.AddIfNew(line.Material as LineBasicMaterial);

                        if (obj == null)
                        {
                            SerializationAdaptor.Object.Children.Add(line);
                        }

                        break;

                    case "Points":

                        var points = child as Points;
                        (points.Material as PointsMaterial).Uuid = SerializationAdaptor.Materials.AddIfNew(points.Material as PointsMaterial);

                        if (obj == null)
                        {
                            SerializationAdaptor.Object.Children.Add(points);
                        }

                        break;

                    case "PointLight":
                    case "AmbientLight":
                    case "SpotLight":
                    case "DirectionalLight":
                    case "RectAreaLight":
                    case "HemisphereLight":
                    case "PerspectiveCamera":
                    case "OrthographicCamera":
                        if (obj == null)
                        {
                            SerializationAdaptor.Object.Children.Add(child);
                        }
                        break;

                    default:
                        Debug.WriteLine((child as Element).Type + "Not supported.", "ThreeLib");
                        break;
                    }
                }
            }

            SerializationAdaptor.Elements.AddRange(SerializationAdaptor.BufferGeometries);
            SerializationAdaptor.Elements.AddRange(SerializationAdaptor.Geometries);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 internal Object3DSerializationAdaptor()
 {
     Object             = new Object3D();
     Metadata.Generator = "ThreeLib-Object3D.toJSON";
 }
Ejemplo n.º 3
0
        internal void ProcessChildren(Object3D obj = null)
        {
            List <Object3D> children;

            if (obj == null)
            {
                children = Children;
            }
            else
            {
                children = obj.Children;

                if (obj is Group)
                {
                    if (!(obj.Parent is Group) && obj.Parent.Parent == null)
                    {
                        SerializationAdapter.Object.Children.Add(obj);
                    }
                }
                else
                {
                    if (obj.Parent.Parent == null)
                    {
                        SerializationAdapter.Object.Children.Add(obj);
                    }
                }
            }

            foreach (var child in children)
            {
                if (child is Group group)                 //childObj.GetType() == typeof(Group)
                {
                    ProcessChildren(group);
                }
                else
                {
                    if (child is IGeometryContainer geometryContainer)
                    {
                        var geometry = geometryContainer.Geometry;

                        //geometry.GetType().Name

                        var id = SerializationAdapter.Geometries.AddIfNew(geometry);
                        if (geometry is ITextGeometry textGeometry)
                        {
                            var font = SerializationAdapter.Fonts.AddIfNew(textGeometry.Parameters.Font.FontData);
                            textGeometry.Parameters.Font.Data = font;
                        }
                        geometry.Uuid = id;
                    }

                    if (child is Object3D obj3D && obj3D.Children.Count > 0)
                    {
                        //obj3D.ProcessChildren();
                        ProcessChildren(obj3D);
                    }

                    if (child is Mesh mesh)
                    {
                        if (mesh.Material is MeshStandardMaterial material)
                        {
                            foreach (var kvp in material.GetTextures())
                            {
                                if (kvp.Value != null)
                                {
                                    var imageId = SerializationAdapter.Images.AddIfNew(kvp.Value.Image);
                                    kvp.Value.Image.Uuid = imageId;
                                    var textureId = SerializationAdapter.Textures.AddIfNew(kvp.Value);
                                    kvp.Value.Uuid = textureId;
                                }
                            }

                            material.Uuid = SerializationAdapter.Materials.AddIfNew(material);
                        }

                        if (obj == null)
                        {
                            SerializationAdapter.Object.Children.Add(mesh);
                        }
                    }
                    else if (child is Line line)
                    {
                        SerializationAdapter.Materials.Add(line.Material as LineBasicMaterial);

                        if (obj == null)
                        {
                            SerializationAdapter.Object.Children.Add(line);
                        }
                    }
                    else if (child is LineSegments lineSegments)
                    {
                        SerializationAdapter.Materials.Add(lineSegments.Material as LineBasicMaterial);

                        if (obj == null)
                        {
                            SerializationAdapter.Object.Children.Add(lineSegments);
                        }
                    }
                    else if (child is Points points)
                    {
                        SerializationAdapter.Materials.Add(points.Material as PointsMaterial);

                        if (obj == null)
                        {
                            SerializationAdapter.Object.Children.Add(points);
                        }
                    }
                    else
                    {
                        if (obj == null)
                        {
                            SerializationAdapter.Object.Children.Add(child);
                        }
                        //Debug.WriteLine((child as Element).Type, "ThreeLib");

                        //switch ((child as Element).Type)
                        //{
                        //	case "PointLight":
                        //	case "AmbientLight":
                        //	case "SpotLight":
                        //	case "DirectionalLight":
                        //	case "HemisphereLight":
                        //	case "PerspectiveCamera":
                        //	case "OrthographicCamera":
                        //		break;
                        //	default:
                        //		Debug.WriteLine((child as Element).Type, "ThreeLib");
                        //		break;
                        //}
                    }
                }
            }
        }