Created
April 17, 2020 19:27
-
-
Save aleannox/207c0089f98eed4d78706ab8fc240b9a to your computer and use it in GitHub Desktop.
[Unity] Fixes wrong Skinned Mesh Renderer bounds occurring with Cloth component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
// Fixes wrong Skinned Mesh Renderer bounds occurring with Cloth component. | |
// Attach this to camera. | |
// | |
// Idea: Before camera determines culling, we override the automatically computed | |
// bounds with our own for all game objects with a Skinned Mesh Render in the present scene. | |
// In this example, we use the bounds of the undeformed mesh | |
// scaled by `boundsExtentFactor` in order to take into account possible rescaling | |
// and rotation, as well as the deformation. A cleaner solution would be to use the | |
// automatically computed bounds and transform them properly. | |
public class FixClothBounds : MonoBehaviour { | |
private List<Bounds> manyBounds; | |
private SkinnedMeshRenderer[] skinnedMeshRenderers; | |
public float boundsExtentFactor; | |
void Start () | |
{ | |
skinnedMeshRenderers = FindObjectsOfType<SkinnedMeshRenderer>(); | |
manyBounds = new List<Bounds>(); | |
for (var i = 0; i < skinnedMeshRenderers.Length; i++) { | |
Bounds bounds = skinnedMeshRenderers[i].sharedMesh.bounds; | |
bounds.Expand(bounds.extents * boundsExtentFactor); | |
manyBounds.Add(bounds); | |
} | |
} | |
private void OnPreCull() | |
{ | |
FixBounds(); | |
} | |
private void FixBounds() { | |
for (var i = 0; i < skinnedMeshRenderers.Length; i++) { | |
skinnedMeshRenderers[i].localBounds = manyBounds[i]; | |
} | |
} | |
} |
Glad it helped! :)
Why are you reading bounds
and writing localBounds
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot, I had a problem with my curtains dissapearing from camera and now it is solved :D