Esempio n. 1
0
        public void TestParameters()
        {
            var materialAsset = AssetSerializer.Load <MaterialAsset>("materials/testMaterial.pdxmat");
            var material      = materialAsset.Material;

            var parameterCreator = new MaterialParametersCreator(material, "testMaterial.pdxmat");

            parameterCreator.CreateParameterCollectionData();
            var parameters = parameterCreator.Parameters;

            Assert.IsTrue(parameters.ContainsKey((MaterialParameters.AlbedoDiffuse)));
            Assert.IsTrue(parameters.ContainsKey((MaterialParameters.AlbedoSpecular)));
            Assert.IsTrue(parameters.ContainsKey((MaterialParameters.NormalMap)));
            Assert.IsTrue(parameters.ContainsKey((MaterialParameters.DisplacementMap)));
            Assert.AreEqual(MaterialDiffuseModel.Lambert, parameters[MaterialParameters.DiffuseModel]);
            Assert.AreEqual(MaterialSpecularModel.BlinnPhong, parameters[MaterialParameters.SpecularModel]);
            Assert.AreEqual(MaterialShadingModel.Phong, parameters[MaterialParameters.ShadingModel]);
            Assert.AreEqual(new Color4(0.4f, 0.1f, 1, 1), parameters[MaterialKeys.SpecularColorValue]);
            Assert.AreEqual(new Color4(1, 1, 0.5f, 0.5f), parameters[MaterialKeys.DiffuseColorValue]);
            Assert.AreEqual(0, parameters[MaterialKeys.SpecularIntensity]);
            Assert.AreEqual(0, parameters[MaterialKeys.SpecularPower]);
            Assert.IsTrue(parameters.ContainsKey((TexturingKeys.Sampler0)));
            Assert.IsTrue(parameters.ContainsKey((TexturingKeys.Texture0)));
            Assert.IsTrue(parameters.ContainsKey((TexturingKeys.Texture1)));
            Assert.IsTrue(parameters.ContainsKey((TexturingKeys.Texture2)));
            Assert.IsTrue(parameters.ContainsKey((TexturingKeys.Texture3)));
            Assert.AreEqual(16, parameters.Count);
        }
Esempio n. 2
0
        public void TestTextureGeneric()
        {
            var materialAsset = AssetSerializer.Load <MaterialAsset>("materials/testTextureGeneric.pdxmat");

            var parameterCreator = new MaterialParametersCreator(materialAsset.Material, "testTextureGeneric.pdxmat");

            parameterCreator.CreateParameterCollectionData();
            var allParameters = parameterCreator.Parameters;

            // TODO: remove Sampler0 (extra sampler)
            Assert.IsTrue(allParameters.ContainsKey(TexturingKeys.Sampler0));
            Assert.IsTrue(allParameters.ContainsKey(TexturingKeys.Sampler1));
            Assert.IsTrue(allParameters.ContainsKey(TexturingKeys.Texture0));
            Assert.IsFalse(allParameters.ContainsKey(TexturingKeys.Texture1));

            var sampler = allParameters[TexturingKeys.Sampler1] as ContentReference <SamplerState>;

            Assert.NotNull(sampler);
            Assert.AreEqual(TextureAddressMode.Mirror, sampler.Value.Description.AddressU);
            Assert.AreEqual(TextureAddressMode.Clamp, sampler.Value.Description.AddressV);
            Assert.AreEqual(TextureFilter.Anisotropic, sampler.Value.Description.Filter);
        }
Esempio n. 3
0
            protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                var material = asset.Material.Clone();

                // Replace all empty Texture nodes by black color texture nodes (allow a display of the element even if material is incomplete)
                var emptyTextureNodeKeys = material.Nodes.Where(m => m.Value is MaterialTextureNode && !IsTextureReferenceValid((MaterialTextureNode)m.Value)).Select(m => m.Key).ToList();

                foreach (var emptyTextureNodeKey in emptyTextureNodeKeys)
                {
                    commandContext.Logger.Warning("Texture node '{0}' of material '{1}' is not pointing to a valid texture reference. " +
                                                  "This node will be replaced by black color Node.", emptyTextureNodeKey, assetItem.Location);
                    material.Nodes[emptyTextureNodeKey] = new MaterialColorNode(new Color4(0));
                }

                // Reduce trees on CPU
                var materialReducer = new MaterialTreeReducer(material);

                materialReducer.ReduceTrees();

                foreach (var reducedTree in materialReducer.ReducedTrees)
                {
                    material.Nodes[reducedTree.Key] = reducedTree.Value;
                }

                // Reduce on GPU
                // TODO: Adapt GPU reduction so that it is compatible Android color/alpha separation
                // TODO: Use the build engine processed output textures instead of the imported one (not existing any more)
                // TODO: Set the reduced texture output format
                // TODO: The graphics device cannot be shared with the Previewer
                //var graphicsDevice = (GraphicsDevice)context.Attributes.GetOrAdd(CompilerContext.GraphicsDeviceKey, key => GraphicsDevice.New(DeviceCreationFlags.None, GraphicsProfile.Level_11_0));
                //using (var materialTextureLayerFlattener = new MaterialTextureLayerFlattener(material, graphicsDevice))
                //{
                //    materialTextureLayerFlattener.PrepareForFlattening(new UDirectory(assetUrl.Directory));
                //    if (materialTextureLayerFlattener.HasCommands)
                //    {
                //        var compiler = EffectCompileCommand.GetOrCreateEffectCompiler(context);
                //        materialTextureLayerFlattener.Run(compiler);
                //        // store Material with modified textures
                //        material = materialTextureLayerFlattener.Material;
                //    }
                //}

                // Separate the textures into color/alpha components on Android to be able to use native ETC1 compression
                if (context.Platform == PlatformType.Android)
                {
                    var alphaComponentSplitter = new TextureAlphaComponentSplitter(assetItem.Package.Session);
                    material = alphaComponentSplitter.Run(material, new UDirectory(assetUrl.GetDirectory())); // store Material with alpha substituted textures
                }

                // Create the parameters
                var materialParameterCreator = new MaterialParametersCreator(material, assetUrl);

                if (materialParameterCreator.CreateParameterCollectionData(commandContext.Logger))
                {
                    return(Task.FromResult(ResultStatus.Failed));
                }

                var materialData = new MaterialData {
                    Parameters = materialParameterCreator.Parameters
                };

                var assetManager = new AssetManager();

                assetManager.Save(assetUrl, materialData);

                return(Task.FromResult(ResultStatus.Successful));
            }