3d black and white line graffiti drawings
Unity3D FreeDraw Drawing Line Graffiti Clear Undo Total part
tags: Drawing Unity3d line drawing Graffiti
This article is based on the Unity3D free plugin FreeDraw
i. Mainly two lists, i record has been drawn, one record is to be rolled back
Directly copy, press the mouse push button, the A D push is used.
using System.Collections; using Organisation.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; namespace FreeDraw { [RequireComponent(typeof(SpriteRenderer))] [RequireComponent(typeof(Collider2D))] // REQUIRES A COLLIDER2D to function // 1. Attach this to a read/write enabled sprite image // 2. Set the drawing_layers to use in the raycast // 3. Attach a 2D collider (like a Box Collider 2D) to this sprite // 4. Hold down left mouse to describe on this texture! /* 1. Append this to a sprite image with read/write enabled / ii. Set the drawing layer to utilise in ray casting / / 3. Attach a 2D collider (similar a box collider second) to this sprite / / 4. Concur downwardly the left mouse push button to describe this texture! */ public class Drawable : MonoBehaviour { // castor color Public static Color Pen_Colour = Colour.cherry-red; // Change these settings to change the default graphics settings // brush width (really radius, in pixels) public static int Pen_Width = 3; public delegate void Brush_Function(Vector2 world_position); //This is the office that is called when a left click occurs. //Enter your own custom castor to modify the brush type / / Fix the default function in the wake method public Brush_Function current_brush; public LayerMask Drawing_Layers; // The canvas is reset to a color of 0000 each time Public Color Reset_Colour = new Colour(0, 0, 0, 0); // By default, reset the sail to transparent // used to reference this particular file without setting all methods to static public static Drawable drawable; // The read/write office must be set in Unity'due south file editor Sprite drawable_sprite; Texture2D drawable_texture; Vector2 previous_drag_position; Color[] clean_colours_array; //all cases Color transparent; Color32[] cur_colors; //The current position where y'all need to change the color Color32[] start_colors; //Initial blank screen, used for emptying processing bool mouse_was_previously_held_down = faux; bool no_drawing_on_current_drag = fake; void Awake() { drawable = this; // Ready the default brush here current_brush = PenBrush; drawable_sprite = this.GetComponent<SpriteRenderer>().sprite; drawable_texture = drawable_sprite.texture; // Initialize the clean pixels to use clean_colours_array = new Colour[(int)drawable_sprite.rect.width * (int)drawable_sprite.rect.height]; for (int x = 0; x < clean_colours_array.Length; x++) clean_colours_array[x] = Reset_Colour; // Reset the canvas image every time information technology is turned on ResetCanvas(); start_colors = drawable_texture.GetPixels32(); } // The default castor blazon. At that place are widths and colors. / / Pass a indicate in world coordinates / / Change the pixels effectually the world to a static pen color public void PenBrush(Vector2 world_point) { / / Go the pixel corresponding to the world coordinates Vector2 pixel_pos = WorldToPixelCoordinates(world_point); Cur_colors = drawable_texture.GetPixels32(); //Get all values if (previous_drag_position == Vector2.zero) { // The first click on the line, the starting point is in the mouse MarkPixelsToColour(pixel_pos, Pen_Width, Pen_Colour); } else { // The second frame starts from the starting point to the position where the mouse moves ColourBetween(previous_drag_position, pixel_pos, Pen_Width, Pen_Colour); } ApplyMarkedPixelChanges(); previous_drag_position = pixel_pos; } // UI uses a helper method to ready the brush the user wants //Create a new brush for any new brushes you lot implement public void SetPenBrush() { // PenBrush is the proper name of the method to set as the current brush current_brush = PenBrush; } individual bool isDraw = false; //////////////////////update//////////////////////////////////////// / / Detect the time the user clicks, and then call the corresponding office void Update() { // Is the user holding downwardly the left mouse button? bool mouse_held_down = Input.GetMouseButton(0); if (mouse_held_down && !no_drawing_on_current_drag) { // Catechumen mouse coordinates to globe coordinates Vector2 mouse_world_position = Camera.principal.ScreenToWorldPoint(Input.mousePosition); // check if the electric current mouse position overlaps with our epitome Collider2D hit = Physics2D.OverlapPoint(mouse_world_position, Drawing_Layers.value); if (hit != nix && hit.transform != nix) { //We are using the texture nosotros are cartoon! / / Apply any function of the electric current brush isDraw = true; current_brush(mouse_world_position); } else { // no more than the border texture previous_drag_position = Vector2.zero; if (!mouse_was_previously_held_down) { // This is a new elevate, the user left click on the sail / / Make sure not to depict before starting a new drag no_drawing_on_current_drag = truthful; } } } // If the mouse is released else if (!mouse_held_down) { previous_drag_position = Vector2.zero; no_drawing_on_current_drag = faux; } mouse_was_previously_held_down = mouse_held_down; // press the left mouse button to draw a line and tape if (Input.GetMouseButtonUp(0)) { if (isDraw) { / / Record data when the mouse is released Record(); isDraw = false; } } // hind legs if (Input.GetKeyDown(KeyCode.A)) { RemoveLastColor(); } //become ahead if (Input.GetKeyDown(KeyCode.D)) { RestoreBackColor(); } } public void Tape() { Last = drawable_texture.GetPixels32(); //record where the electric current film line is recordColors.Add(last); RestoreColors.Clear(); } Private Color32[] last = new Color32[1]; //The terminal i is used for undo List<Color32[]> recordColors = new List<Color32[]>(1); // Color32[] backColors = new Color32[i] ; //Restore before undoing Private Listing<Color32[]> RestoreColors = new List<Color32[]>(1); //Restore earlier resuming /// <summary> /// Undo the last 1 /// </summary> public void RemoveLastColor() { if (recordColors.Count == 0) render; Debug.Log("Number of Undo" + recordColors.Count); if (isOnClane == simulated) RestoreColors.Add(drawable_texture.GetPixels32()); // Add to restore record before deleting again 0 -1 -2 else isOnClane = false; If (recordColors.Count > ane) // There is a blank in the default (start to add). The first i is recorded from the second one. { int alphabetize = recordColors.Count - 2; Color32[] color = recordColors[index]; drawable_texture.SetPixels32(colour); drawable_texture.Utilise(); / / Delete the final line // concluding = recordColors[recordColors.Count - 1]; final = drawable_texture.GetPixels32(); } else if (recordColors.Count == 1) { drawable_texture.SetPixels32(start_colors); drawable_texture.Apply(); } recordColors.RemoveAt(recordColors.Count - 1); } /// <summary> /// Restore /// </summary> public void RestoreBackColor() { if (RestoreColors.Count <= 0) return; Debug.Log("Restored:" + RestoreColors.Count); / / Get the line of the electric current picture int alphabetize = RestoreColors.Count - one; Color32[] color = RestoreColors[alphabetize]; //Apply recovery 0 - 1 -2 drawable_texture.SetPixels32(colour); drawable_texture.Apply(); Terminal = drawable_texture.GetPixels32(); // Become the recorded value earlier restoring / / After the restore, y'all need to add together the recorded data one time, added to the restored screen recordColors.Add(last); RestoreColors.RemoveAt(RestoreColors.Count - ane); Debug.Log("Add together to Undo after restore:" + recordColors.Count); //last = RestoreColors[recordColors.Count - 1]; } Individual bool isOnClane = false; //whether pressed /// <summary> /// Clear the tape data of the confront panel /// </summary> public void Clane_LastColor() { drawable_texture.SetPixels32(start_colors); drawable_texture.Utilize(); if (!recordColors.Contains(start_colors)) { recordColors.Add(start_colors); } Debug.Log("later on immigration" + recordColors.Count); isOnClane = true; RestoreColors.Clear(); } // Set the color of the pixel to a line from the start point to the end point to ensure that everything in the middle is colored. public void ColourBetween(Vector2 start_point, Vector2 end_point, int width, Colour color) { // distance from beginning to finish float distance = Vector2.Distance(start_point, end_point); Vector2 direction = (start_point - end_point).normalized; Vector2 cur_position = start_point; // Calculate how many times should be inserted betwixt the offset and end points based on the amount of fourth dimension elapsed since the terminal update float lerp_steps = 1 / distance; for (float lerp = 0; lerp <= ane; lerp += lerp_steps) { cur_position = Vector2.Lerp(start_point, end_point, lerp); MarkPixelsToColour(cur_position, width, color); } } /// <summary> /// Mark pixels every bit color /// </summary> /// <param proper noun="center_pixel">commencement point to cease point</param> /// <param proper noun="pen_thickness">width of the brush</param> /// <param name="color_of_pen">the colour of the brush</param> public void MarkPixelsToColour(Vector2 center_pixel, int pen_thickness, Color color_of_pen) { // Calculate how many pixels we need to color in each direction (x and y). int center_x = (int)center_pixel.x; int center_y = (int)center_pixel.y; //int extra_radius = Mathf.Min(0, pen_thickness - ii); for (int x = center_x - pen_thickness; x <= center_x + pen_thickness; 10++) { // Check if X wraps around the image so we don't draw pixels on the other side of the epitome. if (ten >= (int)drawable_sprite.rect.width || x < 0) continue; for (int y = center_y - pen_thickness; y <= center_y + pen_thickness; y++) { If(y!=1800) //If you lot exceed 1080, you don't have to change it. MarkPixelToChange(x, y, color_of_pen); } } } /// <summary> /// Get the pixel to mark the modify /// </summary> /// <param proper noun="x"></param> /// <param name="y"></param> /// <param proper noun="color"></param> public void MarkPixelToChange(int x, int y, Color color) { // demand to convert the x and y coordinates to the airplane coordinates of the array int array_pos = y * (int)drawable_sprite.rect.width + 10; // cheque if this location is valid if (array_pos > cur_colors.Length || array_pos < 0) render; cur_colors[array_pos] = color; } /// <summary> /// Pixel changes to the app tag /// </summary> public void ApplyMarkedPixelChanges() { drawable_texture.SetPixels32(cur_colors); drawable_texture.Apply(); } /// <summary> /// earth to pixel coordinates /// </summary> /// <param name="world_position"></param> /// <returns></returns> public Vector2 WorldToPixelCoordinates(Vector2 world_position) { // Change the coordinates to the local coordinates of this image Vector3 local_pos = transform.InverseTransformPoint(world_position); // change these to pixel coordinates float pixelWidth = drawable_sprite.rect.width; bladder pixelHeight = drawable_sprite.rect.pinnacle; float unitsToPixels = pixelWidth / drawable_sprite.premises.size.x * transform.localScale.x; //Demand to center our coordinates bladder centered_x = local_pos.x * unitsToPixels + pixelWidth / ii; float centered_y = local_pos.y * unitsToPixels + pixelHeight / two; // round the current mouse position to the nearest pixel Vector2 pixel_pos = new Vector2(Mathf.RoundToInt(centered_x), Mathf.RoundToInt(centered_y)); render pixel_pos; } /// <summary> /// Reset role changes each pixel to a reset color /// </summary> public void ResetCanvas() { drawable_texture.SetPixels(clean_colours_array); drawable_texture.Apply(); RestoreColors.Clear(); recordColors.Clear(); } // Colour the pixels directly. This method is slower than using markpixelstocolour and so using applymarkedpixelchanges // setpixels32 is much faster than setpixel // based on the heart thickness (pen radius), the number of pixels around the center pixel and the center pixel public void ColourPixels(Vector2 center_pixel, int pen_thickness, Colour color_of_pen) { // Calculate how many pixels nosotros need to color in each direction (10 and y). int center_x = (int)center_pixel.10; int center_y = (int)center_pixel.y; //int extra_radius = Mathf.Min(0, pen_thickness - ii); for (int ten = center_x - pen_thickness; x <= center_x + pen_thickness; x++) { for (int y = center_y - pen_thickness; y <= center_y + pen_thickness; y++) { drawable_texture.SetPixel(x, y, color_of_pen); } } drawable_texture.Apply(); } private void OnDestroy() { ResetCanvas(); } } }
Intelligent Recommendation
Line drawing function line() in matlab
The line() function draws some auxiliary lines in the canvas. line([starting abscissa, end point abscissa], [starting point ordinate, finish point ordinate]), Line([one,two],[3,iv]) volition draw a line from (1,3...
More Recommendation
Canvas realizes graffiti function
This article mainly implements the canvas graffiti part, including graffiti, forrad and astern, clearing the canvass and modifying the brush size. Realization thought The main purpose is to monitor...
Undo and disengage functions of canvass drawing in HTML5
> Implementation principle: Salvage Snapshot: Salvage a canvas snapshot every time you consummate a drawing operationcanvasHistory Assortment (generating a snapshot using canvastoDataURL() Method, the generated...
drawing a line function to achieve unity
See fast hardware, so they reproduced, and intrusion deleted In: Today, with the realization of a component implementation LineRender drawing functions. Online have been related to Bowen, feeling some...
(Reprinted) The simplest line drawing office
The simplest line drawing function forward from:http://www.codeios.com/thread-863-1-one.html The example is very unproblematic. First, there must be a UIImageView, declared equally a member var...
Copyright DMCA © 2018-2022 - All Rights Reserved - www.programmersought.com User Notice
Source: https://programmersought.com/article/12001477072/
0 Response to "3d black and white line graffiti drawings"
Post a Comment