MeshGouraudMaterial.js 9.7 KB

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