{"id":2177,"date":"2017-10-14T02:12:00","date_gmt":"2017-10-13T18:12:00","guid":{"rendered":"http:\/\/www.u3d8.com\/?p=2177"},"modified":"2020-10-14T10:25:32","modified_gmt":"2020-10-14T02:25:32","slug":"unity-mathf%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=2177","title":{"rendered":"Unity Mathf\u8be6\u89e3"},"content":{"rendered":"\n<h4>Ceil\u5411\u4e0a\u53d6\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u5411\u4e0a\u53d6\u503c\uff0c\u5411\u5927\u53d6\u503c\n        Debug.Log(Mathf.Ceil(0.1f)); \/\/1\n        Debug.Log(Mathf.Ceil(0.9f));\/\/1\n        Debug.Log(Mathf.Ceil(-0.1f));\/\/0\n        Debug.Log(Mathf.Ceil(-0.9f));\/\/0<\/code><\/pre>\n\n\n\n<h4>Floor\u5411\u4e0b\u53d6\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u5411\u4e0b\u53d6\u503c\uff0c\u5411\u5c0f\u53d6\u503c\n        Debug.Log(Mathf.Floor(0.1f)); \/\/0\n        Debug.Log(Mathf.Floor(0.9f));\/\/0\n        Debug.Log(Mathf.Floor(-0.1f));\/\/-1\n        Debug.Log(Mathf.Floor(-0.9f));\/\/-1<\/code><\/pre>\n\n\n\n<h4>Round\u56db\u820d\u4e94\u5165<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u56db\u820d\u4e94\u5165\n        Debug.Log(Mathf.Round(0.1f)); \/\/0\n        Debug.Log(Mathf.Round(0.9f));\/\/1\n        Debug.Log(Mathf.Round(-0.1f));\/\/0\n        Debug.Log(Mathf.Round(-0.9f));\/\/-1\n\n        \/\/\u5982\u679c \u9047\u5230 0.5 \u65f6\u5019\u4f1a\u4e0d\u4e00\u6837\uff0c \u7ed3\u679c\u770b\u524d\u4e00\u4f4d\n        \/\/ \u6b63\u6570  \u5076\u6570 -->  -0.5\n        \/\/ \u8d1f\u6570  \u5076\u6570 -->  +0.5\n        \/\/ \u6b63\u6570  \u5947\u6570 -->  +0.5\n        \/\/ \u8d1f\u6570  \u5947\u6570 -->  -0.5\n        Debug.Log(Mathf.Round(0.5f)); \/\/1\n        Debug.Log(Mathf.Round(1.5f));\/\/2\n        Debug.Log(Mathf.Round(-0.5f));\/\/0\n        Debug.Log(Mathf.Round(-1.5f));\/\/-2<\/code><\/pre>\n\n\n\n<h4>Camp\u9650\u5236<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/Camp\uff08value\uff0c min \uff0cmax\uff09\n        \/\/\u9650\u5236\uff1a\u9650\u5236value\u7684\u503c\u5728min \u548c max\u4e4b\u95f4\uff0c\u5982\u679cvalue\u5c0f\u4e8emin\uff0c\u8fd4\u56demin\u3002\n        \/\/\u5982\u679cvalue\u5927\u4e8emax\uff0c\u8fd4\u56demax\uff0c\u8fd4\u56demax\uff0c\u5426\u5219\u8fd4\u56devalue\n        Debug.Log(Mathf.Clamp(12, 10, 20));\/\/12\n        Debug.Log(Mathf.Clamp(5, 10, 20));\/\/10\n        Debug.Log(Mathf.Clamp(25, 10, 20));\/\/20\n        \n        \/\/\u9650\u5236 0-1 \u4e4b\u95f4\uff0c\u5982\u679c\u5c0f\u4e8e0\u8fd4\u56de0 \uff0c\u5982\u679c\u5927\u4e8e1\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56devalue\n        Debug.Log(Mathf.Clamp01(0.1f));\/\/0.1\n        Debug.Log(Mathf.Clamp01(-0.1f));\/\/0\n        Debug.Log(Mathf.Clamp01(2f));\/\/1<\/code><\/pre>\n\n\n\n<h4>Lerp\u63d2\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u63d2\u503c\n        \/\/\u7b2c\u4e09\u4e2a\u53c2\u6570t \uff1a\u8868\u793a\u4e00\u4e2a\u767e\u5206\u6570\uff0c0-1\uff0c\u5982\u679c t= 0.5f\uff0c\u90a3\u4e48\u8fd4\u56de\u503c\u5c31\u4ece50%\u5f00\u59cb\n        \/\/1.\u7b2c\u4e09\u4e2a\u53c2\u6570\u5982\u679c\u662f\u56fa\u5b9a\u503c\uff0c\u5219\u8fd4\u56de\u503c\u662f\u56fa\u5b9a\u503c\u6839\u636e\u53c2\u6570\u5927\u5c0f\u800c\u6539\u53d8\n        \/\/2.\u7b2c\u4e09\u4e2a\u53c2\u6570\u5fc5\u987b\u662f0-1\u4e4b\u95f4\u5982\u679c &lt;= 0 ,\u8fd4\u56de\u7b2c\u4e00\u53c2\u6570\u503c\uff0c\u5982\u679c\u53c2\u6570 >= 1 \u8fd4\u56de\u7b2c2\u4e2a\u53c2\u6570\n        \/\/Mathf.Lerp(a, b, c)\n        \/\/\u539f\u7406 \u8fd4\u56de\u503c =  \uff08b - a\uff09*c + a \uff1b\n        Debug.Log(Mathf.Lerp(1, 100, Time.time));\n        Debug.Log(Mathf.Lerp(1, 100, 0.5f));\n        \/\/\u7269\u4f53\u5300\u901f\u8fd0\u52a8\n        target.transform.position = new Vector3(Mathf.Lerp(0, 15, Time.time), 0, 0);\n\n        \/\/Mathf.LerpAngle(10, 100, Time.time);\n        target.transform.eulerAngles = new Vector3(0, Mathf.LerpAngle(10, 100, Time.time), 0);<\/code><\/pre>\n\n\n\n<h4>InverseLerp\u53cd\u63d2\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u53cd\u63d2\u503c\n        Debug.Log(Mathf.InverseLerp(10, 100, Time.time));<\/code><\/pre>\n\n\n\n<h4>SmoothDamp\u963b\u5c3c\u8fd0\u52a8<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u963b\u5c3c\u8fd0\u52a8\n        float target = 5f;\n        float curretVeloctity = 0f; \/\/\u5f53\u524d\u901f\u5ea6\n        float smothTime = 1f;\/\/\u5e73\u6ed1\u7cfb\u6570 = 1s\n        float mManSpeed = Mathf.Infinity; \/\/x\u8f74\u7684\u6700\u5927\u901f\u5ea6\n        this.transform.position = new Vector3(\n            Mathf.SmoothDamp(transform.position.x, target, ref curretVeloctity, smothTime, mManSpeed),\n            transform.position.y,\n            transform.position.z\n            );<\/code><\/pre>\n\n\n\n<h4>Repeat<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u8fd4\u56de\u503c 0-3 \u4e0d\u505c\u7684\u91cd\u590d\n        float f = Mathf.Repeat(Time.time, 3);\n        transform.localScale = Vector3.one * f;<\/code><\/pre>\n\n\n\n<h4>PingPong<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u8fd4\u56de\u503c 0 - 3- 0 - 3 \u5f80\u8fd4\u7684\u8fd4\u56de\u503c\n        float f2 = Mathf.PingPong(Time.time, 3);\n        transform.localScale = Vector3.one * f2;<\/code><\/pre>\n\n\n\n<h4>\u989c\u8272\u66f2\u7ebf<\/h4>\n\n\n\n<p>\u89c6\u89c9\u6548\u679c\u4e0a\u00a0GammaSpace\u00a0\u56fe\u663e\u793a\u6548\u679c\u8981\u597d\uff0cLinearSpace\u00a0\u8981\u6bd4\u00a0GammmaSpace\u00a0\u66f4\u5bb9\u6613\u8ba1\u7b97\u3002\u56e0\u6b64\uff0c\u00a0\u5f80\u5f80\u5904\u7406\u989c\u8272\u65b9\u9762\u4fe1\u606f\u65f6\uff0c\u00a0\u9700\u8981\u7528\u6240\u8bb2\u76842\u4e2a\u8f6c\u6362\u51fd\u6570\u628aGamma\u00a0Space\u00a0->Linear\u00a0Space\uff0c\u7136\u540e\u5bf9Linear\u00a0Space\u8fdb\u884c\u8ba1\u7b97\u540e\uff0c\u518d\u8f6c\u6362\u4e3aGammaSpace<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/\u989c\u8272\u7684\u6539\u53d8\u662f\u4e00\u4e2a\u66f2\u7ebf\u7684\u589e\u957f\n        Mathf.GammaToLinearSpace();\n        \/\/\u989c\u8272\u7684\u589e\u957f\u662f\u7ebf\u6027\u7684\u589e\u957f\n        Mathf.LinearToGammaSpace();<\/code><\/pre>\n\n\n\n<h4>PerlinNoise\u566a\u58f0\u56fe<\/h4>\n\n\n\n<p>\u8fd4\u56de\u7684\u968f\u673a\u6570\u4e4b\u95f4\u4e0d\u4f1a\u6709\u592a\u5927\u7684\u53d8\u5316\uff0c\u800c\u662f\u6709\u4e00\u5b9a\u7684\u8fc7\u6e21\u6548\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public Material mat;\n    public Vector3 color;\n\n    void Start()\n    {\n        mat = gameObject.GetComponent&lt;MeshRenderer>().material;\n        color = new Vector3(\n            Random.Range(0f, 1f),\n             Random.Range(0f, 1f),\n              Random.Range(0f, 1f));\n    }\n\n    void Update()\n    {\n        float cx = Mathf.PerlinNoise(Time.time, color.x);\n        float cy = Mathf.PerlinNoise(Time.time, color.y);\n        float cz = Mathf.PerlinNoise(Time.time, color.z);\n\n        mat.color = new Color(cx, cy, cz);\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ceil\u5411\u4e0a\u53d6\u503c Floor\u5411\u4e0b\u53d6\u503c Round\u56db\u820d\u4e94\u5165 Camp\u9650\u5236 Lerp &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=2177\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1664,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,397],"tags":[],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2177"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2177"}],"version-history":[{"count":2,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2177\/revisions"}],"predecessor-version":[{"id":2179,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2177\/revisions\/2179"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/media\/1664"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2177"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}