Skip to content

Fixed Collider StaticFlag Not Updating Properly #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 33 additions & 37 deletions Plugins/Editor/Scripts/Control/Managers/MeshInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ internal static void ClearOrCreateMesh(string baseName, bool editorOnly, ref boo
}

sharedMesh = new Mesh();
sharedMesh.name = string.Format("<{0} generated {1}>", baseName, sharedMesh.GetInstanceID());
sharedMesh.name = $"<{baseName} generated {sharedMesh.GetInstanceID()}>";
sharedMesh.MarkDynamic();
if (editorOnly)
sharedMesh.hideFlags = HideFlags.DontSaveInBuild;
Expand Down Expand Up @@ -926,33 +926,28 @@ private static bool NeedToGenerateLightmapUVsForInstance(GeneratedMeshInstance i
return !instance.HasUV2 && instance.RenderSurfaceType == RenderSurfaceType.Normal;
}

private static bool NeedCollider(CSGModel model, GeneratedMeshInstance instance)
private static bool AreBoundsEmpty(GeneratedMeshInstance instance)
{
return //((model.HaveCollider || model.IsTrigger) &&

(//instance.RenderSurfaceType == RenderSurfaceType.Normal ||
instance.RenderSurfaceType == RenderSurfaceType.Collider ||
instance.RenderSurfaceType == RenderSurfaceType.Trigger
) &&

// Make sure the bounds of the mesh are not empty ...
(Mathf.Abs(instance.SharedMesh.bounds.size.x) > MathConstants.EqualityEpsilon ||
Mathf.Abs(instance.SharedMesh.bounds.size.y) > MathConstants.EqualityEpsilon ||
Mathf.Abs(instance.SharedMesh.bounds.size.z) > MathConstants.EqualityEpsilon)
//)
;
return
Mathf.Abs(instance.SharedMesh.bounds.size.x) <= MathConstants.EqualityEpsilon ||
Mathf.Abs(instance.SharedMesh.bounds.size.y) <= MathConstants.EqualityEpsilon ||
Mathf.Abs(instance.SharedMesh.bounds.size.z) <= MathConstants.EqualityEpsilon;
}

private static bool NeedMeshRenderer(RenderSurfaceType renderSurfaceType)
private static bool NeedCollider(RenderSurfaceType surfaceType)
{
return surfaceType == RenderSurfaceType.Collider || surfaceType == RenderSurfaceType.Trigger;
}

private static bool NeedMeshRenderer(RenderSurfaceType renderSurfaceType)
{
return (renderSurfaceType == RenderSurfaceType.Normal ||
renderSurfaceType == RenderSurfaceType.ShadowOnly);
return renderSurfaceType == RenderSurfaceType.Normal || renderSurfaceType == RenderSurfaceType.ShadowOnly;
}

static StaticEditorFlags FilterStaticEditorFlags(StaticEditorFlags modelStaticFlags, RenderSurfaceType renderSurfaceType)
{
if (!NeedMeshRenderer(renderSurfaceType))
return (StaticEditorFlags)0;
return modelStaticFlags;

var meshStaticFlags = modelStaticFlags;
var walkable = renderSurfaceType != RenderSurfaceType.Hidden &&
Expand Down Expand Up @@ -1094,7 +1089,6 @@ public static void Refresh(GeneratedMeshInstance instance, CSGModel owner, bool

var meshFilterComponent = instance.CachedMeshFilter;
var meshRendererComponent = instance.CachedMeshRenderer;
var needMeshCollider = NeedCollider(owner, instance);



Expand Down Expand Up @@ -1183,12 +1177,12 @@ public static void Refresh(GeneratedMeshInstance instance, CSGModel owner, bool
if ((meshFilterComponent.hideFlags & HideFlags.HideInHierarchy) == 0)
{
meshFilterComponent.hideFlags |= HideFlags.HideInHierarchy;
}

if ((meshRendererComponent.hideFlags & HideFlags.HideInHierarchy) == 0)
}
if ((meshRendererComponent.hideFlags & HideFlags.HideInHierarchy) == 0)
{
meshRendererComponent.hideFlags |= HideFlags.HideInHierarchy;
}
}

if (instance.RenderSurfaceType != RenderSurfaceType.ShadowOnly)
{
Expand All @@ -1198,7 +1192,7 @@ public static void Refresh(GeneratedMeshInstance instance, CSGModel owner, bool
instance.ResetUVTime = Time.realtimeSinceStartup;
if (instance.HasUV2)
ClearUVs(instance);
}
}

if ((owner.AutoRebuildUVs || postProcessScene))
{
Expand Down Expand Up @@ -1319,8 +1313,8 @@ public static void Refresh(GeneratedMeshInstance instance, CSGModel owner, bool

if (SOModified)
meshRendererComponentSO.ApplyModifiedProperties();
}
}
}
}
//*/

#if UNITY_2019_2_OR_NEWER
Expand All @@ -1338,7 +1332,7 @@ public static void Refresh(GeneratedMeshInstance instance, CSGModel owner, bool
{
meshRendererComponent.sharedMaterial = requiredMaterial;
instance.Dirty = true;
}
}

} else
{
Expand All @@ -1363,6 +1357,8 @@ public static void Refresh(GeneratedMeshInstance instance, CSGModel owner, bool
// TODO: occludee/reflection probe static

var meshColliderComponent = instance.CachedMeshCollider;
var needMeshCollider = !AreBoundsEmpty(instance) && NeedCollider(instance.RenderSurfaceType);

if (needMeshCollider)
{
if (!meshColliderComponent)
Expand All @@ -1383,10 +1379,10 @@ public static void Refresh(GeneratedMeshInstance instance, CSGModel owner, bool
meshColliderComponent.hideFlags |= HideFlags.HideInHierarchy;
}

var currentPhyicsMaterial = instance.PhysicsMaterial ?? owner.DefaultPhysicsMaterial;
if (meshColliderComponent.sharedMaterial != currentPhyicsMaterial)
var currentPhysicsMaterial = instance.PhysicsMaterial ?? owner.DefaultPhysicsMaterial;
if (meshColliderComponent.sharedMaterial != currentPhysicsMaterial)
{
meshColliderComponent.sharedMaterial = currentPhyicsMaterial;
meshColliderComponent.sharedMaterial = currentPhysicsMaterial;
instance.Dirty = true;
}

Expand Down Expand Up @@ -1633,9 +1629,9 @@ static void AutoUpdateRigidBody(GeneratedMeshes container)
constraints = RigidbodyConstraints.None;
}

if (rigidBody.isKinematic != isKinematic) rigidBody.isKinematic = isKinematic;
if (rigidBody.useGravity != useGravity) rigidBody.useGravity = useGravity;
if (rigidBody.constraints != constraints) rigidBody.constraints = constraints;
if (rigidBody.isKinematic != isKinematic) rigidBody.isKinematic = isKinematic;
if (rigidBody.useGravity != useGravity) rigidBody.useGravity = useGravity;
if (rigidBody.constraints != constraints) rigidBody.constraints = constraints;
container.CachedRigidBody = rigidBody;
} else
{
Expand Down Expand Up @@ -2036,9 +2032,9 @@ private static void UpdateContainerFlags(GeneratedMeshes generatedMeshes)
var showVisibleSurfaces = (RealtimeCSG.CSGSettings.VisibleHelperSurfaces & HelperSurfaceFlags.ShowVisibleSurfaces) != 0;


if (ownerStaticFlags != previousStaticFlags ||
!ownerGameObject.CompareTag(generatedMeshes.gameObject.tag) ||
containerLayer != generatedMeshes.gameObject.layer)
if (ownerStaticFlags != previousStaticFlags
|| !ownerGameObject.CompareTag(generatedMeshes.gameObject.tag)
|| containerLayer != generatedMeshes.gameObject.layer)
{
var containerTag = ownerGameObject.tag;
foreach (var meshInstance in generatedMeshes.MeshInstances)
Expand Down