示例#1
0
        public IDAESceneNode Load(string fileName)
        {
            var loader = new DAELoaderNode(this);

            loader.Open(fileName);
            return(loader.GetSceneGraph());
        }
示例#2
0
        private DAEGeometry CreateGeometry(DAELoaderNode loader, geometry geo)
        {
            DAEGeometry result = new DAEGeometry(loader, geo);

            _geometries.Add(geo.id, result);
            return(result);
        }
示例#3
0
        private IDAEMaterial CreateMaterial(DAELoaderNode loader, material mat)
        {
            DAEEffect    efx = loader.LibEffects.GetEffect(loader, DAEUtils.GetUrl(mat.instance_effect.url).Id);
            IDAEMaterial m   = efx.CreateMaterial(loader);

            //TODO set parameter

            _materials.Add(mat.id, m);

            return(m);
        }
示例#4
0
        internal IDAESceneNode GetLightNode(DAELoaderNode loader, string id)
        {
            foreach (light l in _lib.light)
            {
                if (l.id.Equals(id))
                {
                    return(CreateLightNode(loader, l));
                }
            }

            return(null);
        }
        internal IDAESceneNode GetSceneNode(DAELoaderNode loader, string id)
        {
            foreach (visual_scene scene in _lib.visual_scene)
            {
                if (id.Equals(scene.id))
                {
                    return(CreateSceneNode(loader, scene));
                }
            }

            return(null);
        }
        private IDAESceneNode CreateSceneNode(DAELoaderNode loader, visual_scene scene)
        {
            List <IDAESceneNode> childs = new List <IDAESceneNode>();

            foreach (node n in scene.node)
            {
                DAENode       node      = new DAENode(n);
                IDAESceneNode sceneNode = node.GetSceneNode(loader);
                if (sceneNode != null)
                {
                    childs.Add(sceneNode);
                }
            }

            return(loader.Context.CreateGroupNode(scene.id, DAEMatrix4.Identity, childs.ToArray()));
        }
示例#7
0
        internal DAELoaderNode GetLoaderForUrl(DAEUrl url)
        {
            if (!string.IsNullOrEmpty(url.FilePath))
            {
                string path     = url.FilePath;
                string filePath = Path.GetDirectoryName(this.FileName).Replace(@"\", "/");
                path = filePath + "/" + path.Replace("file:///", "");
                if (!_subLoader.ContainsKey(path))
                {
                    _subLoader[path] = new DAELoaderNode(this.Context);
                    _subLoader[path].Open(path);
                }
                //_subLoader[path].Open(path);
                return(_subLoader[path]);
            }

            return(this);
        }
示例#8
0
        internal DAEGeometry GetGeometry(DAELoaderNode loader, string id)
        {
            if (_geometries.ContainsKey(id))
            {
                return(_geometries[id]);
            }
            else
            {
                foreach (geometry geo in _lib.geometry)
                {
                    if (geo.id.Equals(id))
                    {
                        return(CreateGeometry(loader, geo));
                    }
                }
            }

            return(null);
        }
示例#9
0
        internal IDAESceneNode GetSceneNode(DAELoaderNode loader, string id)
        {
            if (_nodes.ContainsKey(id))
            {
                return(_nodes[id].GetSceneNode(loader));
            }
            else
            {
                foreach (node n in _lib.node)
                {
                    if (n.id.Equals(id))
                    {
                        return(CreateDAENode(n).GetSceneNode(loader));
                    }
                }
            }

            return(null);
        }
示例#10
0
        internal IDAEMaterial GetMaterial(DAELoaderNode loader, string id)
        {
            if (_materials.ContainsKey(id))
            {
                return(_materials[id]);
            }
            else
            {
                foreach (material mat in _lib.material)
                {
                    if (mat.id.Equals(id))
                    {
                        return(CreateMaterial(loader, mat));
                    }
                }
            }

            return(null);
        }
示例#11
0
        internal List <IDAEShapeNode> GetShapeNodes(DAELoaderNode loader, Dictionary <string, string> instanceMaterials, string parentNodeName)
        {
            List <IDAEShapeNode> result = new List <IDAEShapeNode>();

            foreach (DAETriangles tris in _triangles)
            {
                IDAEMaterial mat;
                if (tris.MaterialSymbol != null)
                {
                    mat = loader.LibMaterials.GetMaterial(loader, DAEUtils.GetUrl(instanceMaterials[tris.MaterialSymbol]).Id);
                }
                else
                {
                    mat = loader.LibMaterials.GetDefaultMaterial(loader);
                }
                result.Add(loader.Context.CreateShapeNode(parentNodeName, mat, tris.Geometry));
            }

            return(result);
        }
示例#12
0
        private string CreateTexturePath(DAELoaderNode loader, image img)
        {
            string tex = null;

            if (img.Item is string)
            {
                string path     = img.Item as string;
                string filePath = Path.GetDirectoryName(loader.FileName).Replace(@"\", "/");
                path = filePath + "/" + path.Replace("file:///", "");
                if (File.Exists(path))
                {
                    tex = path;
                }
                else
                {
                    loader.Context.LogError(this, "Image not found: " + path);
                }
            }

            _textures.Add(img.id, tex);
            return(tex);
        }
示例#13
0
        internal string GetTexturePath(DAELoaderNode loader, string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(null);
            }

            if (_textures.ContainsKey(id))
            {
                return(_textures[id]);
            }
            else if (!string.IsNullOrEmpty(id))
            {
                foreach (image img in _lib.image)
                {
                    if (img.id.Equals(id))
                    {
                        return(CreateTexturePath(loader, img));
                    }
                }
            }

            return(null);
        }
示例#14
0
 internal IDAEMaterial GetDefaultMaterial(DAELoaderNode loader)
 {
     return(DAEEffect.GetDefaultMaterial(loader));
 }
示例#15
0
        private IDAESceneNode CreateLightNode(DAELoaderNode loader, light l)
        {
            IDAESceneNode result = null;

            if (l.technique_common.Item is lightTechnique_commonAmbient)
            {
                var ambLight = l.technique_common.Item as lightTechnique_commonAmbient;

                if (ambLight.color != null)
                {
                    result = loader.Context.CreateAmbientLightNode(l.name, GetColor(ambLight.color));
                }
            }
            else if (l.technique_common.Item is lightTechnique_commonDirectional)
            {
                var dirLight = l.technique_common.Item as lightTechnique_commonDirectional;

                if (dirLight.color != null)
                {
                    result = loader.Context.CreateDirectionalLightNode(l.name, GetColor(dirLight.color));
                }
            }
            else if (l.technique_common.Item is lightTechnique_commonPoint)
            {
                var pointLight = l.technique_common.Item as lightTechnique_commonPoint;

                DAEVector4 color;
                if (pointLight.color != null)
                {
                    color = GetColor(pointLight.color);
                }
                else
                {
                    color = new DAEVector4(1, 1, 1, 1);
                }

                double constantAttenuation = 1;
                if (pointLight.constant_attenuation != null)
                {
                    constantAttenuation = pointLight.constant_attenuation.Value;
                }

                double linearAttenuation = 0;
                if (pointLight.linear_attenuation != null)
                {
                    linearAttenuation = pointLight.linear_attenuation.Value;
                }

                double quadraticAttenuation = 0;
                if (pointLight.quadratic_attenuation != null)
                {
                    quadraticAttenuation = pointLight.quadratic_attenuation.Value;
                }

                result = loader.Context.CreatePointLightNode(l.name, color, constantAttenuation, linearAttenuation, quadraticAttenuation);
            }
            else if (l.technique_common.Item is lightTechnique_commonSpot)
            {
                var spotLight = l.technique_common.Item as lightTechnique_commonSpot;

                DAEVector4 color;
                if (spotLight.color != null)
                {
                    color = GetColor(spotLight.color);
                }
                else
                {
                    color = new DAEVector4(1, 1, 1, 1);
                }

                double constantAttenuation = 1;
                if (spotLight.constant_attenuation != null)
                {
                    constantAttenuation = spotLight.constant_attenuation.Value;
                }

                double linearAttenuation = 0;
                if (spotLight.linear_attenuation != null)
                {
                    linearAttenuation = spotLight.linear_attenuation.Value;
                }

                double quadraticAttenuation = 0;
                if (spotLight.quadratic_attenuation != null)
                {
                    quadraticAttenuation = spotLight.quadratic_attenuation.Value;
                }

                double spotExponent = 0;
                if (spotLight.falloff_exponent != null)
                {
                    spotExponent = spotLight.falloff_exponent.Value;
                }

                double spotCutOff = System.Math.PI; // default is 180°
                if (spotLight.falloff_angle != null)
                {
                    spotCutOff = spotLight.falloff_angle.Value / 2;
                }
                else if (l.extra != null && l.extra.Length > 0)
                {
                    foreach (var extra in l.extra)
                    {
                        if (extra.technique != null && extra.technique.Length > 0)
                        {
                            foreach (var extraTech in extra.technique)
                            {
                                if (extraTech.Any != null && extraTech.Any.Length > 0)
                                {
                                    foreach (var ele in extraTech.Any)
                                    {
                                        if (ele.ChildNodes != null)
                                        {
                                            for (int i = 0; i < ele.ChildNodes.Count; i++)
                                            {
                                                var n = ele.ChildNodes[i];
                                                if (n.Name == "falloff")
                                                {
                                                    float angle = 0;
                                                    if (float.TryParse(n.InnerText, NumberStyles.Any, CultureInfo.InvariantCulture, out angle))
                                                    {
                                                        spotCutOff = ((double)angle * System.Math.PI / 180) / 2;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                result = loader.Context.CreateSpotLightNode(l.name, color, constantAttenuation, linearAttenuation, quadraticAttenuation, spotExponent, spotCutOff);
            }

            return(result);
        }
示例#16
0
 internal DAETriangles(DAELoaderNode loader, DAEVertexDescription[] vertices, string materialSymbol)
 {
     this.Geometry       = loader.Context.CreateGeometry(vertices);
     this.MaterialSymbol = materialSymbol;
 }
示例#17
0
 internal IDAEMaterial CreateMaterial(DAELoaderNode loader)
 {
     return(loader.Context.CreateMaterial(_ambient, _diffuse, _specular, _shininess, _isTransparent, loader.LibImages.GetTexturePath(loader, _imageId)));
 }
示例#18
0
        internal DAEGeometry(DAELoaderNode loader, geometry geo)
        {
            if (geo.Item == null || !(geo.Item is mesh))
            {
                // empty or not supported geometry
                return;
            }

            mesh m = geo.Item as mesh;

            // load sources
            Dictionary <string, MeshSource> sources = new Dictionary <string, MeshSource>();

            foreach (source src in m.source)
            {
                if (src.Item is float_array)
                {
                    float_array values = src.Item as float_array;
                    int         stride = (int)src.technique_common.accessor.stride;
                    sources.Add(src.id, new MeshSource(values.Values, stride));
                }
            }

            // load positions
            foreach (InputLocal input in m.vertices.input)
            {
                if (input.semantic.Equals("POSITION"))
                {
                    sources.Add(m.vertices.id, sources[DAEUtils.GetUrl(input.source).Id]);
                    break;
                }
            }

            // load primitives
            foreach (var item in m.Items)
            {
                // load triangles
                if (item is triangles)
                {
                    triangles tris = item as triangles;

                    DAEVertexDescription[] vertices = new DAEVertexDescription[tris.count * 3];
                    int[] p        = DAEUtils.StringToIntArray(tris.p);
                    int   stepSize = p.Length / ((int)tris.count * 3);
                    foreach (InputLocalOffset input in tris.input)
                    {
                        SetVertices(input, p, stepSize, sources, vertices);
                    }

                    if (!ContainsTangents(tris.input))
                    {
                        GenerateMeshTangents(vertices);
                    }

                    _triangles.Add(new DAETriangles(loader, vertices, tris.material));
                }
                else if (item is polylist)
                {
                    polylist polys = item as polylist;

                    int[] polyVertexCounts = DAEUtils.StringToIntArray(polys.vcount);
                    int   vertexCount      = 0;
                    int   triCount         = 0;
                    for (int i = 0; i < polyVertexCounts.Length; i++)
                    {
                        vertexCount += polyVertexCounts[i];
                        triCount    += polyVertexCounts[i] - 2;
                    }

                    DAEVertexDescription[] polyVertices = new DAEVertexDescription[vertexCount];
                    int[] p        = DAEUtils.StringToIntArray(polys.p);
                    int   stepSize = p.Length / vertexCount;

                    foreach (InputLocalOffset input in polys.input)
                    {
                        SetVertices(input, p, stepSize, sources, polyVertices);
                    }

                    // triangulation
                    DAEVertexDescription[] triVertices = new DAEVertexDescription[triCount * 3];
                    int triVertexIdx  = 0;
                    int polyVertexIdx = 0;
                    for (int i = 0; i < polyVertexCounts.Length; i++)
                    {
                        int triVertex = 0;
                        for (int k = 0; k < polyVertexCounts[i]; k++)
                        {
                            if (triVertex == 3)
                            {
                                triVertex = 1;
                                k--;

                                // repeat first vertex
                                triVertices[triVertexIdx++] = polyVertices[polyVertexIdx];
                            }

                            triVertices[triVertexIdx++] = polyVertices[polyVertexIdx + k];
                            triVertex++;
                        }

                        // skip to next polygon
                        polyVertexIdx += polyVertexCounts[i];
                    }

                    if (!ContainsTangents(polys.input))
                    {
                        GenerateMeshTangents(triVertices);
                    }

                    _triangles.Add(new DAETriangles(loader, triVertices, polys.material));
                }
            }
        }
示例#19
0
 internal static IDAEMaterial GetDefaultMaterial(DAELoaderNode loader)
 {
     return(loader.Context.CreateMaterial(new DAEVector4(1, 1, 1, 1), new DAEVector4(1, 1, 1, 1), new DAEVector4(0.5f, 0.5f, 0.5f, 1), 50, false, null));
 }
示例#20
0
        internal IDAESceneNode GetSceneNode(DAELoaderNode loader)
        {
            List <IDAESceneNode> children = new List <IDAESceneNode>();

            // load geometry
            if (_node.instance_geometry != null && _node.instance_geometry.Length > 0)
            {
                foreach (var instGeo in _node.instance_geometry)
                {
                    Dictionary <string, string> instanceMaterials = new Dictionary <string, string>();
                    if (instGeo.bind_material != null)
                    {
                        foreach (instance_material instMat in instGeo.bind_material.technique_common)
                        {
                            instanceMaterials.Add(instMat.symbol, instMat.target);
                        }
                    }

                    DAEGeometry          geo    = loader.LibGeometries.GetGeometry(loader, DAEUtils.GetUrl(instGeo.url).Id);
                    List <IDAEShapeNode> shapes = geo.GetShapeNodes(loader, instanceMaterials, _node.name);

                    foreach (IDAEShapeNode shape in shapes)
                    {
                        children.Add(shape);
                    }
                }
            }

            // load lights
            if (_node.instance_light != null && _node.instance_light.Length > 0)
            {
                foreach (var instLight in _node.instance_light)
                {
                    IDAESceneNode lightNode = loader.LibLights.GetLightNode(loader, DAEUtils.GetUrl(instLight.url).Id);
                    if (lightNode != null)
                    {
                        children.Add(lightNode);
                    }
                }
            }

            // load local children
            if (_node.node1 != null && _node.node1.Length > 0)
            {
                foreach (node child in _node.node1)
                {
                    DAENode       n         = new DAENode(child);
                    IDAESceneNode childNode = n.GetSceneNode(loader);
                    if (childNode != null)
                    {
                        children.Add(childNode);
                    }
                }
            }

            // load remote children
            if (_node.instance_node != null)
            {
                foreach (InstanceWithExtra child in _node.instance_node)
                {
                    IDAESceneNode childNode = null;
                    var           url       = DAEUtils.GetUrl(child.url);
                    if (string.IsNullOrEmpty(url.FilePath))
                    {
                        childNode = loader.LibNodes.GetSceneNode(loader, url.Id);
                    }
                    else
                    {
                        var extLoader = loader.GetLoaderForUrl(url);
                        childNode = extLoader.GetSceneGraph(); //TODO only load node with url.Id
                    }
                    if (childNode != null)
                    {
                        children.Add(childNode);
                    }
                }
            }

            // load transformation
            DAEMatrix4 finalTrans = DAEMatrix4.Identity;

            if (_node.Items != null)
            {
                for (int i = 0; i < _node.Items.Length; i++)
                {
                    object           trans     = _node.Items[i];
                    ItemsChoiceType2 transType = _node.ItemsElementName[i];

                    if (transType == ItemsChoiceType2.matrix)
                    {
                        matrix     m = trans as matrix;
                        DAEMatrix4 k = DAEMatrix4.Identity;
                        k.M11       = m.Values[0];
                        k.M12       = m.Values[1];
                        k.M13       = m.Values[2];
                        k.M14       = m.Values[3];
                        k.M21       = m.Values[4];
                        k.M22       = m.Values[5];
                        k.M23       = m.Values[6];
                        k.M24       = m.Values[7];
                        k.M31       = m.Values[8];
                        k.M32       = m.Values[9];
                        k.M33       = m.Values[10];
                        k.M34       = m.Values[11];
                        k.M41       = m.Values[12];
                        k.M42       = m.Values[13];
                        k.M43       = m.Values[14];
                        k.M44       = m.Values[15];
                        finalTrans *= k;
                    }
                    else if (transType == ItemsChoiceType2.rotate)
                    {
                        rotate r = trans as rotate;
                        finalTrans *= DAEMatrix4.Rotation(r.Values[0], r.Values[1], r.Values[2], (r.Values[3] * System.Math.PI / 180));
                    }
                    else if (transType == ItemsChoiceType2.lookat)
                    {
                        //lookat l = trans as lookat;
                        //finalTrans *= SharpDX.Matrix.LookAtLH(
                        //    new SharpDX.Vector3((float)l.Values[0], (float)l.Values[1], (float)l.Values[2]),
                        //    new SharpDX.Vector3((float)l.Values[3], (float)l.Values[4], (float)l.Values[5]),
                        //    new SharpDX.Vector3((float)l.Values[6], (float)l.Values[7], (float)l.Values[8]));

                        // not implemented
                    }
                    else if (transType == ItemsChoiceType2.scale)
                    {
                        TargetableFloat3 s = trans as TargetableFloat3;
                        finalTrans *= DAEMatrix4.Scaling(s.Values[0], s.Values[1], s.Values[2]);
                    }
                    else if (transType == ItemsChoiceType2.translate)
                    {
                        TargetableFloat3 t = trans as TargetableFloat3;
                        finalTrans *= DAEMatrix4.Translation(t.Values[0], t.Values[1], t.Values[2]);
                    }
                    else if (transType == ItemsChoiceType2.skew)
                    {
                        // not implemented
                    }
                }
            }

            return(loader.Context.CreateGroupNode(_node.name, finalTrans, children.ToArray()));
        }
示例#21
0
 internal DAEEffect GetEffect(DAELoaderNode loader, string id)
 {
     return(_effects[id]);
 }