BFX_DemoTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. public class BFX_DemoTest : MonoBehaviour
  5. {
  6. public bool InfiniteDecal;
  7. public Light DirLight;
  8. public bool isVR = true;
  9. public GameObject BloodAttach;
  10. public GameObject[] BloodFX;
  11. Transform GetNearestObject(Transform hit, Vector3 hitPos)
  12. {
  13. var closestPos = 100f;
  14. Transform closestBone = null;
  15. var childs = hit.GetComponentsInChildren<Transform>();
  16. foreach (var child in childs)
  17. {
  18. var dist = Vector3.Distance(child.position, hitPos);
  19. if (dist < closestPos)
  20. {
  21. closestPos = dist;
  22. closestBone = child;
  23. }
  24. }
  25. var distRoot = Vector3.Distance(hit.position, hitPos);
  26. if (distRoot < closestPos)
  27. {
  28. closestPos = distRoot;
  29. closestBone = hit;
  30. }
  31. return closestBone;
  32. }
  33. public Vector3 direction;
  34. int effectIdx;
  35. void Update()
  36. {
  37. //if (isVR)
  38. //{
  39. // if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger) || OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger))
  40. // {
  41. // RaycastHit hit;
  42. // if (Physics.Raycast(Dir.position, Dir.forward, out hit))
  43. // {
  44. // // var randRotation = new Vector3(0, Random.value * 360f, 0);
  45. // // var dir = CalculateAngle(Vector3.forward, hit.normal);
  46. // float angle = Mathf.Atan2(hit.normal.x, hit.normal.z) * Mathf.Rad2Deg + 180;
  47. // var effectIdx = Random.Range(0, BloodFX.Length);
  48. // var instance = Instantiate(BloodFX[effectIdx], hit.point, Quaternion.Euler(0, angle + 90, 0));
  49. // var settings = instance.GetComponent<BFX_BloodSettings>();
  50. // settings.DecalLiveTimeInfinite = InfiniteDecal;
  51. // settings.LightIntencityMultiplier = DirLight.intensity;
  52. // if (!InfiniteDecal) Destroy(instance, 20);
  53. // }
  54. // }
  55. //}
  56. // else
  57. {
  58. if (Input.GetMouseButtonDown(0))
  59. {
  60. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  61. RaycastHit hit;
  62. if (Physics.Raycast(ray, out hit))
  63. {
  64. // var randRotation = new Vector3(0, Random.value * 360f, 0);
  65. // var dir = CalculateAngle(Vector3.forward, hit.normal);
  66. float angle = Mathf.Atan2(hit.normal.x, hit.normal.z) * Mathf.Rad2Deg + 180;
  67. //var effectIdx = Random.Range(0, BloodFX.Length);
  68. if (effectIdx == BloodFX.Length) effectIdx = 0;
  69. var instance = Instantiate(BloodFX[effectIdx], hit.point, Quaternion.Euler(0, angle + 90, 0));
  70. effectIdx++;
  71. var settings = instance.GetComponent<BFX_BloodSettings>();
  72. //settings.FreezeDecalDisappearance = InfiniteDecal;
  73. settings.LightIntensityMultiplier = DirLight.intensity;
  74. var nearestBone = GetNearestObject(hit.transform.root, hit.point);
  75. if(nearestBone != null)
  76. {
  77. var attachBloodInstance = Instantiate(BloodAttach);
  78. var bloodT = attachBloodInstance.transform;
  79. bloodT.position = hit.point;
  80. bloodT.localRotation = Quaternion.identity;
  81. bloodT.localScale = Vector3.one * Random.Range(0.75f, 1.2f);
  82. bloodT.LookAt(hit.point + hit.normal, direction);
  83. bloodT.Rotate(90, 0, 0);
  84. bloodT.transform.parent = nearestBone;
  85. //Destroy(attachBloodInstance, 20);
  86. }
  87. // if (!InfiniteDecal) Destroy(instance, 20);
  88. }
  89. }
  90. }
  91. }
  92. public float CalculateAngle(Vector3 from, Vector3 to)
  93. {
  94. return Quaternion.FromToRotation(Vector3.up, to - from).eulerAngles.z;
  95. }
  96. }