RenderTexture Free

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Creation of RenderTextureFree page)
 
(Changed the parameters. You can now control the x and y of the destination.)
Line 46: Line 46:
 
 
 
// Return the entire screen in a texture
 
// Return the entire screen in a texture
public static Texture Capture(){ return Capture( 0, 0, 1, 1 ); }
+
public static Texture Capture(){ return Capture( new Rect( 0f, 0f, Screen.width, Screen.height ), 0, 0 ); }
 +
 
 
// Return part of the screen in a texture.
 
// Return part of the screen in a texture.
public static Texture Capture( Rect area ){ return Capture( area.x, area.y, area.width, area.height ); }
+
public static Texture Capture( Rect captureZone, int destX, int destY )
// Return part of the screen in a texture. (0,0) is top left rect, (1,1) is the bottom right
+
public static Texture Capture( float x, float y, float width, float height )
+
 
{
 
{
x = Mathf.Clamp01(x);
+
Texture2D result;
y = Mathf.Clamp01(y);
+
result = new Texture2D( Mathf.RoundToInt( captureZone.width ) + destX,
width = Mathf.Clamp01(width);
+
Mathf.RoundToInt( captureZone.height ) + destY,
height = Mathf.Clamp01(height);
+
TextureFormat.RGB24, false);
+
result.ReadPixels(captureZone, destX, destY, false);
    tex2D = tex2D ?? new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
+
 
    tex2D.ReadPixels( new Rect(Screen.width * x, Screen.height * y, Screen.width * width, Screen.height * height),
+
// That's the heavy part, it takes a lot of time.
                Mathf.RoundToInt(Screen.width * x), Mathf.RoundToInt(Screen.height * y),
+
                result.Apply();
                false);
+
 
    tex2D.Apply(); // That's the heavy part, it takes a lot of time.
+
return result;
    tex = tex2D;
+
+
return tex;
+
 
}
 
}
 
}</syntaxhighlight>
 
}</syntaxhighlight>

Revision as of 01:44, 18 June 2012

Description

Simulate a render texture by copying what is displayed on screen into a texture, GUI included. A Texture.Apply() is performed, which is extremely slow, so it is a bad idea to capture the screen every frames.

Usage

Attach the following script to the object receiving the texture.

using UnityEngine;
using System.Collections;
 
public class RTtest : MonoBehaviour 
{	
	public float refresh = 1f;
 
	// Update is called once per frame
	void Start (){
		InvokeRepeating( "SimulateRenderTexure", 0f, refresh );
	}
 
	void SimulateRenderTexure(){		
		renderer.material.mainTexture = RenderTextureFree.Capture();
	}
}

RenderTextureFree.cs

using UnityEngine;
using System.Collections;
 
/// <summary>
/// Simulate a RenderTexture. The process need a Texture.Apply, which takes a lot of time,
/// so don't use this every frames.
///
/// by Berenger Mantoue, www.berengermantoue.fr
///
/// </summary>
 
public class RenderTextureFree
{
	private static Texture2D tex2D;
	private static Texture tex;
 
	// Return the entire screen in a texture
	public static Texture Capture(){ return Capture( new Rect( 0f, 0f, Screen.width, Screen.height ), 0, 0 ); }
 
	// Return part of the screen in a texture.
	public static Texture Capture( Rect captureZone, int destX, int destY )
	{
		Texture2D result;
		result = new Texture2D( Mathf.RoundToInt( captureZone.width ) + destX,
					Mathf.RoundToInt( captureZone.height ) + destY,
					TextureFormat.RGB24, false);
		result.ReadPixels(captureZone, destX, destY, false);
 
		// That's the heavy part, it takes a lot of time.
                result.Apply();
 
		return result;
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox