Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Experimental.Rendering.Universal;
- public class TestLaser : MonoBehaviour
- {
- public int maxRecursions = 10;
- public float maxStepDistance = 200;
- float airIndex = 1.0f;
- public float glassIndex = 1.5f;
- public GameObject laserToSpawn;
- // Update is called once per frame
- private void OnDrawGizmos() //Update()
- {
- if (!Application.isPlaying)
- {
- return;
- }
- foreach (GameObject laser in GameObject.FindGameObjectsWithTag("Laser")) //every update redraw laser
- {
- laser.SetActive(false);
- }
- DrawPredictedReflection(this.transform.position, this.transform.up, maxRecursions);
- }
- void DrawPredictedReflection(Vector2 position, Vector2 direction, int recursionsRemaing)// int reflectionsRemaining, int splitsRemaining)
- {
- var gizmoHue = (recursionsRemaing / (this.maxRecursions + 1f));
- RaycastHit2D hit2D = Physics2D.Raycast(position, direction, maxStepDistance);
- if (hit2D) //did we hit somthing?
- {
- DrawLaser(position, hit2D.point); //draw a line to it
- if (hit2D.transform.gameObject.tag == "Lens")
- {
- //Debug.Log("Lens Hit...");
- Refract(hit2D.normal, direction, hit2D.point, hit2D.transform.gameObject, recursionsRemaing, airIndex, glassIndex);
- }
- }
- else //nothing hit
- {
- DrawLaser(position, position + direction * maxStepDistance);
- }
- }
- void DrawLaser(Vector2 start, Vector2 end)
- {
- GameObject laser = ObjectPooler.SharedInstance.GetPooledObject("Laser");
- if (laser != null)
- {
- laser.transform.position = Vector2.zero;
- laser.transform.rotation = Quaternion.identity;
- LineRenderer lr = laser.GetComponent<LineRenderer>();
- lr.SetPosition(0, start);
- lr.SetPosition(1, end);
- lr.startColor = Color.white;
- lr.endColor = Color.white;
- laser.SetActive(true);
- }
- }
- void Refract(Vector2 normal, Vector2 direction, Vector2 point, GameObject lastHit, int recursionsRemainging, float n1, float n2)
- {
- float angleOfIncidence = Vector2.Angle(normal, -direction);
- //Debug.Log("Angle of incidence = " + angleOfIncidence);
- float angleOfRefraction = Mathf.Asin((n1 * Mathf.Sin(angleOfIncidence * Mathf.Deg2Rad)) / n2) * Mathf.Rad2Deg; //snells law
- //Debug.Log("Angle of refraction = " + angleOfRefraction);
- Gizmos.color = Color.blue;
- Gizmos.DrawLine(point + normal * 0.1f, point - normal * 0.1f);
- //
- Vector2 testPoint = RotatePointByDeg(point, normal, angleOfIncidence); //laser comming before refraction
- Gizmos.color = Color.red;
- Gizmos.DrawLine(point, testPoint);
- //
- //
- testPoint = RotatePointByDeg(point, -normal, angleOfRefraction); //laser going after refraction
- Gizmos.color = Color.cyan;
- Gizmos.DrawLine(point, testPoint);
- //
- }
- Vector3 RotatePointByDeg(Vector2 origin, Vector2 normal, float angle) //rotate a point about its pivot which should be where the laser hit
- {
- Vector3 pointToRotate = origin + normal * .1f;
- Vector3 pivot = origin;
- Vector3 angles = new Vector3(0, 0, angle);
- Vector3 dir = pointToRotate - pivot;
- dir = Quaternion.Euler(angles) * dir;
- pointToRotate = dir + pivot;
- return pointToRotate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement