ShaderMaterialHelper.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const vertexShader = `
  2. #include <clipping_planes_pars_vertex>
  3. varying vec2 vUv;
  4. void main() {
  5. vec3 transformed = vec3( position );
  6. vec4 mvPosition = modelViewMatrix * vec4( transformed, 1.0 );
  7. #include <clipping_planes_vertex>
  8. vUv = uv;
  9. gl_Position = projectionMatrix * mvPosition;
  10. }
  11. `;
  12. const fragmentShader = `
  13. #include <clipping_planes_pars_fragment>
  14. uniform vec3 iResolution;
  15. uniform sampler2D tDiffuse;
  16. varying vec2 vUv;
  17. uniform float fSharpness;
  18. uniform float fBrightness;
  19. uniform float fContrast;
  20. float sharpness = fSharpness / 100.0;
  21. float brightness = fBrightness / 255.0;
  22. float contrast = fContrast / 100.0;
  23. vec3 texsample(const int x, const int y, in vec2 fragCoord) {
  24. vec2 uv = fragCoord.xy ;
  25. uv = (uv + vec2(x, y)) / iResolution.xy;
  26. return texture2D(tDiffuse, uv).xyz;
  27. }
  28. vec3 texfilter(in vec2 fragCoord) {
  29. vec3 sum = texsample(-1, -1, fragCoord) * 0. + texsample(-1, 0, fragCoord) * -1. * sharpness + texsample(-1, 1, fragCoord) * 0. + texsample(0, -1, fragCoord) * -1. * sharpness + texsample(0, 0, fragCoord) * (4. * sharpness + 1.) + texsample(0, 1, fragCoord) * -1. * sharpness + texsample(1, -1, fragCoord) * 0. + texsample(1, 0, fragCoord) * -1. * sharpness + texsample(1, 1, fragCoord) * 0.;
  30. return sum;
  31. }
  32. void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  33. vec2 fc = fragCoord.xy;
  34. vec3 col = texfilter(fc);
  35. vec3 colB = col.rgb + vec3(brightness);
  36. vec3 colC = (colB - vec3(0.5)) * contrast + vec3(0.5);
  37. fragColor = vec4(colC, 1);
  38. }
  39. void main() {
  40. #include <clipping_planes_fragment>
  41. mainImage(gl_FragColor, vUv * iResolution.xy);
  42. }
  43. `;
  44. var shaderUniformsManager = {
  45. Sharpness: 0.0,
  46. Brightness: 0.0,
  47. Contrast: 100.0,
  48. };
  49. //通过texture生成shaderMaterial
  50. function ShaderMaterialHelper(texture, clippingPlanes) {
  51. const cubeShader = {
  52. uniforms: {
  53. iResolution: {
  54. value: new THREE.Vector3(512, 256, 1),
  55. },
  56. tDiffuse: {
  57. type: "t",
  58. value: texture,
  59. },
  60. fSharpness: { type: "f", value: shaderUniformsManager.Sharpness },
  61. fBrightness: { type: "f", value: shaderUniformsManager.Brightness },
  62. fContrast: { type: "f", value: shaderUniformsManager.Contrast },
  63. },
  64. vertexShader: vertexShader,
  65. fragmentShader: fragmentShader,
  66. };
  67. cubeMaterial = new THREE.ShaderMaterial({
  68. uniforms: cubeShader.uniforms,
  69. vertexShader: cubeShader.vertexShader,
  70. fragmentShader: cubeShader.fragmentShader,
  71. side: THREE.FrontSide,
  72. clippingPlanes: clippingPlanes,
  73. clipping: true,
  74. });
  75. return cubeMaterial;
  76. }