shader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. vec4 result = vec4(
  162. texture2D(texture_video, vec2(uv.x, uv.y*0.666666 + 0.333333 ) ).x,
  163. texture2D(texture_video, vec2(uv.x * 0.5, (uv.y + 1.)*0.333333 * 0.5 )).x,
  164. texture2D(texture_video, vec2(uv.x * 0.5, uv.y*0.333333 * 0.5 ) ).x,
  165. 1
  166. ) * YUV2RGB;
  167. color = clamp(result.rgb, 0.0, 1.0);
  168. }
  169. if( uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 )
  170. {
  171. color = vec3(0,0,0);
  172. }
  173. // gl_FragColor = vec4(shadow, shadow, shadow, 1.0);
  174. gl_FragColor = vec4(shadow * (color + flash_color) * 1.0, 1.0);
  175. }
  176. `
  177. , pureVideoVertex = `precision highp float;
  178. varying vec3 ModelPos;
  179. varying vec4 vPositionFromLight;
  180. varying float fireworkDistance;
  181. varying float fireworkCosTheta;
  182. attribute vec2 uv;
  183. attribute vec3 position;
  184. attribute vec4 world0;
  185. attribute vec4 world1;
  186. attribute vec4 world2;
  187. attribute vec4 world3;
  188. #ifdef NORMAL
  189. attribute vec3 normal;
  190. #endif
  191. uniform vec3 fireworkLightPosition;
  192. uniform mat4 view;
  193. uniform mat4 projection;
  194. uniform mat4 lightSpaceMatrix;
  195. uniform mat4 world;
  196. uniform mat4 worldViewProjection;
  197. float DistanceCalculation(vec3 Q, vec3 P)
  198. {
  199. 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);
  200. }
  201. float CosThetaCalculation(vec3 Q, vec3 P)
  202. {
  203. return max(0.,dot(Q, P));
  204. }
  205. void main()
  206. {
  207. #include<instancesVertex>
  208. vPositionFromLight = lightSpaceMatrix * finalWorld * vec4(position, 1.0);
  209. // fireworkDistance = DistanceCalculation(vec3(finalWorld * vec4(position, 1.0)), fireworkLightPosition);
  210. fireworkDistance = distance(vec3(finalWorld * vec4(position, 1.0)), fireworkLightPosition);
  211. fireworkCosTheta = 1.0;
  212. #ifdef NORMAL
  213. vec3 directionFirework = fireworkLightPosition.xyz - vec3(finalWorld * vec4(position, 1.0));
  214. directionFirework = normalize(directionFirework);
  215. // directionFirework = directionFirework / (directionFirework.x * directionFirework.x + directionFirework.y * directionFirework.y + directionFirework.z * directionFirework.z);
  216. fireworkCosTheta = CosThetaCalculation(directionFirework, normal);
  217. #endif
  218. ModelPos = vec3( view * finalWorld * vec4(position , 1.0));
  219. gl_Position = projection * view * finalWorld * vec4(position , 1.0);
  220. }
  221. `
  222. , panoFragment = `precision highp float;
  223. uniform float isYUV; // false: 0, true: 1.0
  224. varying vec2 TexCoords;
  225. varying vec3 WorldPos;
  226. varying vec3 vNormal;
  227. uniform float haveShadowLight;
  228. varying vec4 vPositionFromLight;
  229. uniform float fireworkLight;
  230. varying float fireworkDistance;
  231. varying float fireworkCosTheta;
  232. uniform sampler2D shadowSampler;
  233. uniform vec3 centre_pose;
  234. uniform sampler2D texture_pano;
  235. const float inv_2_PI = 0.1591549; // 1 / (2 * pi)
  236. const float inv_PI = 0.3183099; // 1 / ( pi)
  237. const vec2 invAtan = vec2(0.1591549, 0.3183099);
  238. float unpack(vec4 color)
  239. {
  240. 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);
  241. return dot(color, bit_shift);
  242. }
  243. float ShadowCalculation(vec4 vPositionFromLight, sampler2D ShadowMap)
  244. {
  245. vec3 projCoords = vPositionFromLight.xyz / vPositionFromLight.w;
  246. vec3 depth = 0.5 * projCoords + vec3(0.5);
  247. vec2 uv = depth.xy;
  248. if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)
  249. {
  250. return 1.0;
  251. }
  252. #ifndef SHADOWFULLFLOAT
  253. float shadow = unpack(texture2D(ShadowMap, uv));
  254. #else
  255. float shadow = texture2D(ShadowMap, uv).x;
  256. #endif
  257. if (depth.z > shadow)
  258. {
  259. return 0.7;
  260. }
  261. else
  262. {
  263. return 1.0;
  264. }
  265. }
  266. vec2 SampleSphericalMap(vec3 pt3d)
  267. {
  268. vec2 uv = vec2( atan(-pt3d.z,pt3d.x), atan( pt3d.y, sqrt(pt3d.x*pt3d.x + pt3d.z * pt3d.z)));
  269. uv.x = 0.5 + uv.x * inv_2_PI ; // yaw: \u6C34\u5E73\u65B9\u5411 \uFF0C0 \u5230 360 \uFF0C \u5BF9\u5E948k\u7684\u5BBD
  270. uv.y = 0.5 + uv.y * inv_PI ; // pitch: \u7AD6\u76F4\u65B9\u5411\uFF0C -64 \u5230 64 \uFF0C\u5BF9\u5E944k\u7684\u957F
  271. return vec2(uv.x,uv.y);
  272. }
  273. vec3 fitUint8Range(vec3 color)
  274. {
  275. if( color.x < 0.0 ){color.x = 0.0;}
  276. if( color.x > 1.0 ){color.x = 1.0;}
  277. if( color.y < 0.0 ){color.y = 0.0;}
  278. if( color.y > 1.0 ){color.y = 1.0;}
  279. if( color.z < 0.0 ){color.z = 0.0;}
  280. if( color.z > 1.0 ){color.z = 1.0;}
  281. return color;
  282. }
  283. void main()
  284. {
  285. // // Debug
  286. // vec3 vLightPosition = vec3(0,10,100);
  287. // // World values
  288. // vec3 vPositionW = vec3( WorldPos.x, WorldPos.y, WorldPos.z );
  289. // vec3 vNormalW = normalize( vNormal) ;
  290. // vec3 viewDirectionW = normalize(vPositionW);
  291. // // Light
  292. // vec3 lightVectorW = normalize(vLightPosition - vPositionW);
  293. // // diffuse
  294. // float ndl = max(0., dot(vNormalW, lightVectorW));
  295. // gl_FragColor = vec4( ndl, ndl, ndl, 1.);
  296. vec2 uv;
  297. vec3 color = vec3(0,0,0);
  298. vec3 flash_color = fireworkLight * 1000.0 / fireworkDistance * fireworkCosTheta * vec3(1,0,0);
  299. float shadow = 1.0;
  300. if (haveShadowLight > 0.5)
  301. {
  302. shadow = ShadowCalculation(vPositionFromLight, shadowSampler);
  303. }
  304. uv = SampleSphericalMap(normalize( WorldPos - centre_pose ));
  305. if( isYUV < 0.5 )
  306. {
  307. color = texture2D(texture_pano, uv).rgb;
  308. }else{
  309. const mat4 YUV2RGB = mat4
  310. (
  311. 1.1643828125, 0, 1.59602734375, -.87078515625,
  312. 1.1643828125, -.39176171875, -.81296875, .52959375,
  313. 1.1643828125, 2.017234375, 0, -1.081390625,
  314. 0, 0, 0, 1
  315. );
  316. vec4 result = vec4(
  317. texture2D(texture_pano, vec2(uv.x, uv.y*0.666666 + 0.333333 ) ).x,
  318. texture2D(texture_pano, vec2(uv.x * 0.5, uv.y*0.333333 ) ).x,
  319. texture2D(texture_pano, vec2(0.5 + uv.x * 0.5, uv.y*0.333333 ) ).x,
  320. 1) * YUV2RGB;
  321. color = fitUint8Range(result.rgb);
  322. }
  323. if( uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 )
  324. {
  325. color = vec3(0,0,0);
  326. }
  327. gl_FragColor = vec4( shadow * (color + flash_color), 1.0);
  328. }`
  329. , panoVertex = `precision highp float;
  330. varying vec2 TexCoords;
  331. varying vec3 vNormal;
  332. varying vec3 WorldPos;
  333. varying vec4 vPositionFromLight;
  334. varying float fireworkDistance;
  335. varying float fireworkCosTheta;
  336. uniform vec3 fireworkLightPosition;
  337. uniform mat4 lightSpaceMatrix;
  338. attribute vec3 normal;
  339. attribute vec2 uv;
  340. attribute vec3 position;
  341. uniform mat4 view;
  342. uniform mat4 projection;
  343. uniform mat4 world;
  344. uniform mat4 worldViewProjection;
  345. attribute vec4 world0;
  346. attribute vec4 world1;
  347. attribute vec4 world2;
  348. attribute vec4 world3;
  349. float DistanceCalculation(vec3 Q, vec3 P)
  350. {
  351. 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);
  352. }
  353. float CosThetaCalculation(vec3 Q, vec3 P)
  354. {
  355. return max(0.,dot(Q, P));
  356. }
  357. void main()
  358. {
  359. #include<instancesVertex>
  360. vPositionFromLight = lightSpaceMatrix * world * vec4(position, 1.0);
  361. fireworkDistance = DistanceCalculation(vec3(finalWorld * vec4(position, 1.0)), fireworkLightPosition);
  362. fireworkCosTheta = 1.0;
  363. vec3 newP = vec3( finalWorld * vec4(position, 1.0) );
  364. WorldPos = newP;
  365. TexCoords = uv;
  366. vNormal = normal;
  367. gl_Position = projection * view * vec4(newP , 1.0);
  368. }
  369. `;