Example #1
0
        public Component_Image PrintToTexture(string script, Vector2I size)
        {
            var data = PrintToByteArray(script, new Size(size.X, size.Y));

            var texture = ComponentUtility.CreateComponent <Component_Image>(null, true, false);

            texture.CreateType   = Component_Image.TypeEnum._2D;
            texture.CreateSize   = new Vector2I((int)size.X, (int)size.Y);
            texture.CreateFormat = PixelFormat.A8R8G8B8;
            texture.CreateUsage  = Component_Image.Usages.WriteOnly;
            texture.Enabled      = true;

            texture.Result.SetData(new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, data) });
            return(texture);
        }
Example #2
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);
            }
Example #4
0
        public void LoadScene(string fileName)
        {
            DestroyScene();

            if (string.IsNullOrEmpty(fileName))
            {
                scene = ComponentUtility.CreateComponent <Component_Scene>(null, true, true);
                scene.BackgroundColor = new ColorValue(0.4, 0.4, 0.4);
            }
            else
            {
                scene = ResourceManager.LoadSeparateInstance <Component_Scene>(fileName, true, null);
            }
            if (scene == null)
            {
                return;
            }

            sceneViewport                          = ParentContainer.Viewport;
            scene.ViewportUpdateBegin             += Scene_ViewportUpdateBegin;
            scene.ViewportUpdateGetCameraSettings += Scene_ViewportUpdateGetCameraSettings;
            sceneViewport.AttachedScene            = scene;
        }
        public Component_Scene CreateScene(bool enable)
        {
            DestroyScene();

            //!!!!можно было бы где-то хранить файлом. где еще так

            scene            = ComponentUtility.CreateComponent <Component_Scene>(null, true, enable);
            sceneNeedDispose = true;

            //!!!!что еще отключать?
            scene.OctreeEnabled = false;

            //rendering pipeline
            {
                var pipeline = (Component_RenderingPipeline)scene.CreateComponent(RenderingSystem.RenderingPipelineDefault, -1, false);
                scene.RenderingPipeline = pipeline;

                //!!!!что еще отключать?
                pipeline.DeferredShading = AutoTrueFalse.False;
                pipeline.LODRange        = new RangeI(0, 0);

                double c  = 1.3;
                double c2 = 0.4;

                if (EditorAPI.DarkTheme)
                {
                    scene.BackgroundColor = new ColorValue(40.0 / 255 * c, 40.0 / 255 * c, 40.0 / 255 * c);
                }
                else
                {
                    scene.BackgroundColor = new ColorValue(22.0 / 255 * c, 44.0 / 255 * c, 66.0 / 255 * c);
                }
                scene.BackgroundColorEnvironmentOverride = new ColorValue(0.8, 0.8, 0.8);

                var backgroundEffects = pipeline.CreateComponent <Component>();
                backgroundEffects.Name = "Background Effects";

                var vignetting = backgroundEffects.CreateComponent <Component_RenderingEffect_Vignetting>();
                if (EditorAPI.DarkTheme)
                {
                    vignetting.Color = new ColorValue(45.0 / 255 * c2, 45.0 / 255 * c2, 45.0 / 255 * c2);
                }
                else
                {
                    vignetting.Color = new ColorValue(24.0 / 255 * c2, 48.0 / 255 * c2, 72.0 / 255 * c2);
                }
                vignetting.Radius = 2;

                var noise = backgroundEffects.CreateComponent <Component_RenderingEffect_Noise>();
                noise.Multiply = new Range(0.9, 1.1);

                var sceneEffects = pipeline.CreateComponent <Component>();
                sceneEffects.Name = "Scene Effects";

                //antialiasing
                var toLDRType        = MetadataManager.GetType("NeoAxis.Component_RenderingEffect_ToLDR");
                var antialiasingType = MetadataManager.GetType("NeoAxis.Component_RenderingEffect_Antialiasing");
                if (toLDRType != null && antialiasingType != null)
                {
                    sceneEffects.CreateComponent(toLDRType);
                    sceneEffects.CreateComponent(antialiasingType);
                }

                pipeline.Enabled = true;
            }

            //ambient light
            {
                var light = scene.CreateComponent <Component_Light>();
                light.Type       = Component_Light.TypeEnum.Ambient;
                light.Brightness = ReferenceUtility.MakeReference("Base\\ProjectSettings.component|PreviewAmbientLightBrightness");
                //light.Brightness = ProjectSettings.Get.PreviewAmbientLightBrightness.Value;
            }

            //directional light
            {
                var light = scene.CreateComponent <Component_Light>();
                light.Type       = Component_Light.TypeEnum.Directional;
                light.Transform  = new Transform(new Vector3(0, 0, 0), Quaternion.FromDirectionZAxisUp(new Vector3(0, 0, -1)), Vector3.One);
                light.Brightness = ReferenceUtility.MakeReference("Base\\ProjectSettings.component|PreviewDirectionalLightBrightness");
                //light.Brightness = ProjectSettings.Get.PreviewDirectionalLightBrightness.Value;
                light.Shadows = false;
                //light.Type = Component_Light.TypeEnum.Point;
                //light.Transform = new Transform( new Vec3( 0, 0, 2 ), Quat.Identity, Vec3.One );
            }

            //!!!!как когда внешне сцена цепляется
            scene.ViewportUpdateGetCameraSettings += Scene_ViewportUpdateGetCameraSettings;

            //connect scene to viewport
            if (ViewportControl != null && ViewportControl.Viewport != null)
            {
                ViewportControl.Viewport.AttachedScene = scene;
            }

            return(scene);
        }
Example #6
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 #7
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.");
        }