Unity签名(一) 书写
生活随笔
收集整理的這篇文章主要介紹了
Unity签名(一) 书写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
白嫖于一個未知來源插件,插件獲取方式加群:
QQ群:782263379
軟件主要代碼注釋解讀
Go
導航
- 軟件主要代碼注釋解讀
- Go
- 前言
- 一、實現了哪些功能?
- 二、項目環境
- Unity-2018.3.8f1
- 1、導入插件:
- 2、找到Signature場景雙擊運行可看效果
- 3、查看主要代碼 Painting.cs
- 4、變量參數
- 5、主要代碼實現--通過2階貝塞爾曲線和3階貝塞爾曲線來實現毛筆的效果
- 6、將上面計算好的坐標點利用DrawBrush()函數,實現顯示
- 7、畫布的清除功能
- 結:基礎功能完事兒----->下一站[Unity簽名(二)重放+撤銷](https://blog.csdn.net/weixin_39635848/article/details/108668377)
前言
注意:接下來的關于簽名插件的書寫、重寫、更換筆體等功能均來自未知來源的插件,本章就是對源代碼的學習
提示:以下是本篇文章正文內容,下面案例可供參考
一、實現了哪些功能?
基礎功能:書寫+重寫+保存
擴展功能:重放+撤回+顯示
二、項目環境
Unity-2018.3.8f1
1、導入插件:
2、找到Signature場景雙擊運行可看效果
3、查看主要代碼 Painting.cs
4、變量參數
BgRawImage:字的背景圖片,也就是圖中粉色的BG;
TexRender :是指畫布,也就是簽名將簽在這上面;后面的保存會用到這個組;
MaterialTemp:寫字用到的材質球;
BrushTypeTexture:筆刷的樣式,決定于不同的Texture;
Text:是用來更新顯示保存成功;
BrushColor:筆刷顏色;
Raw:撤回是需要顯示的圖片;
Num:筆刷的密度;
RawRoot:保存后顯示的圖片
5、主要代碼實現–通過2階貝塞爾曲線和3階貝塞爾曲線來實現毛筆的效果
/// <summary>/// 二階貝塞爾曲線/// </summary>/// <param name="pos"></param>/// <param name="distance"></param>public void TwoOrderBézierCurse(Vector3 pos, float distance){PositionArray[a] = pos;a++;if (a == 3){for (int index = 0; index < num; index++){Vector3 middle = (PositionArray[0] + PositionArray[2]) / 2;PositionArray[1] = (PositionArray[1] - middle) / 2 + middle;float t = (1.0f / num) * index / 2;Vector3 target = Mathf.Pow(1 - t, 2) * PositionArray[0] + 2 * (1 - t) * t * PositionArray[1] +Mathf.Pow(t, 2) * PositionArray[2];float deltaSpeed = (float)(distance - lastDistance) / num;DrawBrush(texRender, (int)target.x, (int)target.y, brushTypeTexture, brushColor, SetScale(lastDistance + (deltaSpeed * index)));}PositionArray[0] = PositionArray[1];PositionArray[1] = PositionArray[2];a = 2;}else{DrawBrush(texRender, (int)endPosition.x, (int)endPosition.y, brushTypeTexture,brushColor, brushScale);}}/// <summary>/// 三階貝塞爾曲線,獲取連續4個點坐標,通過調整中間2點坐標,畫出部分(我使用了num/1.5實現畫出部分曲線)來使曲線平滑;通過速度控制曲線寬度。/// </summary>/// <param name="pos"></param>/// <param name="distance"></param>/// <param name="targetPosOffset"></param>private void ThreeOrderBézierCurse(Vector3 pos, float distance, float targetPosOffset){//記錄坐標PositionArray1[b] = pos;b++;//記錄速度speedArray[s] = distance;s++;if (b == 4){Vector3 temp1 = PositionArray1[1];Vector3 temp2 = PositionArray1[2];//修改中間兩點坐標Vector3 middle = (PositionArray1[0] + PositionArray1[2]) / 2;PositionArray1[1] = (PositionArray1[1] - middle) * 1.5f + middle;middle = (temp1 + PositionArray1[3]) / 2;PositionArray1[2] = (PositionArray1[2] - middle) * 2.1f + middle;for (int index1 = 0; index1 < num / 1.5f; index1++){float t1 = (1.0f / num) * index1;Vector3 target = Mathf.Pow(1 - t1, 3) * PositionArray1[0] +3 * PositionArray1[1] * t1 * Mathf.Pow(1 - t1, 2) +3 * PositionArray1[2] * t1 * t1 * (1 - t1) + PositionArray1[3] * Mathf.Pow(t1, 3);//float deltaspeed = (float)(distance - lastDistance) / num;//獲取速度差值(存在問題,參考)float deltaspeed = (float)(speedArray[3] - speedArray[0]) / num;//float randomOffset = Random.Range(-1/(speedArray[0] + (deltaspeed * index1)), 1 / (speedArray[0] + (deltaspeed * index1)));//模擬毛刺效果float randomOffset = Random.Range(-targetPosOffset, targetPosOffset);DrawBrush(texRender, (int)(target.x + randomOffset), (int)(target.y + randomOffset), brushTypeTexture, brushColor, SetScale(speedArray[0] + (deltaspeed * index1)));}PositionArray1[0] = temp1;PositionArray1[1] = temp2;PositionArray1[2] = PositionArray1[3];speedArray[0] = speedArray[1];speedArray[1] = speedArray[2];speedArray[2] = speedArray[3];b = 3;s = 3;}else{DrawBrush(texRender, (int)endPosition.x, (int)endPosition.y, brushTypeTexture,brushColor, brushScale);}}6、將上面計算好的坐標點利用DrawBrush()函數,實現顯示
void DrawBrush(RenderTexture destTexture, int x, int y, Texture sourceTexture, Color color, float scale){DrawBrush(destTexture, new Rect(x, y, sourceTexture.width, sourceTexture.height), sourceTexture, color, scale);}void DrawBrush(RenderTexture destTexture, Rect destRect, Texture sourceTexture, Color color, float scale){float left = destRect.xMin - destRect.width * scale / 2.0f;float right = destRect.xMin + destRect.width * scale / 2.0f;float top = destRect.yMin - destRect.height * scale / 2.0f;float bottom = destRect.yMin + destRect.height * scale / 2.0f;Graphics.SetRenderTarget(destTexture);GL.PushMatrix();GL.LoadOrtho();materialTemp.SetTexture("_MainTex", brushTypeTexture);materialTemp.SetColor("_Color", color);materialTemp.SetPass(0);GL.Begin(GL.QUADS);GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(left / Screen.width, top / Screen.height, 0);GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(right / Screen.width, top / Screen.height, 0);GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(right / Screen.width, bottom / Screen.height, 0);GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(left / Screen.width, bottom / Screen.height, 0);GL.End();GL.PopMatrix();}7、畫布的清除功能
/// <summary>/// 重繪/// </summary>/// <param name="destTexture"></param>void Clear(RenderTexture destTexture){Graphics.SetRenderTarget(destTexture);GL.PushMatrix();//GL.Clear(true, true, Color.white);GL.Clear(true, true, new Color(0f, 0f, 1f, 0f));GL.PopMatrix();texRender = new RenderTexture(Screen.width / 2, Screen.height / 2, 24, RenderTextureFormat.ARGB32);}結:基礎功能完事兒----->下一站Unity簽名(二)重放+撤銷
總結
以上是生活随笔為你收集整理的Unity签名(一) 书写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: excel平均值公式_推荐一款多人同时编
- 下一篇: MACD背离指标公式