Exemplo n.º 1
0
    private void Update()
    {
        // Pencil/eraser switching
        if (Input.GetKeyDown(KeyCode.P))
        {
            SetEraserEnabled(false);
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            SetEraserEnabled(true);
        }

        if (canDraw)
        {
            // Need to keep track of dragging to make sure lerping isn't done between penup/pendown
            if (Input.GetMouseButtonUp(0))
            {
                isDragging = false;
            }

            if (Input.GetMouseButton(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    var pallet = hit.collider.GetComponent <PaintCanvas>();
                    if (pallet != null)
                    {
                        // Check to make sure the canvas was clicked
                        Renderer     rend         = hit.transform.GetComponent <Renderer>();
                        MeshCollider meshCollider = hit.collider as MeshCollider;

                        if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
                        {
                            return;
                        }

                        Texture2D tex     = rend.material.mainTexture as Texture2D;
                        Vector2   pixelUV = hit.textureCoord;

                        pixelUV.x *= tex.width;
                        pixelUV.y *= tex.height;

                        Color currColor = new Color(0, 0, 0, 0);

                        if (!eraserEnabled)
                        {
                            currColor = GameObject.FindObjectOfType <ColorPicker>().currColor;
                        }

                        int currSize = GameObject.FindObjectOfType <BrushPicker>().brushSize;
                        if (!isDragging)
                        {
                            prevPos = pixelUV;
                        }
                        else
                        {
                            paintCanvas.ColorBetween(prevPos, pixelUV, currColor, currSize);
                        }

                        paintCanvas.BrushAreaWithColor(pixelUV, currColor, currSize);
                        prevPos    = pixelUV;
                        isDragging = true;
                    }
                }
            }
        }
    }