181 lines
6.2 KiB
C#
181 lines
6.2 KiB
C#
using FlaxEditor.Content.Settings;
|
|
using FlaxEngine;
|
|
|
|
namespace Game;
|
|
|
|
/// <summary>
|
|
/// CameraRender Script.
|
|
/// </summary>
|
|
[ExecuteInEditMode]
|
|
public class CameraRender : PostProcessEffect
|
|
{
|
|
public Camera camera;
|
|
|
|
private bool lastEnabled;
|
|
public Material material;
|
|
|
|
private MaterialInstance materialInstance;
|
|
private SceneRenderTask sceneTask;
|
|
private SceneRenderTask sceneTask2;
|
|
|
|
private GPUTexture texture;
|
|
private GPUTexture texture2;
|
|
|
|
//public override PostProcessEffectLocation Location => PostProcessEffectLocation.Default;
|
|
//public override int Order => 110;
|
|
//public override bool CanRender => camera.IsActive;
|
|
|
|
private bool useMainCamera = true;
|
|
public bool rescaleModel = true;
|
|
|
|
private Actor viewModelHolder;
|
|
|
|
private void CreateTextures(int width, int height)
|
|
{
|
|
GPUTextureDescription 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()
|
|
{
|
|
viewModelHolder = Parent.Parent.Parent.Parent.GetChild("ViewModelHolder");
|
|
if (useMainCamera && viewModelHolder != null)
|
|
{
|
|
camera.IsActive = false;
|
|
void foo(Actor actor)
|
|
{
|
|
actor.Layer = 0;
|
|
foreach (Actor actChild in actor.GetChildren<Actor>())
|
|
foo(actChild);
|
|
}
|
|
|
|
foo(viewModelHolder);
|
|
}
|
|
|
|
if (!camera.IsActive)
|
|
return;
|
|
|
|
CreateTextures((int)camera.Viewport.Width, (int)camera.Viewport.Height);
|
|
|
|
// Color pass
|
|
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;
|
|
|
|
// Depth pass
|
|
sceneTask2 = new SceneRenderTask();
|
|
sceneTask2.Order = -2;
|
|
sceneTask2.Camera = camera;
|
|
sceneTask2.ViewMode = ViewMode.Depth;
|
|
sceneTask2.Output = texture2;
|
|
sceneTask2.ViewFlags = ViewFlags.None;
|
|
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);
|
|
|
|
materialInstance.SetParameterValue("New parameter", true);
|
|
materialInstance.SetParameterValue("New parameter 0", ChannelMask.Blue);
|
|
materialInstance.SetParameterValue("New parameter 1", new Color(0.67f));
|
|
|
|
materialInstance.SetParameterValue("New parameter 3", 123f);
|
|
materialInstance.SetParameterValue("New parameter 4", new Float2(1,2));
|
|
materialInstance.SetParameterValue("New parameter 5", new Float3(1,2,3));
|
|
materialInstance.SetParameterValue("New parameter 6", new Float4(1,2,3,4));
|
|
|
|
materialInstance.SetParameterValue("New parameter 8", 123);
|
|
materialInstance.SetParameterValue("New parameter 9", new Matrix(0.666f));
|
|
|
|
materialInstance.SetParameterValue("New parameter 11", new Quaternion(0.5f, 0.5f, 0.5f, 0.5f));
|
|
|
|
materialInstance.SetParameterValue("New parameter 13", new Vector2(1,2));
|
|
materialInstance.SetParameterValue("New parameter 14", new Vector3(1,2,3));
|
|
materialInstance.SetParameterValue("New parameter 15", new Vector4(1,2,3,4));
|
|
//materialInstance.SetParameterValue("New parameter 16", new Transform(new Vector3(1,2,3), new Quaternion(0.5f, 0.5f, 0.5f, 0.5f)));
|
|
|
|
lastEnabled = true;
|
|
}
|
|
|
|
public override void OnDestroy()
|
|
{
|
|
Destroy(ref sceneTask);
|
|
Destroy(ref sceneTask2);
|
|
Destroy(ref texture);
|
|
Destroy(ref texture2);
|
|
}
|
|
|
|
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 lastRescale = false;
|
|
public override void OnUpdate()
|
|
{
|
|
/*#if FLAX_EDITOR
|
|
if (Input.GetKeyDown(KeyboardKeys.F7))
|
|
{
|
|
PhysicsSettings 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 (useMainCamera && rescaleModel != lastRescale)
|
|
{
|
|
lastRescale = rescaleModel;
|
|
if (rescaleModel)
|
|
viewModelHolder.Scale = new Float3(0.75f);
|
|
else
|
|
viewModelHolder.Scale = new Float3(1.0f);
|
|
}
|
|
|
|
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();
|
|
}
|
|
} |