- Home /
CustomRenderTexture (RFloat) wont initialize in the same frame it is created.
How can I initialize a CustomRenderTexture, Update it and use it inside a material in the same frame?
The following code shows how I create a CustomRenderTexture during Start(), then i call Initialize() inside Start to make it all black. During LateUpdate() i Update() the texture and assign it to a material inside a MeshRenderer.
The problem here is, that if I call initialize (it does not matter how often), the texture wont turn black! If i wait one frame (after creation) and then call initialize the texture does turn black.
EDIT: If i change the RenderTextureFormat to ARGB32, the initialization works! My next thought was ok maybe the shader used for TextureAndColor initialization does not support RFloat, so I wrote my own init shader, that simply returns 0. But this does not work either (in the same frame). On the next frame my own shader as well as the built in TextureAndColor initializer works for RFloat color format.
private void Start()
{
primaryWaveTex = new CustomRenderTexture(resolution, resolution, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear);
primaryWaveTex.name = "primaryWaveTex";
primaryWaveTex.filterMode = FilterMode.Point;
primaryWaveTex.dimension = UnityEngine.Rendering.TextureDimension.Cube;
primaryWaveTex.material = primaryWaveMat;
primaryWaveTex.updateMode = CustomRenderTextureUpdateMode.OnDemand;
primaryWaveTex.initializationMode = CustomRenderTextureUpdateMode.OnDemand;
primaryWaveTex.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor;
primaryWaveTex.initializationColor = Color.clear;
primaryWaveTex.doubleBuffered = false;
primaryWaveTex.anisoLevel = 0;
primaryWaveTex.depth = 0;
primaryWaveTex.Create();
primaryWaveTex.Initialize();
}
private void LateUpdate()
{
//commented out to visualize initialized color
//primaryWaveTex.Update();
meshRenderer.material.SetTexture("_Surface", waveTex);
}