コード例 #1
0
            public override EffectInstance EffectInstance(DisplayStyle displayStyle, bool IsShadingPass)
            {
                var ei = base.EffectInstance(displayStyle, IsShadingPass);

                bool hlr          = displayStyle == DisplayStyle.HLR;
                bool flatColors   = displayStyle == DisplayStyle.FlatColors || displayStyle <= DisplayStyle.HLR;
                bool useMaterials = displayStyle > DisplayStyle.HLR && displayStyle != DisplayStyle.FlatColors;
                bool useTextures  = displayStyle > DisplayStyle.Rendering && displayStyle != 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 (DrawContext.IsTransparentPass())
                    {
                        transparency = displayStyle == 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 ?
                                        Rhino.DocObjects.Material.DefaultMaterial.DiffuseColor :
                                        rhinoObject.Attributes.DrawColor(ActiveDocument);

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

                        if (displayStyle >= 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 / Rhino.DocObjects.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.ToHost());
                ei.SetColor(color.ToHost());
                ei.SetDiffuseColor(diffuse.ToHost());
                ei.SetEmissiveColor(emissive.ToHost());
                ei.SetGlossiness(glossiness * 100.0);
                ei.SetSpecularColor(specular.ToHost());
                ei.SetTransparency(transparency);

                return(ei);
            }