MegaPointCacheInstance.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. // This is an instance for a point cache modifier, will simply use data from a base mesh with
  3. // a point cache on to build new mesh data
  4. public class MegaPointCacheInstance : MonoBehaviour
  5. {
  6. [HideInInspector]
  7. public MegaPointCache mod;
  8. [HideInInspector]
  9. public MegaModifyObject modobj;
  10. public GameObject obj;
  11. [HideInInspector]
  12. public Mesh mesh;
  13. public float time = 0.0f; // Animation time
  14. public float speed = 1.0f; // Speed of animation playback
  15. public int updateRate = 0; // update rate of mesh, 0 is every frame 1 every other etc
  16. public int frame = 0; // current frame, can be used to stagger updates
  17. public bool recalcNorms = true; // recalc normals
  18. public bool recalcBounds = true; // recalc bounds
  19. // Call this to set the source game object which has the working point cache modifier attached
  20. public void SetSource(GameObject srcobj)
  21. {
  22. if ( srcobj )
  23. {
  24. if ( mesh == null )
  25. mesh = MegaUtils.GetMesh(gameObject);
  26. Mesh srcmesh = MegaUtils.GetMesh(srcobj);
  27. if ( srcmesh.vertexCount == mesh.vertexCount )
  28. {
  29. obj = srcobj;
  30. mod = (MegaPointCache)srcobj.GetComponent<MegaPointCache>();
  31. modobj = (MegaModifyObject)srcobj.GetComponent<MegaModifyObject>();
  32. mesh = MegaUtils.GetMesh(gameObject);
  33. }
  34. }
  35. }
  36. void Update()
  37. {
  38. if ( mod && modobj && mesh )
  39. {
  40. if ( Application.isPlaying )
  41. time += Time.deltaTime * speed;
  42. frame--;
  43. if ( frame < 0 )
  44. {
  45. frame = updateRate;
  46. mod.ModifyInstance(modobj, time);
  47. mesh.vertices = mod.sverts;
  48. if ( recalcNorms )
  49. mesh.RecalculateNormals();
  50. if ( recalcBounds )
  51. mesh.RecalculateBounds();
  52. }
  53. }
  54. }
  55. }