LogicalError / realtime-CSG-for-unity

Realtime-CSG, CSG level editor for Unity
https://realtimecsg.com
MIT License
765 stars 81 forks source link

triangleIndex of RaycastHit can be wrong after MeshUtility.Optimize when postprocessing #304

Closed hunterhunted closed 2 years ago

hunterhunted commented 4 years ago

MeshUtility.Optimize re-orders the sharedmesh indices, but something internal doesn't update for later raycasts.

Here's where it gets optimized: https://github.com/LogicalError/realtime-CSG-for-unity/blob/2b6143fbbdfaca79a2a57ae1fcd2aadfbe3f5416/RealtimeCSG/Assets/Plugins/RealtimeCSG/Editor/Scripts/Control/Managers/MeshInstanceManager.Build.cs#L120

I noticed a similar issue was solved here: https://github.com/LogicalError/realtime-CSG-for-unity/blob/2b6143fbbdfaca79a2a57ae1fcd2aadfbe3f5416/RealtimeCSG/Assets/Plugins/RealtimeCSG/Editor/Scripts/Control/Managers/MeshInstanceManager.cs#L1401

Using the same workaround, my raycasts during playmode in the editor are getting the correct triangleIndex on optimized meshes from RCSG again:

                    if (child.SharedMesh && !UsesLightmapUVs(model))
                    {
                        MeshUtility.Optimize(child.SharedMesh);

#if UNITY_EDITOR
                        var currentMeshCollider = child.gameObject.GetComponent<MeshCollider>();
                        if (currentMeshCollider && currentMeshCollider.enabled)
                        {
                            currentMeshCollider.enabled = false;
                            currentMeshCollider.enabled = true;
                        }
#endif
                    }
LogicalError commented 4 years ago

Thanks, fixed in 1.562! I'll close this ticket when it's available from the asset store