Exemplo n.º 1
0
    private void paintOnTexture()
    {
        Event      e = Event.current;
        RaycastHit hit;
        Ray        ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.name == target.name)
            {
                float xCenterNormalized = hit.textureCoord.x;
                float yCenterNormalized = hit.textureCoord.y;

                int     size      = 64 * (int)brushSize;
                int     num       = Mathf.FloorToInt(xCenterNormalized * mixMap.width);
                int     num2      = Mathf.FloorToInt(yCenterNormalized * mixMap.height);
                int     num3      = Mathf.RoundToInt((float)size) / 2;
                int     num4      = Mathf.RoundToInt((float)size) % 2;
                int     x         = Mathf.Clamp(num - num3, 0, mixMap.width - 1);
                int     y         = Mathf.Clamp(num2 - num3, 0, mixMap.height - 1);
                int     num7      = Mathf.Clamp((num + num3) + num4, 0, mixMap.width);
                int     num8      = Mathf.Clamp((num2 + num3) + num4, 0, mixMap.height);
                int     width     = num7 - x;
                int     height    = num8 - y;
                Color[] srcPixels = mixMap.GetPixels(x, y, width, height, 0);
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        Color targetColor = currentTextureBrush.GetColor(currentSelectedTexture);
                        int   ix          = (x + j) - ((num - num3) + num4);
                        int   iy          = (y + i) - ((num2 - num3) + num4);
                        int   index       = (i * width) + j;
                        float blendFactor = currentTextureBrush.GetStrengthInt(ix / (int)brushSize, iy / (int)brushSize) *
                                            hardness;
                        srcPixels[index] = Color.Lerp(srcPixels[index], targetColor, blendFactor);
                    }
                }

                mixMap.SetPixels(x, y, width, height, srcPixels, 0);
            }
            mixMap.Apply();
        }
    }