soundvision/UnityProject/Assets/Scripts/Visualizer/PointCloudBehaviour.cs
Chikashi Miyama 15ed32aea0 fix namespace
2019-10-31 10:43:47 +01:00

83 lines
No EOL
2.8 KiB
C#

using UnityEngine;
using UnityEngine.Rendering;
namespace cylvester
{
public class PointCloudBehaviour : MonoBehaviour
{
#pragma warning disable 649
[Header("Modifier Parameters")]
[SerializeField] private float zScale;
[Header("Connections")]
[SerializeField] private KinectManagerBehaviour kinectManagerBehaviour;
[SerializeField] private MeshFilter meshFilter;
#pragma warning restore 649
private Material material_;
private static readonly int Scale = Shader.PropertyToID("_Scale");
private void Start()
{
var texture = kinectManagerBehaviour.KinectSensor.InfraredCamera.InfraredTexture;
var numPixels = texture.height * texture.width;
meshFilter.mesh = new Mesh {
vertices = MakeVertices(texture),
uv = MakeTexCoord(texture),
indexFormat = IndexFormat.UInt32
};
meshFilter.mesh.SetIndices(MakeIndecies(numPixels), MeshTopology.Points, 0, false);
material_ = GetComponent<Renderer>().material;
material_.mainTexture = kinectManagerBehaviour.KinectSensor.InfraredCamera.InfraredTexture;
}
private void Update()
{
material_.SetFloat(Scale, zScale);
}
private Vector3[] MakeVertices(Texture2D texture)
{
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);
}
}
return vertices;
}
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;
}
}
}