Advertisement
raphael76280

Untitled

Jun 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. //TEMP
  6. using UnityEngine.UI;
  7.  
  8.  
  9. public class DynamicTexture : MonoBehaviour {
  10.     public int angle = 360;
  11.  
  12.     public float intensity = 1;
  13.  
  14.     public float radius = 5;
  15.     public int pixelPerUnit = 128;
  16.  
  17.     public LayerMask colliderLayer;
  18.  
  19.     public ComputeShader shader;
  20.     int indexOfKernel;
  21.     int indexOfBlurKernel;
  22.  
  23.  
  24.     float[] points;
  25.     float[] oldPoints;
  26.  
  27.     float[] startAngles;
  28.  
  29.  
  30.     public RenderTexture texture;
  31.     public Light light;
  32.  
  33.  
  34.      bool textureDraw;
  35.  
  36.      RawImage image;
  37.  
  38.      public bool gizDraw = true;
  39.  
  40.     public bool baked = false;
  41.      bool done = false;
  42.     // Use this for initialization
  43.     void Start () {
  44.         //On créer la texture et les tableaux de bonne taille
  45.  
  46.         //on ajoute 1 rayon pour evitet un bug nul
  47.         angle++;
  48.  
  49.         texture = new RenderTexture(2 * (int)radius * pixelPerUnit, 2 * (int)radius * pixelPerUnit, 24);
  50.         texture.antiAliasing = 4;
  51.         texture.enableRandomWrite = true;
  52.         texture.Create();
  53.  
  54.  
  55.         indexOfKernel = shader.FindKernel("CSMain");
  56.         indexOfBlurKernel = shader.FindKernel("Blur");
  57.  
  58.     }
  59.  
  60.     /*
  61.     private void OnDrawGizmosSelected()
  62.     {
  63.  
  64.         if (gizDraw)
  65.             for (int i = 0; i < startAngles.Length; i++)
  66.             {
  67.                 Handles.color = Color.yellow;
  68.                 float x = Mathf.Sin(startAngles[i]) * radius;
  69.                 float y = Mathf.Cos(startAngles[i]) * radius;
  70.                 Handles.Label(new Vector3(x, y, 0), i.ToString());
  71.             }
  72.        
  73.     }
  74.     */
  75.     // Update is called once per frame
  76.     void Update () {
  77.  
  78.         if (baked && done)
  79.             return;
  80.  
  81.  
  82.         points = new float[angle];
  83.         startAngles = new float[angle];
  84.         //On boucle l'angle à 1 de plus pour éviter un bug nul
  85.       /*  if (angle > 361)
  86.             angle -= 360;*/
  87.  
  88.         //Offset celon la rotation Z
  89.         float offset = ((transform.eulerAngles.z) % 360);
  90.        // offset -= Mathf.Tan(angle)*radius;
  91.  
  92.         //On shoot un raycast pour chaque rayon
  93.         for (int i = 0; i < angle; i++)
  94.         {
  95.            // bufferX[id.x] = sin((((angle * 1 / 180) * 3.14f * id.x) / n) + offset) * dist;
  96.  
  97.            // startAngles[i] = Mathf.Round((1f / 180f * i * angle + offset) % 360 * Mathf.Deg2Rad*1000)/1000f;
  98.             startAngles[i] = Mathf.Round((1f / 360f * i * angle + offset) % 360 * Mathf.Deg2Rad * 1000) / 1000f;
  99.             //On limite les valeurs pour éviter de dépasser 2PI
  100.             if (startAngles[i] < 0.01f)
  101.                 startAngles[i] = 0.01f;
  102.  
  103.             if (startAngles[i] > 6.28f)
  104.                 startAngles[i] = 6.28f;
  105.  
  106.             float x = Mathf.Sin(startAngles[i]) * radius;
  107.             float y = Mathf.Cos(startAngles[i]) * radius;
  108.  
  109.  
  110.  
  111.             RaycastHit2D hit;
  112.             hit = Physics2D.Linecast(transform.position, new Vector2(transform.position.x + x, transform.position.y + y), colliderLayer);
  113.             if(gizDraw)
  114.                 Debug.DrawLine(transform.position, new Vector2(transform.position.x + x, transform.position.y + y));
  115.             if (hit.transform != null)
  116.             {
  117.                 //On stock la position en % décimal par rapport au rayon max
  118.                 points[i] = (Vector2.Distance(transform.position, hit.point) / radius)+(0.015f/radius);
  119.                
  120.             }
  121.             else
  122.             {
  123.                 //Sinon on stock presque 100% (bug quand c'est 100%)
  124.                 points[i] = 0.99f;
  125.             }
  126.  
  127.         }
  128.  
  129.         if (oldPoints == points)
  130.             return;
  131.  
  132.         if(gizDraw)
  133.         for (int i = 0; i < points.Length; i++)
  134.         {
  135.             Debug.DrawRay(transform.position, new Vector2(Mathf.Sin(startAngles[i]) * radius, Mathf.Cos(startAngles[i]) * radius), Color.yellow);
  136.         }
  137.  
  138.         generateTexture(2 * (int)radius * pixelPerUnit, 2 * (int)radius * pixelPerUnit);
  139.        
  140.         light.cookie = texture;
  141.        
  142.         light.cookieSize = 2 * radius;
  143.         light.range = radius + 1;
  144.  
  145.        
  146.  
  147.         light.spotAngle = 100;
  148.  
  149.  
  150.         light.gameObject.transform.localEulerAngles = -transform.localEulerAngles;
  151.         light.gameObject.transform.localPosition = new Vector3(0, 0, -radius);
  152.  
  153.         light.intensity = intensity;
  154.  
  155.  
  156.         oldPoints = points;
  157.  
  158.         if (baked)
  159.             done = true;
  160.        
  161.     }
  162.  
  163.     void generateTexture(int width, int height)
  164.     {
  165.         texture.name = Random.Range(0, 999).ToString();
  166.         Vector2 center = new Vector2(width / 2, height / 2);
  167.  
  168.  
  169.         float[] size = new float[2];
  170.         size[0] = width;
  171.         size[1] = height;
  172.         shader.SetFloats("size", size);
  173.  
  174.         shader.SetFloat("radius", radius);
  175.  
  176.         shader.SetFloat("pixelPerUnit", pixelPerUnit);
  177.  
  178.         shader.SetInt("maxAngles", points.Length);
  179.  
  180.         ComputeBuffer start = new ComputeBuffer(startAngles.Length, sizeof(float));
  181.         start.SetData(startAngles);
  182.         shader.SetBuffer(indexOfKernel, "startAngles", start);
  183.  
  184.  
  185.  
  186.         ComputeBuffer pointsBuffer = new ComputeBuffer(points.Length, sizeof(float));
  187.         pointsBuffer.SetData(points);
  188.         shader.SetBuffer(indexOfKernel, "magAngles", pointsBuffer);
  189.  
  190.         shader.SetTexture(indexOfKernel, "Result", texture);
  191.  
  192.         // 32 32 1;
  193.         int sx = (int)width/32;
  194.         int sy = (int)height/ 32;
  195.  
  196.         shader.Dispatch(indexOfKernel,sx, sy, 1);
  197.  
  198.  
  199.  
  200.         shader.SetTexture(indexOfBlurKernel, "Result", texture);
  201.         shader.SetTexture(indexOfBlurKernel, "BlurredResult", texture);
  202.  
  203.  
  204.         for (int i = 0; i < pixelPerUnit/4; i++)
  205.         {
  206.             shader.Dispatch(indexOfBlurKernel, sx, sy, 1);
  207.         }
  208.        
  209.  
  210.         if (image)
  211.             image.texture = texture;
  212.  
  213.  
  214.         start.Release();
  215.         pointsBuffer.Release();
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement