Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5.  
  6. public class PixelSetter : MonoBehaviour {
  7.  
  8. public Transform playerTransform;
  9.  
  10. Transform planeTransform;
  11. Color color;
  12. ParticleSystem ps;
  13. Texture2D texture;
  14.  
  15. int resolution = 400;
  16. int planeSize = 10;
  17. int paintRadius = 1;
  18. int floodSpeed = 20;
  19. float fillDelay = .9f;
  20. float drawDelay = .9f;
  21. bool fillMode = false;
  22. float scale;
  23.  
  24. struct Point
  25. {
  26. public int x, y;
  27. public Point(int px, int py)
  28. {
  29. x = px;
  30. y = py;
  31. }
  32. }
  33.  
  34. void Start()
  35. {
  36. GameObject go = GameObject.Find ("Particle System");
  37. ps = (ParticleSystem) go.GetComponent("ParticleSystem");;
  38. planeTransform = gameObject.transform;
  39.  
  40. scale = resolution / 50;
  41. color = Color.white;
  42.  
  43. var emission = ps.emission;
  44. emission.rateOverDistance = 0;
  45.  
  46. texture = new Texture2D(resolution, resolution,
  47. textureFormat: TextureFormat.RGBA32,
  48. mipChain: false);
  49. texture.filterMode = FilterMode.Point;
  50. GetComponent<Renderer>().material.mainTexture = texture;
  51.  
  52. for (var x = 0; x < resolution; x++) {
  53. for (var y = 0; y < resolution; y++) {
  54. texture.SetPixel (x, y, Color.white);
  55. }
  56. }
  57.  
  58. texture.Apply();
  59. }
  60.  
  61. // Called whenever the player collides with an ink monster.
  62. public void SetColor(Color newColor, Color particleColor) {
  63. // If the same color bug was consumed, increase the players line width
  64. if (newColor == color) {
  65. var em = ps.emission;
  66. em.rateOverDistance = 5 + paintRadius * 2;
  67. paintRadius++;
  68. }
  69.  
  70. //Otherwise, change the paint color and paint particles
  71. else {
  72. var emission = ps.emission;
  73. emission.rateOverDistance = 5;
  74.  
  75. color = newColor;
  76. ps.startColor = particleColor;
  77. paintRadius = 1;
  78. }
  79. }
  80.  
  81.  
  82. void Update() {
  83. StartCoroutine("DrawPoint");
  84.  
  85. if (Input.GetKeyDown("space"))
  86. {
  87. StartCoroutine("Fill");
  88. }
  89.  
  90. if (Input.GetKeyDown("e"))
  91. {
  92. StartCoroutine("Clear");
  93. }
  94.  
  95. // Saves the file
  96. if (Input.GetKeyDown ("f")) {
  97. byte[] bytes = texture.EncodeToPNG ();
  98. System.IO.File.WriteAllBytes (Application.dataPath + "/../image.png", bytes);
  99. }
  100.  
  101. }
  102.  
  103. // Constantly draws a trail behind the player
  104. IEnumerator DrawPoint() {
  105. Color newColor = color;
  106. int x = (resolution / 2 + (int)(scale * playerTransform.position.z));
  107. int y = (resolution/2 - (int)(scale * playerTransform.position.x));
  108.  
  109. // Delay the trail so it matches up with the particles.
  110. yield return new WaitForSeconds(drawDelay);
  111.  
  112. for(var x1 = x - paintRadius; x1 < x + paintRadius + 1; x1++) {
  113. for(var y1 = y - paintRadius; y1 < y + paintRadius + 1; y1++) {
  114. texture.SetPixel(x1, y1 , newColor);
  115. }
  116. }
  117.  
  118. texture.Apply();
  119. }
  120.  
  121. // Clears the floor texture by setting all pixels to white
  122. IEnumerator Clear() {
  123. for (var x = 0; x < resolution; x++) {
  124. for (var y = 0; y < resolution; y++) {
  125. texture.SetPixel (x, y, Color.white);
  126. }
  127. if (x % 10 == 0) yield return null;
  128. }
  129. }
  130.  
  131. // Flood fills the area after a provided delay.
  132. IEnumerator Fill() {
  133. yield return new WaitForSeconds(fillDelay);
  134.  
  135. ps.Emit (100);
  136.  
  137. int x = (resolution / 2 + (int)(scale * playerTransform.position.z));
  138. int y = (resolution/2 - (int)(scale * playerTransform.position.x));
  139.  
  140. Color newColor = color;
  141. Color oldColor = color;
  142.  
  143. Queue<Point> q = new Queue<Point> ();
  144. bool[,] visited = new bool[resolution, resolution];
  145. int numFilled = 0;
  146.  
  147. for(var x1 = x - paintRadius - 1; x1 < x + paintRadius + 2; x1++) {
  148. for(var y1 = y - paintRadius - 1; y1 < y + paintRadius + 2; y1++) {
  149. Color c = texture.GetPixel(x1, y1);
  150. if (!c.Equals(color)) {
  151. oldColor = c;
  152. q.Enqueue(new Point(x1, y1));
  153. }
  154. }
  155. }
  156.  
  157. // Using a DFS, flood fills all pixels that have the old color.
  158. while(q.Count > 0) {
  159. Point p = q.Dequeue();
  160. x = p.x;
  161. y = p.y;
  162. if(x >= 0 && x < resolution && y >= 0 && y < resolution) {
  163.  
  164. if(!visited[x,y]){
  165. visited[x,y] = true;
  166.  
  167. if(texture.GetPixel(x, y).Equals(oldColor)) {
  168. texture.SetPixel(x, y, newColor);
  169.  
  170. // Queue adjacent pixels to have their color changed
  171. q.Enqueue(new Point(x+1, y));
  172. q.Enqueue(new Point(x-1, y));
  173. q.Enqueue(new Point(x, y+1));
  174. q.Enqueue(new Point(x, y-1));
  175.  
  176. numFilled ++;
  177.  
  178. if(numFilled >= floodSpeed) {
  179. floodSpeed = (int) (floodSpeed * 1.1);
  180. texture.Apply();
  181. numFilled = 0;
  182. yield return null;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. texture.Apply();
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement