fs_hdr_mesh.sc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. $input v_pos, v_view, v_normal
  2. /*
  3. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  4. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  5. */
  6. #include "common.sh"
  7. SAMPLERCUBE(s_texCube, 0);
  8. vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
  9. {
  10. float ndotl = dot(_normal, _lightDir);
  11. vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal);
  12. float rdotv = dot(reflected, _viewDir);
  13. return vec2(ndotl, rdotv);
  14. }
  15. float fresnel(float _ndotl, float _bias, float _pow)
  16. {
  17. float facing = (1.0 - _ndotl);
  18. return max(_bias + (1.0 - _bias) * pow(facing, _pow), 0.0);
  19. }
  20. vec4 lit(float _ndotl, float _rdotv, float _m)
  21. {
  22. float diff = max(0.0, _ndotl);
  23. float spec = step(0.0, _ndotl) * max(0.0, _rdotv * _m);
  24. return vec4(1.0, diff, spec, 1.0);
  25. }
  26. void main()
  27. {
  28. vec3 lightDir = vec3(0.0, 0.0, -1.0);
  29. vec3 normal = normalize(v_normal);
  30. vec3 view = normalize(v_view);
  31. vec2 bln = blinn(lightDir, normal, view);
  32. vec4 lc = lit(bln.x, bln.y, 1.0);
  33. float fres = fresnel(bln.x, 0.2, 5.0);
  34. float index = ( (sin(v_pos.x*3.0+u_time)*0.3+0.7)
  35. + ( cos(v_pos.y*3.0+u_time)*0.4+0.6)
  36. + ( cos(v_pos.z*3.0+u_time)*0.2+0.8)
  37. )*M_PI;
  38. vec3 color = vec3(sin(index*8.0)*0.4 + 0.6
  39. , sin(index*4.0)*0.4 + 0.6
  40. , sin(index*2.0)*0.4 + 0.6
  41. );
  42. color *= textureCube(s_texCube, reflect(view, -normal) ).xyz;
  43. gl_FragColor = encodeRGBE8(color.xyz*lc.y + fres*pow(lc.z, 128.0) );
  44. }