-
-
Save AG-Dan/fda4dee8b08c9f491497854d572366ce to your computer and use it in GitHub Desktop.
Changes to the dungeon crawler sample's RenderMinimap.cs file to make it compatible with scriptable render pipelines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_2019_1_OR_NEWER | |
#define RENDER_PIPELINE | |
#endif | |
using UnityEngine; | |
using UnityEngine.UI; | |
#if RENDER_PIPELINE | |
using UnityEngine.Rendering; | |
#endif | |
namespace DunGen.DungeonCrawler | |
{ | |
/// <summary> | |
/// Renders the minimap to a RawImage on a GameObject named "Minimap" | |
/// and the icons to a RawImage on a GameObject named "Minimap Icons" | |
/// </summary> | |
[RequireComponent(typeof(Camera))] | |
sealed class RenderMinimap : MonoBehaviour | |
{ | |
[SerializeField] | |
private Material drawMinimapMaterial = null; | |
[SerializeField] | |
private Camera minimapIconsCamera = null; | |
[SerializeField] | |
private Material createDistanceFieldMaterial = null; | |
private RawImage minimapImage = null; | |
private RawImage minimapIconsImage = null; | |
private Camera minimapCamera; | |
private RenderTexture cameraBuffer; | |
private RenderTexture distanceFieldBuffer; | |
private RenderTexture outputBuffer; | |
private RenderTexture iconsBuffer; | |
private Material createDistanceFieldMaterialInstance; | |
private void OnEnable() | |
{ | |
minimapCamera = GetComponent<Camera>(); | |
#if RENDER_PIPELINE | |
if(GraphicsSettings.currentRenderPipeline != null) | |
RenderPipelineManager.endCameraRendering += OnEndCameraRendering; | |
#endif | |
// Create necessary buffers | |
const int inputRes = 512; | |
const int outputRes = 512; | |
cameraBuffer = new RenderTexture(inputRes, inputRes, 0); | |
distanceFieldBuffer = new RenderTexture(inputRes, inputRes, 0); | |
outputBuffer = new RenderTexture(outputRes, outputRes, 0); | |
iconsBuffer = new RenderTexture(outputRes, outputRes, 0); | |
// Setup material | |
createDistanceFieldMaterialInstance = new Material(createDistanceFieldMaterial); | |
createDistanceFieldMaterialInstance.SetFloat("_TextureSize", inputRes); | |
// Tell the minimap camera to render into an off-screen buffer | |
minimapCamera.targetTexture = cameraBuffer; | |
// Hook the output buffer up to the RawImage component in the UI | |
if (minimapImage == null) | |
{ | |
var minimapObject = GameObject.Find("Minimap"); | |
minimapImage = minimapObject.GetComponent<RawImage>(); | |
} | |
if (minimapIconsImage == null) | |
{ | |
var minimapIconsObject = GameObject.Find("Minimap Icons"); | |
minimapIconsImage = minimapIconsObject.GetComponent<RawImage>(); | |
} | |
minimapImage.texture = outputBuffer; | |
minimapIconsImage.texture = iconsBuffer; | |
minimapIconsCamera.targetTexture = iconsBuffer; | |
} | |
private void OnDisable() | |
{ | |
minimapCamera.targetTexture = null; | |
#if RENDER_PIPELINE | |
RenderPipelineManager.endCameraRendering -= OnEndCameraRendering; | |
#endif | |
Destroy(cameraBuffer); | |
Destroy(distanceFieldBuffer); | |
Destroy(outputBuffer); | |
Destroy(createDistanceFieldMaterialInstance); | |
cameraBuffer = null; | |
distanceFieldBuffer = null; | |
outputBuffer = null; | |
createDistanceFieldMaterialInstance = null; | |
} | |
private void OnEndCameraRendering(ScriptableRenderContext context, Camera camera) | |
{ | |
if(camera == minimapCamera) | |
OnPostRender(); | |
} | |
private void OnPostRender() | |
{ | |
// After the minimap camera is done rendering, convert the contents to a distance field | |
Graphics.Blit(cameraBuffer, distanceFieldBuffer, createDistanceFieldMaterialInstance); | |
// Render the distance field as the final minimap | |
Graphics.Blit(distanceFieldBuffer, outputBuffer, drawMinimapMaterial); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment