refl.shader 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. shader_type spatial;
  2. render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx;
  3. uniform vec4 albedo : hint_color;
  4. uniform sampler2D p_o : hint_albedo;
  5. uniform sampler2D p_b : hint_albedo;
  6. uniform sampler2D p_g : hint_albedo;
  7. uniform sampler2D p_r : hint_albedo;
  8. uniform vec4 blendx;
  9. uniform vec4 emission : hint_color;
  10. uniform float emission_energy;
  11. uniform float iTime;
  12. void vertex() {
  13. }
  14. vec2 uv_sphere(vec3 v)
  15. {
  16. float pi = 3.1415926536;
  17. vec2 uv=vec2(0.5 + atan(v.z, v.x) / (2.0 * pi), acos(v.y) / pi);
  18. uv.y=1.-uv.y;
  19. uv.x=-0.75+uv.x;
  20. uv=fract(uv);
  21. return uv;
  22. }
  23. float hash(float p) { p = fract(p * 0.011); p *= p + 7.5; p *= p + p; return fract(p); }
  24. float noise(vec3 x) {
  25. const vec3 stepx = vec3(110, 241, 171);
  26. vec3 i = floor(x);
  27. vec3 f = fract(x);
  28. float n = dot(i, stepx);
  29. vec3 u = f * f * (3.0 - 2.0 * f);
  30. return mix(mix(mix( hash(n + dot(stepx, vec3(0, 0, 0))), hash(n + dot(stepx, vec3(1, 0, 0))), u.x),
  31. mix( hash(n + dot(stepx, vec3(0, 1, 0))), hash(n + dot(stepx, vec3(1, 1, 0))), u.x), u.y),
  32. mix(mix( hash(n + dot(stepx, vec3(0, 0, 1))), hash(n + dot(stepx, vec3(1, 0, 1))), u.x),
  33. mix( hash(n + dot(stepx, vec3(0, 1, 1))), hash(n + dot(stepx, vec3(1, 1, 1))), u.x), u.y), u.z);
  34. }
  35. float fbm(vec3 x) {
  36. float v = 0.0;
  37. float a = 0.5;
  38. vec3 shift = vec3(100);
  39. for (int i = 0; i < 5; ++i) {
  40. v += a * noise(x);
  41. x = x * 2.0 + shift;
  42. a *= 0.5;
  43. }
  44. return v;
  45. }
  46. mat4 rotationAxisAngle( vec3 v, float angle )
  47. {
  48. float s = sin( angle );
  49. float c = cos( angle );
  50. float ic = 1.0 - c;
  51. return mat4( vec4(v.x*v.x*ic + c, v.y*v.x*ic - s*v.z, v.z*v.x*ic + s*v.y, 0.0),
  52. vec4(v.x*v.y*ic + s*v.z, v.y*v.y*ic + c, v.z*v.y*ic - s*v.x, 0.0),
  53. vec4(v.x*v.z*ic - s*v.y, v.y*v.z*ic + s*v.x, v.z*v.z*ic + c, 0.0),
  54. vec4(0.0, 0.0, 0.0, 1.0 ));
  55. }
  56. void fragment() {
  57. vec3 rd=normalize(((CAMERA_MATRIX*vec4(normalize(-VERTEX),0.0)).xyz));
  58. vec3 nor=normalize((CAMERA_MATRIX * vec4(NORMAL, 0.0)).xyz);
  59. vec3 ref = reflect(rd,nor);
  60. vec2 tuv=uv_sphere(normalize(ref));
  61. vec3 col=vec3(0);
  62. vec3 c1=vec3(0);
  63. vec3 c2=vec3(0);
  64. vec3 c3=vec3(0);
  65. vec3 c4=vec3(0);
  66. if(blendx.x>0.)c1=pow(texture(p_o,tuv).rgb,vec3(1.25));
  67. if(blendx.y>0.)c2=pow(texture(p_b,tuv).rgb,vec3(1.25))*0.15;
  68. if(blendx.z>0.)c3=pow(texture(p_g,tuv).rgb,vec3(1.25));
  69. if(blendx.w>0.)c4=pow(texture(p_r,tuv).rgb,vec3(1.25));
  70. if (blendx.y>0.01)
  71. {
  72. float ts = 0.0;
  73. vec3 p = ((vec4(nor,0.))*rotationAxisAngle(normalize(vec3(1.,1.,0.5)),mod(iTime*0.25,3.1415926*2.))).rgb*15.;
  74. for(int i = 0; i < 4; i++)
  75. {
  76. p = p * (1. - ts * .25 * float(i + 1));
  77. ts = fbm((p + 3.0 * float(i)))*0.135;
  78. }
  79. ts *= pow(max(dot(NORMAL, normalize(VIEW)),0.), 6.0) * 15.0;
  80. ts=clamp(ts,0.,5.);
  81. c2 = vec3(pow(ts,3.), pow(ts,2.), ts)*blendx.y+ts*emission.rgb*0.5;
  82. }
  83. col=c2*blendx.y+c3*blendx.z+c4*blendx.w;
  84. //col=clamp(col,0.,1.);
  85. col=c1*blendx.x+col;
  86. float itt = smoothstep(0.05,0.25,max(dot(NORMAL, normalize(VIEW)),0.));
  87. col*=itt;
  88. ALBEDO=vec3(0.);
  89. METALLIC = 0.;
  90. ROUGHNESS = 1.;
  91. SPECULAR = 0.5;
  92. EMISSION=col*6.;
  93. }