Example #1
0
        private bool CreateShaderPart(ShaderBody shb, RenderMaterial rm, float gamma)
        {
            var crm = rm as ICyclesMaterial;

            ShaderBody.CyclesMaterial mattype = ShaderBody.CyclesMaterial.No;
            if (crm == null)
            {
                // always simulate material, need to know now myself
                // what to read out from the simulated material to
                // populate my own material descriptions.
                var m = rm.SimulateMaterial(true);
                // figure out what type of material we are.
                //var probemat = GuessMaterialFromSmell(rm);
                var probemat = WhatMaterial(rm, m);

                rm.BeginChange(RenderContent.ChangeContexts.Ignore);
                var dcl          = m.DiffuseColor;
                var scl          = m.SpecularColor;
                var rcl          = m.ReflectionColor;
                var rfcl         = m.TransparentColor;
                var emcl         = m.EmissionColor;
                var polish       = (float)m.ReflectionGlossiness;
                var reflectivity = (float)m.Reflectivity;
                var metalic      = 0f;
                var shine        = (float)(m.Shine / Material.MaxShine);

                switch (probemat)
                {
                case ProbableMaterial.Plaster:
                    mattype = ShaderBody.CyclesMaterial.Diffuse;
                    break;

                case ProbableMaterial.Glass:
                case ProbableMaterial.Gem:
                    metalic = 0f;
                    mattype = ShaderBody.CyclesMaterial.Glass;
                    break;

                case ProbableMaterial.Metal:
                    metalic = 1.0f;
                    mattype = ShaderBody.CyclesMaterial.SimpleMetal;
                    break;

                case ProbableMaterial.Plastic:
                    //polish = reflectivity;
                    //shine = polish;
                    //reflectivity = 0f;
                    metalic = 0f;
                    mattype = ShaderBody.CyclesMaterial.SimplePlastic;
                    break;

                case ProbableMaterial.Paint:
                    mattype = ShaderBody.CyclesMaterial.Paint;
                    break;

                case ProbableMaterial.Custom:
                    mattype = ShaderBody.CyclesMaterial.No;
                    break;
                }

                var difftexAlpha = m.AlphaTransparency;

                var col    = RenderEngine.CreateFloat4(dcl.R, dcl.G, dcl.B, 255);
                var spec   = RenderEngine.CreateFloat4(scl.R, scl.G, scl.B, 255);
                var refl   = RenderEngine.CreateFloat4(rcl.R, rcl.G, rcl.B, 255);
                var transp = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255);
                var refr   = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255);
                var emis   = RenderEngine.CreateFloat4(emcl.R, emcl.G, emcl.B, 255);

                //shb.Type = CyclesShader.Shader.Diffuse,
                shb.CyclesMaterialType = mattype;

                shb.Shadeless = m.DisableLighting;

                shb.DiffuseColor        = col;
                shb.SpecularColor       = spec;
                shb.ReflectionColor     = refl;
                shb.ReflectionRoughness = (float)m.ReflectionGlossiness;                 // polish;
                shb.RefractionColor     = refr;
                shb.RefractionRoughness = (float)m.RefractionGlossiness;
                shb.TransparencyColor   = transp;
                shb.EmissionColor       = emis;


                shb.FresnelIOR   = (float)m.FresnelIndexOfRefraction;
                shb.IOR          = (float)m.IndexOfRefraction;
                shb.Roughness    = (float)m.ReflectionGlossiness;
                shb.Reflectivity = reflectivity;
                shb.Metalic      = metalic;
                shb.Transparency = (float)m.Transparency;
                shb.Shine        = shine;
                shb.Gloss        = (float)m.ReflectionGlossiness;

                shb.FresnelReflections = m.FresnelReflections;

                shb.Gamma = gamma;

                shb.Name = m.Name ?? "";

                shb.DiffuseTexture.Amount      = 0.0f;
                shb.BumpTexture.Amount         = 0.0f;
                shb.TransparencyTexture.Amount = 0.0f;
                shb.EnvironmentTexture.Amount  = 0.0f;

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Diffuse))
                {
                    var difftex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Diffuse);

                    BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, difftex, RenderMaterial.StandardChildSlots.Diffuse);
                    if (shb.HasDiffuseTexture)
                    {
                        shb.DiffuseTexture.UseAlpha = difftexAlpha;
                        shb.DiffuseTexture.Amount   = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Diffuse) / 100.0f, 1.0f);
                    }
                }

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Bump))
                {
                    var bumptex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Bump);
                    BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, bumptex, RenderMaterial.StandardChildSlots.Bump);
                    if (shb.HasBumpTexture)
                    {
                        shb.BumpTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Bump) / 100.0f, 1.0f);
                    }
                }

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Transparency))
                {
                    var transtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Transparency);
                    BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, transtex,
                                                                RenderMaterial.StandardChildSlots.Transparency);
                    if (shb.HasTransparencyTexture)
                    {
                        shb.TransparencyTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Transparency) / 100.0f, 1.0f);
                    }
                }

                if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Environment))
                {
                    var envtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Environment);
                    BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, envtex,
                                                                RenderMaterial.StandardChildSlots.Environment);
                    if (shb.HasEnvironmentTexture)
                    {
                        shb.EnvironmentTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Environment) / 100.0f, 1.0f);
                    }
                }

                rm.EndChange();
            }
            else
            {
                crm.BakeParameters();
                shb.Crm = crm;
                shb.CyclesMaterialType = ShaderBody.CyclesMaterial.Xml;
                shb.Gamma = gamma;
                shb.Name  = rm.Name ?? "some cycles material";
            }
            return(true);
        }