{"id":1552,"date":"2018-04-08T21:27:17","date_gmt":"2018-04-08T12:27:17","guid":{"rendered":"http:\/\/naochang.me\/?p=1552"},"modified":"2018-04-08T21:27:17","modified_gmt":"2018-04-08T12:27:17","slug":"%e9%9d%9e%e5%90%8c%e6%9c%9f%e5%8c%96%e3%81%97%e3%81%a6%e9%ab%98%e9%80%9f%e5%8c%96","status":"publish","type":"post","link":"https:\/\/stacstar.jp\/blog\/?p=1552","title":{"rendered":"\u975e\u540c\u671f\u5316\u3057\u3066\u9ad8\u901f\u5316"},"content":{"rendered":"<p>\u4eca\u56de\u753b\u50cf\u51e6\u7406\u304c\u3042\u308a\u3001\u305d\u306e\u9ad8\u901f\u5316\u306e\u305f\u3081Task\u3092\u4f7f\u3063\u3066\u975e\u540c\u671f\u5316\u3057\u307e\u3057\u305f\u3002<br \/>\n\u307e\u305a\u306f\u30ab\u30e1\u30e9\u306e\u753b\u50cf\u3092\u767d\u9ed2\u306b\u3059\u308b\u666e\u901a\u306e\u30b3\u30fc\u30c9\u3002\u3060\u3044\u305f\u3044\u50d5\u306ePC\u306720fps\u51fa\u308b\u304b\u51fa\u306a\u3044\u304f\u3089\u3044\u3002<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: unitycsharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections;\r\nusing UnityEngine;\r\n\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u767d\u9ed2\u30ab\u30e1\u30e9\u3002\r\n\/\/\/ &lt;\/summary&gt;\r\npublic class WBCamera : MonoBehaviour {\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30c6\u30af\u30b9\u30c1\u30e3\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tTexture2D texture;\r\n\tWebCamTexture webCamTex;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30a6\u30a7\u30d6\u30ab\u30e0\u306e\u5927\u304d\u3055\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tint webCamW;\r\n\tint webCamH;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30ab\u30e1\u30e9\u304c\u521d\u671f\u5316\u3055\u308c\u305f\u304b\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tbool cameraInitialized = false;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u5168\u30d4\u30af\u30bb\u30eb\u306e\u30ab\u30e9\u30fc\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tColor32&#x5B;] colors;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ Start.\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tvoid Start() {\r\n\t\ttry {\r\n\t\t\tthis.webCamTex = new WebCamTexture(WebCamTexture.devices&#x5B;0].name, 1920, 1080, 30);\r\n\t\t\tthis.webCamTex.Play();\r\n\t\t\tthis.GetComponent&lt;Renderer&gt;().material.mainTexture = this.webCamTex;\r\n\t\t}\r\n\t\tcatch(Exception e) {\r\n\t\t\tDebug.LogError(&quot;Camera didn't initialize. : &quot; + e.Message);\r\n\t\t}\r\n\r\n\t\tthis.StartCoroutine(this.WaitCameraInitializedCoroutine());\r\n\t}\r\n\t\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ Updatea.\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tvoid Update () {\r\n\t\tif (this.cameraInitialized) {\r\n\t\t\tColor32&#x5B;] webCamColors = this.webCamTex.GetPixels32();\r\n\t\t\tint index = 0;\r\n\r\n\t\t\tif (this.cameraInitialized) {\r\n\t\t\t\tfor (int x = 0; x &lt; this.webCamW; ++x) {\r\n\t\t\t\t\tfor (int y = 0; y &lt; this.webCamH; ++y) {\r\n\t\t\t\t\t\tindex = x + y * this.webCamW;\r\n\t\t\t\t\t\tColor32 webCamC = webCamColors&#x5B;index];\r\n\t\t\t\t\t\tbyte luminance = (byte)(0.298912f * webCamC.r + 0.586611 * webCamC.g + 0.114478 * webCamC.b);\r\n\t\t\t\t\t\tthis.colors&#x5B;index] = new Color32(luminance, luminance, luminance, 255);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tthis.texture.SetPixels32(this.colors);\r\n\t\t\tthis.texture.Apply();\r\n\t\t}\r\n\t}\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30ab\u30e1\u30e9\u306e\u521d\u671f\u5316\u3092\u5f85\u3064\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tIEnumerator WaitCameraInitializedCoroutine() {\r\n\t\twhile (true) {\r\n\t\t\tif (this.webCamTex.width &gt; 16 &amp;&amp; this.webCamTex.height &gt; 16) {\r\n\t\t\t\tprint(&quot;WebCam Initialized : (&quot; + this.webCamTex.width + &quot;, &quot; + this.webCamTex.height + &quot;)&quot;);\r\n\t\t\t\tthis.webCamW = this.webCamTex.width;\r\n\t\t\t\tthis.webCamH = this.webCamTex.height;\r\n\t\t\t\tthis.colors = new Color32&#x5B;this.webCamW * this.webCamH];\r\n\t\t\t\tthis.cameraInitialized = true;\r\n\t\t\t\tthis.texture = new Texture2D(this.webCamW, this.webCamH, TextureFormat.RGBA32, false);\r\n\t\t\t\tthis.GetComponent&lt;Renderer&gt;().material.mainTexture = this.texture;\r\n\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\r\n\t\t\tyield return null;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\u7d9a\u3044\u3066Task\u3092\u4f7f\u3063\u3066\u975e\u540c\u671f\u5316\u3002Task\u3092\u4f7f\u3046\u306b\u306f.Net4.6\u306b\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u304f\u3066\u3001\u305d\u306e\u4ed6\u3082\u308d\u3082\u308d\u8a73\u3057\u3044\u3053\u3068\u306f\u4e0b\u8a18\u3092\u53c2\u8003\u306b\u3002<br \/>\n<a href=\"https:\/\/qiita.com\/divideby_zero\/items\/b6bbcc35aef08e1d7d3e\" rel=\"noopener\" target=\"_blank\">Qiita | Unity2017\u3067\u59cb\u3081\u308bTask(async\uff5eawait)<\/a><\/p>\n<pre class=\"brush: unitycsharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Threading;\r\nusing System.Threading.Tasks;\r\nusing System.Collections;\r\nusing UnityEngine;\r\n\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ \u767d\u9ed2\u30ab\u30e1\u30e9\u3002\r\n\/\/\/ &lt;\/summary&gt;\r\npublic class WBCamera : MonoBehaviour {\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30c6\u30af\u30b9\u30c1\u30e3\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tTexture2D texture;\r\n\tWebCamTexture webCamTex;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30a6\u30a7\u30d6\u30ab\u30e0\u306e\u5927\u304d\u3055\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tint webCamW;\r\n\tint webCamH;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30ab\u30e1\u30e9\u304c\u521d\u671f\u5316\u3055\u308c\u305f\u304b\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tbool cameraInitialized = false;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u5168\u30d4\u30af\u30bb\u30eb\u306e\u30ab\u30e9\u30fc\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tColor32&#x5B;] webCamColors;\r\n\tColor32&#x5B;] colors;\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ Start.\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tvoid Start() {\r\n\t\ttry {\r\n\t\t\tthis.webCamTex = new WebCamTexture(WebCamTexture.devices&#x5B;0].name, 1920, 1080, 30);\r\n\t\t\tthis.webCamTex.Play();\r\n\t\t\tthis.GetComponent&lt;Renderer&gt;().material.mainTexture = this.webCamTex;\r\n\t\t}\r\n\t\tcatch(Exception e) {\r\n\t\t\tDebug.LogError(&quot;Camera didn't initialize. : &quot; + e.Message);\r\n\t\t}\r\n\r\n\t\tthis.StartCoroutine(this.WaitCameraInitializedCoroutine());\r\n\t}\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u767d\u9ed2\u306b\u5909\u63db\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tasync Task ConvertToBWAsync() {\r\n\t\tint index = 0;\r\n\t\tSynchronizationContext context = SynchronizationContext.Current;\r\n\r\n\t\tawait Task.Run(() =&gt; {\r\n\t\t\tif (this.cameraInitialized) {\r\n\t\t\t\tfor (int x = 0; x &lt; this.webCamW; ++x) {\r\n\t\t\t\t\tfor (int y = 0; y &lt; this.webCamH; ++y) {\r\n\t\t\t\t\t\tindex = x + y * this.webCamW;\r\n\t\t\t\t\t\tColor32 webCamC = webCamColors&#x5B;index];\r\n\t\t\t\t\t\tbyte luminance = (byte)(0.298912f * webCamC.r + 0.586611 * webCamC.g + 0.114478 * webCamC.b);\r\n\t\t\t\t\t\tthis.colors&#x5B;index] = new Color32(luminance, luminance, luminance, 255);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tcontext.Post(state =&gt; {\r\n\t\t\t\tif (this.texture != null) { \/\/ \u7834\u68c4\u3055\u308c\u305f\u969b\r\n\t\t\t\t\tthis.texture.SetPixels32(this.colors);\r\n\t\t\t\t\tthis.texture.Apply();\r\n\t\t\t\t\tthis.webCamColors = this.webCamTex.GetPixels32();\r\n\t\t\t\t\tthis.ConvertToBWAsync();\r\n\t\t\t\t}\r\n\t\t\t}, null);\r\n\t\t});\r\n\t}\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u30ab\u30e1\u30e9\u306e\u521d\u671f\u5316\u3092\u5f85\u3064\u3002\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tIEnumerator WaitCameraInitializedCoroutine() {\r\n\t\twhile (true) {\r\n\t\t\tif (this.webCamTex.width &gt; 16 &amp;&amp; this.webCamTex.height &gt; 16) {\r\n\t\t\t\tprint(&quot;WebCam Initialized : (&quot; + this.webCamTex.width + &quot;, &quot; + this.webCamTex.height + &quot;)&quot;);\r\n\t\t\t\tthis.webCamW = this.webCamTex.width;\r\n\t\t\t\tthis.webCamH = this.webCamTex.height;\r\n\t\t\t\tthis.colors = new Color32&#x5B;this.webCamW * this.webCamH];\r\n\t\t\t\tthis.webCamColors = this.webCamTex.GetPixels32();\r\n\t\t\t\tthis.cameraInitialized = true;\r\n\t\t\t\tthis.texture = new Texture2D(this.webCamW, this.webCamH, TextureFormat.RGBA32, false);\r\n\t\t\t\tthis.GetComponent&lt;Renderer&gt;().material.mainTexture = this.texture;\r\n\r\n\t\t\t\tthis.ConvertToBWAsync();\r\n\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\r\n\t\t\tyield return null;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Update\u304b\u3089\u306e\u5909\u63db\u3092\u3084\u3081\u3066\u3001Web\u30ab\u30e1\u30e9\u304c\u521d\u671f\u5316\u3055\u308c\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u975e\u540c\u671f\u3067\u884c\u308f\u308c\u308b\u5909\u63db\u51e6\u7406\u3092\u5b9f\u884c\u3002\u30d5\u30ec\u30fc\u30e0\u30ec\u30fc\u30c8\u81ea\u4f53\u306f50fps\u4ee5\u4e0a\u51fa\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002\u5b9f\u969b\u306e\u767d\u9ed2\u306e\u5909\u63db\u306f\u79d2\u959325\u56de\u7a0b\u5ea6\u3060\u3063\u305f\u306e\u3067\u3001\u767d\u9ed2\u306e\u5909\u63db\u306f\u591a\u5c11\u65e9\u304f\u306a\u3063\u305f\u7a0b\u5ea6\u3060\u3051\u3069\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u306e\u8db3\u3092\u3072\u3063\u3071\u308b\u3053\u3068\u304c\u306a\u304f\u306a\u3063\u305f\u306e\u304c\u5927\u304d\u3044\u3002<br \/>\n\u975e\u540c\u671f\u51e6\u7406\u306f\u3068\u3066\u3082\u96e3\u3057\u3044\u3088\u3046\u3060\u3051\u3069\u3001\u3042\u307e\u308a\u5168\u4f53\u306b\u5f71\u97ff\u3092\u53ca\u307c\u3055\u306a\u3044\u7a0b\u5ea6\u306e\u3053\u3068\u3067\u3042\u308c\u3070\u5272\u3068\u4fbf\u5229\u306b\u4f7f\u3048\u3066\u975e\u5e38\u306b\u3044\u307e\u3084\u3063\u3066\u3044\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u52a9\u3051\u3089\u308c\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u753b\u50cf\u51e6\u7406\u304c\u3042\u308a\u3001\u305d\u306e\u9ad8\u901f\u5316\u306e\u305f\u3081Task\u3092\u4f7f\u3063\u3066\u975e\u540c\u671f\u5316\u3057\u307e\u3057\u305f\u3002 \u307e\u305a\u306f\u30ab\u30e1\u30e9\u306e\u753b\u50cf\u3092\u767d\u9ed2\u306b\u3059\u308b\u666e\u901a\u306e\u30b3\u30fc\u30c9\u3002\u3060\u3044\u305f\u3044\u50d5\u306ePC\u306720fps\u51fa\u308b\u304b\u51fa\u306a\u3044\u304f\u3089\u3044\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[],"class_list":["post-1552","post","type-post","status-publish","format-standard","hentry","category-c","category-unity"],"_links":{"self":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1552"}],"version-history":[{"count":7,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1552\/revisions"}],"predecessor-version":[{"id":1560,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1552\/revisions\/1560"}],"wp:attachment":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}