Guest User

Untitled

a guest
Jul 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1.  
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class WorldUvMapper : UvMapper
  6. {
  7.     /// <summary>
  8.     /// Determines the scale to be applied to the uv-coordinates. (1,1) will repeat the texture once for every local-space unit, (2,2) twice and so on.
  9.     /// </summary>
  10.     public Vector2 scale = Vector2.one;
  11.    
  12.     public override void Map(IList<Vector3> points, Vector3 planeNormal, out Vector4[] tangentsA, out Vector4[] tangentsB, out Vector2[] uvsA, out Vector2[] uvsB)
  13.     {
  14.         // Calculate texture direction vectors
  15.         Vector3 u = Vector3.Cross(planeNormal, Vector3.up);
  16.        
  17.         if (u == Vector3.zero)
  18.         {
  19.             u = Vector3.Cross(planeNormal, Vector3.forward);
  20.         }
  21.        
  22.         Vector3 v = Vector3.Cross(u, planeNormal);
  23.        
  24.         u.Normalize();
  25.         v.Normalize();
  26.        
  27.         // Set tangents
  28.         Vector4 tangentA = new Vector4(u.x, u.y, u.z, 1.0f);
  29.         Vector4 tangentB = new Vector4(u.x, u.y, u.z, -1.0f);
  30.        
  31.         tangentsA = new Vector4[points.Count];
  32.         tangentsB = new Vector4[points.Count];
  33.        
  34.         for (int i = 0; i < points.Count; i++)
  35.         {
  36.             tangentsA[i] = tangentA;
  37.             tangentsB[i] = tangentB;
  38.         }
  39.        
  40.         // Set uvs
  41.         Vector2[] uvs = new Vector2[points.Count];
  42.        
  43.         Vector2 min = Vector2.zero;
  44.        
  45.         for (int i = 0; i < points.Count; i++)
  46.         {
  47.             Vector3 point = points[i];
  48.            
  49.             uvs[i].x = Vector3.Dot(point, u);
  50.             uvs[i].y = Vector3.Dot(point, v);
  51.            
  52.             if (i == 0)
  53.             {
  54.                 min = uvs[i];
  55.             }
  56.             else
  57.             {
  58.                 min = Vector2.Min(uvs[i], min);
  59.             }
  60.         }
  61.        
  62.         for (int i = 0; i < points.Count; i++)
  63.         {
  64.             uvs[i] -= min;
  65.            
  66.             uvs[i].x *= scale.x;
  67.             uvs[i].y *= scale.y;
  68.         }
  69.        
  70.         uvsA = uvs;
  71.         uvsB = uvs;
  72.     }
  73. }
Add Comment
Please, Sign In to add comment