Unity Lua 实现贝塞尔曲线

本文介绍了二次和三次贝塞尔曲线的计算方法,并提供了Lua语言实现的示例代码。通过线性插值和向量运算,可以高效地计算出贝塞尔曲线上的点。
--二次贝塞尔
function BulletSkill:GetBezierPoint(t, p0, p1, p2)
    local p0p1 = p0 * (1 - t) + p1 * t
    local p1p2 = p1 * (1 - t) + p2 * t
    local result = p0p1 * (1 - t) + p1p2 * t
    return result
end
--三次贝塞尔
function BulletSkill:GetTwoBezierPoint(t, p0, p1, p2,p3)
    local p0_1 = Vector3.Lerp(p0,p1,t)
    local p1_2 = Vector3.Lerp(p1,p2,t)
    local p2_3 = Vector3.Lerp(p2,p3,t)
    local p0_1_1_2 = Vector3.Lerp(p0_1,p1_2,t)
    local p1_2_2_3 = Vector3.Lerp(p1_2,p2_3,t)
    local p0_1_1_2_1_2_2_3 = Vector3.Lerp(p0_1_1_2,p1_2_2_3,t)
    return p0_1_1_2_1_2_2_3
end

 

从2阶到7阶的贝赛尔曲线 private static final int MAX_COUNT = 7; // 贝塞尔曲线最大阶数 private static final int REGION_WIDTH = 30; // 合法区域宽度 private static final int FINGER_RECT_SIZE = 60; // 矩形尺寸 private static final int BEZIER_WIDTH = 10; // 贝塞尔曲线线宽 private static final int TANGENT_WIDTH = 6; // 切线线宽 private static final int CONTROL_WIDTH = 12; // 控制点连线线宽 private static final int CONTROL_RADIUS = 12; // 控制点半径 private static final int TEXT_SIZE = 40; // 文字画笔尺寸 private static final int TEXT_HEIGHT = 60; // 文本高度 private static final int RATE = 10; // 移动速率 private static final int HANDLER_WHAT = 100; private static final int FRAME = 1000; // 1000帧 private static final String[] TANGENT_COLORS = {"#7fff00", "#7a67ee", "#ee82ee", "#ffd700", "#1c86ee", "#8b8b00"}; // 切线颜色 private static final int STATE_READY = 0x0001; private static final int STATE_RUNNING = 0x0002; private static final int STATE_STOP = 0x0004; private static final int STATE_TOUCH = 0x0010; private Path mBezierPath = null; // 贝塞尔曲线路径 private Paint mBezierPaint = null; // 贝塞尔曲线画笔 private Paint mMovingPaint = null; // 移动点画笔 private Paint mControlPaint = null; // 控制点画笔 private Paint mTangentPaint = null; // 切线画笔 private Paint mLinePaint = null; // 固定线画笔 private Paint mTextPointPaint = null; // 点画笔 private Paint mTextPaint = null; // 文字画笔 private ArrayList mBezierPoints = null; // 贝塞尔曲线点集 private PointF mBezierPoint = null; // 贝塞尔曲线移动点 private ArrayList mControlPoints = null; // 控制点集 private ArrayList<ArrayList<ArrayList>> mTangentPoints; // 切线点集 private ArrayList<ArrayList> mInstantTangentPoints; private int mR = 0; // 移动速率 private int mRate = RATE; // 速率 private int mState; // 状态 private boolean mLoop = false; // 设置是否循环 private boolean mTangent = true; // 设置是否显示切线 private int mWidth = 0, mHe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值