{"id":2129,"date":"2013-08-05T19:41:01","date_gmt":"2013-08-05T16:41:01","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=2129"},"modified":"2013-08-06T03:42:40","modified_gmt":"2013-08-06T00:42:40","slug":"fov-using-recursive-shadowcasting","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2013\/08\/05\/fov-using-recursive-shadowcasting\/","title":{"rendered":"FOV Using Recursive Shadowcasting"},"content":{"rendered":"<p><a title=\"http:\/\/unitycoder.com\/upload\/demos\/FOVShadowcasting1\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/FOVShadowcasting1\/\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2130\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2013\/08\/05\/fov-using-recursive-shadowcasting\/rogue_fov_shadow_casting_algorithm_unity_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_1.jpg?fit=578%2C561&amp;ssl=1\" data-orig-size=\"578,561\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"rogue_fov_shadow_casting_algorithm_unity_1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_1.jpg?fit=300%2C291&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_1.jpg?fit=578%2C561&amp;ssl=1\" class=\"alignnone size-full wp-image-2130\" alt=\"rogue_fov_shadow_casting_algorithm_unity_1\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_1.jpg?resize=578%2C561\" width=\"578\" height=\"561\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_1.jpg?w=578&amp;ssl=1 578w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_1.jpg?resize=300%2C291&amp;ssl=1 300w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/a><\/p>\n<p>Converted this script to unity: <a title=\"http:\/\/roguebasin.roguelikedevelopment.org\/index.php?title=FOV_using_recursive_shadowcasting\" href=\"http:\/\/roguebasin.roguelikedevelopment.org\/index.php?title=FOV_using_recursive_shadowcasting\" target=\"_blank\">FOV Using Recursive Shadowcasting<\/a>, for a quick test..<\/p>\n<p><strong>Instructions:<\/strong><br \/>\n&#8211; Attach the script to a plane<br \/>\n&#8211; use Unlit\/Texture shader as material (then no need lights..)<br \/>\n&#8211; Move main camera so that it sees the plane<\/p>\n<p><strong>TODO:<\/strong><br \/>\n&#8211; Use this to create <a title=\"http:\/\/unitycoder.com\/blog\/2012\/02\/15\/raycast-realtime-visibility-2-0\/\" href=\"http:\/\/unitycoder.com\/blog\/2012\/02\/15\/raycast-realtime-visibility-2-0\/\" target=\"_blank\">Visibility\/Shadow Mesh<\/a><\/p>\n<p><strong><br \/>\nWebplayer:<br \/>\n<\/strong><a title=\"http:\/\/unitycoder.com\/upload\/demos\/FOVShadowcasting1\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/FOVShadowcasting1\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/FOVShadowcasting1\/<br \/>\n<strong><br \/>\n<\/strong><\/a><\/p>\n<p><strong>Source:<\/strong> (C# &#8220;FovShadow.cs&#8221;)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\n\/\/ Original Sources: http:\/\/roguebasin.roguelikedevelopment.org\/index.php?title=FOV_using_recursive_shadowcasting\r\n\/\/ Converted to Unity by http:\/\/unitycoder.com\/blog\/\r\n\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class FovShadow : MonoBehaviour {\r\n\r\nprivate Texture2D map;\r\nprivate RaycastHit hit;\r\nprivate int size = 256;\r\nint&#x5B;]&#x5B;] multipliers = new int&#x5B;4]&#x5B;];\r\nColor&#x5B;] pix;\r\nColor&#x5B;] emptyMap;\r\n\r\nvoid Start ()\r\n{\r\nmap = new Texture2D(size, size, TextureFormat.ARGB32, false);\r\nrenderer.material.mainTexture = map;\r\nrenderer.material.mainTexture.filterMode = FilterMode.Point;\r\nfor (int y=0;y&lt;map.height;y++)\r\n{\r\nfor (int x=0;x&lt;map.width;x++)\r\n{\r\nif (x==0 || y==0 || x==map.width-1 || y==map.height-1)\r\n{\r\nmap.SetPixel(x,y,Color.white);\r\n}else{\r\nfloat p = Mathf.PerlinNoise(x*0.05f,y*0.05f);\r\nif (p&gt;0.5f)\r\n{\r\nmap.SetPixel(x,y,Color.white);\r\n}else{\r\nmap.SetPixel(x,y,Color.black);\r\n}\r\n}\r\n}\r\n}\r\nmap.Apply(false);\r\nemptyMap = map.GetPixels();\r\npix = map.GetPixels();\r\nmultipliers&#x5B;0] = new&#x5B;]{1, 0, 0, -1, -1, 0, 0, 1};\r\nmultipliers&#x5B;1] = new&#x5B;]{0, 1, -1, 0, 0, -1, 1, 0};\r\nmultipliers&#x5B;2] = new&#x5B;]{0, 1, 1, 0, 0, -1, -1, 0};\r\nmultipliers&#x5B;3] = new&#x5B;]{1, 0, 0, 1, -1, 0, 0, -1};\r\n}\r\n\r\nvoid Update ()\r\n{\r\nmap.SetPixels(emptyMap);\r\nif (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit))\r\n{\r\nVector2 pixelUV = hit.textureCoord;\r\npixelUV.x *= map.width;\r\npixelUV.y *= map.height;\r\ndo_fov(map, (int)pixelUV.x, (int)pixelUV.y, 64);\r\nmap.Apply(false);\r\n}\r\n}\r\n\r\nvoid cast_light(Texture2D map, int x, int y, int radius, int row,float start_slope, float end_slope, int xx, int xy, int yx,\u00a0\u00a0 \u00a0int yy)\r\n{\r\nif (start_slope &lt; end_slope) {\r\nreturn;\r\n}\r\nfloat next_start_slope = start_slope;\r\nfor (int i = row; i &lt;= radius; i++)\r\n{\r\nbool blocked = false;\r\nfor (int dx = -i, dy = -i; dx &lt;= 0; dx++)\r\n{\r\nfloat l_slope = (dx - 0.5f) \/ (dy + 0.5f);\r\nfloat r_slope = (dx + 0.5f) \/ (dy - 0.5f);\r\nif (start_slope &lt; r_slope) {\r\ncontinue;\r\n} else if (end_slope &gt; l_slope) {\r\nbreak;\r\n}\r\nint sax = dx * xx + dy * xy;\r\nint say = dx * yx + dy * yy;\r\nif ((sax &lt; 0 &amp;&amp; (int)Mathf.Abs(sax) &gt; x) ||(say &lt; 0 &amp;&amp; (int)Mathf.Abs(say) &gt; y))\r\n{\r\ncontinue;\r\n}\r\nint ax = x + sax;\r\nint ay = y + say;\r\nif (ax &gt;= map.width || ay &gt;= map.height)\r\n{\r\ncontinue;\r\n}\r\nint radius2 = radius * radius;\r\nif ((int)(dx * dx + dy * dy) &lt; radius2)\r\n{\r\nmap.SetPixel(ax, ay, Color.yellow);\r\n}\r\nif (blocked)\r\n{\r\nif (pix&#x5B;ax+ay*size].r&gt;0)\r\n{\r\nnext_start_slope = r_slope;\r\ncontinue;\r\n} else {\r\nblocked = false;\r\nstart_slope = next_start_slope;\r\n}\r\n} else if (pix&#x5B;ax+ay*size].r&gt;0)\r\n{\r\nblocked = true;\r\nnext_start_slope = r_slope;\r\ncast_light(map, x, y, radius, i + 1, start_slope, l_slope, xx,xy, yx, yy);\r\n}\r\n}\r\nif (blocked) {break;}\r\n}\r\n}\r\n\r\nvoid do_fov(Texture2D map, int x, int y, int radius)\r\n{\r\nfor (int i = 0; i &lt; 8; i++)\r\n{\r\ncast_light(map, x, y, radius, 1, 1.0f, 0.0f, multipliers&#x5B;0]&#x5B;i],multipliers&#x5B;1]&#x5B;i], multipliers&#x5B;2]&#x5B;i], multipliers&#x5B;3]&#x5B;i]);\r\n}\r\n}\r\n}\r\n\r\n<\/pre>\n<p>&#8212;<br \/>\n<strong>Image#2:<\/strong> Testing GL for drawing visibility blocking mesh (left side: red area is not visible, right side: full map)<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2136\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2013\/08\/05\/fov-using-recursive-shadowcasting\/rogue_fov_shadow_casting_algorithm_unity_fog_of_war_gl\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL.jpg?fit=680%2C334&amp;ssl=1\" data-orig-size=\"680,334\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL.jpg?fit=300%2C147&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL.jpg?fit=680%2C334&amp;ssl=1\" class=\"alignnone size-full wp-image-2136\" alt=\"rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL.jpg?resize=680%2C334\" width=\"680\" height=\"334\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_fog_of_war_GL.jpg?resize=300%2C147&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converted this script to unity: FOV Using Recursive Shadowcasting, for a quick test.. Instructions: &#8211; Attach the script to a plane &#8211; use Unlit\/Texture shader as material (then no need lights..) &#8211; Move main camera so that it sees the plane TODO: &#8211; Use this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2130,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,3],"tags":[65,401,7,526,354,52,466,40,172],"class_list":["post-2129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-unity3d","tag-2d","tag-dungeon","tag-fake","tag-fov","tag-grid","tag-light","tag-roguelike","tag-shadow","tag-visibility"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2013\/08\/rogue_fov_shadow_casting_algorithm_unity_1.jpg?fit=578%2C561&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-yl","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/2129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=2129"}],"version-history":[{"count":6,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/2129\/revisions"}],"predecessor-version":[{"id":2137,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/2129\/revisions\/2137"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/2130"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=2129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=2129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=2129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}