MeshGouraudMaterial.js 9.5 KB

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