PackedPhongMaterial.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ( function () {
  2. /**
  3. * `PackedPhongMaterial` inherited from THREE.MeshPhongMaterial
  4. *
  5. * @param {Object} parameters
  6. */
  7. class PackedPhongMaterial extends THREE.MeshPhongMaterial {
  8. constructor( parameters ) {
  9. super();
  10. this.defines = {};
  11. this.type = 'PackedPhongMaterial';
  12. this.uniforms = THREE.UniformsUtils.merge( [ THREE.ShaderLib.phong.uniforms, {
  13. quantizeMatPos: {
  14. value: null
  15. },
  16. quantizeMatUV: {
  17. value: null
  18. }
  19. } ] );
  20. this.vertexShader = [ '#define PHONG', 'varying vec3 vViewPosition;', THREE.ShaderChunk.common, THREE.ShaderChunk.uv_pars_vertex, THREE.ShaderChunk.uv2_pars_vertex, THREE.ShaderChunk.displacementmap_pars_vertex, THREE.ShaderChunk.envmap_pars_vertex, THREE.ShaderChunk.color_pars_vertex, THREE.ShaderChunk.fog_pars_vertex, THREE.ShaderChunk.normal_pars_vertex, THREE.ShaderChunk.morphtarget_pars_vertex, THREE.ShaderChunk.skinning_pars_vertex, THREE.ShaderChunk.shadowmap_pars_vertex, THREE.ShaderChunk.logdepthbuf_pars_vertex, THREE.ShaderChunk.clipping_planes_pars_vertex, `#ifdef USE_PACKED_NORMAL
  21. #if USE_PACKED_NORMAL == 0
  22. vec3 decodeNormal(vec3 packedNormal)
  23. {
  24. float x = packedNormal.x * 2.0 - 1.0;
  25. float y = packedNormal.y * 2.0 - 1.0;
  26. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  27. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  28. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  29. }
  30. #endif
  31. #if USE_PACKED_NORMAL == 1
  32. vec3 decodeNormal(vec3 packedNormal)
  33. {
  34. vec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));
  35. if (v.z < 0.0)
  36. {
  37. v.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  38. }
  39. return normalize(v);
  40. }
  41. #endif
  42. #if USE_PACKED_NORMAL == 2
  43. vec3 decodeNormal(vec3 packedNormal)
  44. {
  45. vec3 v = (packedNormal * 2.0) - 1.0;
  46. return normalize(v);
  47. }
  48. #endif
  49. #endif`, `#ifdef USE_PACKED_POSITION
  50. #if USE_PACKED_POSITION == 0
  51. uniform mat4 quantizeMatPos;
  52. #endif
  53. #endif`, `#ifdef USE_PACKED_UV
  54. #if USE_PACKED_UV == 1
  55. uniform mat3 quantizeMatUV;
  56. #endif
  57. #endif`, `#ifdef USE_PACKED_UV
  58. #if USE_PACKED_UV == 0
  59. vec2 decodeUV(vec2 packedUV)
  60. {
  61. vec2 uv = (packedUV * 2.0) - 1.0;
  62. return uv;
  63. }
  64. #endif
  65. #if USE_PACKED_UV == 1
  66. vec2 decodeUV(vec2 packedUV)
  67. {
  68. vec2 uv = ( vec3(packedUV, 1.0) * quantizeMatUV ).xy;
  69. return uv;
  70. }
  71. #endif
  72. #endif`, 'void main() {', THREE.ShaderChunk.uv_vertex, `#ifdef USE_UV
  73. #ifdef USE_PACKED_UV
  74. vUv = decodeUV(vUv);
  75. #endif
  76. #endif`, THREE.ShaderChunk.uv2_vertex, THREE.ShaderChunk.color_vertex, THREE.ShaderChunk.beginnormal_vertex, `#ifdef USE_PACKED_NORMAL
  77. objectNormal = decodeNormal(objectNormal);
  78. #endif
  79. #ifdef USE_TANGENT
  80. vec3 objectTangent = vec3( tangent.xyz );
  81. #endif
  82. `, THREE.ShaderChunk.morphnormal_vertex, THREE.ShaderChunk.skinbase_vertex, THREE.ShaderChunk.skinnormal_vertex, THREE.ShaderChunk.defaultnormal_vertex, THREE.ShaderChunk.normal_vertex, THREE.ShaderChunk.begin_vertex, `#ifdef USE_PACKED_POSITION
  83. #if USE_PACKED_POSITION == 0
  84. transformed = ( vec4(transformed, 1.0) * quantizeMatPos ).xyz;
  85. #endif
  86. #endif`, THREE.ShaderChunk.morphtarget_vertex, THREE.ShaderChunk.skinning_vertex, THREE.ShaderChunk.displacementmap_vertex, THREE.ShaderChunk.project_vertex, THREE.ShaderChunk.logdepthbuf_vertex, THREE.ShaderChunk.clipping_planes_vertex, 'vViewPosition = - mvPosition.xyz;', THREE.ShaderChunk.worldpos_vertex, THREE.ShaderChunk.envmap_vertex, THREE.ShaderChunk.shadowmap_vertex, THREE.ShaderChunk.fog_vertex, '}' ].join( '\n' ); // Use the original THREE.MeshPhongMaterial's fragmentShader.
  87. this.fragmentShader = [ '#define PHONG', 'uniform vec3 diffuse;', 'uniform vec3 emissive;', 'uniform vec3 specular;', 'uniform float shininess;', 'uniform float opacity;', THREE.ShaderChunk.common, THREE.ShaderChunk.packing, THREE.ShaderChunk.dithering_pars_fragment, THREE.ShaderChunk.color_pars_fragment, THREE.ShaderChunk.uv_pars_fragment, THREE.ShaderChunk.uv2_pars_fragment, THREE.ShaderChunk.map_pars_fragment, THREE.ShaderChunk.alphamap_pars_fragment, THREE.ShaderChunk.aomap_pars_fragment, THREE.ShaderChunk.lightmap_pars_fragment, THREE.ShaderChunk.emissivemap_pars_fragment, THREE.ShaderChunk.envmap_common_pars_fragment, THREE.ShaderChunk.envmap_pars_fragment, THREE.ShaderChunk.cube_uv_reflection_fragment, THREE.ShaderChunk.fog_pars_fragment, THREE.ShaderChunk.bsdfs, THREE.ShaderChunk.lights_pars_begin, THREE.ShaderChunk.normal_pars_fragment, THREE.ShaderChunk.lights_phong_pars_fragment, THREE.ShaderChunk.shadowmap_pars_fragment, THREE.ShaderChunk.bumpmap_pars_fragment, THREE.ShaderChunk.normalmap_pars_fragment, THREE.ShaderChunk.specularmap_pars_fragment, THREE.ShaderChunk.logdepthbuf_pars_fragment, THREE.ShaderChunk.clipping_planes_pars_fragment, 'void main() {', THREE.ShaderChunk.clipping_planes_fragment, 'vec4 diffuseColor = vec4( diffuse, opacity );', 'ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );', 'vec3 totalEmissiveRadiance = emissive;', THREE.ShaderChunk.logdepthbuf_fragment, THREE.ShaderChunk.map_fragment, THREE.ShaderChunk.color_fragment, THREE.ShaderChunk.alphamap_fragment, THREE.ShaderChunk.alphatest_fragment, THREE.ShaderChunk.specularmap_fragment, THREE.ShaderChunk.normal_fragment_begin, THREE.ShaderChunk.normal_fragment_maps, THREE.ShaderChunk.emissivemap_fragment, // accumulation
  88. THREE.ShaderChunk.lights_phong_fragment, THREE.ShaderChunk.lights_fragment_begin, THREE.ShaderChunk.lights_fragment_maps, THREE.ShaderChunk.lights_fragment_end, // modulation
  89. THREE.ShaderChunk.aomap_fragment, 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;', THREE.ShaderChunk.envmap_fragment, 'gl_FragColor = vec4( outgoingLight, diffuseColor.a );', THREE.ShaderChunk.tonemapping_fragment, THREE.ShaderChunk.encodings_fragment, THREE.ShaderChunk.fog_fragment, THREE.ShaderChunk.premultiplied_alpha_fragment, THREE.ShaderChunk.dithering_fragment, '}' ].join( '\n' );
  90. this.setValues( parameters );
  91. }
  92. }
  93. THREE.PackedPhongMaterial = PackedPhongMaterial;
  94. } )();