23 private readonly
int[] scanlinePixelOffset =
new int[2];
24 private int[][] jitterOffsets;
46 private Vector4[] clusterCentersTempData;
52 private Position[] randomPositions;
53 private System.Random random;
55 private struct Position
64 private readonly
int padding_1;
65 private readonly
int padding_2;
77 for (
int k = 0; k < this.randomPositions.Length; k++)
79 this.randomPositions[k].x = this.random.Next(textureSize);
80 this.randomPositions[k].y = this.random.Next(textureSize);
108 $
"Attempting to allocate already allocated {typeof(ClusteringRTsAndBuffers)}"
111 this.rtResult =
new RenderTexture(
115 RenderTextureFormat.ARGBFloat
118 enableRandomWrite =
true,
119 filterMode = FilterMode.Point
127 this.random =
new System.Random();
129 this.cbufRandomPositions =
new ComputeBuffer(
max_num_clusters,
sizeof(
int) * 4);
140 this.cbufClusterCenters =
new ComputeBuffer(this.numClusters * 2,
sizeof(
float) * 4);
141 this.clusterCentersTempData =
new Vector4[this.numClusters * 2];
142#pragma warning disable 162
151#pragma warning restore 162
156 private Vector3 RGB2KKK(Vector3 rgb)
160 new Vector4(1.0f / 3, 1.0f / 2, -1.0f / 4, 0.0f),
161 new Vector4(1.0f / 3, 0.0f, 1.0f / 2, 0.0f),
162 new Vector4(1.0f / 3, -1.0f / 2, -1.0f / 4, 0.0f),
164 ) *
new Vector4(rgb.x, rgb.y, rgb.z, 0);
172 var c = Color.HSVToRGB(i / (
float)(this.numClusters), 1, 1);
174 Vector3 kkkColor = RGB2KKK(
new Vector4(c.r, c.g, c.b, 0));
177 this.clusterCentersTempData[i] =
new Vector4(
186 this.clusterCentersTempData[i + this.
numClusters] =
new Vector4(
203 var c = Color.HSVToRGB((
float)this.random.NextDouble(), 1, 1);
204 Vector4 kkkColor = RGB2KKK(
new Vector4(c.r, c.g, c.b, 0));
207 this.clusterCentersTempData[i] =
new Vector4(
216 this.clusterCentersTempData[i + this.
numClusters] =
new Vector4(
233 ComputeShader csHighlightRemoval,
234 bool staggeredJitter,
238 int kernelSubsample = csHighlightRemoval.FindKernel(
"SubSample");
240 csHighlightRemoval.SetInt(
241 "sub_sample_multiplier",
245 csHighlightRemoval.SetInt(
246 "subsample_mip_level",
252 csHighlightRemoval.SetInts(
254 this.jitterOffsets[Time.frameCount %
this.jitterOffsets.Length]
259 this.scanlinePixelOffset[0] = Time.frameCount % this.
jitterSize;
260 this.scanlinePixelOffset[1] =
261 (Time.frameCount / this.jitterOffsets.Length) % this.jitterSize;
262 csHighlightRemoval.SetInts(
"sub_sample_offset", this.scanlinePixelOffset);
265 csHighlightRemoval.SetTexture(
271 csHighlightRemoval.SetTexture(
277 csHighlightRemoval.SetBool(
"downscale", doDownscale);
279 csHighlightRemoval.Dispatch(
291 this.rtResult.Release();
Call Dispose after using.
static ClusterCenters Get(int numClusters, Vector4[] centersBufferData)
Gets a pooled instance of ClusterCenters. The data is copied, making is safe to edit....
Call Allocate before using and Dispose after using.
const int max_num_clusters
ComputeBuffer cbufRandomPositions
ClusteringRTsAndBuffers(int numClusters, int workingSize, int fullSize, int jitterSize)
Textures are not allocated in the constructor. Call Allocate manually before using.
void Downsample(ComputeShader csHighlightRemoval, bool staggeredJitter, bool doDownscale)
Update textureWorkRes by downsampling textureFullRes
ComputeBuffer cbufClusterCenters
void UpdateRandomPositions()
ClusterCenters GetClusterCenters()
Get a pooled instance of ClusterCenters with a copy of the data from ComputeBuffer....
void SetDeterministicClusterCenters()
void SetClusterCenters(Vector4[] clusterCentersBufferData)
Reads the cluster centers data from the array and loads it into the ComputeBuffer.
ClusteringTextures texturesFullRes
void RandomizeClusterCenters()
ClusteringTextures texturesWorkRes
int PickRandomCluster(int numClusters)
const int kernelSize
Must match the shader.
Call Dispose after using.
readonly RenderTexture rtInput
static int[][] Get(int size)