Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using System.IO;
  7.  
  8. public class UVMeshDeform : MonoBehaviour {
  9.  
  10. Texture2D deformer;
  11. public Material deformermat;
  12. public float midway;
  13. Vector2[] uvdeform;
  14. Vector2[] uvdest;
  15. public GameObject destination;
  16.  
  17. void Start()
  18. {
  19. Mesh oldmesh = GetComponent<MeshFilter>().sharedMesh;
  20. Mesh newmesh = Instantiate(oldmesh) as Mesh;
  21. Mesh destmesh = destination.GetComponent<MeshFilter>().sharedMesh;
  22. GetComponent<MeshFilter>().sharedMesh = newmesh;
  23. Vector3[] verticesold = newmesh.vertices;
  24. Vector3[] verticesnew = destmesh.vertices;
  25. uvdeform = newmesh.uv;
  26. deformer = new Texture2D(512,512,TextureFormat.RGBAFloat,false);
  27. for (int p = 0; p < uvdeform.Length; p++)
  28. {
  29. //Color pixels = deformer.GetPixelBilinear(uvdeform[p].x, uvdeform[p].y);
  30.  
  31. //verticesold[p].x += verticesnew[p].x - verticesold[p].x;
  32. //verticesold[p].y += verticesnew[p].y - verticesold[p].y;
  33. //verticesold[p].z += verticesnew[p].z - verticesold[p].z;
  34.  
  35. Color pixels = new Color((verticesnew[p].x - verticesold[p].x) + (midway / 255), (verticesnew[p].y - verticesold[p].y) + (midway / 255), (verticesnew[p].z - verticesold[p].z) + (midway / 255));
  36.  
  37. deformer.SetPixel(Mathf.RoundToInt(uvdeform[p].x * deformer.width), Mathf.RoundToInt(uvdeform[p].y * deformer.height), pixels);
  38.  
  39. verticesold[p].x += pixels.r - (midway / 255);
  40. verticesold[p].y += pixels.g - (midway / 255);
  41. verticesold[p].z += pixels.b - (midway / 255);
  42.  
  43. //print(pixels.r);
  44. }
  45. byte[] bytes = deformer.EncodeToPNG();
  46. File.WriteAllBytes(Application.dataPath + "SavedScreen.png", bytes);
  47. newmesh.vertices = verticesold;
  48. newmesh.RecalculateBounds();
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement