Beispiel #1
0
        Bitmap SolidColorImage(Color4f color, Size size)
        {
            Bitmap bmp = new Bitmap(size.Width, size.Height);

            using (Graphics gfx = Graphics.FromImage(bmp))
            {
                gfx.FillRectangle(new SolidBrush(color.AsSystemColor()), 0, 0, size.Width, size.Height);
            }

            return(bmp);
        }
        Bitmap BitmapForScene(CreatePreviewEventArgs scene, Color4f color)
        {
            var bitmap = new Bitmap(scene.PreviewImageSize.Width, scene.PreviewImageSize.Height, PixelFormat.Format24bppRgb);

            // Fill the bitmap using the computed color
            using (var g = Graphics.FromImage(bitmap))
            {
                g.Clear(Color.FromArgb(255, color.AsSystemColor()));
                g.DrawRectangle(Pens.Black, 0, 0, bitmap.Width - 1, bitmap.Height - 1);
            }
            return(bitmap);
        }
Beispiel #3
0
        public glTFLoader.Schema.TextureInfo AddMetallicRoughnessTexture(Rhino.DocObjects.Material rhinoMaterial)
        {
            Rhino.DocObjects.Texture metalTexture     = rhinoMaterial.PhysicallyBased.GetTexture(TextureType.PBR_Metallic);
            Rhino.DocObjects.Texture roughnessTexture = rhinoMaterial.PhysicallyBased.GetTexture(TextureType.PBR_Roughness);

            bool hasMetalTexture     = metalTexture == null ? false : metalTexture.Enabled;
            bool hasRoughnessTexture = roughnessTexture == null ? false : roughnessTexture.Enabled;

            RenderTexture renderTextureMetal     = null;
            RenderTexture renderTextureRoughness = null;

            int mWidth  = 0;
            int mHeight = 0;
            int rWidth  = 0;
            int rHeight = 0;

            // Get the textures
            if (hasMetalTexture)
            {
                renderTextureMetal = rhinoMaterial.RenderMaterial.GetTextureFromUsage(Rhino.Render.RenderMaterial.StandardChildSlots.PbrMetallic);
                renderTextureMetal.PixelSize(out mWidth, out mHeight, out int _w0);
            }

            if (hasRoughnessTexture)
            {
                renderTextureRoughness = rhinoMaterial.RenderMaterial.GetTextureFromUsage(RenderMaterial.StandardChildSlots.PbrRoughness);
                renderTextureRoughness.PixelSize(out rWidth, out rHeight, out int _w1);
            }

            int width  = Math.Max(mWidth, rWidth);
            int height = Math.Max(mHeight, rHeight);

            TextureEvaluator evalMetal     = null;
            TextureEvaluator evalRoughness = null;

            // Metal
            if (hasMetalTexture)
            {
                evalMetal = renderTextureMetal.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal);
            }

            // Roughness
            if (hasRoughnessTexture)
            {
                evalRoughness = renderTextureRoughness.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal);
            }

            // Copy Metal to the blue channel, roughness to the green
            var bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            float metallic  = (float)rhinoMaterial.PhysicallyBased.Metallic;
            float roughness = (float)rhinoMaterial.PhysicallyBased.Roughness;

            for (var j = 0; j < height - 1; j += 1)
            {
                for (var i = 0; i < width - 1; i += 1)
                {
                    double x = (double)i / (double)(width - 1);
                    double y = (double)j / (double)(height - 1);

                    Point3d uvw = new Point3d(x, y, 0.0);

                    float g = 0;
                    float b = 0;
                    if (hasMetalTexture)
                    {
                        Color4f metal = evalMetal.GetColor(uvw, Vector3d.Zero, Vector3d.Zero);
                        b = metal.L; //grayscale maps, so we want lumonosity
                    }
                    else
                    {
                        b = metallic;
                    }

                    if (hasRoughnessTexture)
                    {
                        Color4f roughnessColor = evalRoughness.GetColor(uvw, Vector3d.ZAxis, Vector3d.Zero);
                        g = roughnessColor.L; //grayscale maps, so we want lumonosity
                    }
                    else
                    {
                        g = roughness;
                    }

                    Color4f color = new Color4f(0.0f, g, b, 1.0f);
                    bitmap.SetPixel(i, height - j - 1, color.AsSystemColor());
                }
            }

            return(GetTextureInfoFromBitmap(bitmap));
        }
Beispiel #4
0
        private Bitmap ConvertBumpToNormal(RenderTexture bumpMapTexture)
        {
            bumpMapTexture.PixelSize(out int width, out int height, out int depth);

            if (width <= 0)
            {
                width = 1024;
            }

            if (height <= 0)
            {
                height = 1024;
            }

            TextureEvaluator evaluator = bumpMapTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal);

            Bitmap bmp = new Bitmap(width, height);

            //Sobel filter for bump to normal conversion https://en.wikipedia.org/wiki/Sobel_operator

            double widthScaler  = 1.0 / (width - 1);
            double heightScaler = 1.0 / (height - 1);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Point3d aLocation = new Point3d(Mod(x - 1, width) * widthScaler, 1.0 - Mod(y - 1, height) * heightScaler, 0.0);
                    Point3d bLocation = new Point3d(Mod(x, width) * widthScaler, 1.0 - Mod(y - 1, height) * heightScaler, 0.0);
                    Point3d cLocation = new Point3d(Mod(x + 1, width) * widthScaler, 1.0 - Mod(y - 1, height) * heightScaler, 0.0);
                    Point3d dLocation = new Point3d(Mod(x - 1, width) * widthScaler, 1.0 - Mod(y, height) * heightScaler, 0.0);
                    Point3d fLocation = new Point3d(Mod(x + 1, width) * widthScaler, 1.0 - Mod(y, height) * heightScaler, 0.0);
                    Point3d gLocation = new Point3d(Mod(x - 1, width) * widthScaler, 1.0 - Mod(y + 1, height) * heightScaler, 0.0);
                    Point3d hLocation = new Point3d(Mod(x, width) * widthScaler, 1.0 - Mod(y + 1, height) * heightScaler, 0.0);
                    Point3d iLocation = new Point3d(Mod(x + 1, width) * widthScaler, 1.0 - Mod(y + 1, height) * heightScaler, 0.0);

                    float a = evaluator.GetColor(aLocation, Vector3d.Zero, Vector3d.Zero).L;
                    float b = evaluator.GetColor(bLocation, Vector3d.Zero, Vector3d.Zero).L;
                    float c = evaluator.GetColor(cLocation, Vector3d.Zero, Vector3d.Zero).L;
                    float d = evaluator.GetColor(dLocation, Vector3d.Zero, Vector3d.Zero).L;
                    float f = evaluator.GetColor(fLocation, Vector3d.Zero, Vector3d.Zero).L;
                    float g = evaluator.GetColor(gLocation, Vector3d.Zero, Vector3d.Zero).L;
                    float h = evaluator.GetColor(hLocation, Vector3d.Zero, Vector3d.Zero).L;
                    float i = evaluator.GetColor(iLocation, Vector3d.Zero, Vector3d.Zero).L;

                    float dX = a - c + 2.0f * d + -2.0f * f + g - f;
                    float dY = a + 2.0f * b + c - g - 2.0f * h - i;

                    Vector3f normal = new Vector3f(dX, dY, 1.0f);
                    normal.Unitize();

                    normal = normal * 0.5f + new Vector3f(0.5f, 0.5f, 0.5f);

                    Color4f color = new Color4f(normal.X, normal.Y, normal.Z, 1.0f);

                    bmp.SetPixel(x, y, color.AsSystemColor());
                }
            }

            return(bmp);
        }
Beispiel #5
0
        TextureInfo CombineBaseColorAndAlphaTexture(Rhino.Render.RenderTexture baseColorTexture, Rhino.Render.RenderTexture alphaTexture, bool baseColorDiffuseAlphaForTransparency, Color4f baseColor, bool baseColorLinear)
        {
            bool hasBaseColorTexture = baseColorTexture != null;

            int baseColorWidth, baseColorHeight, baseColorDepth;

            baseColorWidth = baseColorHeight = baseColorDepth = 0;

            if (hasBaseColorTexture)
            {
                baseColorTexture.PixelSize(out baseColorWidth, out baseColorHeight, out baseColorDepth);
            }

            alphaTexture.PixelSize(out int alphaWidth, out int alphaHeight, out int alphaDepth);

            int width  = Math.Max(baseColorWidth, alphaWidth);
            int height = Math.Max(baseColorHeight, alphaHeight);

            if (width <= 0)
            {
                width = 1024;
            }

            if (height <= 0)
            {
                height = 1024;
            }

            TextureEvaluator baseColorTextureEvaluator = baseColorTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal);
            TextureEvaluator alphaTextureEvaluator     = alphaTexture.CreateEvaluator(RenderTexture.TextureEvaluatorFlags.Normal);

            Bitmap bitmap = new Bitmap(width, height);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    double x = (double)i / ((double)(width - 1));
                    double y = (double)j / ((double)(height - 1));

                    y = 1.0 - y;

                    Point3d uvw = new Point3d(x, y, 0.0);

                    Color4f baseColorOut = baseColor;

                    if (hasBaseColorTexture)
                    {
                        baseColorOut = baseColorTextureEvaluator.GetColor(uvw, Vector3d.Zero, Vector3d.Zero);

                        if (baseColorLinear)
                        {
                            baseColorOut = Color4f.ApplyGamma(baseColorOut, workflow.PreProcessGamma);
                        }
                    }

                    if (!baseColorDiffuseAlphaForTransparency)
                    {
                        baseColorOut = new Color4f(baseColorOut.R, baseColorOut.G, baseColorOut.B, 1.0f);
                    }

                    Color4f alphaColor = alphaTextureEvaluator.GetColor(uvw, Vector3d.Zero, Vector3d.Zero);

                    float alpha = baseColor.A * alphaColor.L;

                    Color4f colorFinal = new Color4f(baseColorOut.R, baseColorOut.G, baseColorOut.B, alpha);

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

            bitmap.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "out.png"));

            return(GetTextureInfoFromBitmap(bitmap));
        }
Beispiel #6
0
        /// <summary>
        /// This method creates a hexadecimal representation of a Color4f Rhino class.
        /// </summary>
        /// <param name="color">The color to cast</param>
        /// <returns>A string with the hexadecimal with # in the begin representation (without alpha channel)</returns>
        public static string GetColorHex(Color4f color)
        {
            var sysColor = color.AsSystemColor();

            return("#" + sysColor.R.ToString("X2") + sysColor.G.ToString("X2") + sysColor.B.ToString("X2"));
        }