Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- // Attach your source texture to the sourceImage, set the from and to colors and tolerance you want,
- // and then click on the texture in play mode at the start point you want to fill from
- // Don't forget you can use the color pickers in the inspector to make color selection easier
- public class TestFloodFill : MonoBehaviour
- {
- public Texture2D sourceImage;
- public Color32 fromColor = Color.black;
- public Color32 toColor = Color.white;
- public int colorTolerance;
- public bool preserveGradientsWithTolerance = true;
- private GameObject _plane;
- private MeshRenderer _renderer;
- private Collider _collider;
- private Camera _cam;
- private void Start ()
- {
- // Find the scene main camera for later world space conversion
- _cam = Camera.main;
- if (_cam == null)
- {
- Debug.LogError ("Must have a main camera in the scene");
- return;
- }
- // Move it up from zero position
- _cam.transform.position = Vector3.up * 10f;
- // Create a plane on the fly
- _plane = GameObject.CreatePrimitive (PrimitiveType.Plane);
- _renderer = _plane.GetComponent<MeshRenderer> ();
- _renderer.material.shader = Shader.Find ("Unlit/Texture");
- _collider = _plane.GetComponent<Collider> ();
- // Rotate the plane to line up with the camera look later
- _plane.transform.Rotate (0f, 180f, 0f);
- // Use a reusable method to reset the copy of the image back to the one in inspector
- ResetImageToOriginal ();
- // Now point the camera down to look at the plane
- _cam.transform.LookAt (_plane.transform);
- }
- private void Update ()
- {
- // Check for the R key to reset the image
- if (Input.GetKeyDown (KeyCode.R))
- {
- ResetImageToOriginal ();
- }
- // Check for the left-click on the mouse
- if (!Input.GetButtonDown ("Fire1"))
- {
- return;
- }
- Vector3 mousePosition = Input.mousePosition;
- Ray screenPosition = _cam.ScreenPointToRay (mousePosition);
- if (!_collider.Raycast (screenPosition, out RaycastHit hit, 1000))
- {
- return;
- }
- // This is pretty complex maths and I won't try to explain it - it basically works out
- // what proportion of the plane is covered by the screen, and then turns the hit point
- // from the raycast into the source image proportions
- Vector3 hitPoint = _cam.WorldToScreenPoint (hit.point);
- Vector3 planeMin = _cam.WorldToScreenPoint (_renderer.bounds.min);
- Vector3 planeMax = _cam.WorldToScreenPoint (_renderer.bounds.max);
- float xProportion = Mathf.InverseLerp (planeMin.x, planeMax.x, hitPoint.x);
- float yProportion = Mathf.InverseLerp (planeMin.y, planeMax.y, hitPoint.y);
- float xPoint = xProportion * sourceImage.width;
- float yPoint = yProportion * sourceImage.height;
- Vector2 startPosition = new Vector2 (xPoint, yPoint);
- // And call the flood fill!
- // We call it with our copy though, so we can make repeated changes
- // Note that we are casting the material's Texture to the required Texture2D - they are essentially the same,
- // but Texture2D lets us see the color array data whereas Texture doesn't
- Texture2D result = ImageFunctions.FloodFill (
- (Texture2D) _renderer.material.mainTexture,
- startPosition,
- fromColor,
- toColor,
- colorTolerance,
- preserveGradientsWithTolerance);
- // Now we put the result back onto the UI image's texture
- _renderer.material.mainTexture = result;
- }
- private void ResetImageToOriginal ()
- {
- // Set a copy of the the original texture to that image
- // This is so we can modify the copy and leave the original untouched so it can be restored
- int sourceImageWidth = sourceImage.width;
- int sourceImageHeight = sourceImage.height;
- // This first line creates a blank texture of the right settings
- Texture2D copyTexture = new Texture2D (sourceImageWidth, sourceImageHeight, sourceImage.format, false);
- // And then this line copies the actual pixel data from the source image to the copy
- Graphics.CopyTexture (sourceImage, 0, 0, copyTexture, 0, 0);
- // Now we assign the copy to the UI image's material to show it on screen
- _renderer.material.mainTexture = copyTexture;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment