unity shader采集屏幕像素点

     首先在可以GameObject上挂上一个shader脚本,只要让GameObject有颜色就行,主要的采集像素点颜色的操作写在c#脚本中(这个脚本代码还是需要挂在Camera上)

        接着还是直接上c#代码吧。

public class ColorPicker : MonoBehaviour

 {

public BoxCollider pickerCollider;

private bool m_grab;

private Camera m_camera;

private Texture2D m_screenRenderTexture;

private static Texture2D m_staticRectTexture;

private static GUIStyle m_staticRectStyle;

private static Vector3 m_pixelPosition = Vector3.zero;

private Color m_pickedColor = Color.white;

void Awake() 

{// 得到camera组件

m_camera = GetComponent();if (m_camera == null) 

{Debug.LogError("You need to dray this script to a camera!");return;}// 为相机加上一个BoxCollider// 为了得到鼠标事件if (pickerCollider == null) {pickerCollider = gameObject.AddComponent();

// 确保collider在 camera's frustum中

pickerCollider.center = Vector3.zero;

pickerCollider.center += m_camera.transform.worldToLocalMatrix.MultiplyVector(m_camera.transform.forward) * (m_camera.nearClipPlane + 0.2f);

pickerCollider.size = new Vector3(Screen.width, Screen.height, 0.1f);

}

}

// 在左上角的正方形中绘制我们拾取到的颜色

public static void GUIDrawRect( Rect position, Color color )

{

if( m_staticRectTexture == null )

{

m_staticRectTexture = new Texture2D(1, 1);

}

if( m_staticRectStyle == null )

{

m_staticRectStyle = new GUIStyle();

}

m_staticRectTexture.SetPixel(0, 0, color);

m_staticRectTexture.Apply();

m_staticRectStyle.normal.background = m_staticRectTexture;

GUI.Box(position, GUIContent.none, m_staticRectStyle);

}

    //  当相机完成渲染场景后将叫到OnPostRender(),并且这个消息将会被发送到所有挂在这个照相机的脚本上,用它获取(grab)这个屏幕


    void OnPostRender() {

if (m_grab) {

m_screenRenderTexture = new Texture2D(Screen.width, Screen.height);

m_screenRenderTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);

m_screenRenderTexture.Apply();

            m_pickedColor = m_screenRenderTexture.GetPixel(Mathf.FloorToInt(m_pixelPosition.x), Mathf.FloorToInt(m_pixelPosition.y));

          // m_staticRectTexture.SetPixel(Mathf.FloorToInt(m_pixelPosition.x), Mathf.FloorToInt(m_pixelPosition.y),new Color(0,1,0,1));

          // m_screenRenderTexture.Apply();

            m_grab = false;

}

}

void OnMouseDown() {

m_grab = true;

// 记录鼠标位置去拾取该点的像素颜色值

m_pixelPosition = Input.mousePosition;

}

void OnGUI() {

GUI.Box(new Rect(0, 0, 120, 200), "Color Picker");

GUIDrawRect(new Rect(20, 30, 80, 80), m_pickedColor);

GUI.Label(new Rect(10, 120, 100, 20), "R: " + System.Math.Round((double)m_pickedColor.r, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.r * 255)+ ")");

GUI.Label(new Rect(10, 140, 100, 20), "G: " + System.Math.Round((double)m_pickedColor.g, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.g * 255)+ ")");

GUI.Label(new Rect(10, 160, 100, 20), "B: " + System.Math.Round((double)m_pickedColor.b, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.b * 255)+ ")");

GUI.Label(new Rect(10, 180, 100, 20), "A: " + System.Math.Round((double)m_pickedColor.a, 4) + "\t(" + Mathf.FloorToInt(m_pickedColor.a * 255)+ ")");

}

}



unity shader采集屏幕像素点_第1张图片

你可能感兴趣的:(unity shader采集屏幕像素点)