The Swartz-Manning’s first exhibit will provide a detailed history of Aaron Swartz Day. https://www.aaronswartzday.org/vr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HueCube.cs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #if UNITY_EDITOR || UNITY_STANDALONE
  2. using UnityEngine;
  3. using System.Collections;
  4. using ProBuilder2.Common;
  5. using System.Collections.Generic;
  6. namespace ProBuilder2.Examples
  7. {
  8. /**
  9. * Creates a cube on start and colors it's vertices programmitically.
  10. */
  11. public class HueCube : MonoBehaviour
  12. {
  13. pb_Object pb;
  14. void Start()
  15. {
  16. // Create a new ProBuilder cube to work with.
  17. pb = pb_ShapeGenerator.CubeGenerator(Vector3.one);
  18. // Cycle through each unique vertex in the cube (8 total), and assign a color
  19. // to the index in the sharedIndices array.
  20. int si_len = pb.sharedIndices.Length;
  21. Color[] vertexColors = new Color[si_len];
  22. for(int i = 0; i < si_len; i++)
  23. {
  24. vertexColors[i] = HSVtoRGB( (i/(float)si_len) * 360f, 1f, 1f);
  25. }
  26. // Now go through each face (vertex colors are stored the pb_Face class) and
  27. // assign the pre-calculated index color to each index in the triangles array.
  28. Color[] colors = pb.colors;
  29. for(int CurSharedIndex = 0; CurSharedIndex < pb.sharedIndices.Length; CurSharedIndex++)
  30. {
  31. foreach(int CurIndex in pb.sharedIndices[CurSharedIndex].array)
  32. {
  33. colors[CurIndex] = vertexColors[CurSharedIndex];
  34. }
  35. }
  36. pb.SetColors(colors);
  37. // In order for these changes to take effect, you must refresh the mesh
  38. // object.
  39. pb.Refresh();
  40. }
  41. /**
  42. * Convert HSV to RGB.
  43. * http://www.cs.rit.edu/~ncs/color/t_convert.html
  44. * r,g,b values are from 0 to 1
  45. * h = [0,360], s = [0,1], v = [0,1]
  46. * if s == 0, then h = -1 (undefined)
  47. */
  48. static Color HSVtoRGB(float h, float s, float v )
  49. {
  50. float r, g, b;
  51. int i;
  52. float f, p, q, t;
  53. if( s == 0 ) {
  54. // achromatic (grey)
  55. return new Color(v, v, v, 1f);
  56. }
  57. h /= 60; // sector 0 to 5
  58. i = (int)Mathf.Floor( h );
  59. f = h - i; // factorial part of h
  60. p = v * ( 1 - s );
  61. q = v * ( 1 - s * f );
  62. t = v * ( 1 - s * ( 1 - f ) );
  63. switch( i )
  64. {
  65. case 0:
  66. r = v;
  67. g = t;
  68. b = p;
  69. break;
  70. case 1:
  71. r = q;
  72. g = v;
  73. b = p;
  74. break;
  75. case 2:
  76. r = p;
  77. g = v;
  78. b = t;
  79. break;
  80. case 3:
  81. r = p;
  82. g = q;
  83. b = v;
  84. break;
  85. case 4:
  86. r = t;
  87. g = p;
  88. b = v;
  89. break;
  90. default: // case 5:
  91. r = v;
  92. g = p;
  93. b = q;
  94. break;
  95. }
  96. return new Color(r, g, b, 1f);
  97. }
  98. }
  99. }
  100. #endif