using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour {
public float allowance;
public Texture2D wallTexture;
private Texture2D tempTexture;
private float wall_height; // 【获图片宽高信息】
private float wall_width;
RaycastHit hitInfo;
void Start () {
wallTexture = GetComponent<MeshRenderer>().material.mainTexture as Texture2D;
wall_height = wallTexture.height;
wall_width = wallTexture.width;
tempTexture = Instantiate(wallTexture) as Texture2D;
GetComponent<MeshRenderer>().material.mainTexture = tempTexture;
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hitInfo))
{
//获取鼠标点击到的像素的颜色
Vector2 uv = hitInfo.textureCoord;
Color color = wallTexture.GetPixel((int)(uv.x*wall_width),(int)( uv.y*wall_height));
//遍历图像所有像素点
for (int i = 0; i < wall_width; i++)
{
for (int j = 0; j < wall_height; j++)
{
Color tcolor = wallTexture.GetPixel(i, j);
//计算容差
float r = color.r - tcolor.r;
float g = color.g - tcolor.g;
float b = color.b - tcolor.b;
float diff = Mathf.Sqrt(r * r + g * g + b * b)*255;
if (diff< allowance)
{
//改成透明
tempTexture.SetPixel(i, j, new Color(0, 0, 0, 0));
}
}
}
tempTexture.Apply();
}
}
}
}
版权声明:本文为weixin_43737001原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。