forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFatPointsNodeMaterial.js
164 lines (98 loc) · 3.75 KB
/
FatPointsNodeMaterial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
import { temp } from '../core/VarNode.js';
import { varying } from '../core/VaryingNode.js';
import { property } from '../core/PropertyNode.js';
import { attribute } from '../core/AttributeNode.js';
import { cameraProjectionMatrix } from '../accessors/CameraNode.js';
import { materialColor } from '../accessors/MaterialNode.js';
import { modelViewMatrix } from '../accessors/ModelNode.js';
import { positionGeometry } from '../accessors/PositionNode.js';
import { abs, mix, mod, dot, clamp, smoothstep } from '../math/MathNode.js';
import { tslFn, float, vec2, vec3, vec4, If } from '../shadernode/ShaderNode.js';
import { uv } from '../accessors/UVNode.js';
import { materialPointWidth } from '../accessors/FatPointsMaterialNode.js'; // or should this be a property, instead?
import { viewport } from '../display/ViewportNode.js';
import { PointsMaterial } from 'three';
const defaultValues = new PointsMaterial();
class FatPointsNodeMaterial extends NodeMaterial {
constructor( params = {} ) {
super();
this.normals = false;
this.lights = false;
this.useAlphaToCoverage = true;
this.useColor = params.vertexColors;
this.pointWidth = 1;
this.pointColorNode = null;
this.setDefaultValues( defaultValues );
this.setupShaders();
this.setValues( params );
}
setupShaders() {
const useAlphaToCoverage = this.alphaToCoverage;
const useColor = this.useColor;
this.vertexNode = tslFn( () => {
//vUv = uv;
varying( vec2(), 'vUv' ).assign( uv() ); // @TODO: Analyze other way to do this
const instancePosition = attribute( 'instancePosition' );
// camera space
const mvPos = property( 'vec4', 'mvPos' );
mvPos.assign( modelViewMatrix.mul( vec4( instancePosition, 1.0 ) ) );
const aspect = viewport.z.div( viewport.w );
// clip space
const clipPos = cameraProjectionMatrix.mul( mvPos );
// offset in ndc space
const offset = property( 'vec2', 'offset' );
offset.assign( positionGeometry.xy );
offset.assign( offset.mul( materialPointWidth ) );
offset.assign( offset.div( viewport.z ) );
offset.y.assign( offset.y.mul( aspect ) );
// back to clip space
offset.assign( offset.mul( clipPos.w ) );
//clipPos.xy += offset;
clipPos.assign( clipPos.add( vec4( offset, 0, 0 ) ) );
return clipPos;
//vec4 mvPosition = mvPos; // this was used for somethihng...
} )();
this.colorNode = tslFn( () => {
const vUv = varying( vec2(), 'vUv' );
// force assignment into correct place in flow
const alpha = property( 'float', 'alpha' );
alpha.assign( 1 );
const a = vUv.x;
const b = vUv.y;
const len2 = a.mul( a ).add( b.mul( b ) );
if ( useAlphaToCoverage ) {
// force assignment out of following 'if' statement - to avoid uniform control flow errors
const dlen = property( 'float', 'dlen' );
dlen.assign( len2.fwidth() );
alpha.assign( smoothstep( dlen.oneMinus(), dlen.add( 1 ), len2 ).oneMinus() );
} else {
len2.greaterThan( 1.0 ).discard();
}
let pointColorNode;
if ( this.pointColorNode ) {
pointColorNode = this.pointColorNode;
} else {
if ( useColor ) {
const instanceColor = attribute( 'instanceColor' );
pointColorNode = instanceColor; // todo: instance color should be attenuated by material color
} else {
pointColorNode = materialColor;
}
}
return vec4( pointColorNode, alpha );
} )();
this.needsUpdate = true;
}
get alphaToCoverage() {
return this.useAlphaToCoverage;
}
set alphaToCoverage( value ) {
if ( this.useAlphaToCoverage !== value ) {
this.useAlphaToCoverage = value;
this.setupShaders();
}
}
}
export default FatPointsNodeMaterial;
addNodeMaterial( 'FatPointsNodeMaterial', FatPointsNodeMaterial );