玻璃杯中的剪辑..。 2017/7/14 13:47:14
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CleanTire : MonoBehaviour
{
public Texture2D CleanTexture;
Texture2D tmpTexture;
int Width;
int Height;
Vector3[] Vertices;
Vector2[] UV;
Material mat;
// Use this for initialization
void Start () {
mat = GetComponent<Renderer>().material;
Texture2D OriginalTexture = (Texture2D)mat.mainTexture;
Width=OriginalTexture.width;
Height=OriginalTexture.height;
tmpTexture = new Texture2D(Width, Height);
for(int i=0;i<Width;i++)
for(int j=0;j<Height;j++)
{
tmpTexture.SetPixel(i, j, OriginalTexture.GetPixel(i, j));
}
Vertices = GetComponent<MeshFilter>().mesh.vertices;
UV = GetComponent<MeshFilter>().mesh.uv;
}
// Update is called once per frame
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.name ==gameObject.name)
{
Vector3 hitPoint = hit.point;
// mark.transform.position = hitPoint;
int maxX = 0; int maxY = 0;
int minX = 9000; int minY = 9000;
for (int i = 0; i < Vertices.Length; i++)
{
float dis = Vector3.Distance(transform.TransformPoint(Vertices[i]), hitPoint);
if (dis < 0.1f)
{
int x = (int)(UV[i].x * Width);
int y = (int)(UV[i].y * Height);
if (x > maxX) maxX = x; else if (x < minX) minX = x;
if (y > maxY) maxY = y; else if (y < minY) minY = y;
tmpTexture.SetPixel(x, y, CleanTexture.GetPixel(x,y));
//Vertices[i] = OriginalVertices[i] + Normals[i]*0.2f;
}
//else
//Vertices[i] = OriginalVertices[i];
}
// m_Mesh.vertices = Vertices;
// int count = 0;
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
if (i >= minX && i <=maxX && j >= minY && j <=maxY)
{
tmpTexture.SetPixel(i, j, CleanTexture.GetPixel(i,j));
// count++;
}
}
// if (count > 40000) break;
}
tmpTexture.Apply();
mat.mainTexture = tmpTexture;
}
}
}
}
网友评论