PointLight.hx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package h3d.shader;
  2. class PointLight extends hxsl.Shader {
  3. static var SRC = {
  4. @const var enableSpecular : Bool;
  5. @param var color : Vec3;
  6. @param var params : Vec3; // [constant, linear, quadratic]
  7. @param var lightPosition : Vec3;
  8. @global var camera : {
  9. var position : Vec3;
  10. };
  11. /**
  12. Don't use model normal to calculate light amount
  13. **/
  14. @const var isAmbient : Bool;
  15. var lightColor : Vec3;
  16. var lightPixelColor : Vec3;
  17. var transformedPosition : Vec3;
  18. var pixelTransformedPosition : Vec3;
  19. var transformedNormal : Vec3;
  20. var specPower : Float;
  21. var specColor : Vec3;
  22. function calcLighting( position : Vec3 ) : Vec3 {
  23. var dvec = lightPosition - position;
  24. var dist2 = dvec.dot(dvec);
  25. var dist = dist2.sqrt();
  26. var diff : Float = isAmbient ? 1. : transformedNormal.dot(dvec).max(0.);
  27. var factor = 1 / vec3(dist, dist2, dist * dist2).dot(params);
  28. if( !enableSpecular )
  29. return color * diff * factor;
  30. var r = reflect(-dvec.normalize(), transformedNormal).normalize();
  31. var specValue = r.dot((camera.position - position).normalize()).max(0.);
  32. return color * (diff * factor + specColor * pow(specValue, specPower));
  33. }
  34. function vertex() {
  35. lightColor.rgb += calcLighting(transformedPosition);
  36. }
  37. function fragment() {
  38. lightPixelColor.rgb += calcLighting(pixelTransformedPosition);
  39. }
  40. };
  41. public function new() {
  42. super();
  43. color.set(1, 1, 1);
  44. params.set(0, 0, 1);
  45. }
  46. }