Exemplo n.º 1
0
        private ShaderEffect MakeMaterial(MaterialComponent mc)
        {
            WeightComponent   wc  = CurrentNode.GetWeights();
            ShaderCodeBuilder scb = new ShaderCodeBuilder(mc, null, wc); // TODO, CurrentNode.GetWeights() != null);
            var effectParameters  = AssembleEffectParamers(mc, scb);

            ShaderEffect ret = new ShaderEffect(new []
            {
                new EffectPassDeclaration()
                {
                    VS = scb.VS,
                    //VS = VsBones,
                    PS       = scb.PS,
                    StateSet = new RenderStateSet()
                    {
                        ZEnable          = true,
                        AlphaBlendEnable = false
                    }
                }
            },
                                                effectParameters
                                                );

            return(ret);
        }
Exemplo n.º 2
0
        public void SetContext(RenderContext rc)
        {
            if (rc == null)
            {
                throw new ArgumentNullException("rc");
            }

            if (rc != _rc)
            {
                _rc            = rc;
                _meshMap       = new Dictionary <MeshComponent, Mesh>();
                _matMap        = new Dictionary <MaterialComponent, ShaderEffect>();
                _boneMap       = new Dictionary <SceneNodeContainer, float4x4>();
                _defaultEffect = MakeMaterial(new MaterialComponent
                {
                    Diffuse = new MatChannelContainer()
                    {
                        Color = new float3(0.5f, 0.5f, 0.5f)
                    },
                    Specular = new SpecularChannelContainer()
                    {
                        Color     = new float3(1, 1, 1),
                        Intensity = 0.5f,
                        Shininess = 22
                    }
                });
                _defaultEffect.AttachToContext(_rc);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a ShaderEffectComponent from a MaterialComponent
        /// </summary>
        /// <param name="fxProps">The shader effect properties</param>
        /// <param name="wc">Only pass over a WeightComponent if you use bone animations in the current node (usage: pass currentNode.GetWeights())</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static ShaderEffect MakeShaderEffectFromShaderEffectProps(ShaderEffectProps fxProps, Weight wc = null)
        {
            fxProps.MeshProbs = AnalyzeMesh(null);

            var vs = CreateVertexShader(wc, fxProps);
            var ps = CreatePixelShader(fxProps);
            var effectParameters = AssembleEffectParamers(fxProps);

            if (string.IsNullOrEmpty(vs) || string.IsNullOrEmpty(ps))
            {
                throw new Exception("Material could not be evaluated or be built!");
            }

            var ret = new ShaderEffect(new[]
            {
                new EffectPassDeclaration
                {
                    VS       = vs,
                    PS       = ps,
                    StateSet = new RenderStateSet
                    {
                        ZEnable          = true,
                        AlphaBlendEnable = true,
                        SourceBlend      = Blend.SourceAlpha,
                        DestinationBlend = Blend.InverseSourceAlpha,
                        BlendOperation   = BlendOperation.Add,
                    }
                }
            },
                                       effectParameters
                                       );

            return(ret);
        }
Exemplo n.º 4
0
        public void BuildFragmentShaderFor(ShaderEffect shaderComponent)
        {
            if (shaderComponent.GetType() != typeof(ShaderEffectProtoPixel))
            {
                return;
            }

            var effect = (ShaderEffectProtoPixel)shaderComponent;

            effect.CreateFragmentShader(_renderForward);
        }
Exemplo n.º 5
0
        public void RegisterShaderEffect(ShaderEffect ef)
        {
            if (GetShaderEffect(ef) != null)
            {
                return;
            }

            // Setup handler to observe changes of the mesh data and dispose event (deallocation)
            ef.ShaderEffectChanged += ShaderEffectChanged;

            _allShaderEffects.Add(ef.SessionUniqueIdentifier, ef);
        }
Exemplo n.º 6
0
            internal ShaderEffectEventArgs(ShaderEffect effect, ShaderEffectChangedEnum changed, string changedName = null, object changedValue = null)
            {
                Effect  = effect;
                Changed = changed;

                if (changedName == null || changedValue == null)
                {
                    return;
                }

                ChangedEffectName  = changedName;
                ChangedEffectValue = changedValue;
            }
Exemplo n.º 7
0
 private void RenderWithLights(Mesh rm, ShaderEffect effect)
 {
     if (_lights.Count > 0)
     {
         foreach (LightInfo li in _lights)
         {
             // SetupLight(li);
             effect.RenderMesh(rm);
         }
     }
     else
     {
         // No light present - switch on standard light
         effect.SetEffectParam(ShaderCodeBuilder.LightColorName, new float3(1, 1, 1));
         // float4 lightDirHom = new float4(0, 0, -1, 0);
         float4 lightDirHom = _rc.InvModelView * new float4(0, 0, -1, 0);
         // float4 lightDirHom = _rc.TransModelView * new float4(0, 0, -1, 0);
         float3 lightDir = lightDirHom.xyz;
         lightDir.Normalize();
         effect.SetEffectParam(ShaderCodeBuilder.LightDirectionName, lightDir);
         effect.SetEffectParam(ShaderCodeBuilder.LightIntensityName, (float)1);
         effect.RenderMesh(rm);
     }
 }
Exemplo n.º 8
0
 public ShaderEffect GetShaderEffect(ShaderEffect ef)
 {
     return(_allShaderEffects.TryGetValue(ef.SessionUniqueIdentifier, out var shaderEffect) ? shaderEffect : null);
 }
Exemplo n.º 9
0
 private void Remove(ShaderEffect ef)
 {
     _rc.RemoveShader(ef);
 }
Exemplo n.º 10
0
        public void ConvShaderEffect(ShaderEffect fx)
        {
            // TODO: FusMaterialPBR not yet implemented, needs to be done when blender export to principle BRDF shader is implemented properly

            var mat = new FusMaterial();

            if (fx.ParamDecl.ContainsKey(UniformNameDeclarations.AlbedoColor))
            {
                mat.Albedo = new MatChannelContainer();
            }

            if (fx.ParamDecl.ContainsKey(UniformNameDeclarations.SpecularColor))
            {
                mat.Specular = new SpecularChannelContainer();
            }

            if (fx.ParamDecl.ContainsKey(UniformNameDeclarations.NormalMap))
            {
                mat.NormalMap = new NormapMapChannelContainer();
            }

            if (fx.ParamDecl.ContainsKey(UniformNameDeclarations.EmissiveColor))
            {
                mat.Emissive = new MatChannelContainer();
            }

            foreach (var decl in fx.ParamDecl)
            {
                switch (decl.Key)
                {
                case UniformNameDeclarations.AlbedoColor:
                    mat.Albedo.Color = (float4)decl.Value;
                    break;

                case UniformNameDeclarations.AlbedoTexture:
                    mat.Albedo.Texture = (string)decl.Value;
                    break;

                case UniformNameDeclarations.AlbedoMix:
                    mat.Albedo.Mix = (float)decl.Value;
                    break;


                case UniformNameDeclarations.EmissiveColor:
                    mat.Emissive.Color = (float4)decl.Value;
                    break;

                case UniformNameDeclarations.EmissiveTexture:
                    mat.Emissive.Texture = (string)decl.Value;
                    break;

                case UniformNameDeclarations.EmissiveMix:
                    mat.Emissive.Mix = (float)decl.Value;
                    break;


                case UniformNameDeclarations.SpecularColor:
                    mat.Specular.Color = (float4)decl.Value;
                    break;

                case UniformNameDeclarations.SpecularTexture:
                    mat.Specular.Texture = (string)decl.Value;
                    break;

                case UniformNameDeclarations.SpecularMix:
                    mat.Specular.Mix = (float)decl.Value;
                    break;

                case UniformNameDeclarations.SpecularShininess:
                    mat.Specular.Shininess = (float)decl.Value;
                    break;

                case UniformNameDeclarations.SpecularIntensity:
                    mat.Specular.Intensity = (float)decl.Value;
                    break;

                case UniformNameDeclarations.NormalMap:
                    mat.NormalMap.Texture = (string)decl.Value;
                    break;

                case UniformNameDeclarations.NormalMapIntensity:
                    mat.NormalMap.Intensity = (float)decl.Value;
                    break;
                }
            }

            _currentNode.AddComponent(mat);
        }