LineShader.hx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package h3d.shader;
  2. class LineShader extends hxsl.Shader {
  3. static var SRC = {
  4. @global var camera : {
  5. var view : Mat4;
  6. var proj : Mat4;
  7. var viewProj : Mat4;
  8. var projDepth : Float;
  9. var zNear : Float;
  10. var zFar : Float;
  11. };
  12. @global var global : {
  13. var pixelSize : Vec2;
  14. @perObject var modelView : Mat4;
  15. };
  16. @input var input : {
  17. var position : Vec3;
  18. var normal : Vec3;
  19. var uv : Vec2;
  20. }
  21. var output : {
  22. var position : Vec4;
  23. };
  24. var transformedNormal : Vec3;
  25. var transformedPosition : Vec3;
  26. var projectedPosition : Vec4;
  27. @param var lengthScale : Float;
  28. @param var width : Float;
  29. var pdir : Vec4;
  30. function __init__() {
  31. {
  32. var dir = input.normal * global.modelView.mat3(); // keep scale
  33. pdir = vec4(dir * mat3(camera.view), 1) * camera.proj;
  34. pdir.xy *= 1 / sqrt(pdir.x * pdir.x + pdir.y * pdir.y);
  35. transformedPosition += dir * input.uv.x * lengthScale;
  36. transformedNormal = dir.normalize();
  37. }
  38. }
  39. function linearize(d : Float) : Float {
  40. var n = camera.zNear;
  41. var f = camera.zFar;
  42. return (2 * n * f) / (f + n - (2 * d - 1) * (f - n) * camera.projDepth);
  43. }
  44. function vertex() {
  45. projectedPosition.xy += (pdir.yx * vec2(1,-1)) * (input.uv.y - 0.5) * linearize(projectedPosition.z / projectedPosition.w) * global.pixelSize * width;
  46. }
  47. };
  48. public function new( width = 1.5, lengthScale = 1. ) {
  49. super();
  50. this.width = width;
  51. this.lengthScale = lengthScale;
  52. }
  53. }