Advertisement
Guest User

Laser

a guest
May 22nd, 2020
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Experimental.Rendering.Universal;
  5.  
  6. public class TestLaser : MonoBehaviour
  7. {
  8.  
  9. public int maxRecursions = 10;
  10. public float maxStepDistance = 200;
  11.  
  12. float airIndex = 1.0f;
  13. public float glassIndex = 1.5f;
  14.  
  15. public GameObject laserToSpawn;
  16.  
  17.  
  18. // Update is called once per frame
  19. private void OnDrawGizmos() //Update()
  20. {
  21. if (!Application.isPlaying)
  22. {
  23. return;
  24. }
  25. foreach (GameObject laser in GameObject.FindGameObjectsWithTag("Laser")) //every update redraw laser
  26. {
  27. laser.SetActive(false);
  28. }
  29.  
  30.  
  31. DrawPredictedReflection(this.transform.position, this.transform.up, maxRecursions);
  32. }
  33.  
  34. void DrawPredictedReflection(Vector2 position, Vector2 direction, int recursionsRemaing)// int reflectionsRemaining, int splitsRemaining)
  35. {
  36.  
  37. var gizmoHue = (recursionsRemaing / (this.maxRecursions + 1f));
  38.  
  39. RaycastHit2D hit2D = Physics2D.Raycast(position, direction, maxStepDistance);
  40.  
  41. if (hit2D) //did we hit somthing?
  42. {
  43.  
  44. DrawLaser(position, hit2D.point); //draw a line to it
  45.  
  46. if (hit2D.transform.gameObject.tag == "Lens")
  47. {
  48. //Debug.Log("Lens Hit...");
  49. Refract(hit2D.normal, direction, hit2D.point, hit2D.transform.gameObject, recursionsRemaing, airIndex, glassIndex);
  50. }
  51. }
  52. else //nothing hit
  53. {
  54. DrawLaser(position, position + direction * maxStepDistance);
  55. }
  56. }
  57.  
  58. void DrawLaser(Vector2 start, Vector2 end)
  59. {
  60.  
  61. GameObject laser = ObjectPooler.SharedInstance.GetPooledObject("Laser");
  62. if (laser != null)
  63. {
  64.  
  65. laser.transform.position = Vector2.zero;
  66. laser.transform.rotation = Quaternion.identity;
  67. LineRenderer lr = laser.GetComponent<LineRenderer>();
  68. lr.SetPosition(0, start);
  69. lr.SetPosition(1, end);
  70. lr.startColor = Color.white;
  71. lr.endColor = Color.white;
  72. laser.SetActive(true);
  73. }
  74. }
  75.  
  76. void Refract(Vector2 normal, Vector2 direction, Vector2 point, GameObject lastHit, int recursionsRemainging, float n1, float n2)
  77. {
  78. float angleOfIncidence = Vector2.Angle(normal, -direction);
  79. //Debug.Log("Angle of incidence = " + angleOfIncidence);
  80. float angleOfRefraction = Mathf.Asin((n1 * Mathf.Sin(angleOfIncidence * Mathf.Deg2Rad)) / n2) * Mathf.Rad2Deg; //snells law
  81. //Debug.Log("Angle of refraction = " + angleOfRefraction);
  82.  
  83.  
  84. Gizmos.color = Color.blue;
  85. Gizmos.DrawLine(point + normal * 0.1f, point - normal * 0.1f);
  86.  
  87.  
  88. //
  89.  
  90. Vector2 testPoint = RotatePointByDeg(point, normal, angleOfIncidence); //laser comming before refraction
  91. Gizmos.color = Color.red;
  92. Gizmos.DrawLine(point, testPoint);
  93.  
  94. //
  95.  
  96. //
  97.  
  98. testPoint = RotatePointByDeg(point, -normal, angleOfRefraction); //laser going after refraction
  99. Gizmos.color = Color.cyan;
  100. Gizmos.DrawLine(point, testPoint);
  101.  
  102. //
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109. Vector3 RotatePointByDeg(Vector2 origin, Vector2 normal, float angle) //rotate a point about its pivot which should be where the laser hit
  110. {
  111. Vector3 pointToRotate = origin + normal * .1f;
  112. Vector3 pivot = origin;
  113. Vector3 angles = new Vector3(0, 0, angle);
  114. Vector3 dir = pointToRotate - pivot;
  115. dir = Quaternion.Euler(angles) * dir;
  116. pointToRotate = dir + pivot;
  117. return pointToRotate;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement