protected IEnumerable <CompilerParameters> GenerateKeysPermutation(CompilerParameters parameters, IList <KeyValuePair <ParameterKey, List <object> > > parameterKeys, int keyIndex = 0)
 {
     // TODO: Merge this code with the code found in DefaultCompilerParametersGenerator
     if (keyIndex >= parameterKeys.Count)
     {
         yield return(parameters.Clone());
     }
     else
     {
         var keyValues = parameterKeys[keyIndex];
         // Duplicate new parameters collection only for the first level
         if (keyIndex == 0)
         {
             parameters = parameters.Clone();
         }
         foreach (var value in keyValues.Value)
         {
             parameters.SetObject(keyValues.Key, value);
             foreach (var returnParameters in GenerateKeysPermutation(parameters, parameterKeys, keyIndex + 1))
             {
                 yield return(returnParameters);
             }
         }
     }
 }
        public override IEnumerable <CompilerParameters> Generate(AssetCompilerContext context, CompilerParameters baseParameters, ILogger log)
        {
            var allMaterialParameters = new List <ParameterCollection>();

            if (baseParameters.Get(MaterialAssetKeys.GenerateShader))
            {
                var assetManager = new AssetManager();
                var settings     = new AssetManagerLoaderSettings()
                {
                    ContentFilter = AssetManagerLoaderSettings.NewContentFilterByType(typeof(MaterialData)),
                };

                var hashParameters = new HashSet <ObjectId>();

                foreach (var materialAssetItem in context.Package.Assets.Where(item => item.Asset is MaterialAsset))
                {
                    var assetPath = materialAssetItem.Location.GetDirectoryAndFileName();
                    try
                    {
                        var materialData = assetManager.Load <MaterialData>(assetPath, settings);
                        if (materialData != null && materialData.Parameters != null && materialData.Parameters.Count > 0)
                        {
                            var materialParameters = new ParameterCollection();
                            AddToParameters(materialData.Parameters, materialParameters);
                            if (materialParameters.Count > 0)
                            {
                                byte[] buffer1;
                                var    id = ObjectId.FromObject(materialParameters, out buffer1);
                                if (!hashParameters.Contains(id))
                                {
                                    hashParameters.Add(id);
                                    allMaterialParameters.Add(materialParameters);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error while loading material [{0}]", ex, assetPath);
                    }
                }
            }

            if (allMaterialParameters.Count != 0)
            {
                foreach (var materialParams in allMaterialParameters)
                {
                    var compilerParameters = baseParameters.Clone();
                    materialParams.CopyTo(compilerParameters);
                    yield return(compilerParameters);
                }
            }
            else
            {
                yield return(baseParameters.Clone());
            }
        }
 public IEnumerable <CompilerParameters> Generate(AssetCompilerContext context, CompilerParameters baseParameters, ILogger log)
 {
     return(new List <CompilerParameters>()
     {
         baseParameters.Clone()
     });
 }
 protected IEnumerable <CompilerParameters> GenerateKeysPermutation(CompilerParameters parameters, IList <KeyValuePair <ParameterKey, List <object> > > parameterKeys, int keyIndex = 0)
 {
     if (keyIndex >= parameterKeys.Count)
     {
         yield return(parameters.Clone());
     }
     else
     {
         var keyValues = parameterKeys[keyIndex];
         // Create one CompilerParameter per keys
         parameters = parameters.Clone();
         foreach (var value in keyValues.Value)
         {
             parameters.SetObject(keyValues.Key, value);
             foreach (var returnParameters in GenerateKeysPermutation(parameters, parameterKeys, keyIndex + 1))
             {
                 yield return(returnParameters);
             }
         }
     }
 }
        public override IEnumerable <CompilerParameters> Generate(AssetCompilerContext context, CompilerParameters baseParameters, ILogger log)
        {
            // Cache all the entity parameters once
            List <EntityParameters> entityParametersList;
            var entityParametersSet = (ConcurrentDictionary <Guid, List <EntityParameters> >)context.Properties.GetOrAdd(EntityParametersKey, key => new ConcurrentDictionary <Guid, List <EntityParameters> >());

            entityParametersList = entityParametersSet.GetOrAdd(context.Package.Id, key =>
            {
                var assetManager = new AssetManager();

                var settings = new AssetManagerLoaderSettings()
                {
                    ContentFilter = AssetManagerLoaderSettings.NewContentFilterByType(typeof(ModelData), typeof(MeshData), typeof(MaterialData), typeof(LightingConfigurationsSetData)),
                };

                var allEntityParameters = new List <EntityParameters>();
                foreach (var entityAssetItem in context.Package.Assets.Where(item => item.Asset is EntityAsset))
                {
                    var assetPath = entityAssetItem.Location.GetDirectoryAndFileName();
                    try
                    {
                        var entity = assetManager.Load <EntityData>(assetPath, settings);

                        foreach (var modelComponent in entity.Components.Select(x => x.Value).OfType <ModelComponentData>())
                        {
                            foreach (var meshData in modelComponent.Model.Value.Meshes)
                            {
                                var lightingParameters = GetLightingParameters(meshData);
                                var materialParameters = GetMeshMaterialParameters(meshData);

                                if (lightingParameters == null || lightingParameters.Count == 0)
                                {
                                    EntityParameters entityParameters;
                                    entityParameters.MaterialParameters = materialParameters;
                                    entityParameters.ModelParameters    = modelComponent.Parameters;
                                    entityParameters.MeshParameters     = meshData != null ? meshData.Parameters : null;
                                    entityParameters.LightingParameters = null;
                                    allEntityParameters.Add(entityParameters);
                                }
                                else
                                {
                                    foreach (var lightConfig in lightingParameters)
                                    {
                                        EntityParameters entityParameters;
                                        entityParameters.MaterialParameters = materialParameters;
                                        entityParameters.ModelParameters    = modelComponent.Parameters;
                                        entityParameters.MeshParameters     = meshData != null ? meshData.Parameters : null;
                                        entityParameters.LightingParameters = lightConfig;
                                        allEntityParameters.Add(entityParameters);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error while loading model mesh [{0}]", ex, assetPath);
                    }
                }

                return(allEntityParameters);
            });

            var useMeshParameters     = baseParameters.Get(MeshKeys.UseParameters);
            var useMaterialParameters = baseParameters.Get(MaterialAssetKeys.UseParameters) && !baseParameters.Get(MaterialAssetKeys.GenerateShader);

            if ((useMeshParameters || useMaterialParameters) && entityParametersList.Count != 0)
            {
                var hashParameters = new HashSet <ObjectId>();

                foreach (var entityParameters in entityParametersList)
                {
                    // Add parameters in this order
                    // 1. Material
                    // 2. ModelComponent (Entity)
                    // 3. Mesh
                    // 4. Lighting

                    var newParameters = new ParameterCollection();
                    if (useMaterialParameters)
                    {
                        AddToParameters(entityParameters.MaterialParameters, newParameters);
                    }

                    AddToParameters(entityParameters.ModelParameters, newParameters);

                    if (useMeshParameters)
                    {
                        AddToParameters(entityParameters.MeshParameters, newParameters);
                    }

                    AddToParameters(entityParameters.LightingParameters, newParameters);

                    byte[] buffer1;
                    var    id = ObjectId.FromObject(newParameters, out buffer1);
                    if (!hashParameters.Contains(id))
                    {
                        hashParameters.Add(id);
                        var compilerParameters = baseParameters.Clone();
                        newParameters.CopyTo(compilerParameters);
                        yield return(compilerParameters);
                    }
                }
            }
            else
            {
                yield return(baseParameters.Clone());
            }
        }
Ejemplo n.º 6
0
 public IEnumerable <CompilerParameters> Generate(AssetCompilerContext context, CompilerParameters parameters, ILogger log)
 {
     yield return(parameters.Clone());
 }