MeshGouraudMaterial.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. ( function () {
  2. /**
  3. * MeshGouraudMaterial
  4. *
  5. * Lambert illumination model with Gouraud (per-vertex) shading
  6. *
  7. */
  8. const GouraudShader = {
  9. uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib.common, THREE.UniformsLib.specularmap, THREE.UniformsLib.envmap, THREE.UniformsLib.aomap, THREE.UniformsLib.lightmap, THREE.UniformsLib.emissivemap, THREE.UniformsLib.fog, THREE.UniformsLib.lights, {
  10. emissive: {
  11. value: new THREE.Color( 0x000000 )
  12. }
  13. } ] ),
  14. vertexShader: /* glsl */`
  15. #define GOURAUD
  16. varying vec3 vLightFront;
  17. varying vec3 vIndirectFront;
  18. #ifdef DOUBLE_SIDED
  19. varying vec3 vLightBack;
  20. varying vec3 vIndirectBack;
  21. #endif
  22. #include <common>
  23. #include <uv_pars_vertex>
  24. #include <uv2_pars_vertex>
  25. #include <envmap_pars_vertex>
  26. #include <bsdfs>
  27. #include <lights_pars_begin>
  28. #include <color_pars_vertex>
  29. #include <fog_pars_vertex>
  30. #include <morphtarget_pars_vertex>
  31. #include <skinning_pars_vertex>
  32. #include <shadowmap_pars_vertex>
  33. #include <logdepthbuf_pars_vertex>
  34. #include <clipping_planes_pars_vertex>
  35. void main() {
  36. #include <uv_vertex>
  37. #include <uv2_vertex>
  38. #include <color_vertex>
  39. #include <morphcolor_vertex>
  40. #include <beginnormal_vertex>
  41. #include <morphnormal_vertex>
  42. #include <skinbase_vertex>
  43. #include <skinnormal_vertex>
  44. #include <defaultnormal_vertex>
  45. #include <begin_vertex>
  46. #include <morphtarget_vertex>
  47. #include <skinning_vertex>
  48. #include <project_vertex>
  49. #include <logdepthbuf_vertex>
  50. #include <clipping_planes_vertex>
  51. #include <worldpos_vertex>
  52. #include <envmap_vertex>
  53. // inlining legacy <lights_lambert_vertex>
  54. vec3 diffuse = vec3( 1.0 );
  55. GeometricContext geometry;
  56. geometry.position = mvPosition.xyz;
  57. geometry.normal = normalize( transformedNormal );
  58. geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );
  59. GeometricContext backGeometry;
  60. backGeometry.position = geometry.position;
  61. backGeometry.normal = -geometry.normal;
  62. backGeometry.viewDir = geometry.viewDir;
  63. vLightFront = vec3( 0.0 );
  64. vIndirectFront = vec3( 0.0 );
  65. #ifdef DOUBLE_SIDED
  66. vLightBack = vec3( 0.0 );
  67. vIndirectBack = vec3( 0.0 );
  68. #endif
  69. IncidentLight directLight;
  70. float dotNL;
  71. vec3 directLightColor_Diffuse;
  72. vIndirectFront += getAmbientLightIrradiance( ambientLightColor );
  73. vIndirectFront += getLightProbeIrradiance( lightProbe, geometry.normal );
  74. #ifdef DOUBLE_SIDED
  75. vIndirectBack += getAmbientLightIrradiance( ambientLightColor );
  76. vIndirectBack += getLightProbeIrradiance( lightProbe, backGeometry.normal );
  77. #endif
  78. #if NUM_POINT_LIGHTS > 0
  79. #pragma unroll_loop_start
  80. for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  81. getPointLightInfo( pointLights[ i ], geometry, directLight );
  82. dotNL = dot( geometry.normal, directLight.direction );
  83. directLightColor_Diffuse = directLight.color;
  84. vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
  85. #ifdef DOUBLE_SIDED
  86. vLightBack += saturate( - dotNL ) * directLightColor_Diffuse;
  87. #endif
  88. }
  89. #pragma unroll_loop_end
  90. #endif
  91. #if NUM_SPOT_LIGHTS > 0
  92. #pragma unroll_loop_start
  93. for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  94. getSpotLightInfo( spotLights[ i ], geometry, directLight );
  95. dotNL = dot( geometry.normal, directLight.direction );
  96. directLightColor_Diffuse = directLight.color;
  97. vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
  98. #ifdef DOUBLE_SIDED
  99. vLightBack += saturate( - dotNL ) * directLightColor_Diffuse;
  100. #endif
  101. }
  102. #pragma unroll_loop_end
  103. #endif
  104. #if NUM_DIR_LIGHTS > 0
  105. #pragma unroll_loop_start
  106. for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  107. getDirectionalLightInfo( directionalLights[ i ], geometry, directLight );
  108. dotNL = dot( geometry.normal, directLight.direction );
  109. directLightColor_Diffuse = directLight.color;
  110. vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
  111. #ifdef DOUBLE_SIDED
  112. vLightBack += saturate( - dotNL ) * directLightColor_Diffuse;
  113. #endif
  114. }
  115. #pragma unroll_loop_end
  116. #endif
  117. #if NUM_HEMI_LIGHTS > 0
  118. #pragma unroll_loop_start
  119. for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  120. vIndirectFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
  121. #ifdef DOUBLE_SIDED
  122. vIndirectBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometry.normal );
  123. #endif
  124. }
  125. #pragma unroll_loop_end
  126. #endif
  127. #include <shadowmap_vertex>
  128. #include <fog_vertex>
  129. }`,
  130. fragmentShader: /* glsl */`
  131. #define GOURAUD
  132. uniform vec3 diffuse;
  133. uniform vec3 emissive;
  134. uniform float opacity;
  135. varying vec3 vLightFront;
  136. varying vec3 vIndirectFront;
  137. #ifdef DOUBLE_SIDED
  138. varying vec3 vLightBack;
  139. varying vec3 vIndirectBack;
  140. #endif
  141. #include <common>
  142. #include <packing>
  143. #include <dithering_pars_fragment>
  144. #include <color_pars_fragment>
  145. #include <uv_pars_fragment>
  146. #include <uv2_pars_fragment>
  147. #include <map_pars_fragment>
  148. #include <alphamap_pars_fragment>
  149. #include <alphatest_pars_fragment>
  150. #include <aomap_pars_fragment>
  151. #include <lightmap_pars_fragment>
  152. #include <emissivemap_pars_fragment>
  153. #include <envmap_common_pars_fragment>
  154. #include <envmap_pars_fragment>
  155. #include <bsdfs>
  156. #include <lights_pars_begin>
  157. #include <fog_pars_fragment>
  158. #include <shadowmap_pars_fragment>
  159. #include <shadowmask_pars_fragment>
  160. #include <specularmap_pars_fragment>
  161. #include <logdepthbuf_pars_fragment>
  162. #include <clipping_planes_pars_fragment>
  163. void main() {
  164. #include <clipping_planes_fragment>
  165. vec4 diffuseColor = vec4( diffuse, opacity );
  166. ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
  167. vec3 totalEmissiveRadiance = emissive;
  168. #include <logdepthbuf_fragment>
  169. #include <map_fragment>
  170. #include <color_fragment>
  171. #include <alphamap_fragment>
  172. #include <alphatest_fragment>
  173. #include <specularmap_fragment>
  174. #include <emissivemap_fragment>
  175. // accumulation
  176. #ifdef DOUBLE_SIDED
  177. reflectedLight.indirectDiffuse += ( gl_FrontFacing ) ? vIndirectFront : vIndirectBack;
  178. #else
  179. reflectedLight.indirectDiffuse += vIndirectFront;
  180. #endif
  181. #include <lightmap_fragment>
  182. reflectedLight.indirectDiffuse *= BRDF_Lambert( diffuseColor.rgb );
  183. #ifdef DOUBLE_SIDED
  184. reflectedLight.directDiffuse = ( gl_FrontFacing ) ? vLightFront : vLightBack;
  185. #else
  186. reflectedLight.directDiffuse = vLightFront;
  187. #endif
  188. reflectedLight.directDiffuse *= BRDF_Lambert( diffuseColor.rgb ) * getShadowMask();
  189. // modulation
  190. #include <aomap_fragment>
  191. vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;
  192. #include <envmap_fragment>
  193. #include <output_fragment>
  194. #include <tonemapping_fragment>
  195. #include <encodings_fragment>
  196. #include <fog_fragment>
  197. #include <premultiplied_alpha_fragment>
  198. #include <dithering_fragment>
  199. }`
  200. };
  201. //
  202. class MeshGouraudMaterial extends THREE.ShaderMaterial {
  203. constructor( parameters ) {
  204. super();
  205. this.isMeshGouraudMaterial = true;
  206. this.type = 'MeshGouraudMaterial';
  207. //this.color = new THREE.Color( 0xffffff ); // diffuse
  208. //this.map = null;
  209. //this.lightMap = null;
  210. //this.lightMapIntensity = 1.0;
  211. //this.aoMap = null;
  212. //this.aoMapIntensity = 1.0;
  213. //this.emissive = new THREE.Color( 0x000000 );
  214. //this.emissiveIntensity = 1.0;
  215. //this.emissiveMap = null;
  216. //this.specularMap = null;
  217. //this.alphaMap = null;
  218. //this.envMap = null;
  219. this.combine = THREE.MultiplyOperation; // combine has no uniform
  220. //this.reflectivity = 1;
  221. //this.refractionRatio = 0.98;
  222. this.fog = false; // set to use scene fog
  223. this.lights = true; // set to use scene lights
  224. this.clipping = false; // set to use user-defined clipping planes
  225. const shader = GouraudShader;
  226. this.defines = Object.assign( {}, shader.defines );
  227. this.uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  228. this.vertexShader = shader.vertexShader;
  229. this.fragmentShader = shader.fragmentShader;
  230. const exposePropertyNames = [ 'map', 'lightMap', 'lightMapIntensity', 'aoMap', 'aoMapIntensity', 'emissive', 'emissiveIntensity', 'emissiveMap', 'specularMap', 'alphaMap', 'envMap', 'reflectivity', 'refractionRatio', 'opacity', 'diffuse' ];
  231. for ( const propertyName of exposePropertyNames ) {
  232. Object.defineProperty( this, propertyName, {
  233. get: function () {
  234. return this.uniforms[ propertyName ].value;
  235. },
  236. set: function ( value ) {
  237. this.uniforms[ propertyName ].value = value;
  238. }
  239. } );
  240. }
  241. Object.defineProperty( this, 'color', Object.getOwnPropertyDescriptor( this, 'diffuse' ) );
  242. this.setValues( parameters );
  243. }
  244. copy( source ) {
  245. super.copy( source );
  246. this.color.copy( source.color );
  247. this.map = source.map;
  248. this.lightMap = source.lightMap;
  249. this.lightMapIntensity = source.lightMapIntensity;
  250. this.aoMap = source.aoMap;
  251. this.aoMapIntensity = source.aoMapIntensity;
  252. this.emissive.copy( source.emissive );
  253. this.emissiveMap = source.emissiveMap;
  254. this.emissiveIntensity = source.emissiveIntensity;
  255. this.specularMap = source.specularMap;
  256. this.alphaMap = source.alphaMap;
  257. this.envMap = source.envMap;
  258. this.combine = source.combine;
  259. this.reflectivity = source.reflectivity;
  260. this.refractionRatio = source.refractionRatio;
  261. this.wireframe = source.wireframe;
  262. this.wireframeLinewidth = source.wireframeLinewidth;
  263. this.wireframeLinecap = source.wireframeLinecap;
  264. this.wireframeLinejoin = source.wireframeLinejoin;
  265. this.fog = source.fog;
  266. return this;
  267. }
  268. }
  269. THREE.MeshGouraudMaterial = MeshGouraudMaterial;
  270. } )();