using UnityEngine; | |
public class ClippableObject : MonoBehaviour | |
{ | |
//only 3 clip planes for now, will need to modify the shader for more. | |
private int _clipPlanes = 4; | |
public Material[] _materials; | |
public GameObject _planeX; | |
public GameObject _planeXnegative; | |
public GameObject _planeY; | |
public GameObject _planeZ; | |
public void Update() | |
{ | |
Vector3 positionX = _planeX.transform.position; | |
Vector3 normalX = _planeX.transform.up; | |
Vector3 positionY = _planeY.transform.position; | |
Vector3 normalY = _planeY.transform.up; | |
Vector3 positionZ = _planeZ.transform.position; | |
Vector3 normalZ = _planeZ.transform.up; | |
Vector3 positionXnegative = _planeXnegative.transform.position; | |
Vector3 normalXnegative = _planeXnegative.transform.up; | |
if ( _materials != null ) | |
{ | |
for ( int i = 0; i < _materials.Length; i++ ) | |
{ | |
if ( _materials[i] == null ) | |
{ | |
continue; | |
} | |
switch ( _clipPlanes ) | |
{ | |
case 0: | |
//https://docs.unity3d.com/ScriptReference/Material.EnableKeyword.html | |
_materials[i].DisableKeyword( "CLIP_ONE" ); | |
_materials[i].DisableKeyword( "CLIP_TWO" ); | |
_materials[i].DisableKeyword( "CLIP_THREE" ); | |
_materials[i].DisableKeyword( "CLIP_FOUR" ); | |
break; | |
case 1: | |
_materials[i].EnableKeyword( "CLIP_ONE" ); | |
_materials[i].DisableKeyword( "CLIP_TWO" ); | |
_materials[i].DisableKeyword( "CLIP_THREE" ); | |
_materials[i].DisableKeyword( "CLIP_FOUR" ); | |
break; | |
case 2: | |
_materials[i].DisableKeyword( "CLIP_ONE" ); | |
_materials[i].EnableKeyword( "CLIP_TWO" ); | |
_materials[i].DisableKeyword( "CLIP_THREE" ); | |
_materials[i].DisableKeyword( "CLIP_FOUR" ); | |
break; | |
case 3: | |
_materials[i].DisableKeyword( "CLIP_ONE" ); | |
_materials[i].DisableKeyword( "CLIP_TWO" ); | |
_materials[i].EnableKeyword( "CLIP_THREE" ); | |
_materials[i].DisableKeyword( "CLIP_FOUR" ); | |
break; | |
case 4: | |
_materials[i].DisableKeyword( "CLIP_ONE" ); | |
_materials[i].DisableKeyword( "CLIP_TWO" ); | |
_materials[i].DisableKeyword( "CLIP_ THREE" ); | |
_materials[i].EnableKeyword( "CLIP_FOUR" ); | |
break; | |
} | |
//Pasa los planos al shader si es necesario. | |
if ( _clipPlanes >= 1 ) | |
{ | |
_materials[i].SetVector( "_planePos", positionX ); | |
//plano vector normal es el vector 'arriba' girado. | |
_materials[i].SetVector( "_planeNorm", normalX ); | |
} | |
if ( _clipPlanes >= 2 ) | |
{ | |
_materials[i].SetVector( "_planePos2", positionY ); | |
_materials[i].SetVector( "_planeNorm2", normalY ); | |
} | |
if ( _clipPlanes >= 3 ) | |
{ | |
_materials[i].SetVector( "_planePos3", positionZ ); | |
_materials[i].SetVector( "_planeNorm3", normalZ ); | |
} | |
if ( _clipPlanes >= 4 ) | |
{ | |
_materials[i].SetVector( "_planePos4", positionXnegative ); | |
_materials[i].SetVector( "_planeNorm4", normalXnegative ); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment