soundvision/UnityProject/Assets/Scripts/Visualizer/PointCloudBehaviour.cs

83 lines
2.8 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.Rendering;
2019-10-31 09:43:47 +00:00
namespace cylvester
{
2019-07-07 15:56:52 +00:00
public class PointCloudBehaviour : MonoBehaviour
{
2019-07-07 15:56:52 +00:00
#pragma warning disable 649
[Header("Modifier Parameters")]
[SerializeField] private float zScale;
[Header("Connections")]
[SerializeField] private KinectManagerBehaviour kinectManagerBehaviour;
2019-07-07 15:56:52 +00:00
[SerializeField] private MeshFilter meshFilter;
#pragma warning restore 649
private Material material_;
private static readonly int Scale = Shader.PropertyToID("_Scale");
2019-07-07 15:56:52 +00:00
private void Start()
{
2019-10-31 09:43:47 +00:00
var texture = kinectManagerBehaviour.KinectSensor.InfraredCamera.InfraredTexture;
2019-07-07 15:56:52 +00:00
var numPixels = texture.height * texture.width;
meshFilter.mesh = new Mesh {
vertices = MakeVertices(texture),
uv = MakeTexCoord(texture),
indexFormat = IndexFormat.UInt32
2019-07-07 15:56:52 +00:00
};
meshFilter.mesh.SetIndices(MakeIndecies(numPixels), MeshTopology.Points, 0, false);
material_ = GetComponent<Renderer>().material;
2019-10-31 09:43:47 +00:00
material_.mainTexture = kinectManagerBehaviour.KinectSensor.InfraredCamera.InfraredTexture;
2019-07-07 15:56:52 +00:00
}
private void Update()
{
material_.SetFloat(Scale, zScale);
}
2019-07-07 15:56:52 +00:00
private Vector3[] MakeVertices(Texture2D texture)
{
2019-07-07 15:56:52 +00:00
var vertices = new Vector3[texture.width * texture.height];
for (var i = 0; i < texture.height; ++i)
{
var offset = texture.width * i;
var hPhase = (float) i / texture.height;
for (var j = 0; j < texture.width; ++j)
{
var wPhase = (float) j / texture.width;
vertices[offset + j] = new Vector3(wPhase * 10f - 5f, (hPhase * 10f - 5f) * -1f, 0f);
2019-07-07 15:56:52 +00:00
}
}
return vertices;
}
2019-07-07 15:56:52 +00:00
private Vector2[] MakeTexCoord(Texture2D texture)
{
var texCoords = new Vector2[texture.width * texture.height];
for (var i = 0; i < texture.height; ++i)
{
var offset = texture.width * i;
var hPhase = (float) i / texture.height;
for (var j = 0; j < texture.width; ++j)
{
var wPhase = (float) j / texture.width;
texCoords[offset + j].x = wPhase;
texCoords[offset + j].y = hPhase;
}
}
return texCoords;
}
private int[] MakeIndecies(int numPixels)
{
var array = new int[numPixels];
for (var i = 0; i < numPixels; ++i)
array[i] = i;
return array;
}
}
}