PackedPhongMaterial.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * `PackedPhongMaterial` inherited from THREE.MeshPhongMaterial
  3. *
  4. * @param {Object} parameters
  5. */
  6. import {
  7. MeshPhongMaterial,
  8. ShaderChunk,
  9. ShaderLib,
  10. UniformsUtils,
  11. } from 'three';
  12. class PackedPhongMaterial extends MeshPhongMaterial {
  13. constructor( parameters ) {
  14. super();
  15. this.defines = {};
  16. this.type = 'PackedPhongMaterial';
  17. this.uniforms = UniformsUtils.merge( [
  18. ShaderLib.phong.uniforms,
  19. {
  20. quantizeMatPos: { value: null },
  21. quantizeMatUV: { value: null }
  22. }
  23. ] );
  24. this.vertexShader = [
  25. '#define PHONG',
  26. 'varying vec3 vViewPosition;',
  27. ShaderChunk.common,
  28. ShaderChunk.uv_pars_vertex,
  29. ShaderChunk.uv2_pars_vertex,
  30. ShaderChunk.displacementmap_pars_vertex,
  31. ShaderChunk.envmap_pars_vertex,
  32. ShaderChunk.color_pars_vertex,
  33. ShaderChunk.fog_pars_vertex,
  34. ShaderChunk.normal_pars_vertex,
  35. ShaderChunk.morphtarget_pars_vertex,
  36. ShaderChunk.skinning_pars_vertex,
  37. ShaderChunk.shadowmap_pars_vertex,
  38. ShaderChunk.logdepthbuf_pars_vertex,
  39. ShaderChunk.clipping_planes_pars_vertex,
  40. `#ifdef USE_PACKED_NORMAL
  41. #if USE_PACKED_NORMAL == 0
  42. vec3 decodeNormal(vec3 packedNormal)
  43. {
  44. float x = packedNormal.x * 2.0 - 1.0;
  45. float y = packedNormal.y * 2.0 - 1.0;
  46. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  47. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  48. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  49. }
  50. #endif
  51. #if USE_PACKED_NORMAL == 1
  52. vec3 decodeNormal(vec3 packedNormal)
  53. {
  54. vec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));
  55. if (v.z < 0.0)
  56. {
  57. v.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  58. }
  59. return normalize(v);
  60. }
  61. #endif
  62. #if USE_PACKED_NORMAL == 2
  63. vec3 decodeNormal(vec3 packedNormal)
  64. {
  65. vec3 v = (packedNormal * 2.0) - 1.0;
  66. return normalize(v);
  67. }
  68. #endif
  69. #endif`,
  70. `#ifdef USE_PACKED_POSITION
  71. #if USE_PACKED_POSITION == 0
  72. uniform mat4 quantizeMatPos;
  73. #endif
  74. #endif`,
  75. `#ifdef USE_PACKED_UV
  76. #if USE_PACKED_UV == 1
  77. uniform mat3 quantizeMatUV;
  78. #endif
  79. #endif`,
  80. `#ifdef USE_PACKED_UV
  81. #if USE_PACKED_UV == 0
  82. vec2 decodeUV(vec2 packedUV)
  83. {
  84. vec2 uv = (packedUV * 2.0) - 1.0;
  85. return uv;
  86. }
  87. #endif
  88. #if USE_PACKED_UV == 1
  89. vec2 decodeUV(vec2 packedUV)
  90. {
  91. vec2 uv = ( vec3(packedUV, 1.0) * quantizeMatUV ).xy;
  92. return uv;
  93. }
  94. #endif
  95. #endif`,
  96. 'void main() {',
  97. ShaderChunk.uv_vertex,
  98. `#ifdef USE_UV
  99. #ifdef USE_PACKED_UV
  100. vUv = decodeUV(vUv);
  101. #endif
  102. #endif`,
  103. ShaderChunk.uv2_vertex,
  104. ShaderChunk.color_vertex,
  105. ShaderChunk.beginnormal_vertex,
  106. `#ifdef USE_PACKED_NORMAL
  107. objectNormal = decodeNormal(objectNormal);
  108. #endif
  109. #ifdef USE_TANGENT
  110. vec3 objectTangent = vec3( tangent.xyz );
  111. #endif
  112. `,
  113. ShaderChunk.morphnormal_vertex,
  114. ShaderChunk.skinbase_vertex,
  115. ShaderChunk.skinnormal_vertex,
  116. ShaderChunk.defaultnormal_vertex,
  117. ShaderChunk.normal_vertex,
  118. ShaderChunk.begin_vertex,
  119. `#ifdef USE_PACKED_POSITION
  120. #if USE_PACKED_POSITION == 0
  121. transformed = ( vec4(transformed, 1.0) * quantizeMatPos ).xyz;
  122. #endif
  123. #endif`,
  124. ShaderChunk.morphtarget_vertex,
  125. ShaderChunk.skinning_vertex,
  126. ShaderChunk.displacementmap_vertex,
  127. ShaderChunk.project_vertex,
  128. ShaderChunk.logdepthbuf_vertex,
  129. ShaderChunk.clipping_planes_vertex,
  130. 'vViewPosition = - mvPosition.xyz;',
  131. ShaderChunk.worldpos_vertex,
  132. ShaderChunk.envmap_vertex,
  133. ShaderChunk.shadowmap_vertex,
  134. ShaderChunk.fog_vertex,
  135. '}',
  136. ].join( '\n' );
  137. // Use the original MeshPhongMaterial's fragmentShader.
  138. this.fragmentShader = [
  139. '#define PHONG',
  140. 'uniform vec3 diffuse;',
  141. 'uniform vec3 emissive;',
  142. 'uniform vec3 specular;',
  143. 'uniform float shininess;',
  144. 'uniform float opacity;',
  145. ShaderChunk.common,
  146. ShaderChunk.packing,
  147. ShaderChunk.dithering_pars_fragment,
  148. ShaderChunk.color_pars_fragment,
  149. ShaderChunk.uv_pars_fragment,
  150. ShaderChunk.uv2_pars_fragment,
  151. ShaderChunk.map_pars_fragment,
  152. ShaderChunk.alphamap_pars_fragment,
  153. ShaderChunk.aomap_pars_fragment,
  154. ShaderChunk.lightmap_pars_fragment,
  155. ShaderChunk.emissivemap_pars_fragment,
  156. ShaderChunk.envmap_common_pars_fragment,
  157. ShaderChunk.envmap_pars_fragment,
  158. ShaderChunk.fog_pars_fragment,
  159. ShaderChunk.bsdfs,
  160. ShaderChunk.lights_pars_begin,
  161. ShaderChunk.normal_pars_fragment,
  162. ShaderChunk.lights_phong_pars_fragment,
  163. ShaderChunk.shadowmap_pars_fragment,
  164. ShaderChunk.bumpmap_pars_fragment,
  165. ShaderChunk.normalmap_pars_fragment,
  166. ShaderChunk.specularmap_pars_fragment,
  167. ShaderChunk.logdepthbuf_pars_fragment,
  168. ShaderChunk.clipping_planes_pars_fragment,
  169. 'void main() {',
  170. ShaderChunk.clipping_planes_fragment,
  171. 'vec4 diffuseColor = vec4( diffuse, opacity );',
  172. 'ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );',
  173. 'vec3 totalEmissiveRadiance = emissive;',
  174. ShaderChunk.logdepthbuf_fragment,
  175. ShaderChunk.map_fragment,
  176. ShaderChunk.color_fragment,
  177. ShaderChunk.alphamap_fragment,
  178. ShaderChunk.alphatest_fragment,
  179. ShaderChunk.specularmap_fragment,
  180. ShaderChunk.normal_fragment_begin,
  181. ShaderChunk.normal_fragment_maps,
  182. ShaderChunk.emissivemap_fragment,
  183. // accumulation
  184. ShaderChunk.lights_phong_fragment,
  185. ShaderChunk.lights_fragment_begin,
  186. ShaderChunk.lights_fragment_maps,
  187. ShaderChunk.lights_fragment_end,
  188. // modulation
  189. ShaderChunk.aomap_fragment,
  190. 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;',
  191. ShaderChunk.envmap_fragment,
  192. 'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
  193. ShaderChunk.tonemapping_fragment,
  194. ShaderChunk.encodings_fragment,
  195. ShaderChunk.fog_fragment,
  196. ShaderChunk.premultiplied_alpha_fragment,
  197. ShaderChunk.dithering_fragment,
  198. '}',
  199. ].join( '\n' );
  200. this.setValues( parameters );
  201. }
  202. }
  203. export { PackedPhongMaterial };