shader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. var tvFragment = `precision highp float;
  2. varying vec2 vUV;
  3. uniform float tvWidthHeightScale;
  4. uniform float mvWidthHeightScale;
  5. uniform float bforceforceKeepContent;
  6. uniform sampler2D texture_video;
  7. // \u7B49\u6BD4\u4F8B\u7F29\u653E\u753B\u9762\u5360\u6EE1\u5C4F\u5E55\uFF0C\u5B58\u5728\u5185\u5BB9\u7684\u4E22\u5931
  8. vec2 equalScalingFitTvSize(vec2 uv, float tvWidthHeightScale, float mvWidthHeightScale)
  9. {
  10. if( tvWidthHeightScale > mvWidthHeightScale )
  11. {
  12. float scale = mvWidthHeightScale/tvWidthHeightScale;
  13. uv.y = (uv.y - 0.5) * scale + 0.5;
  14. }else if( tvWidthHeightScale < mvWidthHeightScale )
  15. {
  16. float scale = tvWidthHeightScale/mvWidthHeightScale;
  17. uv.x = (uv.x - 0.5) * scale + 0.5;
  18. }
  19. return vec2( uv.x , uv.y);
  20. }
  21. // \u5F3A\u5236\u4FDD\u7559\u753B\u9762\u5185\u5BB9\uFF08\u5E26\u6709\u9ED1\u8FB9\uFF09
  22. vec2 forceKeepContent(vec2 uv, float tvWidthHeightScale, float mvWidthHeightScale)
  23. {
  24. if( tvWidthHeightScale > mvWidthHeightScale )
  25. {
  26. float scale = mvWidthHeightScale/tvWidthHeightScale;
  27. uv.x = (uv.x - 0.5) / scale + 0.5;
  28. }else if( tvWidthHeightScale < mvWidthHeightScale )
  29. {
  30. float scale = tvWidthHeightScale/mvWidthHeightScale;
  31. uv.y = (uv.y - 0.5) / scale + 0.5;
  32. }
  33. return vec2( uv.x , uv.y);
  34. }
  35. void main()
  36. {
  37. vec2 uv = vUV;
  38. vec3 rgb;
  39. vec3 color = vec3(0,0,0);
  40. // \u4E00\u65E6\u8BBE\u7F6E\u4E86mvWidthHeightScale\uFF0C\u5C31\u4F1A\u89E6\u53D1\u7B49\u6BD4\u4F8B\u7F29\u653Eor\u5F3A\u5236\u4FDD\u5185\u5BB9
  41. if(tvWidthHeightScale > 0.0 && mvWidthHeightScale > 0.0)
  42. {
  43. if(bforceforceKeepContent > 0.0){
  44. uv = forceKeepContent(uv, tvWidthHeightScale, mvWidthHeightScale);
  45. }else{
  46. uv = equalScalingFitTvSize(uv, tvWidthHeightScale, mvWidthHeightScale);
  47. }
  48. }
  49. color = texture2D(texture_video, uv).rgb;
  50. if( uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 )
  51. {
  52. color = vec3(0,0,0);
  53. }
  54. gl_FragColor = vec4(color, 1.0);
  55. }
  56. `
  57. , tvVertex = `precision highp float;
  58. varying vec2 vUV;
  59. attribute vec2 uv;
  60. attribute vec3 position;
  61. uniform mat4 view;
  62. uniform mat4 projection;
  63. uniform mat4 world;
  64. void main()
  65. {
  66. vUV = uv;
  67. gl_Position = projection * view * world * vec4(position , 1.0);
  68. }
  69. `;
  70. var pureVideoFragment = `precision highp float;
  71. varying vec3 ModelPos;
  72. uniform float isYUV; // false: 0, true: 1.0
  73. uniform sampler2D texture_video;
  74. // uniform sampler2D chrominanceYTexture;
  75. // uniform sampler2D chrominanceUTexture;
  76. // uniform sampler2D chrominanceVTexture;
  77. uniform float haveShadowLight;
  78. varying vec4 vPositionFromLight;
  79. uniform float fireworkLight;
  80. varying float fireworkDistance;
  81. varying float fireworkCosTheta;
  82. uniform sampler2D shadowSampler;
  83. // uniform float focal;
  84. // uniform float captureWidth;
  85. // uniform float captureHeight;
  86. uniform vec3 focal_width_height;
  87. const float inv_2_PI = 0.1591549; // 1 / (2 * pi)
  88. const float inv_PI = 0.3183099; // 1 / ( pi)
  89. const vec2 invAtan = vec2(0.1591549, 0.3183099);
  90. float unpack(vec4 color)
  91. {
  92. 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);
  93. return dot(color, bit_shift);
  94. }
  95. float ShadowCalculation(vec4 vPositionFromLight, sampler2D ShadowMap)
  96. {
  97. vec3 projCoords = vPositionFromLight.xyz / vPositionFromLight.w;
  98. vec3 depth = 0.5 * projCoords + vec3(0.5);
  99. vec2 uv = depth.xy;
  100. if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)
  101. {
  102. return 1.0;
  103. }
  104. #ifndef SHADOWFULLFLOAT
  105. float shadow = unpack(texture2D(ShadowMap, uv));
  106. #else
  107. float shadow = texture2D(ShadowMap, uv).x;
  108. #endif
  109. if (depth.z > shadow - 1e-4)
  110. {
  111. return 0.7;
  112. }
  113. else
  114. {
  115. return 1.0;
  116. }
  117. }
  118. // const float f = 514.133282; //937.83246;
  119. // const float w = 720.0;
  120. // const float h = 1280.0;
  121. // vec2 SampleTex(vec3 pt3d, vec2 widthHeight)
  122. vec2 SampleTex(vec3 pt3d)
  123. {
  124. // // vec2 uv = vec2( f/w*pt3d.x/pt3d.z, f/h*pt3d.y/pt3d.z ); // \u6A21\u578B\u53D8\u6362\u5230\u76F8\u673A\u5750\u6807\u7CFB\u4E0B
  125. // vec2 uv = vec2( focal/captureWidth*pt3d.x/pt3d.z, focal/captureHeight*pt3d.y/pt3d.z ); // \u6A21\u578B\u53D8\u6362\u5230\u76F8\u673A\u5750\u6807\u7CFB\u4E0B
  126. // uv.x = uv.x + 0.5;
  127. // uv.y = uv.y + 0.5;
  128. // return uv;
  129. return focal_width_height.x / focal_width_height.yz *pt3d.xy/pt3d.z + 0.5;
  130. }
  131. void main()
  132. {
  133. vec3 yuv;
  134. vec3 rgb;
  135. vec2 uv;
  136. vec3 color = vec3(0,0,0);
  137. vec3 flash_color = fireworkLight * 1000.0 / fireworkDistance * fireworkCosTheta * vec3(1,0,0);
  138. float shadow = 1.0;
  139. if (haveShadowLight > 0.5)
  140. {
  141. shadow = ShadowCalculation(vPositionFromLight, shadowSampler);
  142. }
  143. // uv = SampleTex( normalize(ModelPos), vec2(captureWidth, captureHeight));
  144. uv = SampleTex( normalize(ModelPos) );
  145. if( isYUV < 0.5 )
  146. {
  147. color = texture2D(texture_video, uv).rgb;
  148. }else{
  149. const mat4 YUV2RGB = mat4
  150. (
  151. 1.1643828125, 0, 1.59602734375, -.87078515625,
  152. 1.1643828125, -.39176171875, -.81296875, .52959375,
  153. 1.1643828125, 2.017234375, 0, -1.081390625,
  154. 0, 0, 0, 1
  155. );
  156. vec4 result = vec4(
  157. texture2D(texture_video, vec2(uv.x, uv.y*0.666666 + 0.333333 ) ).x,
  158. texture2D(texture_video, vec2(uv.x * 0.5, uv.y*0.333333 ) ).x,
  159. texture2D(texture_video, vec2(0.5 + uv.x * 0.5, uv.y*0.333333 ) ).x,
  160. 1) * YUV2RGB;
  161. color = clamp(result.rgb, 0.0, 1.0);
  162. }
  163. if( uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 )
  164. {
  165. color = vec3(0,0,0);
  166. }
  167. // gl_FragColor = vec4(shadow, shadow, shadow, 1.0);
  168. gl_FragColor = vec4(shadow * (color + flash_color) * 1.0, 1.0);
  169. }
  170. `
  171. , pureVideoVertex = `precision highp float;
  172. varying vec3 ModelPos;
  173. varying vec4 vPositionFromLight;
  174. varying float fireworkDistance;
  175. varying float fireworkCosTheta;
  176. attribute vec2 uv;
  177. attribute vec3 position;
  178. attribute vec4 world0;
  179. attribute vec4 world1;
  180. attribute vec4 world2;
  181. attribute vec4 world3;
  182. #ifdef NORMAL
  183. attribute vec3 normal;
  184. #endif
  185. uniform vec3 fireworkLightPosition;
  186. uniform mat4 view;
  187. uniform mat4 projection;
  188. uniform mat4 lightSpaceMatrix;
  189. uniform mat4 world;
  190. uniform mat4 worldViewProjection;
  191. float DistanceCalculation(vec3 Q, vec3 P)
  192. {
  193. 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);
  194. }
  195. float CosThetaCalculation(vec3 Q, vec3 P)
  196. {
  197. return max(0.,dot(Q, P));
  198. }
  199. void main()
  200. {
  201. #include<instancesVertex>
  202. vPositionFromLight = lightSpaceMatrix * finalWorld * vec4(position, 1.0);
  203. // fireworkDistance = DistanceCalculation(vec3(finalWorld * vec4(position, 1.0)), fireworkLightPosition);
  204. fireworkDistance = distance(vec3(finalWorld * vec4(position, 1.0)), fireworkLightPosition);
  205. fireworkCosTheta = 1.0;
  206. #ifdef NORMAL
  207. vec3 directionFirework = fireworkLightPosition.xyz - vec3(finalWorld * vec4(position, 1.0));
  208. directionFirework = normalize(directionFirework);
  209. // directionFirework = directionFirework / (directionFirework.x * directionFirework.x + directionFirework.y * directionFirework.y + directionFirework.z * directionFirework.z);
  210. fireworkCosTheta = CosThetaCalculation(directionFirework, normal);
  211. #endif
  212. ModelPos = vec3( view * finalWorld * vec4(position , 1.0));
  213. gl_Position = projection * view * finalWorld * vec4(position , 1.0);
  214. }
  215. `
  216. , panoFragment = `precision highp float;
  217. uniform float isYUV; // false: 0, true: 1.0
  218. varying vec2 TexCoords;
  219. varying vec3 WorldPos;
  220. varying vec3 vNormal;
  221. uniform float haveShadowLight;
  222. varying vec4 vPositionFromLight;
  223. uniform float fireworkLight;
  224. varying float fireworkDistance;
  225. varying float fireworkCosTheta;
  226. uniform sampler2D shadowSampler;
  227. uniform vec3 centre_pose;
  228. uniform sampler2D texture_pano;
  229. const float inv_2_PI = 0.1591549; // 1 / (2 * pi)
  230. const float inv_PI = 0.3183099; // 1 / ( pi)
  231. const vec2 invAtan = vec2(0.1591549, 0.3183099);
  232. float unpack(vec4 color)
  233. {
  234. 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);
  235. return dot(color, bit_shift);
  236. }
  237. float ShadowCalculation(vec4 vPositionFromLight, sampler2D ShadowMap)
  238. {
  239. vec3 projCoords = vPositionFromLight.xyz / vPositionFromLight.w;
  240. vec3 depth = 0.5 * projCoords + vec3(0.5);
  241. vec2 uv = depth.xy;
  242. if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)
  243. {
  244. return 1.0;
  245. }
  246. #ifndef SHADOWFULLFLOAT
  247. float shadow = unpack(texture2D(ShadowMap, uv));
  248. #else
  249. float shadow = texture2D(ShadowMap, uv).x;
  250. #endif
  251. if (depth.z > shadow)
  252. {
  253. return 0.7;
  254. }
  255. else
  256. {
  257. return 1.0;
  258. }
  259. }
  260. vec2 SampleSphericalMap(vec3 pt3d)
  261. {
  262. vec2 uv = vec2( atan(-pt3d.z,pt3d.x), atan( pt3d.y, sqrt(pt3d.x*pt3d.x + pt3d.z * pt3d.z)));
  263. uv.x = 0.5 + uv.x * inv_2_PI ; // yaw: \u6C34\u5E73\u65B9\u5411 \uFF0C0 \u5230 360 \uFF0C \u5BF9\u5E948k\u7684\u5BBD
  264. uv.y = 0.5 + uv.y * inv_PI ; // pitch: \u7AD6\u76F4\u65B9\u5411\uFF0C -64 \u5230 64 \uFF0C\u5BF9\u5E944k\u7684\u957F
  265. return vec2(uv.x,uv.y);
  266. }
  267. vec3 fitUint8Range(vec3 color)
  268. {
  269. if( color.x < 0.0 ){color.x = 0.0;}
  270. if( color.x > 1.0 ){color.x = 1.0;}
  271. if( color.y < 0.0 ){color.y = 0.0;}
  272. if( color.y > 1.0 ){color.y = 1.0;}
  273. if( color.z < 0.0 ){color.z = 0.0;}
  274. if( color.z > 1.0 ){color.z = 1.0;}
  275. return color;
  276. }
  277. void main()
  278. {
  279. // // Debug
  280. // vec3 vLightPosition = vec3(0,10,100);
  281. // // World values
  282. // vec3 vPositionW = vec3( WorldPos.x, WorldPos.y, WorldPos.z );
  283. // vec3 vNormalW = normalize( vNormal) ;
  284. // vec3 viewDirectionW = normalize(vPositionW);
  285. // // Light
  286. // vec3 lightVectorW = normalize(vLightPosition - vPositionW);
  287. // // diffuse
  288. // float ndl = max(0., dot(vNormalW, lightVectorW));
  289. // gl_FragColor = vec4( ndl, ndl, ndl, 1.);
  290. vec2 uv;
  291. vec3 color = vec3(0,0,0);
  292. vec3 flash_color = fireworkLight * 1000.0 / fireworkDistance * fireworkCosTheta * vec3(1,0,0);
  293. float shadow = 1.0;
  294. if (haveShadowLight > 0.5)
  295. {
  296. shadow = ShadowCalculation(vPositionFromLight, shadowSampler);
  297. }
  298. uv = SampleSphericalMap(normalize( WorldPos - centre_pose ));
  299. if( isYUV < 0.5 )
  300. {
  301. color = texture2D(texture_pano, uv).rgb;
  302. }else{
  303. const mat4 YUV2RGB = mat4
  304. (
  305. 1.1643828125, 0, 1.59602734375, -.87078515625,
  306. 1.1643828125, -.39176171875, -.81296875, .52959375,
  307. 1.1643828125, 2.017234375, 0, -1.081390625,
  308. 0, 0, 0, 1
  309. );
  310. vec4 result = vec4(
  311. texture2D(texture_pano, vec2(uv.x, uv.y*0.666666 + 0.333333 ) ).x,
  312. texture2D(texture_pano, vec2(uv.x * 0.5, uv.y*0.333333 ) ).x,
  313. texture2D(texture_pano, vec2(0.5 + uv.x * 0.5, uv.y*0.333333 ) ).x,
  314. 1) * YUV2RGB;
  315. color = fitUint8Range(result.rgb);
  316. }
  317. if( uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 )
  318. {
  319. color = vec3(0,0,0);
  320. }
  321. gl_FragColor = vec4( shadow * (color + flash_color), 1.0);
  322. }`
  323. , panoVertex = `precision highp float;
  324. varying vec2 TexCoords;
  325. varying vec3 vNormal;
  326. varying vec3 WorldPos;
  327. varying vec4 vPositionFromLight;
  328. varying float fireworkDistance;
  329. varying float fireworkCosTheta;
  330. uniform vec3 fireworkLightPosition;
  331. uniform mat4 lightSpaceMatrix;
  332. attribute vec3 normal;
  333. attribute vec2 uv;
  334. attribute vec3 position;
  335. uniform mat4 view;
  336. uniform mat4 projection;
  337. uniform mat4 world;
  338. uniform mat4 worldViewProjection;
  339. attribute vec4 world0;
  340. attribute vec4 world1;
  341. attribute vec4 world2;
  342. attribute vec4 world3;
  343. float DistanceCalculation(vec3 Q, vec3 P)
  344. {
  345. 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);
  346. }
  347. float CosThetaCalculation(vec3 Q, vec3 P)
  348. {
  349. return max(0.,dot(Q, P));
  350. }
  351. void main()
  352. {
  353. #include<instancesVertex>
  354. vPositionFromLight = lightSpaceMatrix * world * vec4(position, 1.0);
  355. fireworkDistance = DistanceCalculation(vec3(finalWorld * vec4(position, 1.0)), fireworkLightPosition);
  356. fireworkCosTheta = 1.0;
  357. vec3 newP = vec3( finalWorld * vec4(position, 1.0) );
  358. WorldPos = newP;
  359. TexCoords = uv;
  360. vNormal = normal;
  361. gl_Position = projection * view * vec4(newP , 1.0);
  362. }
  363. `;