Advertisement
SarahNorthway

Untitled

Apr 4th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.57 KB | None | 0 0
  1. Shader "WallThrough/WallThroughEverything" {
  2.  
  3. Properties{
  4. _Color2("Color2", Color) = (0,0,1, .5)
  5. _CutDistance("CutDistance", Float) = 1
  6. _NumberSteps("Number Steps", Int) = 10
  7. _MaxTraceDistance("Max Trace Distance" , Float) = 6.0
  8. _IntersectionPrecision("Intersection Precision" , Float) = 0.0001
  9. _CubeMap("Cube Map" , Cube) = "defaulttexture" {}
  10. _Color("Color", COLOR) = (1,1,1,1)
  11.  
  12.  
  13. //--- Standard Shader Vars ---//
  14.  
  15. // sarah difference from Standard Unity shader
  16. _StencilRef("Stencil Ref ID", Int) = 1
  17. [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Comparison", int) = 7 //Disabled=0,Never,Less,Equal,LessEqual,Greater,NotEqual,GreaterEqual,Always=8
  18. [Enum(UnityEngine.Rendering.StencilOp)] _StencilOp("Stencil Pass Operation", int) = 0 //Keep=0,Zero,Replace,IncrementSaturate,DecrementSaturate,Invert,IncrementWrap,DecrementWrap=7
  19.  
  20. _Color("Color", Color) = (1,1,1,1)
  21. _MainTex("Albedo", 2D) = "white" {}
  22.  
  23. _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
  24.  
  25. _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
  26. [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
  27. _MetallicGlossMap("Metallic", 2D) = "white" {}
  28.  
  29. _BumpScale("Scale", Float) = 1.0
  30. _BumpMap("Normal Map", 2D) = "bump" {}
  31.  
  32. _Parallax("Height Scale", Range(0.005, 0.08)) = 0.02
  33. _ParallaxMap("Height Map", 2D) = "black" {}
  34.  
  35. _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
  36. _OcclusionMap("Occlusion", 2D) = "white" {}
  37.  
  38. _EmissionColor("Color", Color) = (0,0,0)
  39. _EmissionMap("Emission", 2D) = "white" {}
  40.  
  41. _DetailMask("Detail Mask", 2D) = "white" {}
  42.  
  43. _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
  44. _DetailNormalMapScale("Scale", Float) = 1.0
  45. _DetailNormalMap("Normal Map", 2D) = "bump" {}
  46.  
  47. [Enum(UV0,0,UV1,1)] _UVSec("UV Set for secondary textures", Float) = 0
  48.  
  49. // UI-only data
  50. [HideInInspector] _EmissionScaleUI("Scale", Float) = 0.0
  51. [HideInInspector] _EmissionColorUI("Color", Color) = (1,1,1)
  52.  
  53. // Blending state
  54. [HideInInspector] _Mode("__mode", Float) = 0.0
  55. [HideInInspector] _SrcBlend("__src", Float) = 1.0
  56. [HideInInspector] _DstBlend("__dst", Float) = 0.0
  57. [HideInInspector] _ZWrite("__zw", Float) = 1.0
  58.  
  59. //--- End Standard Shader Vars ---//
  60.  
  61. }
  62. SubShader{
  63.  
  64. Pass{
  65.  
  66.  
  67.  
  68. Stencil{
  69. Ref 2
  70. Comp Always
  71. Pass Replace
  72. }
  73. Cull Off
  74. ColorMask 0
  75.  
  76. CGPROGRAM
  77. #pragma vertex vert
  78. #pragma fragment frag
  79.  
  80. uniform float _CutDistance;
  81. uniform float _RenderInside;
  82. uniform float3 _HeadGlobal;
  83.  
  84.  
  85. struct VertexIn
  86. {
  87. float4 position : POSITION;
  88. float3 normal : NORMAL;
  89. float4 texcoord : TEXCOORD0;
  90. float4 tangent : TANGENT;
  91. };
  92.  
  93. struct VertexOut {
  94. float4 pos : POSITION;
  95. float3 normal : NORMAL;
  96. float4 uv : TEXCOORD0;
  97. float3 posGlobal : TEXCOORD1;
  98. };
  99.  
  100. VertexOut vert(VertexIn v) {
  101. VertexOut o;
  102. o.normal = v.normal;
  103. o.uv = v.texcoord;
  104. o.pos = mul(UNITY_MATRIX_MVP, v.position);
  105. o.posGlobal = mul(unity_ObjectToWorld, v.position);
  106.  
  107. return o;
  108. }
  109.  
  110. // Fragment Shader
  111. fixed4 frag(VertexOut i) : COLOR{
  112. float dist = length(i.posGlobal - _HeadGlobal);
  113. float normalisedDist = dist / _CutDistance;
  114.  
  115. if (normalisedDist > 2 && !_RenderInside) {
  116. discard;
  117. }
  118. return float4(0, 0, 0, 0);
  119. }
  120. ENDCG
  121. }
  122.  
  123. //draw 1s to the setencil buffer where the wall should disapear to reveal the inside
  124. Pass{
  125.  
  126. Stencil{
  127. Ref 1
  128. Comp Always
  129. Pass Replace
  130. }
  131. Cull Off
  132.  
  133. CGPROGRAM
  134. #pragma vertex vert
  135. #pragma fragment frag
  136.  
  137. uniform float _CutDistance;
  138. uniform float _RenderInside;
  139. uniform float3 _HeadGlobal;
  140.  
  141.  
  142. struct VertexIn
  143. {
  144. float4 position : POSITION;
  145. float3 normal : NORMAL;
  146. float4 texcoord : TEXCOORD0;
  147. float4 tangent : TANGENT;
  148. };
  149.  
  150. struct VertexOut {
  151. float4 pos : POSITION;
  152. float3 normal : NORMAL;
  153. float4 uv : TEXCOORD0;
  154. float3 posGlobal : TEXCOORD1;
  155. };
  156.  
  157. VertexOut vert(VertexIn v) {
  158. VertexOut o;
  159. o.normal = v.normal;
  160. o.uv = v.texcoord;
  161. o.pos = mul(UNITY_MATRIX_MVP, v.position);
  162. o.posGlobal = mul(unity_ObjectToWorld, v.position);
  163.  
  164. return o;
  165. }
  166.  
  167. // Fragment Shader
  168. fixed4 frag(VertexOut i) : COLOR{
  169. float dist = length(i.posGlobal - _HeadGlobal);
  170. float normalisedDist = dist / _CutDistance;
  171.  
  172. if (normalisedDist > 1 && !_RenderInside) {
  173. discard;
  174. }
  175. return float4(0, 0, 0,0);
  176. }
  177. ENDCG
  178. }
  179.  
  180. Pass{
  181. Name "OUTSIDE"
  182.  
  183. Cull Front
  184. //rely on stencil to not overdraw
  185. ZTest Off
  186. LOD 200
  187.  
  188. //if there is a discard command in the shader you will get no performance boost from this!
  189. Stencil{
  190. Ref 1
  191. Comp Equal
  192. Pass Keep
  193. Fail Keep
  194. }
  195.  
  196.  
  197. CGPROGRAM
  198. #pragma vertex vert
  199. #pragma fragment frag
  200. // Use shader model 3.0 target, to get nicer looking lighting
  201. #pragma target 3.0
  202.  
  203. #include "UnityCG.cginc"
  204. #include "Assets/Shaders/Chunks/noise.cginc"
  205.  
  206. uniform int _NumberSteps;
  207. uniform float _IntersectionPrecision;
  208. uniform float _MaxTraceDistance;
  209. uniform float4 _Color;
  210. uniform samplerCUBE _CubeMap;
  211. uniform float3 _HeadGlobal;
  212.  
  213. struct VertexIn
  214. {
  215. float4 position : POSITION;
  216. float3 normal : NORMAL;
  217. float4 texcoord : TEXCOORD0;
  218. float4 tangent : TANGENT;
  219. };
  220.  
  221. struct VertexOut {
  222. float4 pos : POSITION;
  223. float3 normal : NORMAL;
  224. float4 uv : TEXCOORD0;
  225. float3 ro : TEXCOORD1;
  226. float3 origin : TEXCOORD2;
  227. float3 camPos : TEXCOORD3;
  228. float3 worldOrigin : TEXCOORD4;
  229. };
  230.  
  231. float sdBox(float3 p, float3 b) {
  232.  
  233. float3 d = abs(p) - b;
  234.  
  235. return min(max(d.x, max(d.y, d.z)), 0.0) +
  236. length(max(d, 0.0));
  237.  
  238. }
  239.  
  240. float sdSphere(float3 p, float s) {
  241. return length(p) - s;
  242. }
  243.  
  244. float sdTorus(float3 p, float2 t) {
  245. float2 q = float2(length(p.xy) - t.x, p.z);
  246. return length(q) - t.y;
  247. }
  248.  
  249. float sdCapsule(float3 p, float3 a, float3 b, float r)
  250. {
  251. float3 pa = p - a, ba = b - a;
  252. float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
  253. return length(pa - ba*h) - r;
  254. }
  255.  
  256. float2 smoothU(float2 d1, float2 d2, float k)
  257. {
  258. float a = d1.x;
  259. float b = d2.x;
  260. float h = clamp(0.5 + 0.5*(b - a) / k, 0.0, 1.0);
  261. return float2(lerp(b, a, h) - k*h*(1.0 - h), lerp(d2.y, d1.y, pow(h, 2.0)));
  262. }
  263.  
  264. float3 rotatedBox(float3 p, float4x4 m)
  265. {
  266. float3 q = mul(m, float4(p, 1)).xyz;
  267. return sdBox(q, float3(.2, .2, .2));
  268. }
  269.  
  270.  
  271. float2 map(in float3 pos, in float3 origin) {
  272. float2 res;
  273.  
  274. float n = 0 * noise(pos * 0 + float3(_Time.y, 0, 0)) * 12 + 4 * noise(pos * 4 + float3(0, _Time.y, 0)) + 26 * noise(pos * -5 + float3(0, 0, _Time.y));
  275. res = float2(-sdSphere(pos - _HeadGlobal, 10), 1.);
  276. res.x += n * .01;
  277.  
  278. return res;
  279. }
  280.  
  281. float3 calcNormal(in float3 pos, in float3 origin) {
  282.  
  283. float3 eps = float3(0.001, 0.0, 0.0);
  284. float3 nor = float3(
  285. map(pos + eps.xyy, origin).x - map(pos - eps.xyy, origin).x,
  286. map(pos + eps.yxy, origin).x - map(pos - eps.yxy, origin).x,
  287. map(pos + eps.yyx, origin).x - map(pos - eps.yyx, origin).x);
  288. return normalize(nor);
  289.  
  290. }
  291.  
  292. float2 calcIntersection(in float3 ro, in float3 rd, in float3 origin) {
  293. float h = _IntersectionPrecision * 2;
  294. float t = 0.0;
  295. float res = -1.0;
  296. float id = -1.0;
  297.  
  298. for (int i = 0; i< 20; i++) {
  299. if (h < _IntersectionPrecision || t > _MaxTraceDistance) break;
  300.  
  301. float3 pos = ro + rd*t;
  302. float2 m = map(pos, origin);
  303.  
  304. h = m.x;
  305. t += h;
  306. id = m.y;
  307. }
  308. if (t < _MaxTraceDistance) { res = t; }
  309. if (t > _MaxTraceDistance) { id = -1.0; }
  310.  
  311. return float2(res, id);
  312. }
  313.  
  314. VertexOut vert(VertexIn v) {
  315. VertexOut o;
  316.  
  317. o.normal = v.normal;
  318. o.uv = v.texcoord;
  319. o.pos = mul(UNITY_MATRIX_MVP, v.position);
  320.  
  321. //display in global space
  322. o.ro = mul(unity_ObjectToWorld, float4(v.position.xyz, 1));
  323. o.camPos = _WorldSpaceCameraPos;
  324. o.origin = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
  325. o.worldOrigin = float3(0, 0, 0);
  326.  
  327. return o;
  328.  
  329. }
  330.  
  331.  
  332. // Fragment Shader
  333. fixed4 frag(VertexOut i) : COLOR{
  334. float3 col = _Color;
  335. float3 ro = i.ro;
  336. float3 rd = normalize(ro - i.camPos);
  337. float3 worldOrigin = i.worldOrigin.xyz;
  338.  
  339. float2 res = calcIntersection(ro, rd, worldOrigin);
  340.  
  341. if (res.y > 0) {
  342. float3 pos = ro + rd * res.x;
  343. float3 norm = calcNormal(pos, worldOrigin);
  344.  
  345. float3 fRefl = reflect(-rd, norm);
  346. float3 cubeCol = texCUBE(_CubeMap, -fRefl).rgb;
  347.  
  348. col *= cubeCol;
  349. }
  350. else {
  351. //discard would mean stencil test can't be before fragment shader
  352. //discard;
  353. }
  354.  
  355. return float4(col.r, col.g, col.b, 1);
  356. }
  357.  
  358. ENDCG
  359. }
  360.  
  361. //draw the nice wavy looking texture on the outside
  362.  
  363. Pass{
  364. Name "SURFACE"
  365.  
  366. Blend SrcAlpha OneMinusSrcAlpha
  367. Cull Back
  368. LOD 200
  369.  
  370. //if there is a discard command in the shader you will get no performance boost from this!
  371. Stencil{
  372. //draw if the stencil is greater or equal to 1 (i.e. 1 or 2)
  373. Ref 1
  374. Comp LEqual
  375. Pass Keep
  376. Fail Keep
  377. }
  378.  
  379. CGPROGRAM
  380. #pragma vertex vert
  381. #pragma fragment frag
  382. // Use shader model 3.0 target, to get nicer looking lighting
  383. #pragma target 3.0
  384.  
  385. #include "UnityCG.cginc"
  386. #include "Assets/Shaders/Chunks/noise.cginc"
  387.  
  388. uniform float _Temp1;
  389. uniform float _Temp2;
  390. uniform float _Temp3;
  391. uniform float _Temp4;
  392. uniform float _Temp5;
  393. uniform float _Temp6;
  394. uniform float _CutDistance;
  395. uniform int _NumberSteps;
  396. uniform float _IntersectionPrecision;
  397. uniform float _MaxTraceDistance;
  398. uniform float4 _Color;
  399. uniform float4 _Color2;
  400. uniform float3 _HeadGlobal;
  401. uniform samplerCUBE _CubeMap;
  402.  
  403. struct VertexIn
  404. {
  405. float4 position : POSITION;
  406. float3 normal : NORMAL;
  407. float4 texcoord : TEXCOORD0;
  408. float4 tangent : TANGENT;
  409. };
  410.  
  411. struct VertexOut {
  412. float4 pos : POSITION;
  413. float3 normal : NORMAL;
  414. float4 uv : TEXCOORD0;
  415. float3 camPos : TEXCOORD1;
  416. float3 globalPos : TEXCOORD2;
  417. };
  418.  
  419. float sdBox(float3 p, float3 b) {
  420. float3 d = abs(p) - b;
  421.  
  422. return min(max(d.x, max(d.y, d.z)), 0.0) +
  423. length(max(d, 0.0));
  424. }
  425.  
  426. float sdPlane(float3 p, float4 n) {
  427. return dot(p, n.xyz) + n.w;
  428. }
  429.  
  430. float sdSphere(float3 p, float s) {
  431. return length(p) - s;
  432. }
  433.  
  434. float sdTorus(float3 p, float2 t) {
  435. float2 q = float2(length(p.xy) - t.x, p.z);
  436. return length(q) - t.y;
  437. }
  438.  
  439. float sdCapsule(float3 p, float3 a, float3 b, float r)
  440. {
  441. float3 pa = p - a, ba = b - a;
  442. float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
  443. return length(pa - ba*h) - r;
  444. }
  445.  
  446. float2 smoothU(float2 d1, float2 d2, float k)
  447. {
  448. float a = d1.x;
  449. float b = d2.x;
  450. float h = clamp(0.5 + 0.5*(b - a) / k, 0.0, 1.0);
  451. return float2(lerp(b, a, h) - k*h*(1.0 - h), lerp(d2.y, d1.y, pow(h, 2.0)));
  452. }
  453.  
  454. float3 rotatedBox(float3 p, float4x4 m)
  455. {
  456. float3 q = mul(m, float4(p, 1)).xyz;
  457. return sdBox(q, float3(.2, .2, .2));
  458. }
  459.  
  460.  
  461. float2 map(in float3 pos, in float3 faceNormal, in float planeDist) {
  462. float2 res;
  463. float time = _Time.y * 0.5;
  464.  
  465. //float n = _Temp1* noise(pos * _Temp2 + float3(time, 0, 0)) + _Temp3 * noise(pos * _Temp4 + float3(0, time, 0)) + _Temp5 * noise(pos * _Temp6 + float3(0, 0, time));
  466. float n = 7 * noise(pos * 10 + float3(time, 0, 0)) + 7 * noise(pos * 10 + float3(0, time, 0)) + 7 * noise(pos * 10 + float3(0, 0, time));
  467. res = float2(sdPlane(pos, float4(faceNormal, planeDist)), 1.);
  468. res.x += n *.01;
  469.  
  470. return res;
  471.  
  472. }
  473.  
  474. float3 calcNormal(in float3 pos, in float3 faceNormal, in float planeDist) {
  475. float3 eps = float3(0.001, 0.0, 0.0);
  476. float3 nor = float3(
  477. map(pos + eps.xyy, faceNormal, planeDist).x - map(pos - eps.xyy, faceNormal, planeDist).x,
  478. map(pos + eps.yxy, faceNormal, planeDist).x - map(pos - eps.yxy, faceNormal, planeDist).x,
  479. map(pos + eps.yyx, faceNormal, planeDist).x - map(pos - eps.yyx, faceNormal, planeDist).x);
  480. return normalize(nor);
  481. }
  482.  
  483. float2 calcIntersection(in float3 ro, in float3 rd, in float3 faceNormal, in float planeDist) {
  484. float h = _IntersectionPrecision * 2;
  485. float t = 0.0;
  486. float res = -1.0;
  487. float id = -1.0;
  488.  
  489. for (int i = 0; i< 20; i++) {
  490. if (h < _IntersectionPrecision || t > _MaxTraceDistance) break;
  491.  
  492. float3 pos = ro + rd*t;
  493. float2 m = map(pos, faceNormal, planeDist);
  494.  
  495. h = m.x;
  496. t += h;
  497. id = m.y;
  498. }
  499.  
  500. if (t < _MaxTraceDistance) { res = t; }
  501. if (t > _MaxTraceDistance) { id = -1.0; }
  502.  
  503. return float2(res, id);
  504. }
  505.  
  506. VertexOut vert(VertexIn v) {
  507. VertexOut o;
  508. o.normal = v.normal;
  509. o.uv = v.texcoord;
  510. o.pos = mul(UNITY_MATRIX_MVP, v.position);
  511.  
  512. o.camPos = _WorldSpaceCameraPos;
  513. o.globalPos = mul(unity_ObjectToWorld, v.position);
  514.  
  515. return o;
  516.  
  517. }
  518.  
  519. // Fragment Shader
  520. fixed4 frag(VertexOut i) : COLOR{
  521. float alpha = 1;
  522. float3 col;
  523.  
  524. float dist = length(i.globalPos - _HeadGlobal);
  525. //fade out pixels that are further away from the player
  526. //note that this assumes the StencilBuffer will have "2"s drawn out to twice the _CutDistance radius.
  527. alpha = (1 - (dist / (_CutDistance * 2))) * 2;
  528.  
  529. float3 ro = i.globalPos;
  530. float3 rd = normalize(ro - i.camPos);
  531. float3 normal = i.normal;
  532. float planeDist = dot(normal, -i.globalPos);
  533.  
  534. float2 res = calcIntersection(ro, rd, normal, planeDist);
  535.  
  536. //Clip out the bits of distance field that are far away from the plane
  537. //more and more as you get closer to the surface
  538. float insideAlphaCompare = ((_CutDistance - dist) / _CutDistance);
  539. insideAlphaCompare *= .4; //magic number
  540.  
  541. if (dist > _CutDistance || res.x > insideAlphaCompare) {
  542. float3 pos = ro + rd * res.x;
  543. float3 norm = calcNormal(pos, normal, planeDist);
  544. col = float3(1, 1, 1);
  545.  
  546. float3 fRefl = reflect(-rd, norm);
  547. float3 cubeCol = texCUBE(_CubeMap, -fRefl).rgb;
  548.  
  549. col *= cubeCol;
  550. col *= _Color2;
  551. }
  552. else {
  553. alpha = 0;
  554. }
  555. //alpha = 0;
  556. return float4(col.r, col.g, col.b, alpha);
  557. }
  558.  
  559. ENDCG
  560. }
  561. }
  562. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement