예제 #1
0
        /// <summary>
        /// Can be used to change the material of a prototype at runtime
        /// </summary>
        /// <param name="manager">GPUI Manager</param>
        /// <param name="prototype">GPUI Prototype</param>
        /// <param name="material">New material to set on the renderer</param>
        /// <param name="lodLevel">LOD level</param>
        /// <param name="rendererIndex">Renderer index on the LOD level</param>
        /// <param name="subMeshIndex">Submesh index of the renderer</param>
        public static void ChangeMaterial(GPUInstancerManager manager, GPUInstancerPrototype prototype, Material material, int lodLevel = 0, int rendererIndex = 0, int subMeshIndex = 0)
        {
            GPUInstancerRuntimeData runtimeData = manager.GetRuntimeData(prototype, true);

            if (runtimeData == null)
            {
                return;
            }
            GPUInstancerRenderer gpuiRenderer = runtimeData.instanceLODs[lodLevel].renderers[rendererIndex];

            // Generate proxy GO with a Mesh Renderer to get material property blocks
            GameObject   proxyGameObject = new GameObject("ProxyGO");
            MeshFilter   meshFilter      = proxyGameObject.AddComponent <MeshFilter>();
            MeshRenderer proxyRenderer   = proxyGameObject.AddComponent <MeshRenderer>();

            // Set mesh to proxy GO
            meshFilter.mesh = gpuiRenderer.mesh;
            // Set new material to runtime data
            gpuiRenderer.materials[subMeshIndex] = GPUInstancerConstants.gpuiSettings.shaderBindings.GetInstancedMaterial(material);
            // Set new material to proxy GO
            proxyRenderer.materials[subMeshIndex] = material;
            // Get material property blocks
            proxyRenderer.GetPropertyBlock(gpuiRenderer.mpb);
            if (gpuiRenderer.shadowMPB != null)
            {
                proxyRenderer.GetPropertyBlock(gpuiRenderer.shadowMPB);
            }

            // Destroy proxy GO
            GameObject.Destroy(proxyGameObject);

            // Setup new materials for instancing
            GPUInstancerUtility.SetAppendBuffers(runtimeData);
        }
예제 #2
0
        /// <summary>
        /// Can be used to change the mesh of a prototype at runtime
        /// </summary>
        /// <param name="manager">GPUI Manager</param>
        /// <param name="prototype">GPUI Prototype</param>
        /// <param name="mesh">New mesh to set on the renderer</param>
        /// <param name="lodLevel">LOD level</param>
        /// <param name="rendererIndex">Renderer index on the LOD level</param>
        /// <param name="subMeshIndex">Submesh index of the renderer</param>
        public static void ChangeMesh(GPUInstancerManager manager, GPUInstancerPrototype prototype, Mesh mesh, int lodLevel = 0, int rendererIndex = 0, int subMeshIndex = 0)
        {
            GPUInstancerRuntimeData runtimeData = manager.GetRuntimeData(prototype, true);

            if (runtimeData == null)
            {
                return;
            }
            GPUInstancerRenderer gpuiRenderer = runtimeData.instanceLODs[lodLevel].renderers[rendererIndex];

            if (gpuiRenderer.mesh.subMeshCount != mesh.subMeshCount)
            {
                Debug.LogError("ChangeMesh method can not be used with a mesh that has different amount of submeshes than the original mesh.");
                return;
            }

            if (gpuiRenderer.mesh.vertexCount != mesh.vertexCount)
            {
                int argsLastIndex = gpuiRenderer.argsBufferOffset;
                // Setup the indirect renderer buffer:
                for (int j = 0; j < gpuiRenderer.mesh.subMeshCount; j++)
                {
                    runtimeData.args[argsLastIndex++] = gpuiRenderer.mesh.GetIndexCount(j); // index count per instance
                    runtimeData.args[argsLastIndex++] = 0;                                  // (uint)runtimeData.bufferSize;
                    runtimeData.args[argsLastIndex++] = gpuiRenderer.mesh.GetIndexStart(j); // start index location
                    runtimeData.args[argsLastIndex++] = 0;                                  // base vertex location
                    runtimeData.args[argsLastIndex++] = 0;                                  // start instance location
                }
                runtimeData.argsBuffer.SetData(runtimeData.args);
            }

            gpuiRenderer.mesh = mesh;
        }
예제 #3
0
        protected override void OnEnable()
        {
            base.OnEnable();

            prototypeContents = null;
            _pickerOverride   = null;

            _manager = (target as GPUInstancerManager);
            FillPrototypeList();

            prop_useFloatingOriginHandler = serializedObject.FindProperty("useFloatingOriginHandler");
            prop_floatingOriginTransform  = serializedObject.FindProperty("floatingOriginTransform");

            prop_layerMask          = serializedObject.FindProperty("layerMask");
            prop_disableLightProbes = serializedObject.FindProperty("lightProbeDisabled");

            showSceneSettingsBox     = _manager.showSceneSettingsBox;
            showPrototypeBox         = _manager.showPrototypeBox;
            showAdvancedBox          = _manager.showAdvancedBox;
            showHelpText             = _manager.showHelpText;
            showDebugBox             = _manager.showDebugBox;
            showGlobalValuesBox      = _manager.showGlobalValuesBox;
            showRegisteredPrefabsBox = _manager.showRegisteredPrefabsBox;
            showPrototypesBox        = _manager.showPrototypesBox;
        }
 private void Reset()
 {
     if (GetComponent <GPUInstancerManager>() != null)
     {
         gPUIManager = GetComponent <GPUInstancerManager>();
     }
 }
예제 #5
0
 /// <summary>
 /// [EDITOR-ONLY] Stops rendering GPUI instances for the given manager in editor mode. Can be used after StartEditorSimulation method to disable rendering.
 /// </summary>
 /// <param name="manager">GPUI manager to stop simulation for</param>
 public static void StopEditorSimulation(GPUInstancerManager manager)
 {
     if (!Application.isPlaying && manager.gpuiSimulator != null)
     {
         manager.gpuiSimulator.StopSimulation();
     }
 }
예제 #6
0
        protected override void OnEnable()
        {
            base.OnEnable();

            prototypeContents = null;

            _manager = (target as GPUInstancerManager);
            FillPrototypeList();
        }
예제 #7
0
        /// <summary>
        /// Returns the array that stores the transform data of the instances
        /// </summary>
        /// <param name="manager">GPUI Manager</param>
        /// <param name="prototype">GPUI Prototype</param>
        /// <returns>Instance data array</returns>
        public static Matrix4x4[] GetInstanceDataArray(GPUInstancerManager manager, GPUInstancerPrototype prototype)
        {
            GPUInstancerRuntimeData runtimeData = manager.GetRuntimeData(prototype, true);

            if (runtimeData == null)
            {
                return(null);
            }
            return(runtimeData.instanceDataArray);
        }
예제 #8
0
        public void SetDefaultGPUInstancerBillboardAtlasBindings()
        {
            if (billboardAtlasBindings == null)
            {
#if UNITY_EDITOR
                if (!Application.isPlaying)
                {
                    Undo.RecordObject(this, "GPUInstancerBillboardAtlasBindings instance generated");
                }
#endif
                billboardAtlasBindings = GPUInstancerManager.GetDefaultGPUInstancerBillboardAtlasBindings();
            }
        }
예제 #9
0
        /// <summary>
        /// SetInstanceCount can be used to discard instances that are indexed higher than the given index count
        /// </summary>
        /// <param name="manager">GPUI Manager</param>
        /// <param name="prototype">GPUI Prototype</param>
        /// <param name="instanceCount">New instance count to set on the runtime data</param>
        public static void SetInstanceCount(GPUInstancerManager manager, GPUInstancerPrototype prototype, int instanceCount)
        {
            GPUInstancerRuntimeData runtimeData = manager.GetRuntimeData(prototype, true);

            if (runtimeData == null)
            {
                return;
            }
            if (instanceCount > runtimeData.bufferSize)
            {
                Debug.LogError("Instance count can not be higher than the buffer size.");
                return;
            }
            runtimeData.instanceCount = instanceCount;
        }
예제 #10
0
        public GPUInstancerEditorSimulator(GPUInstancerManager gpuiManager)
        {
            this.gpuiManager = gpuiManager;

            if (sceneViewCameraData == null)
            {
                sceneViewCameraData = new GPUInstancerCameraData(null);
            }
            sceneViewCameraData.renderOnlySelectedCamera = true;

            if (gpuiManager != null)
            {
                EditorApplication.update -= FindSceneViewCamera;
                EditorApplication.update += FindSceneViewCamera;
#if UNITY_2017_2_OR_NEWER
                EditorApplication.pauseStateChanged -= HandlePauseStateChanged;
                EditorApplication.pauseStateChanged += HandlePauseStateChanged;
#else
                EditorApplication.playmodeStateChanged = HandlePlayModeStateChanged;
#endif
            }
        }
예제 #11
0
 /// <summary>
 /// Removes the instances in GPU memory that are inside collider.
 /// </summary>
 /// <param name="manager">GPUI Manager to remove the instances from</param>
 /// <param name="collider">Collider that define the area that the instances will be removed</param>
 /// <param name="offset">Adds an offset around the area that the instances will be removed</param>
 /// <param name="prototypeFilter">If prototypeFilter parameter is given, only the instances of the given prototypes will be removed.</param>
 public static void RemoveInstancesInsideCollider(GPUInstancerManager manager, Collider collider, float offset = 0, List <GPUInstancerPrototype> prototypeFilter = null)
 {
     manager.RemoveInstancesInsideCollider(collider, offset, prototypeFilter);
 }
예제 #12
0
 /// <summary>
 /// [For Advanced Users Only] Returns the float4x4 ComputeBuffer that store the localToWorldMatrix for each instance in GPU memory. This buffer can be used to make
 /// modifications in GPU memory before the rendering process.
 /// </summary>
 /// <param name="manager">GPUI Manager to get the buffer from</param>
 /// <param name="prototype">Prototype that the buffer belongs to</param>
 /// <returns></returns>
 public static ComputeBuffer GetTransformDataBuffer(GPUInstancerManager manager, GPUInstancerPrototype prototype)
 {
     return(manager.GetTransformDataBuffer(prototype));
 }
예제 #13
0
 /// <summary>
 /// Changes the LODBias with the given value. Values lower than the LODBias in your Quality Settings will result in higher quality
 /// but less performance (e.g. more instances will use LOD0), values higher than the LODBias in your Quality Settings will
 /// reduce the quality but increase performance (e.g. less instances will use LOD0)
 /// </summary>
 /// <param name="manager">GPUI Manager to adjust the LOD sizes</param>
 /// <param name="newLODBias">New LODBias value</param>
 /// <param name="prototypeFilter">If prototypeFilter parameter is given, only the LODBiases of the given prototypes will be changed.</param>
 public static void SetLODBias(GPUInstancerManager manager, float newLODBias, List <GPUInstancerPrototype> prototypeFilter = null)
 {
     manager.SetLODBias(newLODBias, prototypeFilter);
 }
예제 #14
0
 /// <summary>
 /// Updates all transform values in GPU memory with the given offset position.
 /// </summary>
 /// <param name="manager">GPUI Manager to apply the offset</param>
 /// <param name="offsetPosition">Offset Position</param>
 public static void SetGlobalPositionOffset(GPUInstancerManager manager, Vector3 offsetPosition)
 {
     GPUInstancerUtility.SetGlobalPositionOffset(manager, offsetPosition);
 }
예제 #15
0
 /// <summary>
 ///     <para>Main GPU Instancer initialization Method. Generates the necessary GPUInstancer runtime data from predifined
 ///     GPU Instancer prototypes that are registered in the manager, and generates all necessary GPU buffers for instancing.</para>
 ///     <para>Use this as the final step after you setup a GPU Instancer manager and all its prototypes.</para>
 ///     <para>Note that you can also use this to re-initialize the GPU Instancer prototypes that are registered in the manager at runtime.</para>
 /// </summary>
 /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param>
 /// <param name="forceNew">If set to false the manager will not run initialization if it was already initialized before</param>
 public static void InitializeGPUInstancer(GPUInstancerManager manager, bool forceNew = true)
 {
     manager.InitializeRuntimeDataAndBuffers(forceNew);
 }
예제 #16
0
 /// <summary>
 ///     <para>Sets the active camera for a specific manager. This camera is used by GPU Instancer for various calculations (including culling operations). </para>
 ///     <para>Use this right after you add or change your camera at runtime. </para>
 /// </summary>
 /// <param name="manager">The manager that defines the prototypes you want to GPU instance.</param>
 /// <param name="camera">The camera that GPU Instancer will use.</param>
 public static void SetCamera(GPUInstancerManager manager, Camera camera)
 {
     manager.SetCamera(camera);
 }
예제 #17
0
 /// <summary>
 /// Returns the prototype list of the given GPUI manager
 /// </summary>
 /// <param name="manager">GPUI Manager</param>
 /// <returns>Prototype List</returns>
 public static List <GPUInstancerPrototype> GetPrototypeList(GPUInstancerManager manager)
 {
     return(manager.prototypeList.ToList());
 }
예제 #18
0
 /// <summary>
 /// Removes the instances in GPU memory that are inside bounds.
 /// </summary>
 /// <param name="manager">GPUI Manager to remove the instances from</param>
 /// <param name="bounds">Bounds that define the area that the instances will be removed</param>
 /// <param name="offset">Adds an offset around the area that the instances will be removed</param>
 /// <param name="prototypeFilter">If prototypeFilter parameter is given, only the instances of the given prototypes will be removed.</param>
 public static void RemoveInstancesInsideBounds(GPUInstancerManager manager, Bounds bounds, float offset = 0, List <GPUInstancerPrototype> prototypeFilter = null)
 {
     manager.RemoveInstancesInsideBounds(bounds, offset, prototypeFilter);
 }