Skip to content

Commit

Permalink
color copy with mips works, but fetching from BiRP SG not yet
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed May 27, 2022
1 parent 533baa2 commit 9420cd2
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@
using System;
using System.Collections.Generic;
using UnityEngine;

[ExecuteAlways, ImageEffectAllowedInSceneView]
public class CopyColorBuiltin : MonoBehaviour
{
public Renderer tempOutput;
private MaterialPropertyBlock tempBlock;

private RenderTexture opaqueTexture;
private RenderTexture temp;
private Dictionary<Camera, RenderTexture> temp = new Dictionary<Camera, RenderTexture>();

private void OnDisable()
{
if(temp != null)
RenderTexture.ReleaseTemporary(temp);
if (temp != null) {
foreach(var kvp in temp)
RenderTexture.ReleaseTemporary(kvp.Value);
}
temp.Clear();
}

private void OnPreRender()
{
SetTexture();
}

private void SetTexture()
{
var current = Camera.current;
if (!temp.ContainsKey(current))
{
Shader.SetGlobalTexture("_CameraOpaqueTexture", Texture2D.blackTexture);
return;
}

Shader.SetGlobalTexture("_CameraOpaqueTexture", temp[current]);

if (tempOutput)
{
if (tempBlock == null) tempBlock = new MaterialPropertyBlock();
tempBlock.SetTexture("_MainTex", temp[current]);
tempOutput.SetPropertyBlock(tempBlock);
}
}

[ImageEffectOpaque]
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
var dsc = src.descriptor;
dsc.useMipMap = true;
dsc.autoGenerateMips = true;
dsc.autoGenerateMips = false;
dsc.msaaSamples = 1;
dsc.width = Mathf.ClosestPowerOfTwo(dsc.width);
dsc.height = Mathf.ClosestPowerOfTwo(dsc.height);

var current = Camera.current;
if (!temp.ContainsKey(current))
temp.Add(current, null);
if(temp[current])
RenderTexture.ReleaseTemporary(temp[current]);

RenderTexture.ReleaseTemporary(temp);
temp = RenderTexture.GetTemporary(dsc);
temp[current] = RenderTexture.GetTemporary(dsc);
temp[current].filterMode = FilterMode.Trilinear;
// temp[current].useMipMap = true;

Graphics.Blit(src, temp);
Graphics.Blit(src, temp[current]);
temp[current].GenerateMips();
Graphics.Blit(src, dest);

Shader.SetGlobalTexture("_CameraOpaqueTexture", temp);
SetTexture();
}
}

0 comments on commit 9420cd2

Please sign in to comment.