Пример #1
0
        /// <summary>
        /// Returns the Color for the Instance Position
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public Color GetCameraColor(StaticInstance instance)
        {
            SetupCameraForVessel(instance);
            // Move the current object out of the cams view
            InstanceUtil.SetLayerRecursively(instance, 0);
            grasCamera.targetTexture = cameraRenderTexture;
            grasCamera.enabled       = true;
            grasCamera.Render();

            //Ray myRay = new Ray(cameraObject.transform.position, instance.CelestialBody.transform.position);
            //RaycastHit castHit = new RaycastHit();
            //if (!Physics.Raycast(myRay, out castHit, float.PositiveInfinity, 1 << 15))
            //{
            //    Log.Normal("NO raycast hit");
            //}
            //else
            //{

            //    Renderer rend = castHit.transform.GetComponentsInChildren<Renderer>(true).FirstOrDefault();

            //    if (rend == null)
            //    {
            //        Log.Normal("No renderer found");
            //    }
            //    if (rend.materials.Length == 0)
            //    {
            //        Log.Normal("No Raycast material found");
            //    }
            //    if (rend.material.shader == null)
            //    {
            //        Log.Normal("No shader found");
            //    }
            //    else
            //    {
            //        RenderTexture myTexture = new RenderTexture(20, 20, 24);
            //        Graphics.Blit(null, myTexture, rend.material);
            //        myTexture.
            //    }
            //}

            // bring it back to the normal scenery
            InstanceUtil.SetLayerRecursively(instance, 15);

            RenderTexture.active = cameraRenderTexture;
            cameraTexture.ReadPixels(new Rect(0, 0, frameWidth, frameHeight), 0, 0);

            grasCamera.targetTexture = null;
            grasCamera.enabled       = false;
            RenderTexture.active     = null;

            Color[] cols = cameraTexture.GetPixels();
            float   r = 0, g = 0, b = 0;
            int     len = cols.Length;

            for (int i = 0; i < len; i++)
            {
                r += cols[i].r;
                g += cols[i].g;
                b += cols[i].b;
            }
            Color outColor = new Color();

            outColor.r = r / len;
            outColor.g = g / len;
            outColor.b = b / len;
            //outColor.a = 0.014f;
            return(outColor);
        }