Beispiel #1
0
        bool CreateTexture()
        {
            Vector2I textureSize = new Vector2I(Round2(size.X), Round2(size.Y));

            texture               = ComponentUtility.CreateComponent <Component_Image>(null, true, false);
            texture.CreateType    = Component_Image.TypeEnum._2D;
            texture.CreateSize    = textureSize;
            texture.CreateMipmaps = false;            // 0;
            //!!!!
            texture.CreateFormat = PixelFormat.A8R8G8B8;
            texture.CreateUsage  = Component_Image.Usages.Dynamic | Component_Image.Usages.WriteOnly;
            texture.Enabled      = true;

            //if( !c.Create( Component_Texture.TypeEnum.Type2D, textureSize, 1, 0, PixelFormat.A8R8G8B8, GpuTexture.Usages.DynamicWriteOnly ) )
            //{
            //	texture.Dispose();
            //	return false;
            //}

            GpuTexture gpuTexture = texture.Result;

            int totalSize = PixelFormatUtility.GetNumElemBytes(gpuTexture.ResultFormat) * gpuTexture.ResultSize.X * gpuTexture.ResultSize.Y;

            byte[] data = new byte[totalSize];
            var    d    = new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, data) };

            gpuTexture.SetData(d);

            textureFormat = texture.CreateFormat;

            //HardwarePixelBuffer pixelBuffer = texture.GetBuffer();
            //pixelBuffer.Lock( HardwareBuffer.LockOptions.Discard );
            //PixelBox pixelBox = pixelBuffer.GetCurrentLock();
            //textureFormat = pixelBox.Format;
            //NativeUtils.ZeroMemory( pixelBox.Data, pixelBox.SlicePitch * 4 );
            //pixelBuffer.Unlock();

            if (textureFormat != PixelFormat.A8R8G8B8 && textureFormat != PixelFormat.A8B8G8R8)
            {
                Log.Warning("UIVideo: CreateTexture: Lock texture format != PixelFormat.A8R8G8B8 and != PixelFormat.A8B8G8R8 ({0}).", textureFormat);
            }

            RenderingSystem.RenderSystemEvent += RenderSystem_RenderSystemEvent;

            return(true);
        }
            //

            public bool Init()
            {
                try
                {
                    texture               = ComponentUtility.CreateComponent <Component_Image>(null, true, false);
                    texture.CreateType    = Component_Image.TypeEnum._2D;
                    texture.CreateSize    = new Vector2I(imageSizeRender, imageSizeRender);
                    texture.CreateMipmaps = false;
                    texture.CreateFormat  = imageFormat;
                    texture.CreateUsage   = Component_Image.Usages.RenderTarget;
                    texture.CreateFSAA    = 0;
                    texture.Enabled       = true;

                    var renderTexture = texture.Result.GetRenderTarget(0, 0);

                    viewport = renderTexture.AddViewport(false, true);
                    viewport.AllowRenderScreenLabels = false;

                    viewport.UpdateBegin += Viewport_UpdateBegin;

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

                    var imageDataBytes = PixelFormatUtility.GetNumElemBytes(imageFormat) * imageSizeRender * imageSizeRender;
                    imageData = NativeUtility.Alloc(NativeUtility.MemoryAllocationType.Utility, imageDataBytes);
                }
                catch (Exception e)
                {
                    Log.Warning(e.Message);
                    return(false);
                }

                return(true);
            }
            public void Update()
            {
                if (Working)
                {
                    try
                    {
                        if (RenderingSystem.LastFrameNumber >= demandedFrame)
                        {
                            //task is done

                            var imageRender = new ImageUtility.Image2D(imageFormat, new Vector2I(imageSizeRender, imageSizeRender), imageData);

                            //get clip rectangle
                            RectangleI rectangle;
                            if (generator.ClampImage)
                            {
                                rectangle = FindActualData(imageRender);

                                //borders
                                rectangle.Expand(imageEmptyBorder);

                                //!!!!станет не квадратный
                                ////clamp
                                //if( rectangle.Left < 0 )
                                //	rectangle.Left = 0;
                                //if( rectangle.Right > imageSizeRender )
                                //	rectangle.Right = imageSizeRender;
                                //if( rectangle.Top < 0 )
                                //	rectangle.Top = 0;
                                //if( rectangle.Bottom > imageSizeRender )
                                //	rectangle.Bottom = imageSizeRender;
                            }
                            else
                            {
                                rectangle = new RectangleI(0, 0, imageSizeRender, imageSizeRender);
                            }

                            var newSize = rectangle.Size;

                            //make clamped image
                            var image2 = new ImageUtility.Image2D(imageFormat, newSize);
                            image2.Blit(Vector2I.Zero, imageRender, rectangle.LeftTop);

                            //make bitmap from clamped image
                            Bitmap bitmap2;
                            unsafe
                            {
                                fixed(byte *pImage2 = image2.Data)
                                {
                                    bitmap2 = new Bitmap(newSize.X, newSize.Y, newSize.X * PixelFormatUtility.GetNumElemBytes(imageFormat), System.Drawing.Imaging.PixelFormat.Format32bppArgb, (IntPtr)pImage2);
                                }
                            }
                            //var bitmapRender = new Bitmap( imageSizeRender, imageSizeRender, imageSizeRender * PixelFormatUtility.GetNumElemBytes( imageFormat ), System.Drawing.Imaging.PixelFormat.Format32bppArgb, imageData );

                            //make result bitmap
                            var bitmapResult = ResizeBitmap(bitmap2, imageSizeResult, imageSizeResult);

                            //save result bitmap
                            var destRealFileName = GetCacheFileNameByResourceVirtualFileName(currentTask.virtualFileName);
                            try
                            {
                                if (!Directory.Exists(Path.GetDirectoryName(destRealFileName)))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(destRealFileName));
                                }

                                bitmapResult.Save(destRealFileName, ImageFormat.Png);
                            }
                            catch (Exception e)
                            {
                                Log.Warning($"PreviewImagesManager: Processor: Update: Unable to save bitmap to \'{destRealFileName}\'. " + e.Message);
                            }
                            finally
                            {
                                bitmap2.Dispose();
                                bitmapResult.Dispose();
                            }

                            //task end
                            ClearTask();

                            //update content browsers
                            foreach (var browser in ContentBrowser.AllInstances)
                            {
                                browser.needUpdateImages = true;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e.Message);
                    }
                }
            }
Beispiel #4
0
        public unsafe void UpdateTexture(bool clearWholeTexture)
        {
            if (size == Vector2I.Zero)
            {
                return;
            }
            if (RenderingSystem.IsDeviceLost())
            {
                return;
            }

            if (texture != null && texture.Disposed)
            {
                texture = null;
            }

            //create texture
            if (texture == null)
            {
                if (!CreateTexture())
                {
                    return;
                }
            }

            if (textureFormat != PixelFormat.A8R8G8B8 && textureFormat != PixelFormat.A8B8G8R8)
            {
                return;
            }

            GpuTexture gpuTexture = texture.Result;

            int totalSize = PixelFormatUtility.GetNumElemBytes(gpuTexture.ResultFormat) * gpuTexture.ResultSize.X * gpuTexture.ResultSize.Y;

            //!!!!можно каждый раз не создавать массив
            byte[] data = new byte[totalSize];

            //HardwarePixelBuffer pixelBuffer = texture.GetBuffer();
            //pixelBuffer.Lock( HardwareBuffer.LockOptions.Discard );
            //PixelBox pixelBox = pixelBuffer.GetCurrentLock();

            //if( clearWholeTexture )
            //	NativeUtility.ZeroMemory( data, pixelBox.SlicePitch * 4 );

            byte *pointer = (byte *)buffer;

            for (int y = 0; y < size.Y; y++)
            {
                //!!!!всегда так?
                int rowPitch = PixelFormatUtility.GetNumElemBytes(gpuTexture.ResultFormat) * gpuTexture.ResultSize.X;

                Marshal.Copy((IntPtr)pointer, data, y * rowPitch, size.X * 4);
                //NativeUtility.CopyMemory( pointer, source, length );
                //pixelBox.WriteDataUnmanaged( y * pixelBox.RowPitch * 4, pointer, size.X * 4 );

                pointer += size.X * 4;
            }

            //pixelBuffer.Unlock();

            var d = new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, data) };

            gpuTexture.SetData(d);

            needUpdateTexture = false;
        }
Beispiel #5
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;
                                                }
                                            }
                                        }
                                    }
                                }
                            });
                        }
                    }
Beispiel #6
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.");
        }