// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
namespace Microsoft.MixedReality.GraphicsTools.Samples.MeshInstancing
{
///
/// Simple instance placement and colorization example.
///
public class InstancingRandom : MonoBehaviour
{
[SerializeField]
private MeshInstancer instancer = null;
[SerializeField, Min(1)]
private int instanceCount = 20000;
[SerializeField]
private float instanceScale = 0.1f;
///
/// Re-spawn instances when a property changes.
///
private void OnValidate()
{
if (instancer != null && instancer.InstanceCount != 0)
{
CreateInstances();
}
}
///
/// Create instances on enable.
///
private void OnEnable()
{
CreateInstances();
}
///
/// Spin the MeshIntancer around at 10 degrees/second.
///
private void Update()
{
transform.Rotate(Vector3.up, 10.0f * Time.deltaTime);
}
///
/// Create a bunch of random instances within a unity sphere.
///
private void CreateInstances()
{
// Clear any existing instances.
instancer = (instancer == null) ? GetComponent() : instancer;
instancer.Clear();
int colorID = Shader.PropertyToID("_Color");
for (int i = 0; i < instanceCount; ++i)
{
var instance = instancer.Instantiate(Random.onUnitSphere, Random.rotation, Random.insideUnitSphere * instanceScale);
instance.SetVector(colorID, Random.ColorHSV());
}
}
}
}