Edwarddv

samplers

Oct 9th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. //so for example, the texture sampler is this
  2.  
  3.     public static Color SampleTextureColour(RaycastHit hit) // Creates a material instance wtf?
  4.     {
  5.         Renderer rend = hit.transform.GetComponent<Renderer>();
  6.         MeshCollider meshCollider = hit.collider as MeshCollider;
  7.         Color colour = Color.white;
  8.  
  9.         if (rend == null || rend.sharedMaterial == null || meshCollider == null)
  10.             return colour;
  11.  
  12.         if (rend.material.HasProperty("_ColourTint"))
  13.             colour = rend.material.GetColor("_ColourTint");
  14.  
  15.         return colour;
  16.  
  17.     }
  18.  
  19. //and the vertex colour sampler is this
  20.  
  21.     public static Color SampleVertexColour(RaycastHit hit) //creates a mesh instance wtf??
  22.     {
  23.         Transform objectHit = hit.transform;
  24.  
  25.         MeshCollider hitCollider = objectHit.GetComponent<MeshCollider>();
  26.         if (hitCollider == null)
  27.             return Color.grey;
  28.  
  29.         MeshFilter meshFilter = objectHit.GetComponent<MeshFilter>();
  30.         if (meshFilter == null)
  31.             return Color.grey;
  32.  
  33.         Mesh _mesh = meshFilter.mesh;
  34.  
  35.         if (_mesh == null)
  36.             return Color.grey;
  37.  
  38.         Renderer _renderer = objectHit.GetComponent<Renderer>();
  39.  
  40.         if (!_renderer.isPartOfStaticBatch)
  41.         {
  42.  
  43.             int[] triangles = _mesh.triangles;
  44.             Color[] hitColors = _mesh.colors;
  45.  
  46.             if (hitColors.Length != 0)
  47.             {
  48.                 //Vertex indices
  49.                 int t1 = triangles[(hit.triangleIndex * 3) + 0];
  50.                 int t2 = triangles[(hit.triangleIndex * 3) + 1];
  51.                 int t3 = triangles[(hit.triangleIndex * 3) + 2];
  52.  
  53.                 //Colors
  54.                 Color c1 = hitColors[t1];
  55.                 Color c2 = hitColors[t2];
  56.                 Color c3 = hitColors[t3];
  57.  
  58.                 Vector3 b = hit.barycentricCoordinate;
  59.  
  60.                 var sampleColor = c1 * b.x + c2 * b.y + c3 * b.z;
  61.  
  62.                 return sampleColor;
  63.             }
  64.  
  65.         }
  66.         return Color.grey;
  67.  
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment