Example #1
0
        public static Component_Material CreateMaterial(Component materialsGroup, MaterialData data)
        {
            //create material
            var material = materialsGroup.CreateComponent <Component_Material>(enabled: false);

            material.Name         = data.Name;
            material.ShadingModel = data.ShadingModel;
            material.TwoSided     = data.TwoSided;
            if (!string.IsNullOrEmpty(data.OpacityTexture))
            {
                material.BlendMode = Component_Material.BlendModeEnum.Masked;
            }

            //create shader graph
            Component_FlowGraph graph;
            {
                graph                = material.CreateComponent <Component_FlowGraph>();
                graph.Name           = material.Name + " shader graph";
                graph.Specialization = ReferenceUtility.MakeReference <Component_FlowGraphSpecialization>(null,
                                                                                                          MetadataManager.GetTypeOfNetType(typeof(Component_FlowGraphSpecialization_Shader)).Name + "|Instance");

                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name             = "Node " + material.Name;
                node.Position         = new Vector2I(10, -7);
                node.ControlledObject = ReferenceUtility.MakeThisReference(node, material);
            }

            const int step     = 9;
            Vector2I  position = new Vector2I(-20, -data.GetTextureCount() * step / 2);

            //BaseColor
            if (!string.IsNullOrEmpty(data.BaseColorTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "BaseColor";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.BaseColorTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.BaseColor = ReferenceUtility.MakeThisReference(material, sample, "RGBA");
            }
            else if (data.BaseColor.HasValue)
            {
                material.BaseColor = data.BaseColor.Value;
            }

            //Metallic
            if (!string.IsNullOrEmpty(data.MetallicTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "Metallic";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.MetallicTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.Metallic = ReferenceUtility.MakeThisReference(material, sample, "R");
            }

            //Roughness
            if (!string.IsNullOrEmpty(data.RoughnessTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "Roughness";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.RoughnessTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.Roughness = ReferenceUtility.MakeThisReference(material, sample, "R");
            }

            //Normal
            if (!string.IsNullOrEmpty(data.NormalTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "Normal";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.NormalTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.Normal = ReferenceUtility.MakeThisReference(material, sample, "RGBA");
            }

            //Displacement
            if (!string.IsNullOrEmpty(data.DisplacementTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "Displacement";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.DisplacementTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.Displacement = ReferenceUtility.MakeThisReference(material, sample, "R");
            }

            //AmbientOcclusion
            if (!string.IsNullOrEmpty(data.AmbientOcclusionTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "AmbientOcclusion";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.AmbientOcclusionTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.AmbientOcclusion = ReferenceUtility.MakeThisReference(material, sample, "R");
            }

            //Emissive
            if (!string.IsNullOrEmpty(data.EmissiveTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "Emissive";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.EmissiveTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.Emissive = ReferenceUtility.MakeThisReference(material, sample, "RGBA");
            }

            //Opacity
            if (!string.IsNullOrEmpty(data.OpacityTexture))
            {
                var node = graph.CreateComponent <Component_FlowGraphNode>();
                node.Name     = "Node Texture Sample " + "Opacity";
                node.Position = position;
                position.Y   += step;

                var sample = node.CreateComponent <Component_ShaderTextureSample>();
                sample.Name    = ComponentUtility.GetNewObjectUniqueName(sample);
                sample.Texture = new Reference <Component_Image>(null, data.OpacityTexture);

                node.ControlledObject = ReferenceUtility.MakeThisReference(node, sample);

                material.Opacity = ReferenceUtility.MakeThisReference(material, sample, "R");
            }

            material.Enabled = true;

            return(material);
        }