using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 腳本掛載到墻上
/// </summary>
public class ImageFusion : MonoBehaviour {
public Texture2D bulletTexture; // 【圖片】彈痕
private Texture2D wallTexture; // 【圖片】墻
private Texture2D NewWallTexture; // 【圖片】墻的備份
private float wall_height; // 【獲取墻和彈痕圖片的寬高信息】
private float wall_width;
private float bullet_height;
private float bullet_width;
RaycastHit hit; // 獲取子彈打擊點
private Queue<Vector2> uiQueues; // 存儲像素點信息
// Use this for initialization
void Start () {
uiQueues = new Queue<Vector2> ();
wallTexture = GetComponent<MeshRenderer> ().material.mainTexture as Texture2D;
// 【備份墻的圖片】
NewWallTexture = Instantiate (wallTexture);
GetComponent<MeshRenderer> ().material.mainTexture = NewWallTexture;
wall_height = wallTexture.height;
wall_width = wallTexture.width;
bullet_height = bulletTexture.height;
bullet_width = bulletTexture.width;
Debug.Log (wall_width);
Debug.Log (bullet_width);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray,out hit)) {
if (hit.collider.name == "Plane") {
// 得到打擊點的圖片UV坐標
// UV坐標是當前圖片寬高的百分比【左下角(0,0),右上角(1,1)】
Vector2 uv = hit.textureCoord;
uiQueues.Enqueue (uv);
for (int i = 0; i < bullet_width; i++) {
for (int j = 0; j < bullet_height; j++) {
// 先減去彈痕寬度的一半得到最左邊的坐標,依次向右遞增
float w = uv.x * wall_width - bullet_width / 2 + i;
// 同理,從下到上遞增
float h = uv.y * wall_height - bullet_height / 2 + j;
Color wallColor = NewWallTexture.GetPixel ((int)w, (int)h);
Color bulletColor = bulletTexture.GetPixel (i, j);
// 修改彈痕位置的像素為兩圖的融合顏色,若不相乘融合會使用彈痕原圖
NewWallTexture.SetPixel ((int)w, (int)h, wallColor * bulletColor);
}
}
// 應用圖片
NewWallTexture.Apply ();
Invoke ("ReturnWall", 3f);
}
}
}
}
void ReturnWall()
{
// 還原以打擊點為原點的圖片像素點
Vector2 uv = uiQueues.Dequeue ();
for (int i = 0; i < bullet_width; i++) {
for (int j = 0; j < bullet_height; j++) {
float w = uv.x * wall_width - bullet_width / 2 + i;
float h = uv.y * wall_height - bullet_height / 2 + j;
// 使用原圖的像素進行還原
Color wallColor = wallTexture.GetPixel ((int)w,(int)h);
NewWallTexture.SetPixel ((int)w, (int)h, wallColor);
}
}
NewWallTexture.Apply ();
}
}
圖片融合
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 這一期和大家聊聊有關長段落文字在PPT中排版的問題,有時候給領導或頭設計幻燈片的時候,由于領導的演講能力和語言表達...
- 原文地址: http://http.developer.nvidia.com/GPUGems3/gpugems3_...