You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.0 KiB

5 years ago
  1. 
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. public class FogVolumePrimitive : MonoBehaviour
  5. {
  6. public FogVolumePrimitive()
  7. {
  8. SphereColl = null;
  9. BoxColl = null;
  10. }
  11. public Transform GetTransform { get { return gameObject.transform; } }
  12. public Vector3 GetPrimitiveScale
  13. {
  14. get
  15. {
  16. return new Vector3(Mathf.Max(MinScale, transform.lossyScale.x),
  17. Mathf.Max(MinScale, transform.lossyScale.y),
  18. Mathf.Max(MinScale, transform.lossyScale.z));
  19. }
  20. }
  21. public Bounds Bounds
  22. {
  23. get
  24. {
  25. if (BoxColl != null) { return BoxColl.bounds; }
  26. else if (SphereColl != null) { return SphereColl.bounds; }
  27. else
  28. {
  29. return new Bounds(gameObject.transform.position, gameObject.transform.lossyScale);
  30. }
  31. }
  32. }
  33. public void AddColliderIfNeccessary(EFogVolumePrimitiveType _type)
  34. {
  35. Type = _type;
  36. switch (Type)
  37. {
  38. case EFogVolumePrimitiveType.None:
  39. {
  40. break;
  41. }
  42. case EFogVolumePrimitiveType.Box:
  43. {
  44. if (BoxColl == null) { BoxColl = gameObject.AddComponent<BoxCollider>(); }
  45. break;
  46. }
  47. case EFogVolumePrimitiveType.Sphere:
  48. {
  49. if (SphereColl == null) { SphereColl = gameObject.AddComponent<SphereCollider>(); }
  50. break;
  51. }
  52. }
  53. }
  54. private void OnEnable()
  55. {
  56. Primitive = gameObject;
  57. _Renderer = Primitive.GetComponent<MeshRenderer>();
  58. if (!PrimitiveMaterial)
  59. {
  60. PrimitiveMaterial = (Material) Resources.Load("PrimitiveMaterial");
  61. }
  62. _Renderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  63. _Renderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
  64. _Renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  65. _Renderer.receiveShadows = false;
  66. GetComponent<MeshRenderer>().material = PrimitiveMaterial;
  67. BoxColl = GetComponent<BoxCollider>();
  68. SphereColl = GetComponent<SphereCollider>();
  69. if (BoxColl == null &&
  70. SphereColl == null)
  71. {
  72. BoxColl = gameObject.AddComponent<BoxCollider>();
  73. Type = EFogVolumePrimitiveType.Box;
  74. }
  75. else
  76. {
  77. if (BoxColl != null) { Type = EFogVolumePrimitiveType.Box; }
  78. else if (SphereColl != null) { Type = EFogVolumePrimitiveType.Sphere; }
  79. else { Type = EFogVolumePrimitiveType.None; }
  80. }
  81. }
  82. public BoxCollider BoxColl;
  83. public SphereCollider SphereColl;
  84. public bool IsPersistent = true;
  85. public EFogVolumePrimitiveType Type;
  86. public bool IsSubtractive;
  87. public Material PrimitiveMaterial;
  88. private GameObject Primitive;
  89. private Renderer _Renderer;
  90. private readonly float MinScale = .0001f;
  91. }