forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
area.cs
55 lines (49 loc) · 1.62 KB
/
area.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Author: Przemyslaw Zaworski, 07.12.2017
//Create custom render texture and assign material with area.shader
//Script calculates ratio between white shape and total UV area.
//It can be use for compute surface area in real-time.
using UnityEngine;
public class area : MonoBehaviour
{
public ComputeShader compute_shader;
public RenderTexture render_texture;
public Material material;
private GUIStyle gui_style = new GUIStyle();
[HideInInspector] public uint[] data;
protected ComputeBuffer compute_buffer;
int handle_init;
int handle_main;
float percent;
void Start ()
{
handle_init = compute_shader.FindKernel("CSInit");
handle_main = compute_shader.FindKernel("CSMain");
compute_buffer = new ComputeBuffer(1, sizeof(uint));
data = new uint[1];
compute_shader.SetTexture(handle_main, "image", render_texture);
compute_shader.SetBuffer(handle_main, "compute_buffer", compute_buffer);
compute_shader.SetBuffer(handle_init, "compute_buffer", compute_buffer);
}
void OnDestroy()
{
if (null != compute_buffer)
{
compute_buffer.Release();
compute_buffer = null;
}
}
void Update()
{
compute_shader.Dispatch(handle_init, 64, 1, 1);
compute_shader.Dispatch(handle_main, render_texture.width / 8, render_texture.height / 8, 1);
compute_buffer.GetData(data);
uint result = data[0];
percent = ((float)result/((float)render_texture.width*(float)render_texture.height))*100.0f;
Graphics.Blit(material.mainTexture,render_texture,material);
}
void OnGUI()
{
gui_style.fontSize = 40;
GUI.Label(new Rect(10, 10, 300, 20), "Percents "+percent.ToString(),gui_style);
}
}