Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ShaderTutorials/Assets/052_Object_Outline/ObjectOutline.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (44 sloc)
1.34 KB
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
public class ObjectOutline : MonoBehaviour | |
{ | |
public Renderer OutlinedObject; | |
public Material WriteObject; | |
public Material ApplyOutline; | |
void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
bool hitSelectable = Physics.Raycast(ray, out var hit) && hit.transform.CompareTag("Selectable"); | |
if (hitSelectable) { | |
OutlinedObject = hit.transform.GetComponent<Renderer>(); | |
} else { | |
OutlinedObject = null; | |
} | |
} | |
} | |
void OnRenderImage(RenderTexture source, RenderTexture destination) | |
{ | |
//setup stuff | |
var commands = new CommandBuffer(); | |
int selectionBuffer = Shader.PropertyToID("_SelectionBuffer"); | |
commands.GetTemporaryRT(selectionBuffer, source.descriptor); | |
//render selection buffer | |
commands.SetRenderTarget(selectionBuffer); | |
commands.ClearRenderTarget(true, true, Color.clear); | |
if (OutlinedObject != null) | |
{ | |
commands.DrawRenderer(OutlinedObject, WriteObject); | |
} | |
//apply everything and clean up in commandbuffer | |
commands.Blit(source, destination, ApplyOutline); | |
commands.ReleaseTemporaryRT(selectionBuffer); | |
//execute and clean up commandbuffer itself | |
Graphics.ExecuteCommandBuffer(commands); | |
commands.Dispose(); | |
} | |
} |