您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RadarScanShader.glsl 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. in vec2 v_textureCoordinates;
  2. uniform sampler2D colorTexture;
  3. uniform sampler2D depthTexture;
  4. uniform vec3 centerWC;
  5. uniform vec3 planeNormalWC;
  6. uniform vec3 lineNormalWC;
  7. uniform float radius;
  8. uniform vec4 color;
  9. float getDepth(){
  10. float z_window = czm_unpackDepth(texture(depthTexture, v_textureCoordinates));
  11. z_window = czm_reverseLogDepth(z_window);
  12. float n_range = czm_depthRange.near;
  13. float f_range = czm_depthRange.far;
  14. return (2.0 * z_window - n_range - f_range) / (f_range - n_range);
  15. }
  16. vec4 toEye(in vec2 uv, in float depth){
  17. vec2 xy = vec2((uv.x * 2.0 - 1.0),(uv.y * 2.0 - 1.0));
  18. vec4 posInCamera =czm_inverseProjection * vec4(xy, depth, 1.0);
  19. posInCamera = posInCamera / posInCamera.w;
  20. return posInCamera;
  21. }
  22. bool isPointOnLineRight(in vec3 ptOnLine, in vec3 lineNormal, in vec3 testPt)
  23. {
  24. vec3 v01 = testPt - ptOnLine;
  25. normalize(v01);
  26. vec3 temp = cross(v01, lineNormal);
  27. vec4 planeNormalEC = czm_view * vec4(planeNormalWC,0);
  28. float d = dot(temp, planeNormalEC.xyz);
  29. return d > 0.5;
  30. }
  31. vec3 pointProjectOnPlane(in vec3 planeNormal, in vec3 planeOrigin, in vec3 point)
  32. {
  33. vec3 v01 = point -planeOrigin;
  34. float d = dot(planeNormal, v01) ;
  35. return (point - planeNormal * d);
  36. }
  37. float distancePointToLine(in vec3 ptOnLine, in vec3 lineNormal, in vec3 testPt)
  38. {
  39. vec3 tempPt = pointProjectOnPlane(lineNormal, ptOnLine, testPt);
  40. return length(tempPt - ptOnLine);
  41. }
  42. void main() {
  43. out_FragColor = texture(colorTexture, v_textureCoordinates);
  44. float depth = getDepth();
  45. vec4 viewPos = toEye(v_textureCoordinates, depth);
  46. vec4 centerEC = czm_view * vec4(centerWC,1);
  47. vec4 planeNormalEC = czm_view * vec4(planeNormalWC,0);
  48. vec4 lineNormalEC = czm_view * vec4(lineNormalWC,0);
  49. vec3 prjOnPlane = pointProjectOnPlane(planeNormalEC.xyz, centerEC.xyz, viewPos.xyz);
  50. float dis = length(prjOnPlane.xyz - centerEC.xyz);
  51. float diameter = radius * 2.0;
  52. if(dis < radius){
  53. float f0 = 1.0 -abs(radius - dis) / radius;
  54. f0 = pow(f0, 64.0);
  55. vec3 lineEndPt = vec3(centerEC.xyz) + vec3(lineNormalEC.xyz) * radius;
  56. float f = 0.0;
  57. if(isPointOnLineRight(centerEC.xyz, lineNormalEC.xyz, prjOnPlane.xyz)) {
  58. float dis1= length(prjOnPlane.xyz - lineEndPt);
  59. f = abs(diameter - dis1) / diameter;
  60. f = pow(f, 3.0);
  61. }
  62. out_FragColor = mix(out_FragColor, color, f + f0);
  63. }
  64. }