IsVisibleFrom

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
Line 55: Line 55:
 
     var planes = GeometryUtility.CalculateFrustumPlanes(camera);
 
     var planes = GeometryUtility.CalculateFrustumPlanes(camera);
 
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
 
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}</javascript>
+
}</syntaxhighlight>

Latest revision as of 20:52, 10 January 2012

Author: Michael Garforth


Contents

[edit] Overview

This C# class gives simple extension access to checking if an Renderer is rendered by a specific Camera.

Due to the way UnityScript/javascript compiles extension methods, it's not accessible in the same way, so another javascript specific class is added below it


[edit] Use

For the c# extension method: renderer.IsVisibleFrom(cam)

For the UnityScript static function: RendererHelper.IsVisibleFrom(renderer, cam)


[edit] Example - TestRendered.cs

using UnityEngine;
 
public class TestRendered : MonoBehaviour
{	
	void Update()
	{
		if (renderer.IsVisibleFrom(Camera.main)) Debug.Log("Visible");
		else Debug.Log("Not visible");
	}
}


[edit] C# - RendererExtensions.cs

The script should be named RendererExtensions.cs


using UnityEngine;
 
public static class RendererExtensions
{
	public static bool IsVisibleFrom(this Renderer renderer, Camera camera)
	{
		Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
		return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
	}
}


[edit] UnityScript - RendererHelpers.js

The script should be named RendererHelpers.js Use this only if you won't be using c#

static function IsRenderedFrom(renderer : Renderer, camera : Camera) : boolean
{
    var planes = GeometryUtility.CalculateFrustumPlanes(camera);
	return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox