MeshSSSNodeMaterial.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import MeshPhysicalNodeMaterial from './MeshPhysicalNodeMaterial.js';
  2. import PhysicalLightingModel from '../../nodes/functions/PhysicalLightingModel.js';
  3. import { transformedNormalView } from '../../nodes/accessors/Normal.js';
  4. import { positionViewDirection } from '../../nodes/accessors/Position.js';
  5. import { float, vec3 } from '../../nodes/tsl/TSLBase.js';
  6. class SSSLightingModel extends PhysicalLightingModel {
  7. constructor( useClearcoat, useSheen, useIridescence, useSSS ) {
  8. super( useClearcoat, useSheen, useIridescence );
  9. this.useSSS = useSSS;
  10. }
  11. direct( { lightDirection, lightColor, reflectedLight }, stack, builder ) {
  12. if ( this.useSSS === true ) {
  13. const material = builder.material;
  14. const { thicknessColorNode, thicknessDistortionNode, thicknessAmbientNode, thicknessAttenuationNode, thicknessPowerNode, thicknessScaleNode } = material;
  15. const scatteringHalf = lightDirection.add( transformedNormalView.mul( thicknessDistortionNode ) ).normalize();
  16. const scatteringDot = float( positionViewDirection.dot( scatteringHalf.negate() ).saturate().pow( thicknessPowerNode ).mul( thicknessScaleNode ) );
  17. const scatteringIllu = vec3( scatteringDot.add( thicknessAmbientNode ).mul( thicknessColorNode ) );
  18. reflectedLight.directDiffuse.addAssign( scatteringIllu.mul( thicknessAttenuationNode.mul( lightColor ) ) );
  19. }
  20. super.direct( { lightDirection, lightColor, reflectedLight }, stack, builder );
  21. }
  22. }
  23. class MeshSSSNodeMaterial extends MeshPhysicalNodeMaterial {
  24. static get type() {
  25. return 'MeshSSSNodeMaterial';
  26. }
  27. constructor( parameters ) {
  28. super( parameters );
  29. this.thicknessColorNode = null;
  30. this.thicknessDistortionNode = float( 0.1 );
  31. this.thicknessAmbientNode = float( 0.0 );
  32. this.thicknessAttenuationNode = float( .1 );
  33. this.thicknessPowerNode = float( 2.0 );
  34. this.thicknessScaleNode = float( 10.0 );
  35. }
  36. get useSSS() {
  37. return this.thicknessColorNode !== null;
  38. }
  39. setupLightingModel( /*builder*/ ) {
  40. return new SSSLightingModel( this.useClearcoat, this.useSheen, this.useIridescence, this.useSSS );
  41. }
  42. copy( source ) {
  43. this.thicknessColorNode = source.thicknessColorNode;
  44. this.thicknessDistortionNode = source.thicknessDistortionNode;
  45. this.thicknessAmbientNode = source.thicknessAmbientNode;
  46. this.thicknessAttenuationNode = source.thicknessAttenuationNode;
  47. this.thicknessPowerNode = source.thicknessPowerNode;
  48. this.thicknessScaleNode = source.thicknessScaleNode;
  49. return super.copy( source );
  50. }
  51. }
  52. export default MeshSSSNodeMaterial;
粤ICP备19079148号