I’ve been trying for the past few days to render a texture in a coroutine and stumbled onto quite weird behaviours:
For example if the coroutine do not wait before rendering or wait for 1 or 3 frames it does so correctly.
But, it it wait for 2 or 4+ frames, it just does not render at all what I’m trying to render.
Does anyone has any idea why this is happening ?
Below is the script and Shader used for that
MyScript.cs:
using System.Collections;
using UnityEngine;
public class MyScript : MonoBehaviour
{
public MeshRenderer myRenderer;
public RenderTexture myRenderTexture;
public Material myMaterial;
[Space] public Mode myTestMode;
public enum Mode
{
Seconds = 0,
NbYields = 1,
}
public float mySecondsWaiting;
public int myMaxIter;
public bool myDoInUpdate;
private Texture2D MyTexture2D;
private void Start()
{
StartCoroutine(MyCoroutine());
}
private void Update()
{
if (myDoInUpdate)
MyFunction();
}
private void MyFunction(Camera cam = null)
{
// Draw to Texture
myMaterial.SetTexture("_MainTex", null);
//m_Material.SetColor("_DrawColor", m_DrawColor);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = myRenderTexture;
GL.Clear(true, true, Color.clear, 0);
GL.PushMatrix();
GL.LoadIdentity();
GL.LoadOrtho();
myMaterial.SetPass(0);
Mesh newM = new Mesh
{
vertices = new[] {Vector3.zero, Vector3.right, Vector3.forward},
triangles = new[] {0, 1, 2},
uv = new[] {Vector2.zero, Vector2.right, Vector2.up}
};
newM.RecalculateNormals();
newM.RecalculateTangents();
Graphics.DrawMeshNow(newM, Vector3.forward, Quaternion.Euler(-90, 0, 0));
GL.PopMatrix();
GL.Flush();
RenderTexture.active = previous;
myRenderer.sharedMaterial.mainTexture = myRenderTexture;
Destroy(newM);
}
private IEnumerator MyCoroutine()
{
switch (myTestMode)
{
case Mode.Seconds:
yield return new WaitForSeconds(mySecondsWaiting);
break;
case Mode.NbYields:
for (int i = 0; i < myMaxIter; ++i)
yield return null;
break;
}
MyFunction();
yield break;
}
}
MyShader.shader:
Shader "Painter"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_DrawColor("Color", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader
{
Tags { "RenderType" = "Transparent" }
// No culling or depth
Cull Off ZWrite Off ZTest Always
CGINCLUDE
#include "UnityCG.cginc"
// Main input texture
sampler2D _MainTex;
//x: 1.0 / width
//y: 1.0 / height
//z: width
//w: height
uniform half4 _MainTex_TexelSize;
float4 _PixelSize;
// Basic drawing settings
float4 _DrawColor;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
ENDCG
// Simple color
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
fixed4 frag(v2f i) : SV_TARGET {
return _DrawColor;
}
ENDCG
}
}
}
EDIT: Tested in 2018.4.17f1
EDIT: Tested in 2019.2.19f1, found another weird behavior that if the material which will receive the main texture is selected, the texture is correctly rendered.
EDIT: Tested in 2019.3.2f1, texture is not rendered if frames are skipped, not rendered if material is selected.