} // Update()

    void GetAndSendLEDChainColors()
    {
        m_boids.DetermineParamValue("_Hemisphere", out m_Hemisphere);

        m_BoidLEDComputeShader.SetFloat("_Hemisphere", m_Hemisphere);
        m_BoidLEDComputeShader.SetInt("_ColorSamplingMethod", m_colorSamplingMethod);
        m_BoidLEDComputeShader.SetFloat("_NeighborRadius", m_neighborRadius);
        m_BoidLEDComputeShader.SetFloat("_SamplingRadius", m_samplingRadius); // you can change inspector variable' value at runtime

        m_BoidLEDComputeShader.Dispatch(m_kernelIDLED, m_threadGroupSize, 1, 1);

        //note:  m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidLEDBuffer", m_BoidLEDBuffer);
        // note:   m_BoidLEDBuffer will be used  in:
        //  m_Boid m_boidLEDInstanceMaterial.SetBuffer("_BoidLEDBuffer", m_LEDColorGenController.m_BoidLEDBuffer);

        // Update is called once per frame
        //m_BoidLEDRenderDebugBuffer.GetData(m_ComputeBufferArray);
        //m_BoidLEDRenderDebugBuffer0.GetData(m_ComputeBufferArray0);

        m_BoidLEDBuffer.GetData(m_BoidLEDArray); // Get the boidLED data to send to the arduino

        // Debug.Log("BoidLEDRender Debug");



        //_BoidLEDRenderDebugBuffer0[pId][0] = (float)minIndex;
        //_BoidLEDRenderDebugBuffer0[pId][1] = minDist;
        //_BoidLEDRenderDebugBuffer0[pId][2] = neighborCnt;

        // for (int i = 0; i < m_totalNumOfLEDs; i++)


        //{
        // Debug.Log(i + "th boid LED ID =" + m_ComputeBufferArray[i].BoidLEDID);
        //Debug.Log(i + "th boid LED (min index) Nearest Neighbor ID=" + m_ComputeBufferArray[i].NearestBoidID);
        //Debug.Log(i + "th boid LED Neighbor Count=" + m_ComputeBufferArray[i].NeighborCount);
        //Debug.Log(i + "th boid LED Neighbor Radius=" + m_ComputeBufferArray[i].NeighborRadius);
        //Debug.Log(i + "th boid LED Nearest Neighbor Color=" + m_ComputeBufferArray[i].NearestBoidColor);
        //Debug.Log(i + "th boid LED Avg Color:" + m_ComputeBufferArray[i].AvgColor);

        //Debug.Log(i + "th boid LED min Index [ver0] =" + m_ComputeBufferArray0[i][0]);
        //Debug.Log(i + "th boid LED min Dist [ver0]=" + m_ComputeBufferArray0[i][1] );
        //Debug.Log(i + "th boid LED Neighbor Count [ver0]=" + m_ComputeBufferArray0[i][2]);

        //Debug.Log(i + "th boid LED Nearest Boid ID [m_BoidLEDBuffer] =" + m_BoidLEDArray[i].NearestBoidID);
        //Debug.Log(i + "th boid LED Nearest Boid Color [m_BoidLEDBuffer] =" + m_BoidLEDArray[i].Color);



        // }


        // Each thread group, e.g.  SV_GroupID = (0,0,0) will contain BLOCK_SIZE * 1 * 1 threads according to the
        // declaration "numthreads(BLOCK_SIZE, 1, 1)]" in the computeshader.

        //This call sets  m_BoidLEDBuffer, which is passed to the LED shader directly

        // LEDColorGenController.m_BoidLEDBuffer will be used to render the LED Branches by  _boidLEDInstanceMaterial.

        // Get the current values of the boidLEDs computed by BoidLEDComputeShader

        // debugging



        //m_BoidLEDBuffer.GetData(m_BoidLEDArray); // Get the boidLED data to send to the arduino

        ////public static float/iny Range(float min, float max);

        // Copy m_BoidLEDArray to m_LEDArray to send them to the master Arduino via serial communication.


        //Debug.Log("In Update() in LEDColorGenController:");



        //https://gamedev.stackexchange.com/questions/128976/writing-and-reading-computebuffer-in-a-shader

        m_BoidLEDBuffer.GetData(m_BoidLEDArray);

        //DEBUG code
        //Debug.Log("In Update(): LEDColorGenController:");

        //for (int i = 0; i < m_totalNumOfLEDs; i++)
        //{

        //    Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position);
        //    Debug.Log(i + "th LED HeadDir" + m_BoidLEDArray[i].HeadDir);
        //    Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color);

        //}



        for (int i = 0; i < m_totalNumOfLEDs; i++)
        {
            m_LEDArray[i * 3]     = (byte)(254 * m_BoidLEDArray[i].Color[0]); // Vector4 Color
            m_LEDArray[i * 3 + 1] = (byte)(254 * m_BoidLEDArray[i].Color[1]);
            m_LEDArray[i * 3 + 2] = (byte)(254 * m_BoidLEDArray[i].Color[2]);


            //Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position.ToString("F4"));
            //Debug.Log(i + "th LED HeadDir" + m_BoidLEDArray[i].HeadDir.ToString("F4") );
            //Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color.ToString("F4") );
            //Debug.Log(i + "th LED Color: NeighborCount" + m_BoidLEDArray[i].NeighborCount);



            //  Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position);
            // Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color);

            //Debug.Log(i + "th LED Color (value range check) from m_boids.m_boidArray"
            //    + m_boids.m_boidArray[  m_BoidLEDArray[i].NearestBoidID ].Color );

            // for debugging, copy m_boidArray colors and positions and scales to m_BoidLEDArray
            //m_BoidLEDArray[i].Position = m_boids.m_boidArray[i].Position;
            //m_BoidLEDArray[i].Scale = m_boids.m_boidArray[i].Scale;
            //m_BoidLEDArray[i].Color = m_boids.m_boidArray[i].Color;

            //m_LEDArray[i * 3] = (byte)(255 * m_BoidLEDArray[i].Color[0]); // Vector4 Color
            //m_LEDArray[i * 3 + 1] = (byte)(255 * m_BoidLEDArray[i].Color[1]);
            //m_LEDArray[i * 3 + 2] = (byte)(255 * m_BoidLEDArray[i].Color[2]);
        }


        //m_BoidLEDBuffer.SetData( m_BoidLEDArray ); // LEDColorGenController.m_BoidLEDBuffer  is used

        // to rendeirng the boid LED cylinders in BoidRendererTreeOfVoice.

        //for (int i = 0; i < m_totalNumOfLEDs; i++)
        //{
        //    m_LEDArray[i * 3] = (byte)(255 * m_BoidLEDArray[i].Color[0]); // Vector4 Color
        //    m_LEDArray[i * 3 + 1] = (byte)(255 * m_BoidLEDArray[i].Color[1]);
        //    m_LEDArray[i * 3 + 2] = (byte)(255 * m_BoidLEDArray[i].Color[2]);
        //}



        if (m_LEDSenderHandler is null)
        {
            Debug.LogError("LEDSenderHandler delegate should be an event handler function in CommHub component");
            // Application.Quit();
#if UNITY_EDITOR
            // Application.Quit() does not work in the editor so
            //  UnityEditor.EditorApplication.isPlaying = false;
            UnityEditor.EditorApplication.Exit(0);
#else
            Application.Quit();
#endif
        }

        m_LEDSenderHandler.Invoke(m_LEDArray);
    } // GetAndSendLEDChainColors()
    void Update()
    {
        //  Debug.Log("I am updating the LED colors ih LEDColorGenController");

        //cf.    m_kernelIDLED = m_BoidComputeShader.FindKernel("SampleLEDColors");

        // Call a particular kernel "SampleLEDColors" in the m_BoidLEDComputeShader;

        // m_BoidBuffer is set by the dispatching  BoidComputeShader in SimpleBoidsTreeOfVoice;

        // Now set m_BoidLEDBuffer by dispatching BoidLEDCOmputeShader.

        float currTime = Time.time; //  seconds


        m_boids.DetermineParamValue("_SamplingRadius", currTime, ref m_samplingRadius);

        //m_boids.DetermineParamValue("_LEDChainHeight", currTime, ref m_LEDChainHeight);

        m_BoidLEDComputeShader.SetFloat("_HemisphereGroundPosition", m_HemisphereGroundPosition);


        m_BoidLEDComputeShader.SetFloat("_SamplingRadius", m_samplingRadius);

        m_BoidLEDComputeShader.Dispatch(m_kernelIDLED, m_threadGroupSize, 1, 1);

        //note:  m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidLEDBuffer", m_BoidLEDBuffer);
        // note:   m_BoidLEDBuffer will be used  in:
        //  m_Boid m_boidLEDInstanceMaterial.SetBuffer("_BoidLEDBuffer", m_LEDColorGenController.m_BoidLEDBuffer);

        // Update is called once per frame
        //m_BoidLEDRenderDebugBuffer.GetData(m_ComputeBufferArray);
        //m_BoidLEDRenderDebugBuffer0.GetData(m_ComputeBufferArray0);

        m_BoidLEDBuffer.GetData(m_BoidLEDArray); // Get the boidLED data to send to the arduino

        // Debug.Log("BoidLEDRender Debug");



        //_BoidLEDRenderDebugBuffer0[pId][0] = (float)minIndex;
        //_BoidLEDRenderDebugBuffer0[pId][1] = minDist;
        //_BoidLEDRenderDebugBuffer0[pId][2] = neighborCnt;

        // for (int i = 0; i < m_totalNumOfLEDs; i++)


        //{
        // Debug.Log(i + "th boid LED ID =" + m_ComputeBufferArray[i].BoidLEDID);
        //Debug.Log(i + "th boid LED (min index) Nearest Neighbor ID=" + m_ComputeBufferArray[i].NearestBoidID);
        //Debug.Log(i + "th boid LED Neighbor Count=" + m_ComputeBufferArray[i].NeighborCount);
        //Debug.Log(i + "th boid LED Neighbor Radius=" + m_ComputeBufferArray[i].NeighborRadius);
        //Debug.Log(i + "th boid LED Nearest Neighbor Color=" + m_ComputeBufferArray[i].NearestBoidColor);
        //Debug.Log(i + "th boid LED Avg Color:" + m_ComputeBufferArray[i].AvgColor);

        //Debug.Log(i + "th boid LED min Index [ver0] =" + m_ComputeBufferArray0[i][0]);
        //Debug.Log(i + "th boid LED min Dist [ver0]=" + m_ComputeBufferArray0[i][1] );
        //Debug.Log(i + "th boid LED Neighbor Count [ver0]=" + m_ComputeBufferArray0[i][2]);

        //Debug.Log(i + "th boid LED Nearest Boid ID [m_BoidLEDBuffer] =" + m_BoidLEDArray[i].NearestBoidID);
        //Debug.Log(i + "th boid LED Nearest Boid Color [m_BoidLEDBuffer] =" + m_BoidLEDArray[i].Color);



        // }


        // Each thread group, e.g.  SV_GroupID = (0,0,0) will contain BLOCK_SIZE * 1 * 1 threads according to the
        // declaration "numthreads(BLOCK_SIZE, 1, 1)]" in the computeshader.

        //This call sets  m_BoidLEDBuffer, which is passed to the LED shader directly

        // LEDColorGenController.m_BoidLEDBuffer will be used to render the LED Branches by  _boidLEDInstanceMaterial.

        // Get the current values of the boidLEDs computed by BoidLEDComputeShader

        // debugging



        //m_BoidLEDBuffer.GetData(m_BoidLEDArray); // Get the boidLED data to send to the arduino

        ////public static float/iny Range(float min, float max);

        // Copy m_BoidLEDArray to m_LEDArray to send them to the master Arduino via serial communication.


        //Debug.Log("In Update() in LEDColorGenController:");



        //https://gamedev.stackexchange.com/questions/128976/writing-and-reading-computebuffer-in-a-shader

        m_BoidLEDBuffer.GetData(m_BoidLEDArray);

        //DEBUG code
        //Debug.Log("In Update(): LEDColorGenController:");

        //for (int i = 0; i < m_totalNumOfLEDs; i++)
        //{

        //    Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position);
        //    Debug.Log(i + "th LED HeadDir" + m_BoidLEDArray[i].HeadDir);
        //    Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color);

        //}



        for (int i = 0; i < m_totalNumOfLEDs; i++)
        {
            m_LEDArray[i * 3]     = (byte)(255 * m_BoidLEDArray[i].Color[0]); // Vector4 Color
            m_LEDArray[i * 3 + 1] = (byte)(255 * m_BoidLEDArray[i].Color[1]);
            m_LEDArray[i * 3 + 2] = (byte)(255 * m_BoidLEDArray[i].Color[2]);


            //Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position.ToString("F4"));
            //Debug.Log(i + "th LED HeadDir" + m_BoidLEDArray[i].HeadDir.ToString("F4") );
            //Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color.ToString("F4") );
            //Debug.Log(i + "th LED Color: NeighborCount" + m_BoidLEDArray[i].NeighborCount);



            //  Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position);
            // Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color);

            //Debug.Log(i + "th LED Color (value range check) from m_boids.m_boidArray"
            //    + m_boids.m_boidArray[  m_BoidLEDArray[i].NearestBoidID ].Color );

            // for debugging, copy m_boidArray colors and positions and scales to m_BoidLEDArray
            //m_BoidLEDArray[i].Position = m_boids.m_boidArray[i].Position;
            //m_BoidLEDArray[i].Scale = m_boids.m_boidArray[i].Scale;
            //m_BoidLEDArray[i].Color = m_boids.m_boidArray[i].Color;

            //m_LEDArray[i * 3] = (byte)(255 * m_BoidLEDArray[i].Color[0]); // Vector4 Color
            //m_LEDArray[i * 3 + 1] = (byte)(255 * m_BoidLEDArray[i].Color[1]);
            //m_LEDArray[i * 3 + 2] = (byte)(255 * m_BoidLEDArray[i].Color[2]);
        }


        //m_BoidLEDBuffer.SetData( m_BoidLEDArray ); // LEDColorGenController.m_BoidLEDBuffer  is used

        // to rendeirng the boid LED cylinders in BoidRendererTreeOfVoice.

        //for (int i = 0; i < m_totalNumOfLEDs; i++)
        //{
        //    m_LEDArray[i * 3] = (byte)(255 * m_BoidLEDArray[i].Color[0]); // Vector4 Color
        //    m_LEDArray[i * 3 + 1] = (byte)(255 * m_BoidLEDArray[i].Color[1]);
        //    m_LEDArray[i * 3 + 2] = (byte)(255 * m_BoidLEDArray[i].Color[2]);
        //}


        Debug.Log("LED Data Send Event Handler called in LEDColorGenController");

        if (m_LEDSenderHandler is null)
        {
            Debug.LogError(" Event Handler Methods should be added to m_LEDSenderHandler in CommHub.cs");
            Application.Quit();
        }
        m_LEDSenderHandler.Invoke(m_LEDArray);
    } // Update()
Пример #3
0
    void Update()
    {
        //  Debug.Log("I am updating the LED colors ih LEDColorGenController");

        //cf.    m_kernelIDLED = m_BoidComputeShader.FindKernel("SampleLEDColors");

        // Call a particular kernel "SampleLEDColors" in the m_BoidLEDComputeShader;

        // m_BoidBuffer is set by the dispatching  BoidComputeShader in SimpleBoidsTreeOfVoice;

        // Now set m_BoidLEDBuffer by dispatching BoidLEDCOmputeShader.

        // float currTime = Time.time; //  seconds


        // m_boids.DetermineParamValue("_SamplingRadius",  out m_samplingRadius);

        // m_BoidLEDComputeShader.SetFloat("_SamplingRadius", m_samplingRadius);

        m_boids.DetermineParamValue("_Hemisphere", out m_Hemisphere);

        m_BoidLEDComputeShader.SetFloat("_Hemisphere", m_Hemisphere);

        m_BoidLEDComputeShader.SetFloat("_SamplingRadius", m_samplingRadius); // you can change inspector variable' value at runtime

        m_BoidLEDComputeShader.Dispatch(m_kernelIDLED, m_threadGroupSize, 1, 1);

        //note:  m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidLEDBuffer", m_BoidLEDBuffer);
        // note:   m_BoidLEDBuffer will be used  in:
        //  m_Boid m_boidLEDInstanceMaterial.SetBuffer("_BoidLEDBuffer", m_LEDColorGenController.m_BoidLEDBuffer);

        // Update is called once per frame
        //m_BoidLEDRenderDebugBuffer.GetData(m_ComputeBufferArray);
        //m_BoidLEDRenderDebugBuffer0.GetData(m_ComputeBufferArray0);

        m_BoidLEDBuffer.GetData(m_BoidLEDArray); // Get the boidLED data to send to the arduino

        // Debug.Log("BoidLEDRender Debug");



        //_BoidLEDRenderDebugBuffer0[pId][0] = (float)minIndex;
        //_BoidLEDRenderDebugBuffer0[pId][1] = minDist;
        //_BoidLEDRenderDebugBuffer0[pId][2] = neighborCnt;

        // for (int i = 0; i < m_totalNumOfLEDs; i++)


        //{
        // Debug.Log(i + "th boid LED ID =" + m_ComputeBufferArray[i].BoidLEDID);
        //Debug.Log(i + "th boid LED (min index) Nearest Neighbor ID=" + m_ComputeBufferArray[i].NearestBoidID);
        //Debug.Log(i + "th boid LED Neighbor Count=" + m_ComputeBufferArray[i].NeighborCount);
        //Debug.Log(i + "th boid LED Neighbor Radius=" + m_ComputeBufferArray[i].NeighborRadius);
        //Debug.Log(i + "th boid LED Nearest Neighbor Color=" + m_ComputeBufferArray[i].NearestBoidColor);
        //Debug.Log(i + "th boid LED Avg Color:" + m_ComputeBufferArray[i].AvgColor);

        //Debug.Log(i + "th boid LED min Index [ver0] =" + m_ComputeBufferArray0[i][0]);
        //Debug.Log(i + "th boid LED min Dist [ver0]=" + m_ComputeBufferArray0[i][1] );
        //Debug.Log(i + "th boid LED Neighbor Count [ver0]=" + m_ComputeBufferArray0[i][2]);

        //Debug.Log(i + "th boid LED Nearest Boid ID [m_BoidLEDBuffer] =" + m_BoidLEDArray[i].NearestBoidID);
        //Debug.Log(i + "th boid LED Nearest Boid Color [m_BoidLEDBuffer] =" + m_BoidLEDArray[i].Color);



        // }


        // Each thread group, e.g.  SV_GroupID = (0,0,0) will contain BLOCK_SIZE * 1 * 1 threads according to the
        // declaration "numthreads(BLOCK_SIZE, 1, 1)]" in the computeshader.

        //This call sets  m_BoidLEDBuffer, which is passed to the LED shader directly

        // LEDColorGenController.m_BoidLEDBuffer will be used to render the LED Branches by  _boidLEDInstanceMaterial.

        // Get the current values of the boidLEDs computed by BoidLEDComputeShader

        // debugging



        //m_BoidLEDBuffer.GetData(m_BoidLEDArray); // Get the boidLED data to send to the arduino

        ////public static float/iny Range(float min, float max);

        // Copy m_BoidLEDArray to m_LEDArray to send them to the master Arduino via serial communication.


        //Debug.Log("In Update() in LEDColorGenController:");



        //https://gamedev.stackexchange.com/questions/128976/writing-and-reading-computebuffer-in-a-shader

        m_BoidLEDBuffer.GetData(m_BoidLEDArray);

        //DEBUG code
        //Debug.Log("In Update(): LEDColorGenController:");

        //for (int i = 0; i < m_totalNumOfLEDs; i++)
        //{

        //    Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position);
        //    Debug.Log(i + "th LED HeadDir" + m_BoidLEDArray[i].HeadDir);
        //    Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color);

        //}



        for (int i = 0; i < m_totalNumOfLEDs; i++)
        {