微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity C# 用图片融合做弹孔

《【Unity】图片融合》
《Unity中贴图融合之弹痕融合》

stem.Collections;
using System.Collections.Generic;
/// 
/// 脚本挂载到墙上
/// 

public class ImageFusion : MonoBehavIoUr {

public Texture2D bulletTexture;        // 【<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>】弹痕 
private Texture2D wallTexture;        // 【<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>】墙
private Texture2D NewWallTexture;    // 【<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>】墙的备份
private float wall_height;        // 【<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>墙和弹痕<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>的宽高信息】 
private float wall_width;
private float bullet_height;
private float bullet_width;
RaycastHit hit;                    // <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>子弹打击点
private Queue<Vector2> uiQueues;    // 存储像素点信息

// Use this for initialization
void Start () {
    uiQueues = new Queue<Vector2> ();

    wallTexture = GetComponent<MeshRenderer> ().material.mainTexture as Texture2D;
    // 【备份墙的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>】
    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") {
                // 得到打击点的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>UV坐标
                // UV坐标是当前<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>宽高的百分比【左下角(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);
                        // <a href="https://www.jb51.cc/tag/xiugai/" target="_blank" class="keywords">修改</a>弹痕位置的像素为两图的融合颜色,若不相乘融合会使用弹痕原图
                        NewWallTexture.SetPixel ((int)w,(int)h,wallColor * bulletColor);
                    }
                }
                // 应用<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>
                NewWallTexture.Apply ();
                Invoke ("ReturnWall",3f);
            }
        }
    }
}

void ReturnWall()
{
    // 还原以打击点为原点的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>像素点
    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,wallColor);
        }
    }
    NewWallTexture.Apply ();
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐