houseShader copy.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. export default {
  2. attributes: ['uv', 'position', 'world0', 'world1', 'world2', 'world3'],
  3. uniforms: ['view', 'projection', 'worldViewProjection', 'world',
  4. 'fireworkLight', 'fireworkLightPosition',
  5. 'haveShadowLight', 'lightSpaceMatrix', 'shadowSampler',
  6. 'isYUV', 'focal_width_height', 'texture_video'],
  7. defines: ["#define SHADOWFULLFLOAT", "#define NUM_BONE_INFLUENCERS 0", "#define NUM_MORPH_INFLUENCERS 0"],
  8. samplers: ['shadowSampler', 'texture_video'],
  9. vertex: `
  10. precision highp float;
  11. varying vec3 ModelPos;
  12. varying vec4 vPositionFromLight;
  13. varying float fireworkDistance;
  14. varying float fireworkCosTheta;
  15. attribute vec2 uv;
  16. attribute vec3 position;
  17. attribute vec4 world0;
  18. attribute vec4 world1;
  19. attribute vec4 world2;
  20. attribute vec4 world3;
  21. #ifdef NORMAL
  22. attribute vec3 normal;
  23. #endif
  24. uniform vec3 fireworkLightPosition;
  25. uniform mat4 view;
  26. uniform mat4 projection;
  27. uniform mat4 lightSpaceMatrix;
  28. uniform mat4 world;
  29. uniform mat4 worldViewProjection;
  30. float DistanceCalculation(vec3 Q, vec3 P)
  31. {
  32. return (Q.x - P.x) * (Q.x - P.x) + (Q.y - P.y) * (Q.y - P.y) + (Q.z - P.z) * (Q.z - P.z);
  33. }
  34. float CosThetaCalculation(vec3 Q, vec3 P)
  35. {
  36. return max(0.,dot(Q, P));
  37. }
  38. void main()
  39. {
  40. #include<instancesVertex>
  41. vPositionFromLight = lightSpaceMatrix * finalWorld * vec4(position, 1.0);
  42. fireworkDistance = distance(vec3(finalWorld * vec4(position, 1.0)), fireworkLightPosition);
  43. fireworkCosTheta = 1.0;
  44. #ifdef NORMAL
  45. vec3 directionFirework = fireworkLightPosition.xyz - vec3(finalWorld * vec4(position, 1.0));
  46. directionFirework = normalize(directionFirework);
  47. fireworkCosTheta = CosThetaCalculation(directionFirework, normal);
  48. #endif
  49. ModelPos = vec3( view * finalWorld * vec4(position , 1.0));
  50. gl_Position = projection * view * finalWorld * vec4(position , 1.0);
  51. }
  52. `,
  53. fragment: `
  54. precision highp float;
  55. varying vec3 ModelPos;
  56. uniform float isYUV; // false: 0, true: 1.0
  57. uniform sampler2D texture_video;
  58. uniform float haveShadowLight;
  59. varying vec4 vPositionFromLight;
  60. uniform float fireworkLight;
  61. varying float fireworkDistance;
  62. varying float fireworkCosTheta;
  63. uniform sampler2D shadowSampler;
  64. uniform vec3 focal_width_height;
  65. const float inv_2_PI = 0.1591549; // 1 / (2 * pi)
  66. const float inv_PI = 0.3183099; // 1 / ( pi)
  67. const vec2 invAtan = vec2(0.1591549, 0.3183099);
  68. float unpack(vec4 color)
  69. {
  70. const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
  71. return dot(color, bit_shift);
  72. }
  73. float ShadowCalculation(vec4 vPositionFromLight, sampler2D ShadowMap)
  74. {
  75. vec3 projCoords = vPositionFromLight.xyz / vPositionFromLight.w;
  76. vec3 depth = 0.5 * projCoords + vec3(0.5);
  77. vec2 uv = depth.xy;
  78. if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)
  79. {
  80. return 1.0;
  81. }
  82. #ifndef SHADOWFULLFLOAT
  83. float shadow = unpack(texture2D(ShadowMap, uv));
  84. #else
  85. float shadow = texture2D(ShadowMap, uv).x;
  86. #endif
  87. if (depth.z > shadow - 1e-4)
  88. {
  89. return 0.7;
  90. }
  91. else
  92. {
  93. return 1.0;
  94. }
  95. }
  96. vec2 SampleTex(vec3 pt3d)
  97. {
  98. return focal_width_height.x / focal_width_height.yz *pt3d.xy/pt3d.z + 0.5;
  99. }
  100. void main()
  101. {
  102. vec3 yuv;
  103. vec3 rgb;
  104. vec2 uv;
  105. vec3 color = vec3(0,0,0);
  106. vec3 flash_color = fireworkLight * 1000.0 / fireworkDistance * fireworkCosTheta * vec3(1,0,0);
  107. float shadow = 1.0;
  108. if (haveShadowLight > 0.5)
  109. {
  110. shadow = ShadowCalculation(vPositionFromLight, shadowSampler);
  111. }
  112. uv = SampleTex( normalize(ModelPos) );
  113. if( isYUV < 0.5 )
  114. {
  115. color = texture2D(texture_video, uv).rgb;
  116. }else{
  117. const mat4 YUV2RGB = mat4
  118. (
  119. 1.1643828125, 0, 1.59602734375, -.87078515625,
  120. 1.1643828125, -.39176171875, -.81296875, .52959375,
  121. 1.1643828125, 2.017234375, 0, -1.081390625,
  122. 0, 0, 0, 1
  123. );
  124. vec4 result = vec4(
  125. texture2D( texture_video, vec2( uv.x, uv.y * 0.666666 + 0.333333 ) ).x,
  126. texture2D( texture_video, vec2( uv.x * 0.5, uv.y * 0.333333 ) ).x,
  127. texture2D( texture_video, vec2( 0.5 + uv.x * 0.5, uv.y * 0.333333 ) ).x,
  128. 1
  129. ) * YUV2RGB;
  130. color = clamp(result.rgb, 0.0, 1.0);
  131. }
  132. if( uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 )
  133. {
  134. color = vec3(0,0,0);
  135. }
  136. gl_FragColor = vec4(shadow * (color + flash_color) * 1.0, 1.0);
  137. }
  138. `
  139. }