UDPPES.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Rendering.PostProcessing;
  5. using udSDK;
  6. using System.Runtime.InteropServices;
  7. [Serializable]
  8. [PostProcess(typeof(UDPPER), PostProcessEvent.BeforeTransparent, "UD/UDPPES")]
  9. public sealed class UDPPES : PostProcessEffectSettings
  10. {
  11. [Tooltip("Apply the depth pass from udShader to _CameraDepthTexture")]
  12. public BoolParameter depthPass = new BoolParameter { value = true };
  13. }
  14. public sealed class UDPPER : PostProcessEffectRenderer<UDPPES>
  15. {
  16. private int width = 1280;
  17. private int height = 1280;
  18. private Color32[] colourBuffer;
  19. private Texture2D colourTexture;
  20. private float[] depthBuffer;
  21. private Texture2D depthTexture;
  22. private udRenderTarget vRenderView;
  23. public override void Init()
  24. {
  25. try
  26. {
  27. GlobalUDContext.Login();
  28. InitialiseBuffers(width, height);
  29. InitialiseTextures();
  30. vRenderView = new udRenderTarget();
  31. vRenderView.Create(GlobalUDContext.uContext, GlobalUDContext.renderer, (uint)width, (uint)height);
  32. vRenderView.SetTargets(ref colourBuffer, 0, ref depthBuffer);
  33. }
  34. catch
  35. {
  36. System.Diagnostics.Debug.WriteLine("Failed to Init");
  37. }
  38. }
  39. private void InitialiseTextures()
  40. {
  41. if (colourTexture == null)
  42. colourTexture = new Texture2D(width, height, TextureFormat.BGRA32, false);
  43. if (depthTexture == null)
  44. depthTexture = new Texture2D(width, height, TextureFormat.RFloat, false);
  45. }
  46. private void InitialiseBuffers(int newWidth, int newHeight)
  47. {
  48. width = newWidth;
  49. height = newHeight;
  50. colourBuffer = new Color32[width * height];
  51. depthBuffer = new float[width * height];
  52. }
  53. void RebuildBuffers(int newWidth, int newHeight)
  54. {
  55. InitialiseBuffers(newWidth, newHeight);
  56. colourTexture.Resize(width, height, TextureFormat.BGRA32, false);
  57. depthTexture.Resize(width, height, TextureFormat.RFloat, false);
  58. vRenderView.Destroy();
  59. vRenderView.Create(GlobalUDContext.uContext, GlobalUDContext.renderer, (uint)width, (uint)height);
  60. vRenderView.SetTargets(ref colourBuffer, 0, ref depthBuffer);
  61. }
  62. public override void Render(PostProcessRenderContext context)
  63. {
  64. Camera cam = context.camera;
  65. cam.depthTextureMode |= DepthTextureMode.Depth;
  66. if (!GlobalUDContext.isCreated)
  67. return;
  68. UDCameraOptions optionsContainer = cam.GetComponent<UDCameraOptions>();
  69. RenderOptions options;
  70. float resolutionScaling;
  71. if (optionsContainer != null)
  72. {
  73. options = optionsContainer.optionsStruct;
  74. resolutionScaling = optionsContainer.resolutionScaling;
  75. }
  76. else
  77. {
  78. optionsContainer = null;
  79. options = new RenderOptions();
  80. resolutionScaling = 1;
  81. }
  82. if ( (int)context.width*resolutionScaling != width || (int)context.height*resolutionScaling != height)
  83. RebuildBuffers( (int)(context.width*resolutionScaling), (int)(context.height*resolutionScaling));
  84. GameObject[] objects = GameObject.FindGameObjectsWithTag("UDSModel");
  85. udRenderInstance[] modelArray = UDUtilities.getUDSInstances();
  86. if (modelArray.Length > 0)
  87. {
  88. vRenderView.SetMatrix(udSDK.udRenderTargetMatrix.View, UDUtilities.GetUDMatrix(cam.worldToCameraMatrix));
  89. vRenderView.SetMatrix(udSDK.udRenderTargetMatrix.Projection, UDUtilities.GetUDMatrix(cam.projectionMatrix));
  90. //interface to input render options: this allows setting of render flags, picking and filtering from unity objects attached to the camera
  91. GlobalUDContext.renderer.Render(vRenderView, modelArray, modelArray.Length, options);
  92. //pass the depth buffer back to the unity interface for further processing:
  93. if (optionsContainer != null && optionsContainer.recordDepthBuffer)
  94. optionsContainer.setDepthImageFromZ(depthBuffer);//for as yet unimplemented features
  95. //make sure that the textures exist before operating on them
  96. if (colourTexture == null || depthTexture == null)
  97. InitialiseTextures();
  98. colourTexture.SetPixels32(colourBuffer);
  99. colourTexture.Apply();
  100. depthTexture.LoadRawTextureData<float>(new Unity.Collections.NativeArray<float>(depthBuffer, Unity.Collections.Allocator.Temp));
  101. depthTexture.Apply();
  102. var sheet = context.propertySheets.Get(Shader.Find("Hidden/UDSDK/UDSDKShader"));
  103. sheet.properties.SetTexture("_udCol", colourTexture);
  104. sheet.properties.SetTexture("_udDep", depthTexture);
  105. context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
  106. if(settings.depthPass)
  107. context.command.BlitFullscreenTriangle(context.source, BuiltinRenderTextureType.Depth, sheet, 0);
  108. }
  109. }
  110. }