简单的贴图融合

来源:互联网 发布:试客系统整站源码 编辑:程序博客网 时间:2024/04/30 01:52

一张是墙面的图片,一张时弹孔的图片,鼠标点击墙面,打孔的图片取代你鼠标点击的那部分墙面图片,过段时间后墙面回复原状

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class WallScript : MonoBehaviour {
Texture2D copy_wallTexture;
Texture2D wall_texture ;
public Texture2D bullet_texture;
float wall_Width;
float wall_Height;
float bullet_Width;
float bullet_Height;
Queue<Vector2> UV;
void Start () {
wall_texture = (Texture2D) GetComponent<MeshRenderer> ().material.mainTexture;

//获得墙面图标的宽度和高度
wall_Width = wall_texture.width;
wall_Height = wall_texture.height;

//子弹图片的宽度和高度
bullet_Width = bullet_texture.width;
bullet_Height = bullet_texture.height;

拷贝一张未加弹孔图片的墙壁图片
copy_wallTexture = Instantiate (wall_texture)as Texture2D;
UV = new Queue<Vector2> ();
}


RaycastHit hit;
void Update () {
if (Input.GetMouseButton (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
bool result = Physics.Raycast (ray, out hit);
if (result) {
if (hit.transform.name == "Wall") {
//获取射线在墙上的UV坐标
Vector2 uv = hit.textureCoord2;
UV.Enqueue (uv);
//将Uv坐标转换成墙上的坐标 -- 代表子弹的中心
float x = uv.x * wall_Width;
float y = uv.y * wall_Height;
for (int i = 0; i < bullet_Width; i++) {
for (int j = 0; j < bullet_Height; j++) {
//获取wall上的坐标
float X = x - bullet_Width / 2.0f + i; 
float Y = y - bullet_Height / 2.0f + j; 
Color wall_color = wall_texture.GetPixel ((int)X, (int)Y);
Color bullet_color = bullet_texture.GetPixel (i, j);
wall_texture.SetPixel((int)X,(int)Y,bullet_color * wall_color);
}
}
}
wall_texture.Apply ();

//3秒后执行GoBack这个方法
Invoke ("GoBack", 3);
}
}
}
void GoBack()
{
if (UV.Count > 0) {
Vector2 uv= UV.Dequeue ();
float x = uv.x * wall_Width;
float y = uv.y * wall_Height;
for (int i = 0; i < bullet_Width; i++) {
for (int j = 0; j < bullet_Height; j++) {
float X = x - bullet_Width / 2 + i;
float Y = y - bullet_Height / 2 + j;
Color Copy_color = copy_wallTexture.GetPixel ((int)X, (int)Y);
wall_texture.SetPixel ((int)X, (int)Y, Copy_color);
}
}
wall_texture.Apply ();
}
}
}

0 0
原创粉丝点击