Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
PostComputeTransform.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.Rendering;
3 
4 namespace droid.Runtime.Utilities.GameObjects.NeodroidCamera {
8  [RequireComponent(typeof(Camera))]
9  [ExecuteInEditMode]
10  public class PostComputeTransform : MonoBehaviour {
14  public ComputeShader _TransformationComputeShader;
15 
16  //[SerializeField] Material _post_material;
17 
18  [Header("Specific", order = 102)]
19  [SerializeField]
20  Camera _camera = null;
21 
22  ComputeBuffer _transformation_compute_buffer;
23  CommandBuffer _transformation_command_buffer;
24 
25  void Awake() {
26  if (this._camera == null) {
27  this._camera = this.GetComponent<Camera>();
28  }
29 
30  this.MyRenderTexture = new RenderTexture(256, 256, 0) {enableRandomWrite = true};
31  this.MyRenderTexture.Create();
32 
33  if (this._TransformationComputeShader) {
34  var kernel_id = this._TransformationComputeShader.FindKernel("CSMain");
35 
36  this._transformation_command_buffer = new CommandBuffer();
37 
38  /*
39  var target_texture = this._camera.targetTexture;
40  this._TransformationComputeShader.SetTexture(kernel_id,
41  "Result",
42  target_texture);
43 
44  this._transformation_compute_buffer = new ComputeBuffer(target_texture.width * target_texture.height * target_texture.depth,
45  sizeof(float)) {name = "My Buffer"};
46 
47 */
48 
49  //Graphics.SetRandomWriteTarget(1, my_buffer);
50 
51  //myBuffer.SetData(minMaxHeight);
52  //this.my_buffer.targetTexture.GetNativeDepthBufferPtr().GetBuffer(0, "minMax", minMaxBuffer);
53 
54  //_TransformationComputeShader.SetFloat("gamma", 2.2);
55  //_TransformationComputeShader.Dispatch(0, map.Length, 1, 1);
56 
57  //my_buffer.SetData(*target_texture.GetNativeTexturePtr());
58 
59 // this._TransformationComputeShader.SetTexture(kernelHandle, "Result", target_texture);
60 
61  this._transformation_command_buffer.SetComputeTextureParam(this._TransformationComputeShader,
62  kernel_id,
63  "Result",
64  this.MyRenderTexture);
65  //this._transformation_command_buffer.SetComputeBufferParam(this._TransformationComputeShader, kernel_id,"Result",this._transformation_compute_buffer);
66  this._transformation_command_buffer.DispatchCompute(this._TransformationComputeShader,
67  kernel_id,
68  256 / 32,
69  256 / 32,
70  1);
71  //this._camera.AddCommandBuffer(CameraEvent.AfterEverything, this._transformation_command_buffer);
72  }
73  }
74 
75  void Update() {
76  if (this._TransformationComputeShader) {
77  this._TransformationComputeShader.SetTexture(0, "Result", this.MyRenderTexture);
78  //this._TransformationComputeShader.SetBuffer(0,"",this._transformation_compute_buffer);
79  this._TransformationComputeShader.Dispatch(0, 256 / 32, 256 / 32, 1);
80  }
81  }
82 
86  public RenderTexture MyRenderTexture { get; set; }
87 
88 // Postprocess the image
89 /*
90  void OnRenderImage(RenderTexture source, RenderTexture destination) {
91 
92 
93 
94  Graphics.Blit(source, destination);
95  }
96 */
97 
101  public void OnDisable() { this.Cleanup(); }
102 
103  void Cleanup() {
104  if (this._transformation_command_buffer != null) {
105  this._camera.RemoveCommandBuffer(CameraEvent.AfterEverything, this._transformation_command_buffer);
106  }
107  }
108 
109  void OnDestroy() {
110  this.Cleanup();
111 
112 //DestroyImmediate(this._GammaCommandBuffer);
113  }
114  }
115 }
ComputeShader _TransformationComputeShader
void OnDisable()