FatPointsNodeMaterial.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
  2. import { varying } from '../core/VaryingNode.js';
  3. import { property } from '../core/PropertyNode.js';
  4. import { attribute } from '../core/AttributeNode.js';
  5. import { cameraProjectionMatrix } from '../accessors/CameraNode.js';
  6. import { materialColor } from '../accessors/MaterialNode.js';
  7. import { modelViewMatrix } from '../accessors/ModelNode.js';
  8. import { positionGeometry } from '../accessors/PositionNode.js';
  9. import { smoothstep } from '../math/MathNode.js';
  10. import { tslFn, vec2, vec4 } from '../shadernode/ShaderNode.js';
  11. import { uv } from '../accessors/UVNode.js';
  12. import { materialPointWidth } from '../accessors/FatPointsMaterialNode.js'; // or should this be a property, instead?
  13. import { viewport } from '../display/ViewportNode.js';
  14. import { color } from 'three/nodes';
  15. import { PointsMaterial } from 'three';
  16. const defaultValues = new PointsMaterial();
  17. class FatPointsNodeMaterial extends NodeMaterial {
  18. constructor( params = {} ) {
  19. super();
  20. this.normals = false;
  21. this.lights = false;
  22. this.useAlphaToCoverage = true;
  23. this.useColor = params.vertexColors;
  24. this.pointWidth = 1;
  25. this.pointColorNode = null;
  26. this.setDefaultValues( defaultValues );
  27. this.setupShaders();
  28. this.setValues( params );
  29. }
  30. setupShaders() {
  31. const useAlphaToCoverage = this.alphaToCoverage;
  32. const useColor = this.useColor;
  33. this.vertexNode = tslFn( () => {
  34. //vUv = uv;
  35. varying( vec2(), 'vUv' ).assign( uv() ); // @TODO: Analyze other way to do this
  36. const instancePosition = attribute( 'instancePosition' );
  37. // camera space
  38. const mvPos = property( 'vec4', 'mvPos' );
  39. mvPos.assign( modelViewMatrix.mul( vec4( instancePosition, 1.0 ) ) );
  40. const aspect = viewport.z.div( viewport.w );
  41. // clip space
  42. const clipPos = cameraProjectionMatrix.mul( mvPos );
  43. // offset in ndc space
  44. const offset = property( 'vec2', 'offset' );
  45. offset.assign( positionGeometry.xy );
  46. offset.assign( offset.mul( materialPointWidth ) );
  47. offset.assign( offset.div( viewport.z ) );
  48. offset.y.assign( offset.y.mul( aspect ) );
  49. // back to clip space
  50. offset.assign( offset.mul( clipPos.w ) );
  51. //clipPos.xy += offset;
  52. clipPos.assign( clipPos.add( vec4( offset, 0, 0 ) ) );
  53. return clipPos;
  54. //vec4 mvPosition = mvPos; // this was used for somethihng...
  55. } )();
  56. this.colorNode = tslFn( () => {
  57. const vUv = varying( vec2(), 'vUv' );
  58. // force assignment into correct place in flow
  59. const alpha = property( 'float', 'alpha' );
  60. alpha.assign( 1 );
  61. const a = vUv.x;
  62. const b = vUv.y;
  63. const len2 = a.mul( a ).add( b.mul( b ) );
  64. if ( useAlphaToCoverage ) {
  65. // force assignment out of following 'if' statement - to avoid uniform control flow errors
  66. const dlen = property( 'float', 'dlen' );
  67. dlen.assign( len2.fwidth() );
  68. alpha.assign( smoothstep( dlen.oneMinus(), dlen.add( 1 ), len2 ).oneMinus() );
  69. } else {
  70. len2.greaterThan( 1.0 ).discard();
  71. }
  72. let pointColorNode;
  73. if ( this.pointColorNode ) {
  74. pointColorNode = this.pointColorNode;
  75. } else {
  76. if ( useColor ) {
  77. const instanceColor = attribute( 'instanceColor' );
  78. pointColorNode = color( instanceColor ).mul( color( materialColor ) );
  79. } else {
  80. pointColorNode = materialColor;
  81. }
  82. }
  83. return vec4( pointColorNode, alpha );
  84. } )();
  85. this.needsUpdate = true;
  86. }
  87. get alphaToCoverage() {
  88. return this.useAlphaToCoverage;
  89. }
  90. set alphaToCoverage( value ) {
  91. if ( this.useAlphaToCoverage !== value ) {
  92. this.useAlphaToCoverage = value;
  93. this.setupShaders();
  94. }
  95. }
  96. }
  97. export default FatPointsNodeMaterial;
  98. addNodeMaterial( 'FatPointsNodeMaterial', FatPointsNodeMaterial );