Change size according to pressure (Apple Pencil)

As you know unity already have api to take pressure value from the device. You can find it here

So it's not a big deal to bind this value to the line thickness or maybe line transparency.

Currently this property is not available on the point or brush context. but you can use following script to modify Base point scale value.

using NodeInspector;
using PaintCraft.Utils;
using UnityEngine;

namespace PaintCraft.Tools.Filters.PropertyBinders{


    [NodeMenuItem("PropertyBinders/BindScaleToPressure")]
    public class BindScaleToPressure : FilterWithNextNode
    {    
        [EnumFlags]
        public PointType PointType = PointType.BasePoint | PointType.InterpolatedPoint;

        public AnimationCurve Curve = new AnimationCurve(){keys = new []{new Keyframe(0.0f, 0.5f), new Keyframe(1.0f, 0.8f)  }};

        #region implemented abstract members of FilterWithNextNode
        public override bool FilterBody (BrushContext brushLineContext)
        {
            int lineID = -1;
            foreach (var kvp in brushLineContext.SourceInputHandler.ContextByLineId)
            {
                if (kvp.Value == brushLineContext)
                {
                    lineID = kvp.Key;
                }
            }
            if (lineID != -1)
            {
                Touch targetTouch = new Touch();
                bool touchSet = false;
                foreach (Touch touch in Input.touches)
                {
                    if (touch.fingerId == lineID)
                    {
                        targetTouch = touch;
                        touchSet = true;
                        break;
                    }
                }

                if (touchSet)
                {
                    float finalPresure = Curve.Evaluate(targetTouch.pressure);
                    brushLineContext.BasePoints.Last.Value.PointColor.Alpha = finalPresure;
                }
            }

            return true;
        }
        #endregion
    }
}

after that just add this filter PropertyBinders/BindScaleToPressure to the tool what you like, but make sure that you don't use any filters which overwrite point scale

You can see that this filter also has a curve where you can adjust the target point scale according to the input value.

If you want to change alpha instead of scale you need to modify following line

brushLineContext.BasePoints.Last.Value.Scale = finalPresure;

to

brushLineContext.BasePoints.Last.Value.PointColor.Alpha = finalPresure;

Last updated