JD_RotShader.shader 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. Shader "ChivaVR/JD_RotShader"
  2. {
  3. Properties{
  4. [Header(JD Config Fixed)]
  5. _RotLineLength("缠绕部分长度",Float) = 1
  6. _Width("缠绕部分宽度",Float) = 0.2
  7. _JDJuanLength("胶带卷长度",Float) = 0.1
  8. [Header(JD Control Parameter)]
  9. _Radius("缠绕半径",Float) = 0.2
  10. _MaxAngle("最大缠绕角度",Float) = 500
  11. _Value("Value",Float) = 720
  12. _BackAngle("伸出部分旋转角度",Range(0,180)) = 0
  13. [Header(JD Shape Parameter)]
  14. _MoveOffset("旋转偏移量",Float) = 0.3
  15. _DampOffsetAngle("不参与偏移角度",Range(0,450)) = 0
  16. _UpOffset("单侧高度偏移量",Float) = 0.1
  17. _Warp("Warp扭曲系数(默认0)",Range(-1,1)) = 0
  18. _WarpPos("WarpPos",Range(0,4)) = 0
  19. [Header(JD Line Parameter)]
  20. [Toggle]_HiddenLine("是否隐藏伸展部分",Float) = 0
  21. [Toggle]_HiddenJDJuan("是否隐藏胶带卷",Float) = 0
  22. _LineShowLength("连接部分长度",Float) = 0.2
  23. [Header(JD Texture)]
  24. _Color("Color", Color) = (0.5019608,0.5019608,0.5019608,1)
  25. _MainTex("胶带贴图", 2D) = "white" {}
  26. _BumpMap("胶带法线贴图", 2D) = "bump" {}
  27. _SecondTex("胶带卷贴图", 2D) = "bump" {}
  28. _SecondMap("胶带卷法线贴图", 2D) = "bump" {}
  29. _Metallic("Metallic", Range(0, 1)) = 0
  30. _Gloss("Gloss", Range(0, 1)) = 0.8
  31. }
  32. SubShader{
  33. Tags {
  34. "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"
  35. }
  36. Pass {
  37. Name "FORWARD"
  38. Tags {
  39. "LightMode" = "ForwardBase"
  40. }
  41. // 关闭深度写入
  42. //ZWrite Off
  43. // 开启混合模式,并设置混合因子为SrcAlpha和OneMinusSrcAlpha
  44. Blend SrcAlpha OneMinusSrcAlpha
  45. CGPROGRAM
  46. #pragma vertex vert
  47. #pragma fragment frag
  48. #define UNITY_PASS_FORWARDBASE
  49. #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
  50. #define _GLOSSYENV 1
  51. #include "UnityCG.cginc"
  52. #include "AutoLight.cginc"
  53. #include "Lighting.cginc"
  54. #include "UnityPBSLighting.cginc"
  55. #include "UnityStandardBRDF.cginc"
  56. #pragma multi_compile_fwdbase_fullshadows
  57. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  58. #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  59. #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  60. #pragma multi_compile_fog
  61. #pragma only_renderers d3d9 d3d11 glcore gles
  62. #pragma target 3.0
  63. #pragma shader_feature _HIDDENLINE_ON
  64. #pragma shader_feature _HIDDENJDJUAN_ON
  65. float _RotLineLength;
  66. float _Width;
  67. float _JDJuanLength;
  68. float _Radius;
  69. float _MaxAngle;
  70. float _Value;
  71. float _BackAngle;
  72. float _MoveOffset;
  73. float _DampOffsetAngle;
  74. float _UpOffset;
  75. float _Warp;
  76. float _WarpPos;
  77. float _HiddenLine;
  78. float _HiddenJDJuan;
  79. float _LineShowLength;
  80. uniform float4 _Color;
  81. uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
  82. uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST;
  83. uniform sampler2D _SecondTex; uniform float4 _SecondTex_ST;
  84. uniform sampler2D _SecondMap; uniform float4 _SecondMap_ST;
  85. uniform float _Metallic;
  86. uniform float _Gloss;
  87. struct VertexInput {
  88. float4 vertex : POSITION;
  89. float3 normal : NORMAL;
  90. float4 tangent : TANGENT;
  91. float2 texcoord0 : TEXCOORD0;
  92. float2 texcoord1 : TEXCOORD1;
  93. float2 texcoord2 : TEXCOORD2;
  94. };
  95. struct VertexOutput {
  96. float4 pos : SV_POSITION;
  97. float2 uv0 : TEXCOORD0;
  98. float2 uv1 : TEXCOORD1;
  99. float2 uv2 : TEXCOORD2;
  100. float4 posWorld : TEXCOORD3;
  101. float3 normalDir : TEXCOORD4;
  102. float3 tangentDir : TEXCOORD5;
  103. float3 bitangentDir : TEXCOORD6;
  104. float flag : TEXCOORD11;
  105. LIGHTING_COORDS(7,8)
  106. UNITY_FOG_COORDS(9)
  107. #if defined(LIGHTMAP_ON) || defined(UNITY_SHOULD_SAMPLE_SH)
  108. float4 ambientOrLightmapUV : TEXCOORD10;
  109. #endif
  110. };
  111. VertexOutput vert(VertexInput v) {
  112. VertexOutput o = (VertexOutput)0;
  113. float s;
  114. float c;
  115. v.vertex += float4(0, _Radius, 0, 0);
  116. _Value = clamp(_Value,0, _MaxAngle);
  117. //缠绕区域
  118. if ((v.vertex.x / _RotLineLength) < (_Value / _MaxAngle))
  119. {
  120. float pAngel = _MaxAngle / _RotLineLength;
  121. sincos(radians(v.vertex.x*pAngel % 360), s, c);
  122. float4x4 rotate = {
  123. c,s,0,0,
  124. -s,c,0,0,
  125. 0,0,1,0,
  126. 0,0,0,1 };
  127. float vertexZ = v.vertex.z;
  128. if (_Value < 0)
  129. {
  130. vertexZ = 0;
  131. }
  132. if (vertexZ < 0)
  133. {
  134. vertexZ = 0;
  135. }
  136. float warpY = _Warp * sin(_WarpPos + v.vertex.x / _RotLineLength);
  137. //偏移量控制
  138. if ((v.vertex.x / _RotLineLength * _MaxAngle) > _DampOffsetAngle)
  139. {
  140. v.vertex.z += clamp((v.vertex.x / _RotLineLength * _MaxAngle) - _DampOffsetAngle,0, _MaxAngle) / 360 * _MoveOffset;
  141. }
  142. if (v.vertex.x / _RotLineLength * _MaxAngle > 2)
  143. {
  144. v.vertex = mul(rotate, float3(0, -vertexZ / _Width / 2 * _UpOffset + v.vertex.y + warpY, v.vertex.z));
  145. }
  146. else
  147. {
  148. v.vertex = mul(rotate, float3(0, _Radius + warpY, v.vertex.z));
  149. }
  150. v.normal = normalize(mul(rotate,v.normal));
  151. v.tangent = normalize(mul(rotate, v.tangent));
  152. }
  153. else //伸出区域
  154. {
  155. sincos(radians(_Value % 360), s, c);
  156. float4x4 rotate = {
  157. c,s,0,0,
  158. -s,c,0,0,
  159. 0,0,1,0,
  160. 0,0,0,1 };
  161. float warpY = _Warp * sin(_WarpPos + (_Value / _MaxAngle)* _RotLineLength);
  162. float vertexZ = v.vertex.z;
  163. if (_Value < 0)
  164. {
  165. _Value = 0;
  166. }
  167. float3 currentPoint = mul(rotate, float3(0, _Radius + warpY, 0));
  168. float3 currentPointNormalize = normalize(float3(currentPoint.x, currentPoint.y, currentPoint.z));
  169. float3 dirNormail = normalize(cross(currentPointNormalize, float3(0, 0, 1)));
  170. float sC;
  171. float cC;
  172. sincos(radians(-_BackAngle), sC, cC);
  173. float4x4 rotateCAngle = {
  174. cC,sC,0,0,
  175. -sC,cC,0,0,
  176. 0,0,1,0,
  177. 0,0,0,1 };
  178. if (_Value > _DampOffsetAngle)
  179. {
  180. v.vertex.z += clamp(_Value - _DampOffsetAngle, 0, _Value) / 360 * _MoveOffset;
  181. }
  182. v.normal = normalize(mul(rotate, v.normal));
  183. v.tangent = normalize(mul(rotate, v.tangent));
  184. #if _HIDDENLINE_ON //隐藏伸出区域
  185. _HiddenLine = 1;
  186. v.vertex = float4(currentPoint
  187. + float3(0, 0, v.vertex.z)
  188. , 0);
  189. #else //显示伸出区域
  190. _HiddenLine = 0;
  191. o.flag = v.vertex.x;
  192. if ((_RotLineLength*((_MaxAngle - _Value) / _MaxAngle))*3.1415 > _LineShowLength)
  193. {
  194. if ((v.vertex.x - _RotLineLength) > 0)
  195. {
  196. #ifdef _HIDDENJDJUAN_ON
  197. _HiddenJDJuan = 1;
  198. v.vertex = float4(currentPoint
  199. + dirNormail * _LineShowLength
  200. + float3(0, 0, v.vertex.z)
  201. , 0);
  202. #else
  203. _HiddenJDJuan = 0;
  204. v.vertex = float4(currentPoint
  205. + dirNormail * (v.vertex.x - _RotLineLength + _LineShowLength)
  206. + float3(0, 0, v.vertex.z)
  207. + currentPointNormalize * (v.vertex.y - _Radius)
  208. , 0);
  209. #endif
  210. }
  211. else if ((v.vertex.x - (_Value / _MaxAngle)*_RotLineLength)*3.1415 < _LineShowLength)
  212. {
  213. v.vertex = float4(currentPoint
  214. + dirNormail * (v.vertex.x - (_Value / _MaxAngle)*_RotLineLength)*3.1415
  215. + float3(0, 0, v.vertex.z)
  216. + currentPointNormalize * (v.vertex.y - _Radius)
  217. , 0);
  218. }
  219. else
  220. {
  221. v.vertex = float4(currentPoint
  222. + dirNormail * _LineShowLength
  223. + float3(0, 0, v.vertex.z)
  224. + currentPointNormalize * (v.vertex.y - _Radius)
  225. , 0);
  226. }
  227. }
  228. else
  229. {
  230. #ifdef _HIDDENJDJUAN_ON
  231. _HiddenJDJuan = 1;
  232. v.vertex = float4(currentPoint
  233. + dirNormail * ((((_MaxAngle - _Value) / _MaxAngle)* _RotLineLength) / _JDJuanLength * _LineShowLength)
  234. + float3(0, 0, v.vertex.z)
  235. , 0);
  236. #else
  237. v.vertex = float4(currentPoint
  238. + dirNormail * (v.vertex.x - (_Value / _MaxAngle)* _RotLineLength)
  239. + float3(0, 0, v.vertex.z)
  240. + currentPointNormalize * (v.vertex.y - _Radius)
  241. , 0);
  242. #endif
  243. }
  244. #endif
  245. v.vertex = float4(mul(rotateCAngle, (v.vertex.rgb - currentPoint)) + currentPoint,0);
  246. v.normal = normalize(mul(rotateCAngle, v.normal));
  247. v.tangent = normalize(mul(rotateCAngle, v.tangent));
  248. }
  249. o.uv0 = v.texcoord0;
  250. o.uv1 = v.texcoord1;
  251. o.uv2 = v.texcoord2;
  252. #ifdef LIGHTMAP_ON
  253. o.ambientOrLightmapUV.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  254. o.ambientOrLightmapUV.zw = 0;
  255. #elif UNITY_SHOULD_SAMPLE_SH
  256. #endif
  257. #ifdef DYNAMICLIGHTMAP_ON
  258. o.ambientOrLightmapUV.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  259. #endif
  260. o.normalDir = UnityObjectToWorldNormal(v.normal);
  261. o.tangentDir = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz);
  262. o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  263. o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  264. float3 lightColor = _LightColor0.rgb;
  265. o.pos = UnityObjectToClipPos(v.vertex);
  266. UNITY_TRANSFER_FOG(o,o.pos);
  267. TRANSFER_VERTEX_TO_FRAGMENT(o)
  268. return o;
  269. }
  270. float4 frag(VertexOutput i) : COLOR {
  271. i.normalDir = normalize(i.normalDir);
  272. float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
  273. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  274. float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
  275. if (i.flag < _RotLineLength)
  276. {
  277. _BumpMap_var = UnpackNormal(tex2D(_BumpMap, TRANSFORM_TEX(i.uv0, _BumpMap)));
  278. }
  279. else
  280. {
  281. _BumpMap_var = UnpackNormal(tex2D(_SecondMap, TRANSFORM_TEX(i.uv0, _SecondMap)));
  282. }
  283. float3 normalLocal = _BumpMap_var.rgb;
  284. float3 normalDirection = normalize(mul(normalLocal, tangentTransform)); // Perturbed normals
  285. float3 viewReflectDirection = reflect(-viewDirection, normalDirection);
  286. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  287. float3 lightColor = _LightColor0.rgb;
  288. float3 halfDirection = normalize(viewDirection + lightDirection);
  289. ////// Lighting:
  290. float attenuation = LIGHT_ATTENUATION(i);
  291. float3 attenColor = attenuation * _LightColor0.xyz;
  292. float Pi = 3.141592654;
  293. float InvPi = 0.31830988618;
  294. ///////// Gloss:
  295. float gloss = _Gloss;
  296. float perceptualRoughness = 1.0 - _Gloss;
  297. float roughness = perceptualRoughness * perceptualRoughness;
  298. float specPow = exp2(gloss * 10.0 + 1.0);
  299. /////// GI Data:
  300. UnityLight light;
  301. #ifdef LIGHTMAP_OFF
  302. light.color = lightColor;
  303. light.dir = lightDirection;
  304. light.ndotl = LambertTerm(normalDirection, light.dir);
  305. #else
  306. light.color = half3(0.f, 0.f, 0.f);
  307. light.ndotl = 0.0f;
  308. light.dir = half3(0.f, 0.f, 0.f);
  309. #endif
  310. UnityGIInput d;
  311. d.light = light;
  312. d.worldPos = i.posWorld.xyz;
  313. d.worldViewDir = viewDirection;
  314. d.atten = attenuation;
  315. #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
  316. d.ambient = 0;
  317. d.lightmapUV = i.ambientOrLightmapUV;
  318. #else
  319. d.ambient = i.ambientOrLightmapUV;
  320. #endif
  321. #if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
  322. d.boxMin[0] = unity_SpecCube0_BoxMin;
  323. d.boxMin[1] = unity_SpecCube1_BoxMin;
  324. #endif
  325. #if UNITY_SPECCUBE_BOX_PROJECTION
  326. d.boxMax[0] = unity_SpecCube0_BoxMax;
  327. d.boxMax[1] = unity_SpecCube1_BoxMax;
  328. d.probePosition[0] = unity_SpecCube0_ProbePosition;
  329. d.probePosition[1] = unity_SpecCube1_ProbePosition;
  330. #endif
  331. d.probeHDR[0] = unity_SpecCube0_HDR;
  332. d.probeHDR[1] = unity_SpecCube1_HDR;
  333. Unity_GlossyEnvironmentData ugls_en_data;
  334. ugls_en_data.roughness = 1.0 - gloss;
  335. ugls_en_data.reflUVW = viewReflectDirection;
  336. UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data);
  337. lightDirection = gi.light.dir;
  338. lightColor = gi.light.color;
  339. ////// Specular:
  340. float NdotL = saturate(dot(normalDirection, lightDirection));
  341. float LdotH = saturate(dot(lightDirection, halfDirection));
  342. float3 specularColor = _Metallic;
  343. float specularMonochrome;
  344. float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
  345. if (i.flag < _RotLineLength)
  346. {
  347. _MainTex_var = tex2D(_MainTex, TRANSFORM_TEX(i.uv0, _MainTex));
  348. }
  349. else
  350. {
  351. _MainTex_var = tex2D(_SecondTex, TRANSFORM_TEX(i.uv0, _SecondTex));
  352. }
  353. float3 diffuseColor = (_MainTex_var.rgb*_Color.rgb); // Need this for specular when using metallic
  354. diffuseColor = DiffuseAndSpecularFromMetallic(diffuseColor, specularColor, specularColor, specularMonochrome);
  355. specularMonochrome = 1.0 - specularMonochrome;
  356. float NdotV = abs(dot(normalDirection, viewDirection));
  357. float NdotH = saturate(dot(normalDirection, halfDirection));
  358. float VdotH = saturate(dot(viewDirection, halfDirection));
  359. float visTerm = SmithJointGGXVisibilityTerm(NdotL, NdotV, roughness);
  360. float normTerm = GGXTerm(NdotH, roughness);
  361. float specularPBL = (visTerm*normTerm) * UNITY_PI;
  362. #ifdef UNITY_COLORSPACE_GAMMA
  363. specularPBL = sqrt(max(1e-4h, specularPBL));
  364. #endif
  365. specularPBL = max(0, specularPBL * NdotL);
  366. #if defined(_SPECULARHIGHLIGHTS_OFF)
  367. specularPBL = 0.0;
  368. #endif
  369. half surfaceReduction;
  370. #ifdef UNITY_COLORSPACE_GAMMA
  371. surfaceReduction = 1.0 - 0.28*roughness*perceptualRoughness;
  372. #else
  373. surfaceReduction = 1.0 / (roughness*roughness + 1.0);
  374. #endif
  375. specularPBL *= any(specularColor) ? 1.0 : 0.0;
  376. float3 directSpecular = attenColor * specularPBL*FresnelTerm(specularColor, LdotH);
  377. half grazingTerm = saturate(gloss + specularMonochrome);
  378. float3 indirectSpecular = (gi.indirect.specular);
  379. indirectSpecular *= FresnelLerp(specularColor, grazingTerm, NdotV);
  380. indirectSpecular *= surfaceReduction;
  381. float3 specular = (directSpecular + indirectSpecular);
  382. /////// Diffuse:
  383. NdotL = max(0.0,dot(normalDirection, lightDirection));
  384. half fd90 = 0.5 + 2 * LdotH * LdotH * (1 - gloss);
  385. float nlPow5 = Pow5(1 - NdotL);
  386. float nvPow5 = Pow5(1 - NdotV);
  387. float3 directDiffuse = ((1 + (fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;
  388. float3 indirectDiffuse = float3(0,0,0);
  389. indirectDiffuse += gi.indirect.diffuse;
  390. float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
  391. /// Final Color:
  392. float3 finalColor = diffuse + specular;
  393. fixed4 finalRGBA = fixed4(finalColor, _Color.a);
  394. UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
  395. return finalRGBA;
  396. }
  397. ENDCG
  398. }
  399. }
  400. }