capture.shader 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. shader_type spatial;
  2. render_mode blend_mix,depth_draw_opaque,cull_disabled,diffuse_burley,specular_schlick_ggx,unshaded;
  3. // Created by Danil (2020+) https://github.com/danilw
  4. // The MIT License
  5. uniform sampler2D texture_capture : hint_albedo;
  6. uniform vec4 color:hint_color;
  7. uniform float shape_size=0.1;
  8. uniform float iTime;
  9. // set next values
  10. const float sec_to_record=8.;
  11. const float box_size=20.;
  12. const vec2 image_size=vec2(702.,351.); // capture image size, GLES2 does not have textureSize function in shaders
  13. const int default_fps=60;
  14. // this shader only for 16x16 (256) particles
  15. // this shader only for GLES2, for GLES3 beter rewrite this logic to use real particles
  16. int get_index(in vec2 uv){
  17. return int(uv.x*16.)*16+int(mod(floor(uv.y*16.),16.));
  18. }
  19. vec2 get_index_v2(in vec2 uv){
  20. return floor(uv*16.);
  21. }
  22. mat3 rotx(float a){float s = sin(a);float c = cos(a);return mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, c, s), vec3(0.0, -s, c)); }
  23. mat3 roty(float a){float s = sin(a);float c = cos(a);return mat3(vec3(c, 0.0, s), vec3(0.0, 1.0, 0.0), vec3(-s, 0.0, c));}
  24. mat3 rotz(float a){float s = sin(a);float c = cos(a);return mat3(vec3(c, s, 0.0), vec3(-s, c, 0.0), vec3(0.0, 0.0, 1.0 ));}
  25. varying flat float frame_fade; // fade on end of animation-time
  26. varying flat float dist_fade; //fade when element close to box border
  27. void vertex() {
  28. float total_frames=float(default_fps)*sec_to_record;
  29. // int iFrame=int(mod((TIME*60.)*1.,total_frames));
  30. int iFrame=int(clamp((iTime*60.),0.,total_frames));
  31. frame_fade=1.-smoothstep(0.85,1.,float(iFrame)/total_frames);
  32. int idx=get_index(UV);
  33. int tidx=idx+256*iFrame;
  34. ivec2 idtx=ivec2(int(mod(float(tidx*2),image_size.x)),(tidx*2)/int(image_size.x));
  35. vec3 pos1=textureLod(texture_capture,(vec2(idtx)+0.5)/image_size,0.).xyz;
  36. idtx=ivec2(int(mod(float(tidx*2+1),image_size.x)),(tidx*2+1)/int(image_size.x));
  37. vec3 pos2=textureLod(texture_capture,(vec2(idtx)+0.5)/image_size,0.).xyz;
  38. vec3 rpos1=vec3(pos1.x,pos1.z,pos2.y);
  39. vec3 rpos2=vec3(pos1.y,pos2.x,pos2.z);
  40. vec3 pos=vec3(ivec3(rpos2*65280.)+ivec3(rpos1*256.))/65536.;
  41. pos=pos+vec3(-0.5,0.,-0.5);
  42. dist_fade=1.-smoothstep(0.85,1.,2.*max(abs(pos.x),abs(pos.z)));
  43. if(any(greaterThan(abs(pos.xz),vec2(0.495)))){
  44. pos=vec3(-9999.+float(idx)/256.);
  45. }
  46. vec3 dirx=pos*box_size*(1./(shape_size*4.));
  47. mat4 mat_world = mat4(normalize(CAMERA_MATRIX[0])*length(WORLD_MATRIX[0]),normalize(CAMERA_MATRIX[1])*length(WORLD_MATRIX[0]),normalize(CAMERA_MATRIX[2])*length(WORLD_MATRIX[2]),vec4(dirx,1.));
  48. VERTEX*=rotx(3.141516926*0.5);
  49. VERTEX=(mat_world*vec4(VERTEX,1.)).xyz;
  50. VERTEX+=dirx;
  51. VERTEX*=shape_size*2.;
  52. }
  53. void fragment() {
  54. ALBEDO=color.rgb*frame_fade*dist_fade;
  55. }