Skip to content

Commit b13eccc

Browse files
authored
Examples: Move jsm/nodes to ES6. (mrdoob#21801)
1 parent 0a059ef commit b13eccc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+4317
-4252
lines changed

examples/jsm/nodes/accessors/CameraNode.js

Lines changed: 122 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,232 +3,234 @@ import { FunctionNode } from '../core/FunctionNode.js';
33
import { FloatNode } from '../inputs/FloatNode.js';
44
import { PositionNode } from '../accessors/PositionNode.js';
55

6-
function CameraNode( scope, camera ) {
6+
class CameraNode extends TempNode {
77

8-
TempNode.call( this, 'v3' );
8+
constructor( scope, camera ) {
99

10-
this.setScope( scope || CameraNode.POSITION );
11-
this.setCamera( camera );
10+
super( 'v3' );
1211

13-
}
14-
15-
CameraNode.Nodes = ( function () {
16-
17-
var depthColor = new FunctionNode( [
18-
'float depthColor( float mNear, float mFar ) {',
19-
20-
' #ifdef USE_LOGDEPTHBUF_EXT',
12+
this.setScope( scope || CameraNode.POSITION );
13+
this.setCamera( camera );
2114

22-
' float depth = gl_FragDepthEXT / gl_FragCoord.w;',
15+
}
2316

24-
' #else',
17+
setCamera( camera ) {
2518

26-
' float depth = gl_FragCoord.z / gl_FragCoord.w;',
19+
this.camera = camera;
20+
this.updateFrame = camera !== undefined ? this.onUpdateFrame : undefined;
2721

28-
' #endif',
22+
}
2923

30-
' return 1.0 - smoothstep( mNear, mFar, depth );',
24+
setScope( scope ) {
3125

32-
'}'
33-
].join( '\n' ) );
26+
switch ( this.scope ) {
3427

35-
return {
36-
depthColor: depthColor
37-
};
28+
case CameraNode.DEPTH:
3829

39-
} )();
30+
delete this.near;
31+
delete this.far;
4032

41-
CameraNode.POSITION = 'position';
42-
CameraNode.DEPTH = 'depth';
43-
CameraNode.TO_VERTEX = 'toVertex';
44-
45-
CameraNode.prototype = Object.create( TempNode.prototype );
46-
CameraNode.prototype.constructor = CameraNode;
47-
CameraNode.prototype.nodeType = 'Camera';
33+
break;
4834

49-
CameraNode.prototype.setCamera = function ( camera ) {
35+
}
5036

51-
this.camera = camera;
52-
this.updateFrame = camera !== undefined ? this.onUpdateFrame : undefined;
37+
this.scope = scope;
5338

54-
};
39+
switch ( scope ) {
5540

56-
CameraNode.prototype.setScope = function ( scope ) {
41+
case CameraNode.DEPTH:
5742

58-
switch ( this.scope ) {
43+
const camera = this.camera;
5944

60-
case CameraNode.DEPTH:
45+
this.near = new FloatNode( camera ? camera.near : 1 );
46+
this.far = new FloatNode( camera ? camera.far : 1200 );
6147

62-
delete this.near;
63-
delete this.far;
48+
break;
6449

65-
break;
50+
}
6651

6752
}
6853

69-
this.scope = scope;
54+
getType( /* builder */ ) {
7055

71-
switch ( scope ) {
56+
switch ( this.scope ) {
7257

73-
case CameraNode.DEPTH:
58+
case CameraNode.DEPTH:
7459

75-
var camera = this.camera;
60+
return 'f';
7661

77-
this.near = new FloatNode( camera ? camera.near : 1 );
78-
this.far = new FloatNode( camera ? camera.far : 1200 );
62+
}
7963

80-
break;
64+
return this.type;
8165

8266
}
8367

84-
};
68+
getUnique( /* builder */ ) {
69+
70+
switch ( this.scope ) {
8571

86-
CameraNode.prototype.getType = function ( /* builder */ ) {
72+
case CameraNode.DEPTH:
73+
case CameraNode.TO_VERTEX:
8774

88-
switch ( this.scope ) {
75+
return true;
8976

90-
case CameraNode.DEPTH:
77+
}
9178

92-
return 'f';
79+
return false;
9380

9481
}
9582

96-
return this.type;
83+
getShared( /* builder */ ) {
9784

98-
};
85+
switch ( this.scope ) {
9986

100-
CameraNode.prototype.getUnique = function ( /* builder */ ) {
87+
case CameraNode.POSITION:
10188

102-
switch ( this.scope ) {
89+
return false;
10390

104-
case CameraNode.DEPTH:
105-
case CameraNode.TO_VERTEX:
91+
}
10692

107-
return true;
93+
return true;
10894

10995
}
11096

111-
return false;
97+
generate( builder, output ) {
11298

113-
};
99+
let result;
114100

115-
CameraNode.prototype.getShared = function ( /* builder */ ) {
101+
switch ( this.scope ) {
116102

117-
switch ( this.scope ) {
103+
case CameraNode.POSITION:
118104

119-
case CameraNode.POSITION:
105+
result = 'cameraPosition';
120106

121-
return false;
107+
break;
122108

123-
}
109+
case CameraNode.DEPTH:
124110

125-
return true;
111+
const depthColor = builder.include( CameraNode.Nodes.depthColor );
126112

127-
};
113+
result = depthColor + '( ' + this.near.build( builder, 'f' ) + ', ' + this.far.build( builder, 'f' ) + ' )';
128114

129-
CameraNode.prototype.generate = function ( builder, output ) {
115+
break;
116+
117+
case CameraNode.TO_VERTEX:
130118

131-
var result;
119+
result = 'normalize( ' + new PositionNode( PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
132120

133-
switch ( this.scope ) {
121+
break;
134122

135-
case CameraNode.POSITION:
123+
}
136124

137-
result = 'cameraPosition';
125+
return builder.format( result, this.getType( builder ), output );
138126

139-
break;
127+
}
140128

141-
case CameraNode.DEPTH:
129+
onUpdateFrame( /* frame */ ) {
142130

143-
var depthColor = builder.include( CameraNode.Nodes.depthColor );
131+
switch ( this.scope ) {
144132

145-
result = depthColor + '( ' + this.near.build( builder, 'f' ) + ', ' + this.far.build( builder, 'f' ) + ' )';
133+
case CameraNode.DEPTH:
146134

147-
break;
135+
const camera = this.camera;
148136

149-
case CameraNode.TO_VERTEX:
137+
this.near.value = camera.near;
138+
this.far.value = camera.far;
150139

151-
result = 'normalize( ' + new PositionNode( PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
140+
break;
152141

153-
break;
142+
}
154143

155144
}
156145

157-
return builder.format( result, this.getType( builder ), output );
146+
copy( source ) {
158147

159-
};
148+
super.copy( source );
160149

161-
CameraNode.prototype.onUpdateFrame = function ( /* frame */ ) {
150+
this.setScope( source.scope );
162151

163-
switch ( this.scope ) {
152+
if ( source.camera ) {
164153

165-
case CameraNode.DEPTH:
154+
this.setCamera( source.camera );
166155

167-
var camera = this.camera;
156+
}
157+
158+
switch ( source.scope ) {
168159

169-
this.near.value = camera.near;
170-
this.far.value = camera.far;
160+
case CameraNode.DEPTH:
171161

172-
break;
162+
this.near.number = source.near;
163+
this.far.number = source.far;
164+
165+
break;
166+
167+
}
168+
169+
return this;
173170

174171
}
175172

176-
};
173+
toJSON( meta ) {
177174

178-
CameraNode.prototype.copy = function ( source ) {
175+
let data = this.getJSONNode( meta );
179176

180-
TempNode.prototype.copy.call( this, source );
177+
if ( ! data ) {
181178

182-
this.setScope( source.scope );
179+
data = this.createJSONNode( meta );
183180

184-
if ( source.camera ) {
181+
data.scope = this.scope;
185182

186-
this.setCamera( source.camera );
183+
if ( this.camera ) data.camera = this.camera.uuid;
187184

188-
}
185+
switch ( this.scope ) {
189186

190-
switch ( source.scope ) {
187+
case CameraNode.DEPTH:
191188

192-
case CameraNode.DEPTH:
189+
data.near = this.near.value;
190+
data.far = this.far.value;
193191

194-
this.near.number = source.near;
195-
this.far.number = source.far;
192+
break;
196193

197-
break;
194+
}
198195

199-
}
196+
}
200197

201-
return this;
198+
return data;
202199

203-
};
200+
}
204201

205-
CameraNode.prototype.toJSON = function ( meta ) {
202+
}
206203

207-
var data = this.getJSONNode( meta );
204+
CameraNode.Nodes = ( function () {
208205

209-
if ( ! data ) {
206+
const depthColor = new FunctionNode( [
207+
'float depthColor( float mNear, float mFar ) {',
210208

211-
data = this.createJSONNode( meta );
209+
' #ifdef USE_LOGDEPTHBUF_EXT',
212210

213-
data.scope = this.scope;
211+
' float depth = gl_FragDepthEXT / gl_FragCoord.w;',
214212

215-
if ( this.camera ) data.camera = this.camera.uuid;
213+
' #else',
216214

217-
switch ( this.scope ) {
215+
' float depth = gl_FragCoord.z / gl_FragCoord.w;',
218216

219-
case CameraNode.DEPTH:
217+
' #endif',
220218

221-
data.near = this.near.value;
222-
data.far = this.far.value;
219+
' return 1.0 - smoothstep( mNear, mFar, depth );',
223220

224-
break;
221+
'}'
222+
].join( '\n' ) );
225223

226-
}
224+
return {
225+
depthColor: depthColor
226+
};
227227

228-
}
228+
} )();
229229

230-
return data;
230+
CameraNode.POSITION = 'position';
231+
CameraNode.DEPTH = 'depth';
232+
CameraNode.TO_VERTEX = 'toVertex';
231233

232-
};
234+
CameraNode.prototype.nodeType = 'Camera';
233235

234236
export { CameraNode };

0 commit comments

Comments
 (0)