IsVisibleFrom

From Unify Community Wiki
Revision as of 13:43, 29 January 2011 by Mike (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: Michael Garforth


Contents

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


Use

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

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


Example - TestRendered.cs

<csharp>using UnityEngine;

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


C# - RendererExtensions.cs

The script should be named RendererExtensions.cs


<csharp>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); } }</csharp>


UnityScript - RendererHelpers.js

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

<javascript>static function IsRenderedFrom(renderer : Renderer, camera : Camera) {

   var planes = GeometryUtility.CalculateFrustumPlanes(camera);

return GeometryUtility.TestPlanesAABB(planes, renderer.bounds); }</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox