暫無描述
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.

URPCameraBinder.cs 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #if VISUAL_EFFECT_GRAPH_0_0_1_OR_NEWER
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. namespace UnityEngine.VFX.Utility
  5. {
  6. /// <summary>
  7. /// Camera parameter binding helper class.
  8. /// </summary>
  9. [VFXBinder("URP/URP Camera")]
  10. public class URPCameraBinder : VFXBinderBase
  11. {
  12. /// <summary>
  13. /// Camera URP additional data.
  14. /// </summary>
  15. public UniversalAdditionalCameraData AdditionalData;
  16. Camera m_Camera;
  17. [VFXPropertyBinding("UnityEditor.VFX.CameraType"), SerializeField]
  18. ExposedProperty CameraProperty = "Camera";
  19. ExposedProperty m_Position;
  20. ExposedProperty m_Angles;
  21. ExposedProperty m_Scale;
  22. ExposedProperty m_FieldOfView;
  23. ExposedProperty m_NearPlane;
  24. ExposedProperty m_FarPlane;
  25. ExposedProperty m_AspectRatio;
  26. ExposedProperty m_Dimensions;
  27. ExposedProperty m_ScaledDimensions;
  28. ExposedProperty m_DepthBuffer;
  29. ExposedProperty m_ColorBuffer;
  30. ExposedProperty m_Orthographic;
  31. ExposedProperty m_OrthographicSize;
  32. ExposedProperty m_LensShift;
  33. /// <summary>
  34. /// Set a camera property.
  35. /// </summary>
  36. /// <param name="name">Property name.</param>
  37. public void SetCameraProperty(string name)
  38. {
  39. CameraProperty = name;
  40. UpdateSubProperties();
  41. }
  42. void UpdateSubProperties()
  43. {
  44. // Get Camera component from URP additional data
  45. if (AdditionalData != null)
  46. {
  47. m_Camera = AdditionalData.GetComponent<Camera>();
  48. }
  49. // Update VFX Sub Properties
  50. m_Position = CameraProperty + "_transform_position";
  51. m_Angles = CameraProperty + "_transform_angles";
  52. m_Scale = CameraProperty + "_transform_scale";
  53. m_Orthographic = CameraProperty + "_orthographic";
  54. m_FieldOfView = CameraProperty + "_fieldOfView";
  55. m_NearPlane = CameraProperty + "_nearPlane";
  56. m_FarPlane = CameraProperty + "_farPlane";
  57. m_OrthographicSize = CameraProperty + "_orthographicSize";
  58. m_AspectRatio = CameraProperty + "_aspectRatio";
  59. m_Dimensions = CameraProperty + "_pixelDimensions";
  60. m_LensShift = CameraProperty + "_lensShift";
  61. m_DepthBuffer = CameraProperty + "_depthBuffer";
  62. m_ColorBuffer = CameraProperty + "_colorBuffer";
  63. m_ScaledDimensions = CameraProperty + "_scaledPixelDimensions";
  64. }
  65. static void RequestHistoryAccess(IPerFrameHistoryAccessTracker access)
  66. {
  67. access?.RequestAccess<RawColorHistory>();
  68. access?.RequestAccess<RawDepthHistory>();
  69. }
  70. /// <summary>
  71. /// OnEnable implementation.
  72. /// </summary>
  73. protected override void OnEnable()
  74. {
  75. base.OnEnable();
  76. if (AdditionalData != null)
  77. AdditionalData.history.OnGatherHistoryRequests += RequestHistoryAccess;
  78. UpdateSubProperties();
  79. }
  80. /// <summary>
  81. /// OnDisable implementation.
  82. /// </summary>
  83. protected override void OnDisable()
  84. {
  85. base.OnDisable();
  86. if (AdditionalData != null)
  87. AdditionalData.history.OnGatherHistoryRequests -= RequestHistoryAccess;
  88. }
  89. private void OnValidate()
  90. {
  91. UpdateSubProperties();
  92. if (AdditionalData != null)
  93. AdditionalData.history.OnGatherHistoryRequests += RequestHistoryAccess;
  94. }
  95. /// <summary>
  96. /// Returns true if the Visual Effect and the configuration of the binder are valid to perform the binding.
  97. /// </summary>
  98. /// <param name="component">Component to be tested.</param>
  99. /// <returns>True if the Visual Effect and the configuration of the binder are valid to perform the binding.</returns>
  100. public override bool IsValid(VisualEffect component)
  101. {
  102. return AdditionalData != null
  103. && m_Camera != null
  104. && component.HasVector3(m_Position)
  105. && component.HasVector3(m_Angles)
  106. && component.HasVector3(m_Scale)
  107. && component.HasBool(m_Orthographic)
  108. && component.HasFloat(m_FieldOfView)
  109. && component.HasFloat(m_NearPlane)
  110. && component.HasFloat(m_FarPlane)
  111. && component.HasFloat(m_OrthographicSize)
  112. && component.HasFloat(m_AspectRatio)
  113. && component.HasVector2(m_Dimensions)
  114. && component.HasVector2(m_LensShift)
  115. && component.HasTexture(m_DepthBuffer)
  116. && component.HasTexture(m_ColorBuffer)
  117. && component.HasVector2(m_ScaledDimensions);
  118. }
  119. /// <summary>
  120. /// Update bindings for a visual effect.
  121. /// </summary>
  122. /// <param name="component">Component to update.</param>
  123. public override void UpdateBinding(VisualEffect component)
  124. {
  125. var asset = UniversalRenderPipeline.asset;
  126. if (AdditionalData == null || asset == null)
  127. return;
  128. var targetSpace = component.visualEffectAsset.GetExposedSpace(m_Position);
  129. Matrix4x4 readTransform;
  130. if (targetSpace == VFXSpace.Local)
  131. {
  132. readTransform = component.transform.worldToLocalMatrix * AdditionalData.transform.localToWorldMatrix;
  133. }
  134. else
  135. {
  136. readTransform = AdditionalData.transform.localToWorldMatrix;
  137. }
  138. component.SetVector3(m_Position, readTransform.GetPosition());
  139. component.SetVector3(m_Angles, readTransform.rotation.eulerAngles);
  140. component.SetVector3(m_Scale, readTransform.lossyScale);
  141. component.SetBool(m_Orthographic, m_Camera.orthographic);
  142. component.SetFloat(m_OrthographicSize, m_Camera.orthographicSize);
  143. // While field of View is set in degrees for the camera, it is expected in radians in VFX
  144. component.SetFloat(m_FieldOfView, Mathf.Deg2Rad * m_Camera.fieldOfView);
  145. component.SetFloat(m_NearPlane, m_Camera.nearClipPlane);
  146. component.SetFloat(m_FarPlane, m_Camera.farClipPlane);
  147. component.SetVector2(m_LensShift, m_Camera.lensShift);
  148. component.SetFloat(m_AspectRatio, m_Camera.aspect);
  149. var cameraSize = new Vector2(m_Camera.pixelWidth, m_Camera.pixelHeight);
  150. component.SetVector2(m_Dimensions, cameraSize);
  151. var scaledSize = new Vector2(m_Camera.scaledPixelWidth, m_Camera.scaledPixelHeight) * asset.renderScale;
  152. component.SetVector2(m_ScaledDimensions, scaledSize);
  153. var depth = AdditionalData.history.GetHistoryForRead<RawDepthHistory>()?.GetCurrentTexture();
  154. var color = AdditionalData.history.GetHistoryForRead<RawColorHistory>()?.GetCurrentTexture();
  155. if (depth != null)
  156. component.SetTexture(m_DepthBuffer, depth);
  157. if (color != null)
  158. component.SetTexture(m_ColorBuffer, color);
  159. }
  160. /// <summary>
  161. /// To string implementation.
  162. /// </summary>
  163. /// <returns>String containing the binder information.</returns>
  164. public override string ToString()
  165. {
  166. return string.Format($"URP Camera : '{(AdditionalData == null ? "null" : AdditionalData.gameObject.name)}' -> {CameraProperty}");
  167. }
  168. }
  169. }
  170. #endif