{"id":1664,"date":"2016-10-03T14:37:50","date_gmt":"2016-10-03T06:37:50","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1664"},"modified":"2016-10-03T17:31:04","modified_gmt":"2016-10-03T09:31:04","slug":"simple-drop-shadow-shader","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1664\/","title":{"rendered":"Simple drop shadow shader."},"content":{"rendered":"<p>just in case I forget how to do this shadow shader, and it also fix the repeat\/unlimit shadow length\u00a0projection issue.<\/p>\n<p>ref:\u00a0<a href=\"https:\/\/forum.unity3d.com\/threads\/add-one-pixel-border-to-a-rendertexture-result-in-shader.389121\/\">https:\/\/forum.unity3d.com\/threads\/add-one-pixel-border-to-a-rendertexture-result-in-shader.389121\/<\/a><\/p>\n<pre class=\"brush:ruby\">Shader \"Cg projector shader for drop shadows\" {\r\n\tProperties{\r\n\t\t_ShadowTex(\"Projected Image\", 2D) = \"white\" {}\r\n\t\t_ShadowStrength(\"Shadow Strength\", Float) = 0.65\r\n\t}\r\n\tSubShader{\r\n\t\tPass{\r\n\t\t\tZWrite off\r\n\t\t\tBlend Zero OneMinusSrcAlpha\r\n\t\t\tOffset -1, -1\r\n\t\t\tCGPROGRAM\r\n\t\t\t#pragma vertex vert\r\n\t\t\t#pragma fragment frag\r\n\r\n\t\t\t\/\/ User-specified properties\r\n\t\t\tuniform sampler2D _ShadowTex;\r\n\t\t\tuniform float _ShadowStrength;\r\n\t\t\t\/\/ Projector-specific uniforms\r\n\t\t\tuniform float4x4 unity_Projector; \/\/ UnityAPI : from object space to projector space\r\n\t\t\tstruct vertexInput {\r\n\t\t\t\tfloat4 vertex : POSITION;\r\n\t\t\t\tfloat3 normal : NORMAL;\r\n\t\t\t};\r\n\t\t\tstruct vertexOutput {\r\n\t\t\t\tfloat4 pos : SV_POSITION;\r\n\t\t\t\tfloat4 tex : TEXCOORD0;\r\n\t\t\t\t\/\/ position in projector space\r\n\t\t\t\t\/\/ From: http:\/\/en.wikibooks.org\/wiki\/Cg_Programming\/Unity\/Projectors\r\n\t\t\t};\r\n\t\t\t\r\n\t\t\tvertexOutput vert(vertexInput input) {\r\n\t\t\t\tvertexOutput output;\r\n\t\t\t\toutput.tex = mul(unity_Projector, input.vertex);\r\n\t\t\t\toutput.pos = mul(UNITY_MATRIX_MVP, input.vertex);\r\n\t\t\t\treturn output;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tfloat4 frag(vertexOutput input) : COLOR {\r\n\t\t\t\tif (input.tex.w &gt; 0.0) \/\/ in front of projector?\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/ Ref: https:\/\/forum.unity3d.com\/threads\/add-one-pixel-border-to-a-rendertexture-result-in-shader.389121\/\r\n\t\t\t\t\t\/\/ to improve the calculation speed, the if-else checking to locate the one-pixel border points on texture was combine into one checking.\r\n\t\t\t\t\t\/\/ by folding square texture coordinate in to triangle (math model)\r\n\t\t\t\t\t\/\/ ref. UV are alway between 0.0 ~ 1.0\r\n\t\t\t\t\t\/\/ e.g. for the point x=1.0, y=0.0\r\n\t\t\t\t\tfloat2 uvmasks = min(input.tex, 1.0 - input.tex); \/\/ min(Vector2(1.0,0.0), Vector2(0.0,-1.0))\r\n\r\n\t\t\t\t\t\/\/ that uvmasks will result the minimum value (0.0, -1.0); in this case it's smaller then (0.0, 0.0)\r\n\t\t\t\t\t\/\/ and then we take the point color based on the result is bigger then zero point.\r\n\t\t\t\t\t\/\/ smaller than zero will return the white color by OneMinusSrcAlpha(black)\r\n\t\t\t\t\treturn min(uvmasks.x, uvmasks.y) &lt; 0.0 ? float4(0.0, 0.0, 0.0, 0.0) : tex2D(_ShadowTex, input.tex.xy) *_ShadowStrength;\r\n\t\t\t\t}\r\n\t\t\t\telse \/\/ behind projector\r\n\t\t\t\t{\r\n\t\t\t\t\treturn float4(0.0, 0.0, 0.0, 0.0);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tENDCG\r\n\t\t}\r\n\t}\r\n\t\/\/ The definition of a fallback shader should be commented out \r\n\t\/\/ during development:\r\n\tFallback \"Projector\/Light\"\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>just in case I forget how to do this shadow shader &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88,11],"tags":[],"class_list":["post-1664","post","type-post","status-publish","format-standard","hentry","category-shader","category-unity3d"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1664"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1664\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}