Beispiel #1
0
        private RenderMaterial GetMaterial(RhinoObject o)
        {
            var material       = o.GetMaterial(true);
            var renderMaterial = new RenderMaterial();

            // If it's a default material use the display color.
            if (!material.HasId)
            {
                renderMaterial.diffuse = o.Attributes.DrawColor(Doc).ToArgb();
                return(renderMaterial);
            }

            // Otherwise, extract what properties we can.
            renderMaterial.name     = material.Name;
            renderMaterial.diffuse  = material.DiffuseColor.ToArgb();
            renderMaterial.emissive = material.EmissionColor.ToArgb();

            renderMaterial.opacity   = 1 - material.Transparency;
            renderMaterial.metalness = material.Reflectivity;

            if (material.Name.ToLower().Contains("glass") && renderMaterial.opacity == 0)
            {
                renderMaterial.opacity = 0.3;
            }

            return(renderMaterial);
        }
            public override DB3D.EffectInstance EffectInstance(DB.DisplayStyle displayStyle, bool IsShadingPass)
            {
                var ei = base.EffectInstance(displayStyle, IsShadingPass);

                bool hlr          = displayStyle == DB.DisplayStyle.HLR;
                bool flatColors   = displayStyle == DB.DisplayStyle.FlatColors || displayStyle <= DB.DisplayStyle.HLR;
                bool useMaterials = displayStyle > DB.DisplayStyle.HLR && displayStyle != DB.DisplayStyle.FlatColors;
                bool useTextures  = displayStyle > DB.DisplayStyle.Rendering && displayStyle != DB.DisplayStyle.FlatColors;

                var ambient      = Color.Black;
                var color        = Color.Black;
                var diffuse      = Color.Black;
                var emissive     = Color.Black;
                var glossiness   = 1.0;
                var specular     = Color.Black;
                var transparency = 0.0;

                if (IsShadingPass)
                {
                    if (DB3D.DrawContext.IsTransparentPass())
                    {
                        transparency = displayStyle == DB.DisplayStyle.Wireframe ? 0.8 : 0.5;
                        var previewColor = Color.Silver;
                        if (flatColors)
                        {
                            emissive = previewColor;
                        }
                        else
                        {
                            diffuse = previewColor;
                            ambient = Color.FromArgb(diffuse.R / 2, diffuse.G / 2, diffuse.B / 2);
                            if (useTextures)
                            {
                                glossiness = 0.5;
                                specular   = Color.White;
                            }
                        }
                    }
                    else
                    {
                        var drawColor = rhinoObject.IsLocked ?
                                        Rhino.ApplicationSettings.AppearanceSettings.LockedObjectColor :
                                        hlr ?
                                        Color.White :
                                        useMaterials ?
                                        Material.DefaultMaterial.DiffuseColor :
                                        rhinoObject.Attributes.DrawColor(ActiveDocument);

                        if (drawColor == Color.Black)
                        {
                            drawColor = Color.Gray;
                        }

                        if (displayStyle >= DB.DisplayStyle.HLR)
                        {
                            if (flatColors)
                            {
                                emissive = drawColor;
                            }
                            else
                            {
                                var material = rhinoObject.GetMaterial(true);
                                ambient  = Color.FromArgb(material.DiffuseColor.R / 2, material.DiffuseColor.G / 2, material.DiffuseColor.B / 2);
                                diffuse  = material.DiffuseColor;
                                emissive = material.EmissionColor;
                                if (material.Shine != 0.0)
                                {
                                    double s  = material.Shine / Material.MaxShine;
                                    double _s = 1.0 - s;
                                    specular = Color.FromArgb
                                               (
                                        material.SpecularColor.A,
                                        (int)(material.SpecularColor.R * s + material.DiffuseColor.R * _s),
                                        (int)(material.SpecularColor.G * s + material.DiffuseColor.G * _s),
                                        (int)(material.SpecularColor.B * s + material.DiffuseColor.B * _s)
                                               );
                                    glossiness = s;
                                }

                                transparency = material.Transparency;
                            }
                        }
                    }
                }
                else
                {
                    if (part.FaceCount == -1)
                    {
                        var previewColor = Color.Silver;
                        diffuse = previewColor;
                        ambient = Color.FromArgb(diffuse.R / 2, diffuse.G / 2, diffuse.B / 2);
                        if (useTextures)
                        {
                            glossiness = 0.5;
                            specular   = Color.White;
                        }
                    }
                }

                ei.SetAmbientColor(ambient.ToColor());
                ei.SetColor(color.ToColor());
                ei.SetDiffuseColor(diffuse.ToColor());
                ei.SetEmissiveColor(emissive.ToColor());
                ei.SetGlossiness(glossiness * 100.0);
                ei.SetSpecularColor(specular.ToColor());
                ei.SetTransparency(transparency);

                return(ei);
            }