Files
GoakeFlax/Source/Game/CameraRender.cs

134 lines
4.6 KiB
C#

using System;
using System.Runtime;
using FlaxEditor.Content.Settings;
using FlaxEngine;
using Console = Cabrito.Console;
namespace Game
{
/// <summary>
/// CameraRender Script.
/// </summary>
[ExecuteInEditMode]
public class CameraRender : PostProcessEffect
{
public Camera camera;
public Material material;
private GPUTexture texture;
private GPUTexture texture2;
private MaterialInstance materialInstance;
private SceneRenderTask sceneTask;
private SceneRenderTask sceneTask2;
private void CreateTextures(int width, int height)
{
var textureDesc = GPUTextureDescription.New2D(width, height, PixelFormat.R8G8B8A8_UNorm);
// Prepare texture and SceneRenderTask for viewmodel camera
if (texture == null)
texture = new GPUTexture();
if (texture.Init(ref textureDesc))
Console.Print("Failed to create camera texture");
// Prepare depth texture and SceneRenderTask for viewmodel camera
textureDesc.Format = PixelFormat.R8_UNorm;
if (texture2 == null)
texture2 = new GPUTexture();
if (texture2.Init(ref textureDesc))
Console.Print("Failed to create camera depth texture");
}
public override void OnAwake()
{
if (!camera.IsActive)
return;
CreateTextures((int)camera.Viewport.Width, (int)camera.Viewport.Height);
sceneTask = new SceneRenderTask();
sceneTask.Order = -1;
sceneTask.Camera = camera;
sceneTask.ViewMode = ViewMode.Default;
sceneTask.Output = texture;
sceneTask.ViewFlags = ViewFlags.DefaultGame;
sceneTask.Enabled = true;
sceneTask.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage;
sceneTask2 = new SceneRenderTask();
sceneTask2.Order = -2;
sceneTask2.Camera = camera;
sceneTask2.ViewMode = ViewMode.Depth;
sceneTask2.Output = texture2;
sceneTask2.ViewFlags = ViewFlags.DefaultGame;
sceneTask2.Enabled = true;
sceneTask2.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage;
// Setup material instance and parameters
if (materialInstance == null)
materialInstance = material.CreateVirtualInstance();
materialInstance.SetParameterValue("Input", texture);
materialInstance.SetParameterValue("Depth", texture2);
lastEnabled = true;
}
public override void OnDestroy()
{
Destroy(ref sceneTask);
Destroy(ref sceneTask2);
Destroy(ref texture);
Destroy(ref texture2);
}
public override PostProcessEffectLocation Location => PostProcessEffectLocation.Default;
public override int Order => 110;
public override bool CanRender => camera.IsActive;
public override void Render(GPUContext context, ref RenderContext renderContext, GPUTexture input, GPUTexture output)
{
if (texture == null || texture2 == null)
return;
Renderer.DrawPostFxMaterial(context, ref renderContext, materialInstance, output, input.View());
}
private bool lastEnabled;
public override void OnUpdate()
{
#if FLAX_EDITOR
if (Input.GetKeyDown(KeyboardKeys.F7))
{
var physicsSettings = GameSettings.Load<PhysicsSettings>();
physicsSettings.EnableSubstepping = !physicsSettings.EnableSubstepping;
GameSettings.Save(physicsSettings);
//GameSettings.Apply();
}
#endif
if (lastEnabled != camera.IsActive)
{
lastEnabled = camera.IsActive;
sceneTask.Enabled = lastEnabled;
sceneTask2.Enabled = lastEnabled;
sceneTask.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage * 0.5f;
sceneTask2.RenderingPercentage = MainRenderTask.Instance.RenderingPercentage * 0.5f;
}
if (!camera.IsActive)
return;
if (texture == null)
OnAwake();
if ((int)camera.Viewport.Width != texture.Width || (int)camera.Viewport.Height != texture.Height)
CreateTextures((int)camera.Viewport.Width, (int)camera.Viewport.Height);
}
public override void OnEnable()
{
//OnAwake();
}
}
}