MeshGouraudMaterial.js 9.6 KB

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