Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. /**
  5. * 頂点加工のテスト。
  6. */
  7. public class VerticesTest : MonoBehaviour {
  8. /**
  9. * 揺らぐ距離間。
  10. */
  11. public float swingDistance = 0.1f;
  12. /**
  13. * MeshFilter.
  14. */
  15. private MeshFilter meshFilter;
  16. /**
  17. * メッシュデータの頂点。
  18. */
  19. private Vector3[] vertices;
  20. /**
  21. * 重複のないオリジナル頂点データ。
  22. */
  23. private List<Vector3> originalVertices = new List<Vector3>();
  24. /**
  25. * 現在の重複のない頂点データ。
  26. */
  27. private List<Vector3> currentVertices = new List<Vector3>();
  28. /**
  29. * 動かす際のターゲットとなる重複のない頂点データ。
  30. */
  31. private List<Vector3> targetlVertices = new List<Vector3>();
  32. /**
  33. * 頂点ごとの時間の差。
  34. */
  35. private List<float> timeGap = new List<float>();
  36. /**
  37. * 頂点との対応表。
  38. */
  39. private Dictionary<int, int> correspondenceTable = new Dictionary<int, int>();
  40. /**
  41. * Awake.
  42. */
  43. public void Awake() {
  44. this.meshFilter = this.GetComponent<MeshFilter>();
  45. this.vertices = this.meshFilter.mesh.vertices;
  46. for (int i = 0; i < vertices.Length; ++i) {
  47. Vector3 vertex = vertices[i];
  48. if (!this.originalVertices.Contains(vertex)) {
  49. this.correspondenceTable[i] = this.originalVertices.Count;
  50. this.originalVertices.Add(vertex);
  51. this.currentVertices.Add(vertex);
  52.  
  53. // ランダムな位置を作る
  54. this.targetlVertices.Add(new Vector3(
  55. vertex.x + Random.Range(-this.swingDistance, this.swingDistance),
  56. vertex.y + Random.Range(-this.swingDistance, this.swingDistance),
  57. vertex.z + Random.Range(-this.swingDistance, this.swingDistance)
  58. ));
  59. }
  60. else {
  61. this.correspondenceTable[i] = this.originalVertices.FindIndex(vec => vec == vertex);
  62. }
  63. }
  64. for (int i = 0; i < this.originalVertices.Count; ++i) {
  65. this.timeGap.Add(Random.Range(0.0f, 1.0f));
  66. }
  67. }
  68. /**
  69. * Update.
  70. */
  71. public void Update() {
  72. // 現在位置の更新
  73. for (int i = 0; i < this.currentVertices.Count; ++i) {
  74. Vector3 originalPos = this.originalVertices[i];
  75. Vector3 targetPos = this.targetlVertices[i];
  76. Vector3 currentPos = this.currentVertices[i];
  77. this.timeGap[i] += Time.deltaTime * 1.6f;
  78. currentPos = Vector3.Slerp(originalPos, targetPos, Mathf.PingPong(this.timeGap[i], 1.0f));
  79. this.currentVertices[i] = currentPos;
  80. }
  81. // verticesに渡す頂点を作成
  82. for (int i = 0; i < this.vertices.Length; ++i) {
  83. int vid = this.correspondenceTable[i];
  84. this.vertices[i] = this.currentVertices[vid];
  85. }
  86. // 回転演出
  87. //this.transform.Rotate(Vector3.up * Time.deltaTime * 12.0f, Space.World);
  88. // 頂点を渡す
  89. this.meshFilter.mesh.vertices = this.vertices;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement