void Start() { goList = new List <GPUInstancerPrefab>(); // Define the buffer to the Prefab Manager. if (prefabManager != null && prefabManager.isActiveAndEnabled) { GPUInstancerAPI.DefinePrototypeVariationBuffer <Vector4>(prefabManager, prefab.prefabPrototype, bufferName); } // Generate instances inside a radius. for (int i = 0; i < instances; i++) { GPUInstancerPrefab prefabInstance = Instantiate(prefab); prefabInstance.transform.localPosition = Random.insideUnitSphere * 20; prefabInstance.transform.SetParent(transform); goList.Add(prefabInstance); // Register the variation buffer for this instance. prefabInstance.AddVariation(bufferName, (Vector4)Random.ColorHSV()); } // Register the generated instances to the manager and initialize the manager. if (prefabManager != null && prefabManager.isActiveAndEnabled) { GPUInstancerAPI.RegisterPrefabInstanceList(prefabManager, goList); GPUInstancerAPI.InitializeGPUInstancer(prefabManager); } }
/// <summary> /// <para>Updates the variation value for this prefab instance. The variation buffer for the prototype must be defined /// with <see cref="DefinePrototypeVariationBuffer{T}"/> before using this.</para> /// </summary> /// <typeparam name="T">The type of variation buffer. Must be defined in the instance prototype's shader.</typeparam> /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param> /// <param name="prefabInstance">The prefab instance to update the variation at.</param> /// <param name="bufferName">The name of the variation buffer in the prototype's shader.</param> /// <param name="value">The value of the variation.</param> public static void UpdateVariation <T>(GPUInstancerPrefabManager manager, GPUInstancerPrefab prefabInstance, string bufferName, T value) { prefabInstance.AddVariation(bufferName, value); manager.UpdateVariationData(prefabInstance, bufferName, value); }
/// <summary> /// <para>Sets the variation value for this prefab instance. The variation buffer for the prototype must be defined /// with <see cref="DefinePrototypeVariationBuffer{T}"/> before using this.</para> /// </summary> /// <typeparam name="T">The type of variation buffer. Must be defined in the instance prototype's shader.</typeparam> /// <param name="prefabInstance">The prefab instance to add the variation to.</param> /// <param name="bufferName">The name of the variation buffer in the prototype's shader.</param> /// <param name="value">The value of the variation.</param> public static void AddVariation <T>(GPUInstancerPrefab prefabInstance, string bufferName, T value) { prefabInstance.AddVariation(bufferName, value); }