Example #1
0
        void RenderMaterial(Component_Camera camera, string destRealFileName)
        {
            var renderToFile = RenderToFile;
            var scene        = renderToFile.ParentRoot as Component_Scene;

            var textureFileNames = new string[5];

            //ImageUtility.Image2D opacityImage = null;
            Vector2I[,] opacityImageNearestCellTable = null;

            //write textures
            for (int nChannel = 0; nChannel < 5; nChannel++)
            {
                var channel = (MaterialChannel)nChannel;

                Component_Image texture     = null;
                Component_Image textureRead = null;

                try
                {
                    //!!!!все каналы
                    //!!!!какие еще параметры?

                    var prefix = Path.GetFileNameWithoutExtension(destRealFileName) + "_";

                    string fileName = "";
                    switch (nChannel)
                    {
                    case 0: fileName = prefix + "Opacity.png"; break;

                    case 1: fileName = prefix + "BaseColor.png"; break;

                    case 2: fileName = prefix + "Metallic.png"; break;

                    case 3: fileName = prefix + "Roughness.png"; break;

                    case 4: fileName = prefix + "Normal.png"; break;
                    }

                    var fullPath = Path.Combine(Path.GetDirectoryName(destRealFileName), fileName);

                    //create
                    var resolution = renderToFile.Resolution.Value;

                    PixelFormat format = PixelFormat.A8R8G8B8;

                    texture               = ComponentUtility.CreateComponent <Component_Image>(null, true, false);
                    texture.CreateType    = Component_Image.TypeEnum._2D;
                    texture.CreateSize    = resolution;
                    texture.CreateMipmaps = false;
                    texture.CreateFormat  = format;
                    texture.CreateUsage   = Component_Image.Usages.RenderTarget;
                    texture.CreateFSAA    = 0;
                    texture.Enabled       = true;

                    var renderTexture = texture.Result.GetRenderTarget(0, 0);
                    var viewport      = renderTexture.AddViewport(true, true);
                    viewport.AttachedScene = scene;

                    textureRead               = ComponentUtility.CreateComponent <Component_Image>(null, true, false);
                    textureRead.CreateType    = Component_Image.TypeEnum._2D;
                    textureRead.CreateSize    = resolution;
                    textureRead.CreateMipmaps = false;
                    textureRead.CreateFormat  = format;
                    textureRead.CreateUsage   = Component_Image.Usages.ReadBack | Component_Image.Usages.BlitDestination;
                    textureRead.CreateFSAA    = 0;
                    textureRead.Enabled       = true;
                    //!!!!
                    textureRead.Result.PrepareNativeObject();


                    var restorePipeline = scene.RenderingPipeline;

                    var pipeline = ComponentUtility.CreateComponent <Component_RenderingPipeline_Default>(null, true, true);
                    switch (nChannel)
                    {
                    case 0: pipeline.DebugMode = Component_RenderingPipeline_Basic.DebugModeEnum.Normal; break;

                    case 1: pipeline.DebugMode = Component_RenderingPipeline_Basic.DebugModeEnum.BaseColor; break;

                    case 2: pipeline.DebugMode = Component_RenderingPipeline_Basic.DebugModeEnum.Metallic; break;

                    case 3: pipeline.DebugMode = Component_RenderingPipeline_Basic.DebugModeEnum.Roughness; break;

                    case 4: pipeline.DebugMode = Component_RenderingPipeline_Basic.DebugModeEnum.Normal; break;
                    }

                    try
                    {
                        scene.RenderingPipeline = pipeline;
                        scene.GetDisplayDevelopmentDataInThisApplicationOverride += Scene_GetDisplayDevelopmentDataInThisApplicationOverride;

                        var cameraSettings = new Viewport.CameraSettingsClass(viewport, camera);

                        viewport.Update(true, cameraSettings);

                        //clear temp data
                        viewport.RenderingContext.MultiRenderTarget_DestroyAll();
                        viewport.RenderingContext.DynamicTexture_DestroyAll();
                    }
                    finally
                    {
                        scene.RenderingPipeline = restorePipeline;
                        scene.GetDisplayDevelopmentDataInThisApplicationOverride -= Scene_GetDisplayDevelopmentDataInThisApplicationOverride;
                    }

                    texture.Result.GetRealObject(true).BlitTo(viewport.RenderingContext.CurrentViewNumber, textureRead.Result.GetRealObject(true), 0, 0);


                    //!!!!pitch

                    //get data
                    var totalBytes = PixelFormatUtility.GetNumElemBytes(format) * resolution.X * resolution.Y;
                    var data       = new byte[totalBytes];
                    unsafe
                    {
                        fixed(byte *pBytes = data)
                        {
                            var demandedFrame = textureRead.Result.GetRealObject(true).Read((IntPtr)pBytes, 0);

                            while (RenderingSystem.CallBgfxFrame() < demandedFrame)
                            {
                            }
                        }
                    }

                    var image = new ImageUtility.Image2D(format, resolution, data);

                    if (channel == MaterialChannel.Opacity)
                    {
                        //convert pixels
                        for (int y = 0; y < image.Size.Y; y++)
                        {
                            for (int x = 0; x < image.Size.X; x++)
                            {
                                var pixel = image.GetPixel(new Vector2I(x, y));

                                if (pixel.ToVector3F() != Vector3F.Zero)
                                {
                                    pixel = Vector4F.One;
                                }
                                else
                                {
                                    pixel = Vector4F.Zero;
                                }

                                image.SetPixel(new Vector2I(x, y), pixel);
                            }
                        }

                        //opacityImageNearestCellTable
                        if (renderToFile.FillTransparentPixelsByNearPixels)
                        {
                            var boolOpacityImage = new bool[image.Size.X, image.Size.Y];
                            for (int y = 0; y < image.Size.Y; y++)
                            {
                                for (int x = 0; x < image.Size.X; x++)
                                {
                                    var c = image.GetPixelByte(new Vector2I(x, y));
                                    boolOpacityImage[x, y] = c.Red == 0;
                                }
                            }

                            var distanceMap = GetDistanceMap(image);

                            opacityImageNearestCellTable = new Vector2I[image.Size.X, image.Size.Y];
                            for (int y = 0; y < image.Size.Y; y++)
                            {
                                for (int x = 0; x < image.Size.X; x++)
                                {
                                    opacityImageNearestCellTable[x, y] = new Vector2I(x, y);
                                }
                            }

                            var table = opacityImageNearestCellTable;

                            //!!!!slowly

                            Parallel.For(0, image.Size.Y, delegate(int y)                                //for( int y = 0; y < image.Size.Y; y++ )
                            {
                                for (int x = 0; x < image.Size.X; x++)
                                {
                                    var transparent = boolOpacityImage[x, y];
                                    if (transparent)
                                    {
                                        for (int n = 0; n < distanceMap.Length; n++)                                         //foreach( var indexItem in distanceMap )
                                        {
                                            ref var indexItem = ref distanceMap[n];

                                            var takeFrom = new Vector2I(x, y) + indexItem.offset;
                                            if (takeFrom.X >= 0 && takeFrom.X < image.Size.X && takeFrom.Y >= 0 && takeFrom.Y < image.Size.Y)
                                            {
                                                var transparent2 = boolOpacityImage[takeFrom.X, takeFrom.Y];
                                                if (!transparent2)
                                                {
                                                    table[x, y] = takeFrom;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            });
                        }
                    }
Example #2
0
 private void Scene_ViewportUpdateBegin(Component_Scene scene, Viewport viewport, Viewport.CameraSettingsClass overrideCameraSettings)
 {
     ProjectUtility.UpdateSceneAntialiasingByAppSettings(scene);
 }
Example #3
0
        void RenderScreenshot(Component_Camera camera, string destRealFileName)
        {
            var renderToFile = RenderToFile;
            var scene        = renderToFile.ParentRoot as Component_Scene;

            Component_Image texture     = null;
            Component_Image textureRead = null;

            try
            {
                //create
                var resolution = renderToFile.Resolution.Value;

                //!!!!impl
                var         hdr    = false;     //HDR.Value;
                PixelFormat format = hdr ? PixelFormat.Float16RGBA : PixelFormat.A8R8G8B8;
                //PixelFormat format = hdr ? PixelFormat.Float32RGBA : PixelFormat.A8R8G8B8;

                texture               = ComponentUtility.CreateComponent <Component_Image>(null, true, false);
                texture.CreateType    = Component_Image.TypeEnum._2D;
                texture.CreateSize    = resolution;
                texture.CreateMipmaps = false;
                texture.CreateFormat  = format;
                texture.CreateUsage   = Component_Image.Usages.RenderTarget;
                texture.CreateFSAA    = 0;
                texture.Enabled       = true;

                var renderTexture = texture.Result.GetRenderTarget(0, 0);
                var viewport      = renderTexture.AddViewport(true, true);
                viewport.AttachedScene = scene;

                textureRead               = ComponentUtility.CreateComponent <Component_Image>(null, true, false);
                textureRead.CreateType    = Component_Image.TypeEnum._2D;
                textureRead.CreateSize    = resolution;
                textureRead.CreateMipmaps = false;
                textureRead.CreateFormat  = format;
                textureRead.CreateUsage   = Component_Image.Usages.ReadBack | Component_Image.Usages.BlitDestination;
                textureRead.CreateFSAA    = 0;
                textureRead.Enabled       = true;
                //!!!!
                textureRead.Result.PrepareNativeObject();

                //render
                //var image2D = new ImageUtility.Image2D( PixelFormat.Float32RGB, new Vector2I( size * 4, size * 3 ) );

                //var position = Transform.Value.Position;

                //for( int face = 0; face < 6; face++ )
                //{

                //Vector3 dir = Vector3.Zero;
                //Vector3 up = Vector3.Zero;

                ////flipped
                //switch( face )
                //{
                //case 0: dir = -Vector3.YAxis; up = Vector3.ZAxis; break;
                //case 1: dir = Vector3.YAxis; up = Vector3.ZAxis; break;
                //case 2: dir = Vector3.ZAxis; up = -Vector3.XAxis; break;
                //case 3: dir = -Vector3.ZAxis; up = Vector3.XAxis; break;
                //case 4: dir = Vector3.XAxis; up = Vector3.ZAxis; break;
                //case 5: dir = -Vector3.XAxis; up = Vector3.ZAxis; break;
                //}

                try
                {
                    scene.GetDisplayDevelopmentDataInThisApplicationOverride += Scene_GetDisplayDevelopmentDataInThisApplicationOverride;

                    var cameraSettings = new Viewport.CameraSettingsClass(viewport, camera);

                    //var cameraSettings = new Viewport.CameraSettingsClass( viewport, 1, 90, NearClipPlane.Value, FarClipPlane.Value, position, dir, up, ProjectionType.Perspective, 1, 1, 1 );

                    viewport.Update(true, cameraSettings);

                    //clear temp data
                    viewport.RenderingContext.MultiRenderTarget_DestroyAll();
                    viewport.RenderingContext.DynamicTexture_DestroyAll();
                }
                finally
                {
                    scene.GetDisplayDevelopmentDataInThisApplicationOverride -= Scene_GetDisplayDevelopmentDataInThisApplicationOverride;
                }

                texture.Result.GetRealObject(true).BlitTo(viewport.RenderingContext.CurrentViewNumber, textureRead.Result.GetRealObject(true), 0, 0);


                //!!!!pitch

                //get data
                var totalBytes = PixelFormatUtility.GetNumElemBytes(format) * resolution.X * resolution.Y;
                var data       = new byte[totalBytes];
                unsafe
                {
                    fixed(byte *pBytes = data)
                    {
                        var demandedFrame = textureRead.Result.GetRealObject(true).Read((IntPtr)pBytes, 0);

                        while (RenderingSystem.CallBgfxFrame() < demandedFrame)
                        {
                        }
                    }
                }

                var image = new ImageUtility.Image2D(format, resolution, data);

                //reset alpha channel
                for (int y = 0; y < image.Size.Y; y++)
                {
                    for (int x = 0; x < image.Size.X; x++)
                    {
                        var pixel = image.GetPixel(new Vector2I(x, y));
                        pixel.W = 1.0f;
                        image.SetPixel(new Vector2I(x, y), pixel);
                    }
                }

                //image.Data
                //image2D.Blit( index * size, faceImage );

                //Vector2I index = Vector2I.Zero;
                //switch( face )
                //{
                //case 0: index = new Vector2I( 2, 1 ); break;
                //case 1: index = new Vector2I( 0, 1 ); break;
                //case 2: index = new Vector2I( 1, 0 ); break;
                //case 3: index = new Vector2I( 1, 2 ); break;
                //case 4: index = new Vector2I( 1, 1 ); break;
                //case 5: index = new Vector2I( 3, 1 ); break;
                //}

                //var faceImage = new ImageUtility.Image2D( format, new Vector2I( size, size ), data );
                //image2D.Blit( index * size, faceImage );
                //}

                if (!Directory.Exists(Path.GetDirectoryName(destRealFileName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(destRealFileName));
                }

                if (!ImageUtility.Save(destRealFileName, image.Data, image.Size, 1, image.Format, 1, 0, out var error))
                {
                    throw new Exception(error);
                }
            }
            catch (Exception e)
            {
                EditorMessageBox.ShowWarning(e.Message);
                return;
            }
            finally
            {
                texture?.Dispose();
                textureRead?.Dispose();
            }

            ScreenNotifications.Show("Rendering completed successfully.");
        }