Skip to content

Instantly share code, notes, and snippets.

@TSUMIKISEISAKU
Created September 7, 2023 05:09
Show Gist options
  • Save TSUMIKISEISAKU/7b8adbc13516bb0a6f700d1f0ef9e89b to your computer and use it in GitHub Desktop.
Save TSUMIKISEISAKU/7b8adbc13516bb0a6f700d1f0ef9e89b to your computer and use it in GitHub Desktop.
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel Paint
#pragma kernel CheckProgress
// paint marking texture for drawing and check the progress
// render texture for drawing marking
RWTexture2D<float4> _drawTexture_Write;
// render texture for drawing marking
RWTexture2D<float4> _drawTexture_Read;
// render texture to check the progress of marking
RWTexture2D<float4> _checkTexture_Write;
// render texture to check the progress of marking
RWTexture2D<float4> _checkTexture_Read;
// mask for checking progress
Texture2D<float4> _maskTexture;
// UV coords to paint
float2 _paintUV;
// radius of the paint area
float _paintRadius;
// resolution of _drawTexture
uint _drawTexResolution;
// resolution of _checkTexture
uint _checkTexResolution;
// progress of painting in unit value
RWStructuredBuffer<float> _progress;
// size of thread group
#define SIMULATION_BLOCK_SIZE 8
// paint marking textures
[numthreads(SIMULATION_BLOCK_SIZE, SIMULATION_BLOCK_SIZE,1)]
void Paint (uint3 id : SV_DispatchThreadID)
{
// paint _drawTexture
float dist = distance((float2)_paintUV, (float2)id.xy / _drawTexResolution);
float paintIndensity = saturate(smoothstep(_paintRadius, _paintRadius / 2, dist));
_drawTexture_Write[id.xy] = float4(saturate(max(_drawTexture_Read[id.xy].x, paintIndensity)), 0, 0, 1);
// paint _checkTexture
if (id.x >= _checkTexResolution || id.y >= _checkTexResolution) return;
dist = distance((float2)_paintUV, (float2)id.xy / _checkTexResolution);
paintIndensity = step(dist, _paintRadius * 1.2);
_checkTexture_Write[id.xy] = float4(saturate(_checkTexture_Read[id.xy].x + paintIndensity), 0, 0, 1);
}
// check painting progress using _checkTexture
[numthreads(1,1, 1)]
void CheckProgress(uint3 id : SV_DispatchThreadID)
{
int count = 0;
float totalCount = 0;
float paintValThresh = 0.85;
for (uint y = 0; y < _checkTexResolution; y++)
{
for (uint x = 0; x < _checkTexResolution; x++)
{
uint2 coord = uint2(x, y);
if (_maskTexture[coord].x <= 0) continue;
// if the pixel color is more than the threshold, count this pixel as a painted one.
if (_checkTexture_Read[coord].x > paintValThresh)
{
count++;
}
totalCount++;
}
}
_progress[0] = count / totalCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment