Skip to content

Instantly share code, notes, and snippets.

@aleannox
Created April 17, 2020 19:27
Show Gist options
  • Save aleannox/207c0089f98eed4d78706ab8fc240b9a to your computer and use it in GitHub Desktop.
Save aleannox/207c0089f98eed4d78706ab8fc240b9a to your computer and use it in GitHub Desktop.
[Unity] Fixes wrong Skinned Mesh Renderer bounds occurring with Cloth component
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];
}
}
}
@zilkin
Copy link

zilkin commented Oct 23, 2020

Thanks a lot, I had a problem with my curtains dissapearing from camera and now it is solved :D

@aleannox
Copy link
Author

Glad it helped! :)

@nukadelic
Copy link

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