Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 texture_albedo : hint_albedo;
  5. uniform float specular;
  6. uniform float metallic;
  7. uniform float roughness : hint_range(0,1);
  8. uniform float point_size : hint_range(0,128);
  9. uniform sampler2D texture_metallic : hint_white;
  10. uniform vec4 metallic_texture_channel;
  11. uniform sampler2D texture_roughness : hint_white;
  12. uniform vec4 roughness_texture_channel;
  13. uniform sampler2D texture_normal : hint_normal;
  14. uniform float normal_scale : hint_range(-16,16);
  15. uniform vec3 uv1_scale;
  16. uniform vec3 uv1_offset;
  17. uniform vec3 uv2_scale;
  18. uniform vec3 uv2_offset;
  19.  
  20. vec2 hash2D2D (vec2 s)
  21. {
  22.     //magic numbers
  23.     return fract(sin(mod(vec2(dot(s, vec2(127.1,311.7)), dot(s, vec2(269.5,183.3))), 3.14159))*43758.5453);
  24. }
  25.  
  26. //stochastic sampling
  27. vec4 textureStochastic(sampler2D tex, vec2 UV)
  28. {
  29.     //triangle vertices and blend weights
  30.     //BW_vx[0...2].xyz = triangle verts
  31.     //BW_vx[3].xy = blend weights (z is unused)
  32.     mat4 BW_vx;
  33.  
  34.     //uv transformed into triangular grid space with UV scaled by approximation of 2*sqrt(3)
  35.     vec2 newUV = (mat2(vec2(1.0 , 0.0) , vec2(-0.57735027 , 1.15470054))* UV * 3.464);
  36.  
  37.     //vertex IDs and barycentric coords
  38.     vec2 vxID = vec2 (floor(newUV));
  39.     vec3 fracted = vec3 (fract(newUV), 0);
  40.     fracted.z = 1.0-fracted.x-fracted.y;
  41.  
  42.     BW_vx = ((fracted.z>0.0) ?
  43.         mat4(vec4(vxID, 0,0), vec4(vxID + vec2(0, 1), 0,0), vec4(vxID + vec2(1, 0), 0,0), vec4(fracted,0)) :
  44.         mat4(vec4(vxID + vec2 (1, 1), 0,0), vec4(vxID + vec2 (1, 0), 0,0), vec4(vxID + vec2 (0, 1), 0,0), vec4(-fracted.z, 1.0-fracted.y, 1.0-fracted.x,0)));
  45.  
  46.     //calculate derivatives to avoid triangular grid artifacts
  47.     vec2 dx = dFdx(UV);
  48.     vec2 dy = dFdy(UV);
  49.     vec2 fakegradient = smoothstep(dx,dy,vec2(0.5,0.5));
  50.     //blend samples with calculated weights
  51.     return (texture(tex, UV + hash2D2D(BW_vx[0].xy), 1) * BW_vx[3].x +
  52.            texture(tex, UV + hash2D2D(BW_vx[1].xy), 1) * BW_vx[3].y +
  53.            texture(tex, UV + hash2D2D(BW_vx[2].xy), 1) * BW_vx[3].z);
  54.    
  55. }
  56.  
  57. void vertex() {
  58.     UV=UV*uv1_scale.xy+uv1_offset.xy;
  59. }
  60.  
  61.  
  62.  
  63.  
  64. void fragment() {
  65.     vec2 base_uv = UV;
  66.     vec4 albedo_tex = textureStochastic(texture_albedo,base_uv);
  67.     vec4 albedo_tex_a = textureStochastic(texture_albedo,base_uv+vec2(1,3));
  68.     vec4 albedo_tex_b = textureStochastic(texture_albedo,base_uv);
  69.     float f = albedo_tex.x*8.0;
  70.     albedo_tex = mix( albedo_tex_a, albedo_tex_b, smoothstep(0.2,0.8,f-0.1*(albedo_tex_a-albedo_tex_b)));
  71.     ALBEDO = albedo.rgb * albedo_tex.rgb;
  72.     float metallic_tex = dot(textureStochastic(texture_metallic,base_uv),metallic_texture_channel);
  73.     METALLIC = metallic_tex * metallic;
  74.     float roughness_tex = dot(textureStochastic(texture_roughness,base_uv),roughness_texture_channel);
  75.     ROUGHNESS = roughness_tex * roughness;
  76.     SPECULAR = specular;
  77.     NORMALMAP = textureStochastic(texture_normal,base_uv).rgb;
  78.     NORMALMAP_DEPTH = normal_scale;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement