udRenderTarget.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. namespace udSDK
  5. {
  6. public enum udRenderTargetMatrix
  7. {
  8. Camera, // The local to world-space transform of the camera (View is implicitly set as the inverse)
  9. View, // The view-space transform for the model (does not need to be set explicitly)
  10. Projection, // The projection matrix (default is 60 degree LH)
  11. Viewport, // Viewport scaling matrix (default width and height of viewport)
  12. Count
  13. };
  14. public class udRenderTarget
  15. {
  16. public IntPtr pRenderView = IntPtr.Zero;
  17. private udContext context;
  18. private GCHandle colorBufferHandle;
  19. private GCHandle depthBufferHandle;
  20. ~udRenderTarget()
  21. {
  22. Destroy();
  23. }
  24. public void Destroy()
  25. {
  26. if (colorBufferHandle.IsAllocated)
  27. colorBufferHandle.Free();
  28. if (depthBufferHandle.IsAllocated)
  29. depthBufferHandle.Free();
  30. udError error = udRenderTarget_Destroy(ref pRenderView);
  31. if (error != udSDK.udError.udE_Success)
  32. throw new Exception("udRenderView.Destroy failed.");
  33. pRenderView = IntPtr.Zero;
  34. }
  35. public void Create(udContext context, udRenderContext renderer, UInt32 width, UInt32 height)
  36. {
  37. if (context.pContext == IntPtr.Zero)
  38. throw new Exception("context not instantiated");
  39. if (renderer.pRenderer == IntPtr.Zero)
  40. throw new Exception("renderer not instantiated");
  41. udError error = udRenderTarget_Create(context.pContext, ref pRenderView, renderer.pRenderer, width, height);
  42. if (error != udSDK.udError.udE_Success)
  43. throw new Exception("udRenderView.Create failed: " + error.ToString());
  44. this.context = context;
  45. }
  46. public void SetTargets(ref UnityEngine.Color32[] colorBuffer, UInt32 clearColor, ref float[] depthBuffer)
  47. {
  48. if (colorBufferHandle.IsAllocated)
  49. colorBufferHandle.Free();
  50. if (depthBufferHandle.IsAllocated)
  51. depthBufferHandle.Free();
  52. colorBufferHandle = GCHandle.Alloc(colorBuffer, GCHandleType.Pinned);
  53. depthBufferHandle = GCHandle.Alloc(depthBuffer, GCHandleType.Pinned);
  54. udError error = udRenderTarget_SetTargets(pRenderView, colorBufferHandle.AddrOfPinnedObject(), clearColor, depthBufferHandle.AddrOfPinnedObject());
  55. if (error != udSDK.udError.udE_Success)
  56. throw new Exception("udRenderView.SetTargets failed.");
  57. }
  58. public void GetMatrix(udRenderTargetMatrix matrixType, double[] cameraMatrix)
  59. {
  60. udError error = udRenderTarget_GetMatrix(pRenderView, matrixType, cameraMatrix);
  61. if (error != udSDK.udError.udE_Success)
  62. throw new Exception("udRenderView.GetMatrix failed.");
  63. }
  64. public void SetMatrix(udRenderTargetMatrix matrixType, double[] cameraMatrix)
  65. {
  66. if (pRenderView == IntPtr.Zero)
  67. throw new Exception("view not instantiated");
  68. udError error = udRenderTarget_SetMatrix(pRenderView, matrixType, cameraMatrix);
  69. if (error != udSDK.udError.udE_Success)
  70. throw new Exception("udRenderView.SetMatrix failed: " + error.ToString());
  71. }
  72. [DllImport(UDSDKLibrary.name)]
  73. private static extern udError udRenderTarget_Create(IntPtr pContext, ref IntPtr ppRenderView, IntPtr pRenderer, UInt32 width, UInt32 height);
  74. [DllImport(UDSDKLibrary.name)]
  75. private static extern udError udRenderTarget_Destroy(ref IntPtr ppRenderView);
  76. [DllImport(UDSDKLibrary.name)]
  77. private static extern udError udRenderTarget_SetTargets(IntPtr pRenderView, IntPtr pColorBuffer, UInt32 colorClearValue, IntPtr pDepthBuffer);
  78. [DllImport(UDSDKLibrary.name)]
  79. private static extern udError udRenderTarget_GetMatrix(IntPtr pRenderView, udRenderTargetMatrix matrixType, double[] cameraMatrix);
  80. [DllImport(UDSDKLibrary.name)]
  81. private static extern udError udRenderTarget_SetMatrix(IntPtr pRenderView, udRenderTargetMatrix matrixType, double[] cameraMatrix);
  82. }
  83. }