Example #1
0
    IEnumerator Sequence()
    {
        WebCamDevice[] devices       = WebCamTexture.devices;
        string         camDevice     = WebCamTexture.devices.First().name;
        WebCamTexture  webCamTexture = new WebCamTexture(camDevice, 1280, 720, 30);

        webCamTexture.Play();

        while (true)
        {
            while (!Input.GetMouseButton(0))
            {
                yield return(null);
            }

            reactionAudioSource.Stop();

            animator.SetBool("IsOpen", true);

            while (Input.GetMouseButton(0))
            {
                yield return(null);
            }

            chewAudioSource.Play();

            StartCoroutine(Snapshot());

            animator.SetBool("IsOpen", false);

            HSBColor hsb;

            float[] diffs    = new float[foodColors.Length];
            Color   avgColor = cameraFeed.gameObject.GetComponent <DeviceCameraController>().GetAvgColor();

            for (int i = 0; i < foodColors.Length; i++)
            {
                diffs[i] = 0f;

                var hsbTarget = new HSBColor(foodColors[i].color);

                diffs[i] += Mathf.Abs(HSBColor.Distance(HSBColor.FromColor(avgColor), hsbTarget));
            }

            int   minIndex = 0;
            float min      = diffs[0];

            for (int i = 1; i < foodColors.Length; i++)
            {
//              Debug.Log(i + " " + diffs[i]);
                if (diffs[i] < min)
                {
                    minIndex = i;
                    min      = diffs[i];
                }
            }

            //Debug.Log(foodColors[minIndex].name);

            while (chewAudioSource.isPlaying)
            {
                yield return(null);
            }

            Destroy(GameObject.FindGameObjectWithTag("ChewBits"));

            var foodColor = foodColors[minIndex];
            reactionAudioSource.PlayOneShot(foodColor.clips[Random.Range(0, foodColor.clips.Length)]);
        }
    }