TC_CamCapture.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TerrainComposer2
  4. {
  5. [ExecuteInEditMode]
  6. public class TC_CamCapture : MonoBehaviour
  7. {
  8. public Camera cam;
  9. public int collisionMask;
  10. [System.NonSerialized] public Terrain terrain;
  11. Transform t;
  12. public CollisionDirection collisionDirection;
  13. RenderTexture rtCapture;
  14. void Start()
  15. {
  16. t = transform;
  17. cam = GetComponent<Camera>();
  18. cam.aspect = 1;
  19. }
  20. private void OnDestroy()
  21. {
  22. DisposeRTCapture();
  23. }
  24. public void Capture(int collisionMask, CollisionDirection collisionDirection, int outputId, Vector2 resolution)
  25. {
  26. if (TC_Area2D.current.currentTerrainArea == null) return;
  27. bool create = false;
  28. if (rtCapture == null) create = true;
  29. else if (rtCapture.width != resolution.x || rtCapture.height != resolution.y)
  30. {
  31. TC_Compute.DisposeRenderTexture(ref rtCapture);
  32. create = true;
  33. }
  34. if (create)
  35. {
  36. rtCapture = new RenderTexture((int)resolution.x, (int)resolution.y, 16, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear);
  37. cam.targetTexture = rtCapture;
  38. }
  39. // Debug.Log("Capture");
  40. this.collisionMask = collisionMask;
  41. terrain = TC_Area2D.current.currentTerrain;
  42. // this.collisionDirection = collisionDirection;
  43. cam.cullingMask = collisionMask;
  44. SetCamera(collisionDirection, outputId);
  45. cam.Render();
  46. }
  47. public void DisposeRTCapture()
  48. {
  49. cam.targetTexture = null;
  50. TC_Compute.DisposeRenderTexture(ref rtCapture);
  51. }
  52. public void SetCamera(CollisionDirection collisionDirection, int outputId)
  53. {
  54. if (t == null) Start();
  55. if (collisionDirection == CollisionDirection.Up)
  56. {
  57. t.position = new Vector3(TC_Area2D.current.bounds.center.x, -1 + TC_Settings.instance.generateOffset.y, TC_Area2D.current.bounds.center.z);
  58. t.rotation = Quaternion.Euler(-90, 0, 0);
  59. }
  60. else
  61. {
  62. t.position = new Vector3(TC_Area2D.current.bounds.center.x, TC_Area2D.current.bounds.center.y + 1 + TC_Settings.instance.generateOffset.y, TC_Area2D.current.bounds.center.z);
  63. t.rotation = Quaternion.Euler(90, 0, 0);
  64. }
  65. float orthographicSize = TC_Area2D.current.bounds.extents.x;
  66. if (outputId == TC.heightOutput) orthographicSize += TC_Area2D.current.resExpandBorderSize;
  67. cam.orthographicSize = orthographicSize;
  68. cam.nearClipPlane = 0;
  69. cam.farClipPlane = TC_Area2D.current.currentTerrainArea.terrainSize.y + 1;
  70. // Debug.Log(t.position);
  71. // Vector3 size = area.currentTerrain.terrainData.size;
  72. // t.position = new Vector3(area.area.center.x, -1, area.area.center.y);
  73. }
  74. }
  75. }