MeshMatcapNodeMaterial.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
  2. import { materialReference } from '../accessors/MaterialReferenceNode.js';
  3. import { diffuseColor } from '../core/PropertyNode.js';
  4. import { vec2, vec3 } from '../shadernode/ShaderNode.js';
  5. import { positionViewDirection } from '../accessors/PositionNode.js';
  6. import { MeshMatcapMaterial } from 'three';
  7. import { transformedNormalView } from '../accessors/NormalNode.js';
  8. import { mix } from '../math/MathNode.js';
  9. const defaultValues = new MeshMatcapMaterial();
  10. class MeshMatcapNodeMaterial extends NodeMaterial {
  11. constructor( parameters ) {
  12. super();
  13. this.isMeshMatcapNodeMaterial = true;
  14. this.lights = false;
  15. this.setDefaultValues( defaultValues );
  16. this.setValues( parameters );
  17. }
  18. setupVariants( builder ) {
  19. const x = vec3( positionViewDirection.z, 0.0, positionViewDirection.x.negate() ).normalize();
  20. const y = positionViewDirection.cross( x );
  21. const uv = vec2( x.dot( transformedNormalView ), y.dot( transformedNormalView ) ).mul( 0.495 ).add( 0.5 ) ; // 0.495 to remove artifacts caused by undersized matcap disks
  22. let matcapColor;
  23. if ( builder.material.matcap ) {
  24. matcapColor = materialReference( 'matcap', 'texture' ).context( { getUV: () => uv } );
  25. } else {
  26. matcapColor = vec3( mix( 0.2, 0.8, uv.y ) ); // default if matcap is missing
  27. }
  28. diffuseColor.rgb.mulAssign( matcapColor.rgb );
  29. }
  30. }
  31. export default MeshMatcapNodeMaterial;
  32. addNodeMaterial( 'MeshMatcapNodeMaterial', MeshMatcapNodeMaterial );