private void Scene_ViewportUpdateGetCameraSettings(Component_Scene scene, Viewport viewport, ref bool processed) { // Get default camera or the camera from the editor. Component_Camera camera = scene.CameraDefault; if (camera == null) { camera = scene.Mode.Value == Component_Scene.ModeEnum._3D ? scene.CameraEditor : scene.CameraEditor2D; } // Get camera settings by game mode. if (gameMode != null) { var cameraSettings = gameMode.GetCameraSettings(viewport, camera); if (cameraSettings != null) { viewport.CameraSettings = cameraSettings; processed = true; return; } } // Create new camera: //camera = (Component_Camera)camera.Clone(); ////camera = new Component_Camera(); //camera.Transform = new Transform( cameraPosition, Quaternion.LookAt( ( lookTo - cameraPosition ).GetNormalize(), up ) ); //camera.FixedUp = up; if (camera != null) { viewport.CameraSettings = new Viewport.CameraSettingsClass(viewport, camera); processed = true; } }
private void Scene_ViewportUpdateGetCameraSettings(Component_Scene scene, Viewport viewport, ref bool processed) { Component_Camera camera = scene.CameraDefault; if (camera == null) { camera = scene.Mode.Value == Component_Scene.ModeEnum._3D ? scene.CameraEditor : scene.CameraEditor2D; } // Create new camera: //camera = (Component_Camera)camera.Clone(); ////camera = new Component_Camera(); //camera.Transform = new Transform( cameraPosition, Quaternion.LookAt( ( lookTo - cameraPosition ).GetNormalize(), up ) ); //camera.FixedUp = up; if (camera != null) { viewport.CameraSettings = new Viewport.CameraSettingsClass(viewport, camera); processed = true; } else { viewport.CameraSettings = new Viewport.CameraSettingsClass(viewport, 1, 90, 0.1, 1000, Vector3.Zero, Vector3.XAxis, Vector3.ZAxis, ProjectionType.Perspective, 1, 1, 1); processed = true; } }
//public Vec3 GetSceneCenter() //{ // if( scene != null ) // { // var bounds = scene.CalculateTotalBoundsOfObjectsInSpace(); // return bounds.GetCenter(); // } // return Vec3.Zero; //} protected virtual void Scene_ViewportUpdateGetCameraSettings(Component_Scene scene, Viewport viewport, ref bool processed) { if (!cameraMode2D) { var cameraPosition = cameraLookTo - cameraDirection.GetVector() * cameraInitialDistance * cameraZoomFactor; var center = cameraLookTo; // GetSceneCenter(); Vector3 from = cameraPosition; //center + cameraDirection.GetVector() * cameraDistance; Vector3 to = center; Degree fov = 65; // 75; //!!!! Component_Camera camera = new Component_Camera(); camera.AspectRatio = (double)viewport.SizeInPixels.X / (double)viewport.SizeInPixels.Y; camera.FieldOfView = fov; camera.NearClipPlane = Math.Max(cameraInitialDistance / 10000, 0.01); //.1; camera.FarClipPlane = Math.Max(1000, cameraInitialDistance * 2); camera.Transform = new Transform(from, Quaternion.LookAt((to - from).GetNormalize(), Vector3.ZAxis)); //camera.Position = from; //camera.Direction = ( to - from ).GetNormalize(); camera.FixedUp = Vector3.ZAxis; viewport.CameraSettings = new Viewport.CameraSettingsClass(viewport, camera); ////!!!!в методе больше параметров //double aspect = (double)viewport.SizeInPixels.X / (double)viewport.SizeInPixels.Y; //var settings = new Viewport.CameraSettingsClass( viewport, aspect, fov, .1f, 1000, from, ( to - from ).GetNormalize(), Vec3.ZAxis ); //viewport.CameraSettings = settings; } else { var from = cameraLookTo + new Vector3(0, 0, scene.CameraEditor2DPositionZ); var to = cameraLookTo; Component_Camera camera = new Component_Camera(); camera.AspectRatio = (double)viewport.SizeInPixels.X / (double)viewport.SizeInPixels.Y; camera.NearClipPlane = 0.01; // Math.Max( cameraInitialDistance / 10000, 0.01 );//.1; camera.FarClipPlane = 1000; // Math.Max( 1000, cameraInitialDistance * 2 ); camera.Transform = new Transform(from, Quaternion.LookAt((to - from).GetNormalize(), Vector3.YAxis)); camera.Projection = ProjectionType.Orthographic; camera.FixedUp = Vector3.YAxis; //!!!!need consider size by X camera.Height = cameraInitBounds.GetSize().Y * 1.4; viewport.CameraSettings = new Viewport.CameraSettingsClass(viewport, camera); } processed = true; }
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; } } } } } }); } }
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."); }
internal void PerformViewportUpdateGetCameraSettings(ref Component_Camera camera) { OnViewportUpdateGetCameraSettings(ref camera); ViewportUpdateGetCameraSettings?.Invoke(this, ref camera); }
protected virtual void OnViewportUpdateGetCameraSettings(ref Component_Camera camera) { }