101 lines
2.7 KiB
C#
101 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
namespace Artngame.SKYMASTER
|
|
{
|
|
[ExecuteInEditMode]
|
|
public class RenderReplacementShaderToTexture : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private Shader replacementShader;
|
|
|
|
[SerializeField]
|
|
private RenderTextureFormat renderTextureFormat = RenderTextureFormat.ARGBFloat;
|
|
|
|
[SerializeField]
|
|
private FilterMode filterMode = FilterMode.Bilinear;
|
|
|
|
[SerializeField]
|
|
private int renderTextureDepth = 24;
|
|
|
|
[SerializeField]
|
|
private CameraClearFlags cameraClearFlags = CameraClearFlags.Color;
|
|
|
|
[SerializeField]
|
|
private Color background = Color.black;
|
|
|
|
[SerializeField]
|
|
private string targetTexture = "_CameraDepthNormalsTextureA";
|
|
|
|
private RenderTexture renderTexture;
|
|
|
|
public Camera cameraA;
|
|
|
|
public LayerMask maskLayers;
|
|
|
|
public bool createEditorCamera;
|
|
|
|
private void Start()
|
|
{
|
|
if (Application.isPlaying)
|
|
{
|
|
if (cameraA != null)
|
|
{
|
|
Object.Destroy(cameraA.gameObject);
|
|
}
|
|
createCamera();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!Application.isPlaying)
|
|
{
|
|
if (renderTexture == null)
|
|
{
|
|
Camera component = GetComponent<Camera>();
|
|
renderTexture = new RenderTexture(component.pixelWidth, component.pixelHeight, renderTextureDepth, renderTextureFormat);
|
|
renderTexture.filterMode = filterMode;
|
|
}
|
|
Shader.SetGlobalTexture(targetTexture, renderTexture);
|
|
if (cameraA != null)
|
|
{
|
|
cameraA.targetTexture = renderTexture;
|
|
cameraA.SetReplacementShader(replacementShader, "RenderType");
|
|
cameraA.clearFlags = cameraClearFlags;
|
|
cameraA.backgroundColor = background;
|
|
cameraA.cullingMask = maskLayers;
|
|
cameraA.renderingPath = RenderingPath.Forward;
|
|
}
|
|
}
|
|
if (createEditorCamera && !Application.isPlaying)
|
|
{
|
|
if (cameraA != null)
|
|
{
|
|
Object.DestroyImmediate(cameraA.gameObject);
|
|
}
|
|
createCamera();
|
|
createEditorCamera = false;
|
|
}
|
|
}
|
|
|
|
private void createCamera()
|
|
{
|
|
Camera component = GetComponent<Camera>();
|
|
renderTexture = new RenderTexture(component.pixelWidth, component.pixelHeight, renderTextureDepth, renderTextureFormat);
|
|
renderTexture.filterMode = filterMode;
|
|
Shader.SetGlobalTexture(targetTexture, renderTexture);
|
|
GameObject gameObject = new GameObject("Camera" + targetTexture);
|
|
cameraA = gameObject.AddComponent<Camera>();
|
|
cameraA.CopyFrom(component);
|
|
cameraA.transform.SetParent(base.transform);
|
|
cameraA.targetTexture = renderTexture;
|
|
cameraA.SetReplacementShader(replacementShader, "RenderType");
|
|
cameraA.depth = component.depth - 1f;
|
|
cameraA.clearFlags = cameraClearFlags;
|
|
cameraA.backgroundColor = background;
|
|
cameraA.cullingMask = maskLayers;
|
|
cameraA.renderingPath = RenderingPath.Forward;
|
|
}
|
|
}
|
|
}
|