private IEnumerator AddExtraInstancesAtRuntime() { // Extra instance count that can be added at runtime is limited by the "Extra buffer size" property defined for this prefab prototype in the manager. for (int i = 0; i < prefab.prefabPrototype.extraBufferSize; i++) { GPUInstancerPrefab prefabInstance = Instantiate(prefab); prefabInstance.transform.localPosition = Random.insideUnitSphere * 5; // Move the extra spheres to the outer sphere prefabInstance.transform.localPosition = prefabInstance.transform.localPosition.normalized * (prefabInstance.transform.localPosition.magnitude + 10f); prefabInstance.transform.SetParent(parentTransform); if (!prefabInstance.prefabPrototype.addRuntimeHandlerScript) { GPUInstancerAPI.AddPrefabInstance(prefabManager, prefabInstance); } extraInstancesList.Add(prefabInstance); if (!addRemoveInstantlyToggle.isOn) { yield return(new WaitForSeconds(0.001f)); } } EnableButton("RemoveExtraInstancesButton"); if (instancesList.Count == 0) { EnableButton("AddInstancesButton"); } else { EnableButton("RemoveInstancesButton"); } }
private IEnumerator AddInstancesAtRuntime() { for (int i = 0; i < instanceCount; i++) { GPUInstancerPrefab prefabInstance = Instantiate(prefab); prefabInstance.transform.localPosition = Random.insideUnitSphere * 10; prefabInstance.transform.SetParent(parentTransform); if (!prefabInstance.prefabPrototype.addRuntimeHandlerScript) { GPUInstancerAPI.AddPrefabInstance(prefabManager, prefabInstance); } instancesList.Add(prefabInstance); if (!addRemoveInstantlyToggle.isOn) { yield return(new WaitForSeconds(0.001f)); } } EnableButton("RemoveInstancesButton"); if (extraInstancesList.Count == 0) { EnableButton("AddExtraInstancesButton"); } else { EnableButton("RemoveExtraInstancesButton"); } }
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); } }
private void OnTriggerEnter(Collider collider) { if (prefabManager != null && prefabManager.isActiveAndEnabled && collider.gameObject) { GPUInstancerPrefab prefabInstance = collider.gameObject.GetComponent <GPUInstancerPrefab>(); if (prefabInstance != null && prefabInstance.prefabPrototype.enableRuntimeModifications && prefabInstance.state != PrefabInstancingState.Disabled) { prefabManager.DisableIntancingForInstance(prefabInstance); _enteredInstances.Add(prefabInstance); } } }
//private void OnTriggerExit(Collider collider) //{ // if (GPUInstancerPrefabManager.Instance != null && // collider.gameObject && // collider.gameObject.GetComponent<GPUInstancerPrefab>() && // collider.gameObject.GetComponent<GPUInstancerPrefab>().prefabPrototype.enableRuntimeModifications && // !gameObject.GetComponent<Collider>().bounds.Intersects(collider.bounds)) // { // GPUInstancerPrefabManager.Instance.AddInstance(collider.gameObject.GetComponent<GPUInstancerPrefab>()); // } //} public bool IsInsideCollider(GPUInstancerPrefab prefabInstance) { Collider instanceCollider = prefabInstance.GetComponent <Collider>(); if (instanceCollider == null) { return(false); } else { return(_collider.bounds.Intersects(instanceCollider.bounds)); } }
private GPUInstancerPrefab InstantiateInCircle(Vector3 center, int column) { SetRandomPosInCircle(center, column - Mathf.FloorToInt(columnSize / 2f), Random.Range(300f, 500f)); allocatedRot = Quaternion.FromToRotation(Vector3.forward, center - allocatedPos); allocatedGO = Instantiate(asteroidObjects[Random.Range(0, asteroidObjects.Count)], allocatedPos, allocatedRot); allocatedGO.transform.parent = goParent.transform; allocatedLocalEulerRot.x = Random.Range(-180f, 180f); allocatedLocalEulerRot.y = Random.Range(-180f, 180f); allocatedLocalEulerRot.z = Random.Range(-180f, 180f); allocatedGO.transform.localRotation = Quaternion.Euler(allocatedLocalEulerRot); allocatedLocalScaleFactor = Random.Range(0.02f, 0.3f); allocatedLocalScale.x = allocatedLocalScaleFactor; allocatedLocalScale.y = allocatedLocalScaleFactor; allocatedLocalScale.z = allocatedLocalScaleFactor; allocatedGO.transform.localScale = allocatedLocalScale; instantiatedCount++; return(allocatedGO); }
private void Awake() { gpuiPrefab = GetComponent <GPUInstancerPrefab>(); if (_managerDictionary == null) { _managerDictionary = new Dictionary <GPUInstancerPrefabPrototype, GPUInstancerPrefabManager>(); GPUInstancerPrefabManager[] prefabManagers = FindObjectsOfType <GPUInstancerPrefabManager>(); if (prefabManagers != null && prefabManagers.Length > 0) { foreach (GPUInstancerPrefabManager pm in prefabManagers) { foreach (GPUInstancerPrefabPrototype prototype in pm.prototypeList) { if (!_managerDictionary.ContainsKey(prototype)) { _managerDictionary.Add(prototype, pm); } } } } } }
/// <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); }
/// <summary> /// <para>Updates and synchronizes the GPU Instancer transform data (position, rotation and scale) for the given prefab instance.</para> /// <para>Use this if you want to update, rotate, and/or scale prefab instances after initialization.</para> /// <para>The updated values are taken directly from the transformation operations made beforehand on the instance's Unity transform component. /// (These operations will not reflect on the GPU Instanced prefab automatically unless you use this method).</para> /// </summary> /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param> /// <param name="prefabInstance">The prefab instance to update the transform values of. The instance's Unity transform component must be updated beforehand.</param> public static void UpdateTransformDataForInstance(GPUInstancerPrefabManager manager, GPUInstancerPrefab prefabInstance) { manager.UpdateTransformDataForInstance(prefabInstance); }
/// <summary> /// <para>Enables GPU instancing and disables Unity renderers for the given prefab instance without re-adding it to the list of registerd prefabs.</para> /// <para>Use this if you want to unpause GPU Instancing for a prefab.</para> /// <para>Note that the prefab must be enabled for runtime modifications in the manager in order for this to work (for performance reasons).</para> /// <para>Also note that you can also add <seealso cref="GPUInstancerModificationCollider"/> to a game object to use its collider to automatically /// enable/disable instancing when a prefab instance enters/exits its collider.</para> /// </summary> /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param> /// <param name="prefabInstance">The prefab instance to enable the GPU Instancing of.</param> /// <param name="disableMeshRenderers">If set to false Mesh Renderer components will not be disabled after enabling instancing. Should be used only for instances /// that have already disabled mesh renderers to speed up the process.</param> public static void EnableInstancingForInstance(GPUInstancerPrefabManager manager, GPUInstancerPrefab prefabInstance, bool setRenderersDisabled = true) { manager.EnableInstancingForInstance(prefabInstance, setRenderersDisabled); }
/// <summary> /// <para>Removes a prefab instance from an already initialized list of registered instances. </para> /// <para>Use this if you want to remove a prefab instance after you have initialized a list of prefabs with <see cref="InitializeGPUInstancer"/> /// (usually before destroying the GameObject).</para> /// <para>The prefab of this instance must be previously defined in the given manager (either at runtime or editor time).</para> /// <para>Note that the prefab must be enabled for adding and removal in the manager in order for this to work (for performance reasons).</para> /// </summary> /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param> /// <param name="prefabInstance">The prefab instance to remove.</param> /// <param name="setRenderersEnabled">If set to false Mesh Renderer components will not be enabled after removing prefab instance which will make the /// instance invisible.</param> public static void RemovePrefabInstance(GPUInstancerPrefabManager manager, GPUInstancerPrefab prefabInstance, bool setRenderersEnabled = true) { manager.RemovePrefabInstance(prefabInstance, setRenderersEnabled); }
/// <summary> /// <para>Adds a new prefab instance for GPU instancing to an already initialized list of registered instances. </para> /// <para>Use this if you want to add another instance of a prefab after you have initialized a list of prefabs with <see cref="InitializeGPUInstancer"/>.</para> /// <para>The prefab of this instance must be previously defined in the given manager (either at runtime or editor time).</para> /// <para>Note that the prefab must be enabled for adding and removal in the manager in order for this to work (for performance reasons).</para> /// <para>Also note that the number of total instances is limited by the count of already initialized instances plus the extra amount you define in the manager.</para> /// </summary> /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param> /// <param name="prefabInstance">The prefab instance to add.</param> public static void AddPrefabInstance(GPUInstancerPrefabManager manager, GPUInstancerPrefab prefabInstance) { manager.AddPrefabInstance(prefabInstance); }
public void AddEnteredInstance(GPUInstancerPrefab prefabInstance) { _enteredInstances.Add(prefabInstance); }
/// <summary> /// <para>Enables GPU instancing and disables Unity renderers for the given prefab instance without re-adding it to the list of registerd prefabs.</para> /// <para>Use this if you want to unpause GPU Instancing for a prefab.</para> /// <para>Note that the prefab must be enabled for runtime modifications in the manager in order for this to work (for performance reasons).</para> /// <para>Also note that you can also add <seealso cref="GPUInstancerModificationCollider"/> to a game object to use its collider to automatically /// enable/disable instancing when a prefab instance enters/exits its collider.</para> /// </summary> /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param> /// <param name="prefabInstance">The prefab instance to enable the GPU Instancing of.</param> public static void EnableInstancingForInstance(GPUInstancerPrefabManager manager, GPUInstancerPrefab prefabInstance) { manager.EnableInstancingForInstance(prefabInstance); }
/// <summary> /// <para>Removes a prefab instance from an already initialized list of registered instances. </para> /// <para>Use this if you want to remove a prefab instance after you have initialized a list of prefabs with <see cref="InitializeGPUInstancer"/> /// (usually before destroying the GameObject).</para> /// <para>The prefab of this instance must be previously defined in the given manager (either at runtime or editor time).</para> /// <para>Note that the prefab must be enabled for adding and removal in the manager in order for this to work (for performance reasons).</para> /// </summary> /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param> /// <param name="prefabInstance">The prefab instance to remove.</param> public static void RemovePrefabInstance(GPUInstancerPrefabManager manager, GPUInstancerPrefab prefabInstance) { manager.RemovePrefabInstance(prefabInstance); }