/// <summary>
        /// Creates the default resource for the given type name..
        /// </summary>
        /// <typeparam name="T">The type for which the generic resource should be created.</typeparam>
        internal static T CreateDefaultResource <T>()
            where T : Resource
        {
            var resourceType = typeof(T);
            T?  result       = null;

            // Try to create default resources
            if (resourceType == typeof(MaterialResource))
            {
                result = new StandardMaterialResource() as T;
            }
            else if (resourceType == typeof(TextureResource))
            {
                result = new StandardTextureResource(
                    new AssemblyResourceLink(
                        typeof(ResourceDictionary),
                        "SeeingSharp.Resources.Textures.Blank_16x16.png")) as T;
            }
            else if (resourceType == typeof(GeometryResource))
            {
                var dummyGeometry = new Geometry();
                dummyGeometry.FirstSurface.BuildCube(
                    Vector3.Zero,
                    new Vector3(1f, 1f, 1f))
                .SetVertexColor(Color4.White);
                result = new GeometryResource(dummyGeometry) as T;
            }

            //Try to create the resource using the standard constructor
            if (result == null)
            {
                var standardConstructor =
                    resourceType.GetTypeInfo().DeclaredConstructors
                    .FirstOrDefault(actConstructor => actConstructor.GetParameters().Length <= 0);
                if (standardConstructor != null)
                {
                    result = Activator.CreateInstance(resourceType) as T;
                }
            }

            if (result == null)
            {
                throw new SeeingSharpGraphicsException("Unable to create default resource for resource type " + resourceType.FullName);
            }
            return(result);
        }
Exemplo n.º 2
0
        private static void ProcessMaterials(ImportedModelContainer modelContainer, Scene scene)
        {
            var materialCount = scene.MaterialCount;

            for (var materialIndex = 0; materialIndex < materialCount; materialIndex++)
            {
                var actMaterial = scene.Materials[materialIndex];

                modelContainer.AddResource(new ImportedResourceInfo(
                                               modelContainer.GetResourceKey("Material", materialIndex.ToString()),
                                               _ =>
                {
                    var materialResource = new StandardMaterialResource();
                    if (actMaterial.HasColorDiffuse)
                    {
                        materialResource.UseVertexColors      = false;
                        materialResource.MaterialDiffuseColor =
                            AssimpHelper.Color4FromAssimp(actMaterial.ColorDiffuse);
                    }
                    return(materialResource);
                }));
            }
        }