Thứ Hai, 18 tháng 11, 2013

Cookbook - Tạo texture từ ảnh chụp màn hình


  - Tạo texture từ ảnh chụp màn hình -
Nếu bạn muốn game hay người chơi của bạn chụp nhanh ảnh trong game và sử dụng nó như là một texture, bài này sẽ chỉ bạn cách làm thế nào. Bài này có thể rất hữu ích nếu bạn dự định  thực hiện trưng bày những bức ảnh trong game hay hiển thị một bức ảnh trong thời khắc then chốt vào phút cuối (đại loại trong game đua xe, tốc độ...).


Cách để làm

B1.  Nhấp phải vào thẻ Project và chọn Import Package | Skyboxes rồi nhấn Import để sử dụng gói giả lập bầu trời.


B2. Nhấp chuột chọn Main Camera trong thẻ Hierarchy và qua thẻ Inspector, kéo thanh trượt xuống dưới cùng và chọn Add component. Gõ vào khung search vừa hiện lên từ Skybox và click chuột chọn component vừa search để thêm chức năng này cho Main Camera.


B3. Vẫn trong thẻ Inspector, tại component Skybox vừa thêm, click chọn hình tròn phía sau Custom Skybox và chọn Eerie Skybox trong hộp thoại Select Material vừa hiện lên.


B4. Nhấp phải vào thẻ Project và chọn Import Package | Character Controller.

B5. Nhấp chọn Main Camera, sau đó vào Component | Camera Control | Mouse Look.


B6.  Tại thẻ Project, bấm nút Creat | C# Script và đổi tên thành ScreenTexture.

 

B7.  Double click vào file C# vừa tạo và chèn đoạn code sau vào:

using UnityEngine;
using System.Collections;

public class ScreenTexture : MonoBehaviour
{
    public int photoWidth = 50;
    public int photoHeight = 50;
    public int thumbProportion = 25;
    public Color borderColor = Color.white;
    public int borderWidth = 2;
    private Texture2D texture;
    private Texture2D border;
    private int screenWidth;
    private int screenHeight;
    private int frameWidth;
    private int frameHeight;
    private bool  shoot = false;

    void  Start ()
    {
        screenWidth = Screen.width;
        screenHeight = Screen.height;
        frameWidth = Mathf.RoundToInt (screenWidth * photoWidth * 0.01f);
        frameHeight = Mathf.RoundToInt (screenHeight * photoHeight * 0.01f);
        texture = new Texture2D (frameWidth, frameHeight, TextureFormat.RGB24, false);
        border = new Texture2D (1, 1, TextureFormat.ARGB32, false);
        border.SetPixel (0, 0, borderColor);
        border.Apply ();
    }

    void  Update ()
    {
        if (Input.GetKeyUp (KeyCode.Mouse0))
            StartCoroutine (CaptureScreen ());
    }

    void  OnGUI ()
    {
        GUI.DrawTexture (new Rect ((screenWidth * 0.5f) - (frameWidth * 0.5f) - borderWidth * 2, ((screenHeight * 0.5f) - (frameHeight * 0.5f)) - borderWidth, frameWidth + borderWidth * 2, borderWidth), border, ScaleMode.StretchToFill);
        GUI.DrawTexture (new Rect ((screenWidth * 0.5f) - (frameWidth * 0.5f) - borderWidth * 2, (screenHeight * 0.5f) + (frameHeight * 0.5f), frameWidth + borderWidth * 2, borderWidth), border, ScaleMode.StretchToFill);
        GUI.DrawTexture (new Rect ((screenWidth * 0.5f) - (frameWidth * 0.5f) - borderWidth * 2, (screenHeight * 0.5f) - (frameHeight * 0.5f), borderWidth, frameHeight), border, ScaleMode.StretchToFill);
        GUI.DrawTexture (new Rect ((screenWidth * 0.5f) + (frameWidth * 0.5f), (screenHeight * 0.5f) - (frameHeight * 0.5f), borderWidth, frameHeight), border, ScaleMode.StretchToFill);
        if (shoot) {
            GUI.DrawTexture (new Rect (10, 10, frameWidth * thumbProportion * 0.01f, frameHeight * thumbProportion * 0.01f), texture, ScaleMode.StretchToFill);
        }
    }

    IEnumerator  CaptureScreen ()
    {
        yield return new WaitForEndOfFrame();
        texture.ReadPixels (new Rect ((screenWidth * 0.5f) - (frameWidth * 0.5f), (screenHeight * 0.5f) - (frameHeight * 0.5f), frameWidth, frameHeight), 0, 0);
        texture.Apply ();
        shoot = true;
    }
}

B8.  Lưu đoạn script bạn vừa nhập lại và kéo thả nó vào Main Camera ở thẻ Hierarchy.

B9.  Tại thẻ Inspector, trong component Screen Capture, đổi giá trị
         Photo Width và Photo Height = 25 và Thumb Proportion = 75, như hình dưới đây:


B10.  Nhấp nút Play để kiểm tra thành quả. Bạn có thể chụp bất kì ảnh gì chỉ bằng cú click chuột.



Cách hoạt động

Với 1 cú click chuột, hàm lưu các điểm ảnh sẽ khởi động theo một hình chữ nhật và gán nó vào texture được vẽ nên bằng GUI.

Còn nữa

Bạn còn có thể sử dụng nó để:

1. Gán chất liệu cho vật thể:
Bạn có thể gán texture của bạn vào bất kì chất liệu nào cho các đối tượng khác bằng cách thêm dòng code tương tự vào cuối function (hàm) CaptureScreen:
GameObject.Find("MyObject").renderer.material.mainTexture = texture;


2. Sử dụng như một ảnh chụp màn hình:
Bạn có thể xuất nó thành định dạng PNG và lưu trữ nó. Tham khảo tài liệu dưới đây:
http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.EncodeToPNG.html.

Không có nhận xét nào:

Đăng nhận xét