Beispiel #1
0
        public static T[] ToArray <T>(this Rhino.Display.Color4f cl)
        {
            var conv = new T[4];

            if (typeof(T) == typeof(float))
            {
                conv[0] = (T)((object)(cl.R));
                conv[1] = (T)((object)(cl.G));
                conv[2] = (T)((object)(cl.B));
                conv[3] = (T)((object)(cl.A));
            }
            else
            {
                conv[0] = (T)Convert.ChangeType((byte)(Math.Min(cl.R, 1.0f) * 255.0f), typeof(T));
                conv[1] = (T)Convert.ChangeType((byte)(Math.Min(cl.G, 1.0f) * 255.0f), typeof(T));
                conv[2] = (T)Convert.ChangeType((byte)(Math.Min(cl.B, 1.0f) * 255.0f), typeof(T));
                conv[3] = (T)Convert.ChangeType((byte)(Math.Min(cl.A, 1.0f) * 255.0f), typeof(T));
                //conv[0] = (T)((object)((byte)Math.Min(cl.R, 1.0f) * 255.0f));
                //conv[1] = (T)((object)((byte)Math.Min(cl.G, 1.0f) * 255.0f));
                //conv[2] = (T)((object)((byte)Math.Min(cl.B, 1.0f) * 255.0f));
                //conv[3] = (T)((object)((byte)Math.Min(cl.A, 1.0f) * 255.0f));
            }

            return(conv);
        }
        public CustomTextureEvaluator(CustomTexture tex, RenderTexture.TextureEvaluatorFlags evaluatorFlags)
            : base(evaluatorFlags)
        {
            m_color1      = new Rhino.Display.Color4f(tex.Color1);
            m_color2      = new Rhino.Display.Color4f(tex.Color2);
            m_mapping     = tex.LocalMappingTransform;  //TODO - evaluator flags needs to be checked for efDisableLocalMapping
            m_bSwapColors = tex.SwapColors;

            m_bTexture1On = tex.Texture1On && tex.Texture1Amount > 0.0;
            m_bTexture2On = tex.Texture2On && tex.Texture2Amount > 0.0;

            m_dTexture1Amount = tex.Texture1Amount;
            m_dTexture2Amount = tex.Texture2Amount;

            if (m_bTexture1On)
            {
                RenderTexture child = tex.FindChild(tex.ChildSlotNameFromParamName("color-one")) as RenderTexture;
                if (child != null)
                {
                    textureEvaluatorOne = child.CreateEvaluator(evaluatorFlags);
                }
            }

            if (m_bTexture2On)
            {
                RenderTexture child = tex.FindChild(tex.ChildSlotNameFromParamName("color-two")) as RenderTexture;
                if (child != null)
                {
                    textureEvaluatorTwo = child.CreateEvaluator(evaluatorFlags);
                }
            }
        }
Beispiel #3
0
    /// <summary>
    /// Displays the standard modal color picker dialog.
    /// </summary>
    /// <param name="colorInOut">The initial color to set the picker to and also accepts the user's choice.</param>
    /// <param name="bUseAlpha">Specifies if the color picker should allow changes to the alpha channel or not.</param>
    /// <returns>true if a color was picked, false if the user canceled the picker dialog.</returns>
    public static bool ShowColorPicker(ref Rhino.Display.Color4f colorInOut, bool bUseAlpha)
    {
      Rhino.Display.Color4f c = new Rhino.Display.Color4f();

      bool b = 1==UnsafeNativeMethods.Rdk_Globals_ShowColorPicker(colorInOut, bUseAlpha, ref c);

      if (b) { colorInOut = c; }
      
      return b;
    }
Beispiel #4
0
 public static float[] ToFloatArray(this Rhino.Display.Color4f color)
 {
     return(new float[]
     {
         color.R,
         color.G,
         color.B,
         color.A,
     });
 }
Beispiel #5
0
        /// <summary>
        /// Copies all vertex colors to a linear array of float in rgb order
        /// </summary>
        /// <returns>The float array.</returns>
        public static float[] ToFloatArray(this MeshVertexColorList cl)
        {
            int count = cl.Count;
            var rc    = new float[count * 3];
            int index = 0;

            foreach (var c in cl)
            {
                Rhino.Display.Color4f c4f = new Rhino.Display.Color4f(c);
                rc[index++] = c4f.R;
                rc[index++] = c4f.G;
                rc[index++] = c4f.B;
            }
            return(rc);
        }
Beispiel #6
0
        public static Rhino.Display.Color4f UnapplyGamma(Rhino.Display.Color4f color, float gamma = 2.2f)
        {
            if (Math.Abs(gamma - 1.0f) > float.Epsilon)
            {
                double inv = 1.0f / gamma;

                float r = (float)Math.Pow(color.R, inv);
                float g = (float)Math.Pow(color.G, inv);
                float b = (float)Math.Pow(color.B, inv);

                return(new Rhino.Display.Color4f(r, g, b, color.A));
            }

            return(color);
        }
Beispiel #7
0
        static int OnGetColor(int serial_number, Point3d uvw, Vector3d duvwdx, Vector3d duvwdy, ref Rhino.Display.Color4f color)
        {
            int rc = 0;
            TextureEvaluator eval = FromSerialNumber(serial_number);

            if (eval != null)
            {
                Rhino.Display.Color4f c = eval.GetColor(uvw, duvwdx, duvwdy);
                if (c != Rhino.Display.Color4f.Empty)
                {
                    color = c;
                    rc    = 1;
                }
            }
            return(rc);
        }
Beispiel #8
0
        public virtual Rhino.Display.Color4f GetColor(Rhino.Geometry.Point3d uvw, Rhino.Geometry.Vector3d duvwdx, Rhino.Geometry.Vector3d duvwdy)
        {
            if (m_runtime_serial_number > 0)
            {
                return(Rhino.Display.Color4f.Empty);
            }
            IntPtr pConstThis = ConstPointer();

            Rhino.Display.Color4f rc = new Rhino.Display.Color4f();

            if (!UnsafeNativeMethods.Rdk_TextureEvaluator_GetColor(pConstThis, uvw, duvwdx, duvwdy, ref rc))
            {
                return(Rhino.Display.Color4f.Empty);
            }
            return(rc);
        }
        public override Rhino.Display.Color4f GetColor(Rhino.Geometry.Point3d _uvw, Rhino.Geometry.Vector3d duvwdx, Rhino.Geometry.Vector3d duvwdy)
        {
            //Apply the mapping transform to get tiling and offset to work
            Rhino.Geometry.Point3d uvw = new Rhino.Geometry.Point3d(m_mapping * _uvw);

            double d = uvw.X * 20;
            int    i = (int)d;

            bool useColorOne = false;

            if (i % 2 == 0)
            {
                useColorOne = true;
            }
            else
            {
                d = uvw.Y * 20;
                i = (int)d;
                if (i % 2 == 0)
                {
                    useColorOne = true;
                }
            }

            if (m_bSwapColors)
            {
                useColorOne = !useColorOne;
            }

            bool             textureOn     = useColorOne ? m_bTexture1On : m_bTexture2On;
            double           textureAmount = useColorOne ? m_dTexture1Amount : m_dTexture2Amount;
            TextureEvaluator texEval       = useColorOne ? textureEvaluatorOne : textureEvaluatorTwo;

            Rhino.Display.Color4f color = useColorOne ? m_color1 : m_color2;

            if (textureOn && texEval != null)
            {
                //Ensure that the original UVW is passed into the child texture evaluator
                color = color.BlendTo((float)textureAmount, texEval.GetColor(_uvw, duvwdx, duvwdy));
            }

            return(new Rhino.Display.Color4f(color));
        }
Beispiel #10
0
        public Rhino.Render.RenderTexture GetRenderTexture(int textureIndex, Rhino.Display.Color4f factor)
        {
            System.Drawing.Bitmap bmp = GetTextureBitmap(textureIndex, out string name);

            if (bmp == null)
            {
                return(null);
            }

            int width  = bmp.Width;
            int height = bmp.Height;


            System.Drawing.Bitmap resolvedBmp = new System.Drawing.Bitmap(width, height);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Rhino.Display.Color4f colorAt = new Rhino.Display.Color4f(bmp.GetPixel(i, j));

                    float r = GltfUtils.Clamp(colorAt.R * factor.R, 0.0f, 1.0f);
                    float g = GltfUtils.Clamp(colorAt.G * factor.G, 0.0f, 1.0f);
                    float b = GltfUtils.Clamp(colorAt.B * factor.B, 0.0f, 1.0f);
                    float a = GltfUtils.Clamp(colorAt.A * factor.A, 0.0f, 1.0f);

                    Rhino.Display.Color4f colorFinal = new Rhino.Display.Color4f(r, g, b, a);

                    resolvedBmp.SetPixel(i, j, colorFinal.AsSystemColor());
                }
            }

            Rhino.Render.RenderTexture renderTexture = Rhino.Render.RenderTexture.NewBitmapTexture(resolvedBmp, doc);

            renderTexture.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program);

            renderTexture.Name = name;

            renderTexture.EndChange();

            return(renderTexture);
        }
Beispiel #11
0
            /// <summary>
            /// Displays the standard modal color picker dialog for floating point colors.
            /// </summary>
            /// <param name="parent">Parent window for this dialog, should always pass this if calling from a form or user control.</param>
            /// <param name="color">The initial color to set the picker to and also accepts the user's choice.</param>
            /// <param name="allowAlpha">Specifies if the color picker should allow changes to the alpha channel or not.</param>
            /// <returns>true if a color was picked, false if the user canceled the picker dialog.</returns>
            public static bool ShowColorDialog(System.Windows.Forms.IWin32Window parent, ref Rhino.Display.Color4f color, bool allowAlpha)
            {
                if (null == parent)
                {
                    parent = RhinoApp.MainWindow();
                }

                IntPtr hWnd = IntPtr.Zero;

                if (null != parent)
                {
                    hWnd = parent.Handle;
                }

                Rhino.Display.Color4f c = Rhino.Display.Color4f.Empty;

                bool rc = (1 == UnsafeNativeMethods.Rdk_Globals_ShowColorPicker(hWnd, color, allowAlpha, ref c));

                if (rc)
                {
                    color = c;
                }
                return(rc);
            }
 public Rhino.Display.Color4f ToColor4f()
 {
   Rhino.Display.Color4f v = new Rhino.Display.Color4f();
   UnsafeNativeMethods.Rdk_Variant_GetRdkColorValue(ConstPointer(), ref v);
   return v;
 }
Beispiel #13
0
    public virtual Rhino.Display.Color4f GetColor(Rhino.Geometry.Point3d uvw, Rhino.Geometry.Vector3d duvwdx, Rhino.Geometry.Vector3d duvwdy)
    {
      if (m_runtime_serial_number > 0)
        return Rhino.Display.Color4f.Empty;
      IntPtr pConstThis = ConstPointer();
      Rhino.Display.Color4f rc = new Rhino.Display.Color4f();

      if (!UnsafeNativeMethods.Rdk_TextureEvaluator_GetColor(pConstThis, uvw, duvwdx, duvwdy, ref rc))
        return Rhino.Display.Color4f.Empty;
      return rc;
    }
Beispiel #14
0
 /// <summary>
 /// Displays the standard modal color picker dialog for floating point colors.
 /// </summary>
 /// <param name="color">The initial color to set the picker to and also accepts the user's choice.</param>
 /// <param name="allowAlpha">Specifies if the color picker should allow changes to the alpha channel or not.</param>
 /// <returns>true if a color was picked, false if the user canceled the picker dialog.</returns>
 public static bool ShowColorDialog(ref Rhino.Display.Color4f color, bool allowAlpha)
 {
     return(ShowColorDialog(null, ref color, allowAlpha));
 }
Beispiel #15
0
 public Variant(Rhino.Display.Color4f v) : this()
 {
     SetValue(v);
 }
Beispiel #16
0
 /// <summary>
 /// Blend color with the decal color at a given point.
 /// </summary>
 /// <param name="point">The point in space or, if the decal is uv-mapped, the uv-coordinate of that point.</param>
 /// <param name="normal">The face normal of the given point.</param>
 /// <param name="colInOut">The color to blend the decal color to.</param>
 /// <param name="uvOut">the UV on the texture that the color point was read from.</param>
 /// <returns>true if the given point hits the decal, else false.</returns>
 public bool Color(Rhino.Geometry.Point3d point, Rhino.Geometry.Vector3d normal, ref Rhino.Display.Color4f colInOut, ref Rhino.Geometry.Point2d uvOut)
 {
     return(1 == UnsafeNativeMethods.Rdk_Decal_Color(ConstPointer(), point, normal, ref colInOut, ref uvOut));
 }
 /// <summary>
 /// If x or y are out of range, the function will fail and may crash Rhino.
 /// </summary>
 /// <param name="x">The horizontal pixel position. No validation is done on this value.
 /// The caller is responsible for ensuring that it is within the frame buffer.</param>
 /// <param name="y">The vertical pixel position. No validation is done on this value.
 /// The caller is responsible for ensuring that it is within the frame buffer.</param>
 /// <param name="value">The color to store in the channel at the specified position.</param>
 public void SetValue(int x, int y, Rhino.Display.Color4f value)
 {
     UnsafeNativeMethods.Rdk_RenderWindowChannel_SetColorValue(ConstPointer(), x, y, value);
 }
        public Rhino.Render.RenderMaterial Convert()
        {
            RenderMaterial pbr = RenderContentType.NewContentFromTypeId(ContentUuids.PhysicallyBasedMaterialType, doc) as RenderMaterial;

            pbr.BeginChange(RenderContent.ChangeContexts.Program);

            pbr.Name = converter.GetUniqueName(material.Name);

            if (material.PbrMetallicRoughness != null)
            {
                Rhino.Display.Color4f baseColor = material.PbrMetallicRoughness.BaseColorFactor.ToColor4f();

                if (material.PbrMetallicRoughness.BaseColorTexture != null)
                {
                    int index = material.PbrMetallicRoughness.BaseColorTexture.Index;

                    RenderTexture texture = converter.GetRenderTexture(index, baseColor);

                    pbr.SetChild(texture, Rhino.Render.ParameterNames.PhysicallyBased.BaseColor);
                    pbr.SetChildSlotOn(Rhino.Render.ParameterNames.PhysicallyBased.BaseColor, true, RenderContent.ChangeContexts.Program);
                }

                baseColor = GltfUtils.UnapplyGamma(baseColor);

                pbr.SetParameter(PhysicallyBased.BaseColor, baseColor);

                double roughness = material.PbrMetallicRoughness.RoughnessFactor;

                double metalness = material.PbrMetallicRoughness.MetallicFactor;

                if (material.PbrMetallicRoughness.MetallicRoughnessTexture != null)
                {
                    int index = material.PbrMetallicRoughness.MetallicRoughnessTexture.Index;

                    RhinoGltfMetallicRoughnessConverter metallicRoughness = converter.GetMetallicRoughnessTexture(index);

                    pbr.SetChild(metallicRoughness.MetallicTexture, PhysicallyBased.Metallic);
                    pbr.SetChildSlotOn(PhysicallyBased.Metallic, true, RenderContent.ChangeContexts.Program);
                    pbr.SetChildSlotAmount(PhysicallyBased.Metallic, metalness * 100.0, RenderContent.ChangeContexts.Program);

                    pbr.SetChild(metallicRoughness.RoughnessTexture, PhysicallyBased.Roughness);
                    pbr.SetChildSlotOn(PhysicallyBased.Roughness, true, RenderContent.ChangeContexts.Program);
                    pbr.SetChildSlotAmount(PhysicallyBased.Roughness, roughness * 100.0, RenderContent.ChangeContexts.Program);
                }
                else
                {
                    pbr.SetParameter(PhysicallyBased.Roughness, roughness);

                    pbr.SetParameter(PhysicallyBased.Metallic, metalness);
                }
            }

            Rhino.Display.Color4f emissionColor = material.EmissiveFactor.ToColor4f();

            emissionColor = GltfUtils.UnapplyGamma(emissionColor);

            pbr.SetParameter(PhysicallyBased.Emission, emissionColor);

            if (material.EmissiveTexture != null)
            {
                RenderTexture emissiveTexture = converter.GetRenderTexture(material.EmissiveTexture.Index);

                pbr.SetChild(emissiveTexture, PhysicallyBased.Emission);
                pbr.SetChildSlotOn(PhysicallyBased.Emission, true, RenderContent.ChangeContexts.Program);
            }

            if (material.OcclusionTexture != null)
            {
                RenderTexture occlusionTexture = converter.GetRenderTexture(material.OcclusionTexture.Index);

                pbr.SetChild(occlusionTexture, PhysicallyBased.AmbientOcclusion);
                pbr.SetChildSlotOn(PhysicallyBased.AmbientOcclusion, true, RenderContent.ChangeContexts.Program);
                pbr.SetChildSlotAmount(PhysicallyBased.AmbientOcclusion, material.OcclusionTexture.Strength * 100.0, RenderContent.ChangeContexts.Program);
            }

            if (material.NormalTexture != null)
            {
                RenderTexture normalTexture = converter.GetRenderTexture(material.NormalTexture.Index);

                pbr.SetChild(normalTexture, PhysicallyBased.Bump);
                pbr.SetChildSlotOn(PhysicallyBased.Bump, true, RenderContent.ChangeContexts.Program);
            }

            string clearcoatText    = "";
            string transmissionText = "";
            string iorText          = "";
            string specularText     = "";

            if (material.Extensions != null)
            {
                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_clearcoat.Tag, out object clearcoatValue))
                {
                    clearcoatText = clearcoatValue.ToString();
                }

                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_transmission.Tag, out object transmissionValue))
                {
                    transmissionText = transmissionValue.ToString();
                }

                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_ior.Tag, out object iorValue))
                {
                    iorText = iorValue.ToString();
                }

                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_specular.Tag, out object specularValue))
                {
                    specularText = specularValue.ToString();
                }
            }

            HandleClearcoat(clearcoatText, pbr);

            HandleTransmission(transmissionText, pbr);

            HandleIor(iorText, pbr);

            HandleSpecular(specularText, pbr);

            pbr.EndChange();

            doc.RenderMaterials.BeginChange(RenderContent.ChangeContexts.Program);

            doc.RenderMaterials.Add(pbr);

            doc.RenderMaterials.EndChange();

            return(pbr);
        }
 /// <summary>
 /// Show the color dialog and change the mask color if necessary.  This
 /// only works on Windows, Mac has a ColorShade control to handle display
 /// of the color dialog.
 /// </summary>
 public void ShowMaskColorDialog()
 {
     #if ON_OS_WINDOWS
       var color = new Rhino.Display.Color4f(maskColor);
       var parent = RhinoWindows.Forms.WindowsInterop.ObjectAsIWin32Window(Window);
       if (null == parent) parent = RhinoApp.MainWindow();
       if (Rhino.UI.Dialogs.ShowColorDialog(parent, ref color, false))
     maskColor = color.AsSystemColor();
     #endif
 }
Beispiel #20
0
 public void SetValue(Rhino.Display.Color4f v)
 {
     UnsafeNativeMethods.Rdk_Variant_SetRdkColorValue(NonConstPointer(), v);
 }
Beispiel #21
0
 public Rhino.Display.Color4f ToColor4f()
 {
     Rhino.Display.Color4f v = new Rhino.Display.Color4f();
     UnsafeNativeMethods.Rdk_Variant_GetRdkColorValue(ConstPointer(), ref v);
     return(v);
 }
Beispiel #22
0
        private bool AttemptConvertVertexColors(glTFLoader.Schema.MeshPrimitive primitive, Rhino.Geometry.Mesh rhinoMesh)
        {
            if (!primitive.Attributes.TryGetValue(VertexColorAttributeTag, out int vertexColorAccessorIndex))
            {
                return(false);
            }

            glTFLoader.Schema.Accessor vertexColorAccessor = converter.GetAccessor(vertexColorAccessorIndex);

            if (vertexColorAccessor == null)
            {
                return(false);
            }

            glTFLoader.Schema.BufferView vertexColorBufferView = converter.GetBufferView(vertexColorAccessor.BufferView);

            if (vertexColorBufferView == null)
            {
                return(false);
            }

            byte[] vertexColorBuffer = converter.GetBuffer(vertexColorBufferView.Buffer);

            if (vertexColorBuffer == null)
            {
                return(false);
            }

            int vertexColorOffset = vertexColorAccessor.ByteOffset + vertexColorBufferView.ByteOffset;

            int vertexColorStride = vertexColorBufferView.ByteStride.HasValue ? vertexColorBufferView.ByteStride.Value : TotalStride(vertexColorAccessor.ComponentType, vertexColorAccessor.Type);

            int vertexColorComponentCount = ComponentsCount(vertexColorAccessor.Type);

            int vertexColorComponentSize = ComponentSize(vertexColorAccessor.ComponentType);

            List <float> vertexColors = new List <float>();

            for (int i = 0; i < vertexColorAccessor.Count; i++)
            {
                int vertexColorIndex = vertexColorOffset + i * vertexColorStride;

                for (int j = 0; j < vertexColorComponentCount; j++)
                {
                    int location = vertexColorIndex + j * vertexColorComponentSize;

                    float channelColor = 0.0f;

                    if (vertexColorAccessor.ComponentType == glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT)
                    {
                        channelColor = BitConverter.ToSingle(vertexColorBuffer, location);
                    }
                    else if (vertexColorAccessor.ComponentType == glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_SHORT)
                    {
                        ushort value = BitConverter.ToUInt16(vertexColorBuffer, location);
                        channelColor = (float)value / (float)ushort.MaxValue;
                    }
                    else if (vertexColorAccessor.ComponentType == glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_BYTE)
                    {
                        byte value = vertexColorBuffer[location];
                        channelColor = (float)value / (float)byte.MaxValue;
                    }

                    vertexColors.Add(channelColor);
                }
            }

            int countVertexColors = vertexColors.Count / vertexColorComponentCount;

            for (int i = 0; i < countVertexColors; i++)
            {
                int index = i * vertexColorComponentCount;

                if (vertexColorAccessor.Type == glTFLoader.Schema.Accessor.TypeEnum.VEC3)
                {
                    float r = GltfUtils.Clamp(vertexColors[index + 0], 0.0f, 1.0f);
                    float g = GltfUtils.Clamp(vertexColors[index + 1], 0.0f, 1.0f);
                    float b = GltfUtils.Clamp(vertexColors[index + 2], 0.0f, 1.0f);

                    Rhino.Display.Color4f color = new Rhino.Display.Color4f(r, g, b, 1.0f);

                    rhinoMesh.VertexColors.Add(color.AsSystemColor());
                }
                else if (vertexColorAccessor.Type == glTFLoader.Schema.Accessor.TypeEnum.VEC4)
                {
                    float r = GltfUtils.Clamp(vertexColors[index + 0], 0.0f, 1.0f);
                    float g = GltfUtils.Clamp(vertexColors[index + 1], 0.0f, 1.0f);
                    float b = GltfUtils.Clamp(vertexColors[index + 2], 0.0f, 1.0f);
                    float a = GltfUtils.Clamp(vertexColors[index + 3], 0.0f, 1.0f);

                    Rhino.Display.Color4f color = new Rhino.Display.Color4f(r, g, b, a);

                    rhinoMesh.VertexColors.Add(color.AsSystemColor());
                }
            }

            return(true);
        }
 public ColorField(string internalName, string friendlyName, Rhino.Display.Color4f defaultValue)
   : base(internalName, friendlyName)
 {
   m_defaultValue = defaultValue;
 }