Question by
compuguru992000 · Jan 19, 2019 at 05:19 AM ·
lightingtexture2drendertexturecodepagecookie
RenderTexture to Spotlight Cookie
I have been trying to get a render texture to work in a spotlight as a cookie, the camera going to the render texture has clear solid color with alpha 0 so only objects in camera(particle system) are shown in the texture preview panel. code below that is not working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextureToSpotlightTest : MonoBehaviour {
public RenderTexture rText;
public Light light;
Texture2D texture;
// Use this for initialization
void Start ()
{
StartCoroutine(Picture());
}
IEnumerator Picture()
{
while (true)
{
yield return new WaitForEndOfFrame();
Texture2D myTexture = toTexture2D(rText);
light.cookie = myTexture;
}
}
Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
tex.alphaIsTransparency = true;
tex.wrapMode = TextureWrapMode.Clamp;
return tex;
}
}
Comment