Search Unity

Hidden Error in Dynamic Light based on Background plane

Discussion in 'Vuforia' started by xxxDOSxxx, Mar 30, 2018.

  1. xxxDOSxxx

    xxxDOSxxx

    Joined:
    Sep 29, 2016
    Posts:
    7
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class REAL_LIGHTING : MonoBehaviour {
    6.     public Material matObj, my_mat;
    7.     public GameObject m_Main;
    8.     public Light Main_light, Second_light;
    9. //Debug Images
    10.     public Image m_image1, m_image2;
    11.     public Text m_t;
    12.     public Texture2D my_texture2d;
    13.  
    14.     void Update () {
    15.         if (matObj == null)
    16.         {
    17.             DontDestroyOnLoadObjects();
    18.             my_mat = this.GetComponent<Renderer>().material;
    19.             my_texture2d = my_mat.mainTexture as Texture2D;
    20.             m_image2.material = my_mat;
    21.         }
    22.         transform.LookAt(Camera.main.transform);
    23.         Main_light.color = AverageColorFromTexture(my_texture2d);
    24.     }
    25.     private void LateUpdate()
    26.     {
    27.    
    28.         Second_light.color = Main_light.color;
    29.         m_image1.color = Main_light.color;
    30.    
    31.     }
    32.  
    33.     public void DontDestroyOnLoadObjects()
    34.     {
    35.         matObj = m_Main.transform.GetChild(0).GetComponent<Renderer>().material;
    36.  
    37.         this.GetComponent<Renderer>().material = matObj;
    38.  
    39.     }
    40.  
    41.     Color32 AverageColorFromTexture(Texture2D tex)
    42.     {
    43.  
    44.         Color32[] texColors = tex.GetPixels32();
    45.  
    46.         //int total = texColors.Length;
    47.         int total = 300000;
    48.         float r = 0;
    49.         float g = 0;
    50.         float b = 0;
    51.  
    52.         for (int i = 0; i < total/1000; i++)
    53.         {
    54.  
    55.             r += texColors[(i * 1000)% total].r;
    56.  
    57.             g += texColors[(i * 1000) % total].g;
    58.  
    59.             b += texColors[(i * 1000) % total].b;
    60.  
    61.         }
    62.         //m_t.text = r/1000;
    63.         m_t.text = " "+(byte)((r / (total / 1000)) + 10)+ " ! " + (byte)((g / (total / 1000)) + 10) + " ! " + (byte)((b / (total / 1000)) + 10) + " ! " + 255;
    64.         return new Color32((byte)((r / (total / 1000)) +10), (byte)((g / (total / 1000)) + 10), (byte)((b / (total / 1000)) + 10), 255);
    65.  
    66.     }
    67. }
    Changes color of Light based on vuforia Background plane material texture (video camera output).

    Works great on PC dos not work on Android for some reason.
    My best gess is " .mainTexture as Texture2D;" not working
    or .GetPixels32();
    If anyone knows why pls help.
     
    Last edited: Mar 30, 2018