raphael76280

Sunstealer DynamicLight

Jan 22nd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. public class DynamicLight : MonoBehaviour
  7. {
  8.     [Header("Generation")]
  9.     public int precision = 720;
  10.     public bool rotationOffset;
  11.     public enum Shapes // your custom enumeration
  12.     {
  13.         Linear,
  14.         Circle
  15.     };
  16.  
  17.     public Shapes shape = Shapes.Circle;
  18.  
  19.     public float angle = 360;
  20.     public float width = 1;
  21.  
  22.     /* Dégradé intégré
  23.     public Gradient colors = new Gradient();
  24.     public float colorsLoopInSeconds = 0;
  25.     private float colorTime;
  26.     */
  27.  
  28.  
  29.     //public float distanceMin = 1;
  30.     //public float distanceMax = 1;
  31.     public float distance;
  32.  
  33.     [Header("Render")]
  34.     public Color color;
  35.     public float intensity = 0.1f;
  36.     public Texture texture;
  37.  
  38.     public LayerMask LightLayer;
  39.     public LayerMask colliderLayer;
  40.     public LayerMask lighttest;
  41.  
  42.  
  43.  
  44.     [Header("Variables")]
  45.     Vector2 velocity;
  46.     private List<Vector2> points;
  47.     private List<Vector3> vertices;
  48.     private int[] triangles;
  49.     private int uid;
  50.     private Vector3 rotOffset;
  51.     Light objLight;
  52.     Camera objCam;
  53.  
  54.  
  55.  
  56.     //[ConditionalField("orientation", Shape.Circular)] public float angle = 2;
  57.     //public float length;
  58.  
  59.     float DegToPi(float deg)
  60.     {
  61.         return deg * 1 / 180;
  62.     }
  63.  
  64.     float PiToDeg(float pi)
  65.     {
  66.         return pi * 180;
  67.     }
  68.  
  69.     private void Awake()
  70.     {
  71.         //We check if the setup is good
  72.         if (LightLayer == 0)
  73.             Debug.LogError("Please assign a layer for the light");
  74.         if (colliderLayer== 0)
  75.             Debug.LogError("Please assign a layer for the obstacles");
  76.  
  77.         if (LightLayer != (LightLayer | (1 << gameObject.layer)))
  78.             Debug.LogError("Please the layer of light's gameObject must be the same of the LightLayer variable");
  79.  
  80.         if (precision <= 0)
  81.             precision = 1;
  82.     }
  83.  
  84.  
  85.     private void Start()
  86.     {
  87.         Debug.LogWarning("Corrigé lighttest !");
  88.         Debug.LogWarning("Editeur!");
  89.         Debug.LogWarning("Material !");
  90.         uid = Random.Range(0, 1000);
  91.         //shootRay(precision);
  92.  
  93.         //We set the offset
  94.         if (rotationOffset)
  95.             rotOffset = transform.eulerAngles;
  96.         else
  97.             rotOffset = Vector3.zero;
  98.  
  99.         //We add the needed components
  100.         if (!transform.GetComponent<MeshFilter>())
  101.             gameObject.AddComponent<MeshFilter>();
  102.         if (!transform.GetComponent<MeshRenderer>())
  103.             gameObject.AddComponent<MeshRenderer>();
  104.  
  105.         //colorTime = colorsLoopInSeconds; Dégradé
  106.  
  107.         //We create the render object
  108.         CreateRenderer();
  109.     }
  110.  
  111.     private void Update()
  112.     {
  113.        // Debug.Log(transform.parent.transform.eulerAngles.z);
  114.        // Debug.Log("cos: " + Mathf.Cos(DegToPi(transform.parent.transform.eulerAngles.z)) + " sin: " + Mathf.Sin(DegToPi(transform.parent.transform.eulerAngles.z)));
  115.        
  116.         //We avoid some errors
  117.         if (angle > 360)
  118.             angle = angle - 360;
  119.         if (angle < 0)
  120.             angle = 0;
  121.  
  122.         //We start the calculation of collision
  123.         if (shape == Shapes.Circle)
  124.             shootRayCircle((int)(precision*angle));
  125.         else
  126.             shootRayLine(precision);
  127.  
  128.  
  129.         /* Dégradé intégré
  130.         if (colorTime <= 0)
  131.             colorTime = colorsLoopInSeconds;
  132.         else
  133.         {
  134.             colorTime -= Time.deltaTime;
  135.         }
  136.  
  137.         float timeStop = colorsLoopInSeconds - colorTime;
  138.         float timeStopCoord =  timeStop / colorsLoopInSeconds;
  139.         color = colors.Evaluate(timeStopCoord);
  140.         */
  141.        
  142.     }
  143.  
  144.     private void LateUpdate()
  145.     {
  146.         //We update the light with the parent
  147.         transform.localPosition = new Vector3(0, 0, 0 + uid);
  148.         transform.localEulerAngles = new Vector3(transform.parent.transform.eulerAngles.x, transform.parent.transform.eulerAngles.y, -transform.parent.transform.eulerAngles.z);
  149.  
  150.         //We update the renderer (in case of color change or others variables)
  151.         updateRenderer();
  152.     }
  153.  
  154.  
  155.  
  156.  
  157.     void OnDrawGizmosSelected()
  158.     {
  159.         // Draw a yellow sphere at the transform's position
  160.         Gizmos.color = Color.blue;
  161.         Gizmos.DrawSphere(transform.position, 0.1f);
  162.  
  163.  
  164.         Gizmos.color = Color.yellow;
  165.         if (EditorApplication.isPlaying)
  166.         {
  167.             for (int i = 0; i < vertices.Count; i++)
  168.             {
  169.  
  170.                 // Gizmos.DrawSphere(transform.TransformPoint(new Vector3(vertices[i].x, vertices[i].y, 0)), 0.05f);
  171.                 Handles.color = Color.yellow;
  172.                 Handles.Label(new Vector3(vertices[i].x, vertices[i].y, 0), i.ToString());
  173.             }
  174.  
  175.  
  176.             Gizmos.color = new Color(0.9f, 0.4f, 0.55f);
  177.             for (int i = 0; i < points.Count; i++)
  178.             {
  179.               //  Gizmos.DrawSphere(transform.TransformPoint(new Vector3(points[i].x, points[i].y, 0)), 0.03f);
  180.                
  181.             }
  182.         }
  183.     }
  184.  
  185.  
  186.     void shootRayCircle(int n)
  187.     {
  188.         /* Variation distance
  189.         if (distanceMax - distanceMin > 0)
  190.         {
  191.             distance = distanceMin + (Mathf.PingPong(Time.time * 0.5f, distanceMax - distanceMin));
  192.         }
  193.         else
  194.         {
  195.             distance = distanceMin;
  196.         }
  197.         */
  198.         points = new List<Vector2>();
  199.         for (int i = 0; i <= n; i++)
  200.         {
  201.             float a = transform.parent.transform.eulerAngles.z + rotOffset.z;
  202.             float offset = (DegToPi(-a)* Mathf.PI) - DegToPi(1.5f * angle);
  203.             //DegToPi(-transform.eulerAngles.z * Mathf.PI)
  204.            
  205.  
  206.             float x = Mathf.Sin(((DegToPi(angle) * Mathf.PI * i) / n) + offset) * distance;
  207.             float y = Mathf.Cos(((DegToPi(angle) * Mathf.PI * i) / n) + offset) * distance;
  208.             RaycastHit2D hit = Physics2D.Linecast(transform.position, new Vector2(transform.position.x + x, transform.position.y + y), colliderLayer);
  209.             Debug.DrawLine(transform.position, new Vector2(transform.position.x + x, transform.position.y + y), new Color(0.8f, 0,0));
  210.            Vector2 obj = new Vector2(x,y);
  211.             if (hit.collider != null)
  212.             {
  213.  
  214.                 if (Vector2.Distance(transform.position, hit.transform.position) < distance * Mathf.PI)
  215.                 {
  216.                     obj = transform.InverseTransformPoint(new Vector2(hit.point.x, hit.point.y));
  217.                 }
  218.             }
  219.  
  220.             points.Add(obj);
  221.  
  222.         }
  223.  
  224.         GenerateMesh();
  225.  
  226.     }
  227.  
  228.     void shootRayLine(int precision)
  229.     {
  230.         points = new List<Vector2>();
  231.         for (float y = 0; y < distance; y += (float)distance / (10f * (float)precision))
  232.         {
  233.             float x = distance;
  234.             RaycastHit2D hit = Physics2D.Linecast(new Vector2(transform.position.x, transform.position.y+y), new Vector2(transform.position.x + x, transform.position.y + y), colliderLayer);
  235.             Debug.DrawLine(new Vector2(transform.position.x, transform.position.y+y), new Vector2(transform.position.x + x, transform.position.y + y), new Color(0.8f, 0, 0));
  236.             Vector2 obj = new Vector2(x, y);
  237.             if (hit.collider != null)
  238.             {
  239.  
  240.                 if (Vector2.Distance(transform.position, hit.transform.position) < distance * Mathf.PI)
  241.                 {
  242.                     obj = transform.InverseTransformPoint(new Vector2(hit.point.x, hit.point.y));
  243.                 }
  244.             }
  245.  
  246.             points.Add(obj);
  247.         }
  248.         for (float x = width; x > -width; x-=(float)width/(10f*(float)precision))
  249.         {
  250.             float y = distance;
  251.             RaycastHit2D hit = Physics2D.Linecast(new Vector2(transform.position.x+x, transform.position.y), new Vector2(transform.position.x + x, transform.position.y + y), colliderLayer);
  252.             Debug.DrawLine(new Vector2(transform.position.x+x, transform.position.y), new Vector2(transform.position.x + x, transform.position.y + y), new Color(0.8f, 0, 0));
  253.             Vector2 obj = new Vector2(x, y);
  254.             if (hit.collider != null)
  255.             {
  256.  
  257.                 if (Vector2.Distance(transform.position, hit.transform.position) < distance * Mathf.PI)
  258.                 {
  259.                     obj = transform.InverseTransformPoint(new Vector2(hit.point.x, hit.point.y));
  260.                 }
  261.             }
  262.  
  263.             points.Add(obj);
  264.         }
  265.         for (float y = distance; y > 0; y -= (float)distance / (10f * (float)precision))
  266.         {
  267.             float x = -distance;
  268.             RaycastHit2D hit = Physics2D.Linecast(new Vector2(transform.position.x, transform.position.y + y), new Vector2(transform.position.x + x, transform.position.y + y), colliderLayer);
  269.             Debug.DrawLine(new Vector2(transform.position.x, transform.position.y + y), new Vector2(transform.position.x + x, transform.position.y + y), new Color(0.8f, 0, 0));
  270.             Vector2 obj = new Vector2(x, y);
  271.             if (hit.collider != null)
  272.             {
  273.  
  274.                 if (Vector2.Distance(transform.position, hit.transform.position) < distance * Mathf.PI)
  275.                 {
  276.                     obj = transform.InverseTransformPoint(new Vector2(hit.point.x, hit.point.y));
  277.                 }
  278.             }
  279.  
  280.             points.Add(obj);
  281.         }
  282.  
  283.         GenerateMesh();
  284.     }
  285.  
  286.     void GenerateMesh()
  287.     {
  288.         Mesh mesh = new Mesh();
  289.         //mesh.c
  290.         vertices = new List<Vector3>();
  291.         vertices.Add(transform.InverseTransformPoint(transform.position));
  292.         //vertices.Add(new Vector3(transform.position.x, transform.position.y, transform.position.z +uid));
  293.         for (int i = 1; i < points.Count + 1; i++)
  294.         {
  295.              
  296.             if (i > 1 && i < points.Count + 2)
  297.             {
  298.              
  299.                 if (new Vector2(Mathf.Round(points[i-2].x *10), Mathf.Round(points[i - 2].y * 10)) != new Vector2(Mathf.Round(vertices[vertices.Count -1].x*10), Mathf.Round(vertices[vertices.Count - 1].y * 10)))
  300.                 {
  301.                     vertices.Add(new Vector3(points[i - 1].x, points[i - 1].y, 0));
  302.                 }
  303.             }else
  304.             {
  305.                 vertices.Add(new Vector3(points[i - 1].x, points[i - 1].y, 0));
  306.             }
  307.         }
  308.         if (angle == 360)
  309.         vertices.Add(new Vector3(points[0].x, points[0].y, 0));
  310.  
  311.         //Debug.Log(points.Count);
  312.         //Debug.Log(vertices.Count);
  313.  
  314.  
  315.         mesh.vertices = vertices.ToArray();
  316.  
  317.         // int[] triangles = new int[9] { 0, 1, 1, 2, 2, 3, 3, 4, 4};
  318.         //mesh.triangles = triangles;
  319.  
  320.         triangles = new int[vertices.Count * 3 - 3];
  321.         for (int i = 0; i < vertices.Count - 2; i++)
  322.         {
  323.             // Debug.Log(i + "|" + vertices.Count);
  324.             if (i == 0)
  325.             {
  326.  
  327.                 triangles[i * 3] = 0;
  328.                 triangles[i * 3 + 1] = 1;
  329.                 triangles[i * 3 + 2] = 2;
  330.             }
  331.             else
  332.             {
  333.                 triangles[i * 3] = 0;
  334.                 triangles[i * 3 + 1] = i + 1;
  335.                 triangles[i * 3 + 2] = i + 2;
  336.             }
  337.         }
  338.        // triangles[(vertices.Count - 1) * 3 - 3] = vertices.Count - 2;
  339.         //triangles[(vertices.Count - 1) * 3 - 2] = vertices.Count - 1;
  340.         //triangles[(vertices.Count - 1) * 3 - 1] = 0;
  341.         mesh.triangles = triangles;
  342.         //this is also acceptable!
  343.         //mesh.SetTriangles(new int[9]{0,1,2,3,4,3,2,1,0}, 0);
  344.  
  345.         Vector2[] uvs = new Vector2[vertices.Count];
  346.         for (int i = 0; i < vertices.Count; i++)
  347.         {
  348.             //uvs[i] = new Vector2(vertices[i].x - transform.position.x, vertices[i].y - transform.position.y);
  349.             uvs[i] = vertices[i];
  350.         }
  351.  
  352.  
  353.         mesh.uv = uvs;
  354.  
  355.         Vector3[] normals = new Vector3[vertices.Count];
  356.         for (int i = 0; i < vertices.Count; i++)
  357.         {
  358.             normals[i] = Vector3.back;
  359.         }
  360.         mesh.normals = normals;
  361.  
  362.         //you could also call this instead...
  363.         //mesh.RecalculateNormals();
  364.  
  365.  
  366.         //grab our filter.. set the mesh
  367.         mesh.name = transform.parent.name;
  368.         MeshFilter filter = GetComponent<MeshFilter>();
  369.         filter.mesh = mesh;
  370.         //filter.sharedMesh.
  371.  
  372.         //you can do your material stuff here...
  373.         MeshRenderer r = GetComponent<MeshRenderer>();
  374.         Material m = r.material;
  375.         float scale = 0.5f / distance;
  376.         m.mainTextureScale = new Vector2(scale, scale);
  377.         m.mainTexture = texture;
  378.     }
  379.  
  380.  
  381.     void CreateRenderer()
  382.     {
  383.         // GameObject objPrefab = new GameObject();
  384.         GameObject obj = new GameObject();
  385.         obj.transform.parent = transform.parent;
  386.         obj.name = transform.parent.name + "LightRenderer";
  387.         obj.transform.localPosition = new Vector3(0, 0, uid - 1);
  388.         objCam = obj.AddComponent<Camera>();
  389.         objCam.clearFlags = CameraClearFlags.SolidColor;
  390.         objCam.nearClipPlane = 0;
  391.         objCam.farClipPlane = 1;
  392.         objCam.backgroundColor = new Color(0,0,0,0);
  393.         objCam.cullingMask = LightLayer;
  394.         objCam.orthographic = true;
  395.         //objCam.fieldOfView = 60;
  396.         objCam.orthographicSize = distance + 1;
  397.  
  398.         RenderTexture lightRenderTexture = new RenderTexture(4000, 4000, 0, RenderTextureFormat.ARGB32);
  399.         lightRenderTexture.Create();
  400.         lightRenderTexture.name = transform.parent.name + "Light";
  401.         objCam.targetTexture = lightRenderTexture;
  402.  
  403.         objLight = obj.AddComponent<Light>();
  404.         objLight.type = LightType.Directional;
  405.         objLight.range = 11;
  406.         objLight.intensity = intensity;
  407.         objLight.cookie = lightRenderTexture;
  408.         objLight.cookieSize = (distance+1)*2;
  409.  
  410.  
  411.        
  412.         objLight.color = color;
  413.         Debug.LogError("Ici le lightest");
  414.         //objLight.cullingMask = 311;
  415.  
  416.     }
  417.  
  418.     void updateRenderer()
  419.     {
  420.         objCam.orthographicSize = distance + 1;
  421.         objLight.intensity = intensity;
  422.         objLight.color = color;
  423.     }
  424.  
  425.  
  426. }
Add Comment
Please, Sign In to add comment