Skip to content

Commit 6e51f78

Browse files
committed
Core: Vector, Matrix, Quaternion and Color instanceof
1 parent 3a76bc7 commit 6e51f78

25 files changed

+136
-91
lines changed

editor/js/Sidebar.Scene.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ function SidebarScene( editor ) {
373373

374374
if ( scene.background ) {
375375

376-
if ( scene.background.isColor ) {
376+
if ( scene.background instanceof THREE.Color ) {
377377

378378
backgroundType.setValue( 'Color' );
379379
backgroundColor.setHexValue( scene.background.getHex() );

examples/js/controls/FirstPersonControls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204

205205
this.lookAt = function ( x, y, z ) {
206206

207-
if ( x.isVector3 ) {
207+
if ( x instanceof THREE.Vector3 ) {
208208

209209
_target.copy( x );
210210

examples/js/renderers/SVGRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159

160160
const background = scene.background;
161161

162-
if ( background && background.isColor ) {
162+
if ( background instanceof THREE.Color ) {
163163

164164
removeChildNodes();
165165
_svg.style.backgroundColor = background.getStyle();

examples/jsm/controls/FirstPersonControls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class FirstPersonControls {
187187

188188
this.lookAt = function ( x, y, z ) {
189189

190-
if ( x.isVector3 ) {
190+
if ( x instanceof Vector3 ) {
191191

192192
_target.copy( x );
193193

examples/jsm/nodes/core/NodeUtils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ export const getValueType = ( value ) => {
3030

3131
return 'bool';
3232

33-
} else if ( value?.isVector2 === true ) {
33+
} else if ( value instanceof Vector2 ) {
3434

3535
return 'vec2';
3636

37-
} else if ( value?.isVector3 === true ) {
37+
} else if ( value instanceof Vector3 ) {
3838

3939
return 'vec3';
4040

41-
} else if ( value?.isVector4 === true ) {
41+
} else if ( value instanceof Vector4 ) {
4242

4343
return 'vec4';
4444

45-
} else if ( value?.isMatrix3 === true ) {
45+
} else if ( value instanceof Matrix3 ) {
4646

4747
return 'mat3';
4848

49-
} else if ( value?.isMatrix4 === true ) {
49+
} else if ( value instanceof Matrix4 ) {
5050

5151
return 'mat4';
5252

53-
} else if ( value?.isColor === true ) {
53+
} else if ( value instanceof Color ) {
5454

5555
return 'color';
5656

examples/jsm/nodes/geometry/RangeNode.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Node from '../core/Node.js';
22
import { attribute, float } from '../shadernode/ShaderNodeBaseElements.js';
3-
import { MathUtils, InstancedBufferAttribute } from 'three';
3+
import { Color, Vector2, Vector3, Vector4, MathUtils, InstancedBufferAttribute } from 'three';
44

55
class RangeNode extends Node {
66

@@ -19,10 +19,10 @@ class RangeNode extends Node {
1919

2020
let length = 1;
2121

22-
if ( min.isVector2 ) length = 2;
23-
else if ( min.isVector3 ) length = 3;
24-
else if ( min.isVector4 ) length = 4;
25-
else if ( min.isColor ) length = 3;
22+
if ( min instanceof Vector2 ) length = 2;
23+
else if ( min instanceof Vector3 ) length = 3;
24+
else if ( min instanceof Vector4 ) length = 4;
25+
else if ( min instanceof Color ) length = 3;
2626

2727
return length;
2828

@@ -61,7 +61,7 @@ class RangeNode extends Node {
6161

6262
}
6363

64-
} else if ( min.isColor ) {
64+
} else if ( min instanceof Color ) {
6565

6666
for ( let i = 0; i < length; i += 3 ) {
6767

examples/jsm/nodes/materials/SpriteNodeMaterial.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import NodeMaterial from './NodeMaterial.js';
2-
import { SpriteMaterial } from 'three';
2+
import { Vector2, SpriteMaterial } from 'three';
33
import {
44
vec2, vec3, vec4,
55
uniform, add, mul, sub,
@@ -59,7 +59,7 @@ class SpriteNodeMaterial extends NodeMaterial {
5959

6060
let alignedPosition = vertex.xy;
6161

62-
if ( builder.object.center?.isVector2 === true ) {
62+
if ( builder.object.center instanceof Vector2 ) {
6363

6464
alignedPosition = sub( alignedPosition, sub( uniform( builder.object.center ), vec2( 0.5 ) ) );
6565

examples/jsm/renderers/SVGRenderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Object3D,
88
Vector3
99
} from 'three';
10+
1011
import { Projector } from '../renderers/Projector.js';
1112
import { RenderableFace } from '../renderers/Projector.js';
1213
import { RenderableLine } from '../renderers/Projector.js';
@@ -170,7 +171,7 @@ class SVGRenderer {
170171

171172
const background = scene.background;
172173

173-
if ( background && background.isColor ) {
174+
if ( background instanceof Color ) {
174175

175176
removeChildNodes();
176177
_svg.style.backgroundColor = background.getStyle();

examples/jsm/renderers/webgpu/WebGPUBackground.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class WebGPUBackground {
3838
_clearColor.copy( renderer._clearColor );
3939
_clearAlpha = renderer._clearAlpha;
4040

41-
} else if ( background.isColor === true ) {
41+
} else if ( background instanceof Color ) {
4242

4343
// background is an opaque color
4444

src/core/Object3D.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Quaternion } from '../math/Quaternion.js';
2-
import { Vector3 } from '../math/Vector3.js';
3-
import { Matrix4 } from '../math/Matrix4.js';
41
import { EventDispatcher } from './EventDispatcher.js';
5-
import { Euler } from '../math/Euler.js';
62
import { Layers } from './Layers.js';
3+
import { Color } from '../math/Color.js';
4+
import { Vector3 } from '../math/Vector3.js';
75
import { Matrix3 } from '../math/Matrix3.js';
6+
import { Matrix4 } from '../math/Matrix4.js';
7+
import { Quaternion } from '../math/Quaternion.js';
8+
import { Euler } from '../math/Euler.js';
89
import * as MathUtils from '../math/MathUtils.js';
910

1011
let _object3DId = 0;
@@ -259,7 +260,7 @@ class Object3D extends EventDispatcher {
259260

260261
// This method does not support objects having non-uniformly-scaled parent(s)
261262

262-
if ( x.isVector3 ) {
263+
if ( x instanceof Vector3 ) {
263264

264265
_target.copy( x );
265266

@@ -709,7 +710,7 @@ class Object3D extends EventDispatcher {
709710

710711
if ( this.background ) {
711712

712-
if ( this.background.isColor ) {
713+
if ( this.background instanceof Color ) {
713714

714715
object.background = this.background.toJSON();
715716

src/extras/PMREMGenerator.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {
2+
BackSide,
23
CubeReflectionMapping,
34
CubeRefractionMapping,
45
CubeUVReflectionMapping,
6+
HalfFloatType,
57
LinearEncoding,
68
LinearFilter,
7-
NoToneMapping,
89
NoBlending,
9-
RGBAFormat,
10-
HalfFloatType
10+
NoToneMapping,
11+
RGBAFormat
1112
} from '../constants.js';
1213

1314
import { BufferAttribute } from '../core/BufferAttribute.js';
@@ -16,12 +17,11 @@ import { Mesh } from '../objects/Mesh.js';
1617
import { OrthographicCamera } from '../cameras/OrthographicCamera.js';
1718
import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js';
1819
import { ShaderMaterial } from '../materials/ShaderMaterial.js';
19-
import { Vector3 } from '../math/Vector3.js';
20-
import { Color } from '../math/Color.js';
2120
import { WebGLRenderTarget } from '../renderers/WebGLRenderTarget.js';
2221
import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
2322
import { BoxGeometry } from '../geometries/BoxGeometry.js';
24-
import { BackSide } from '../constants.js';
23+
import { Color } from '../math/Color.js';
24+
import { Vector3 } from '../math/Vector3.js';
2525

2626
const LOD_MIN = 4;
2727

@@ -317,15 +317,11 @@ class PMREMGenerator {
317317
let useSolidColor = false;
318318
const background = scene.background;
319319

320-
if ( background ) {
321-
322-
if ( background.isColor ) {
323-
324-
backgroundMaterial.color.copy( background );
325-
scene.background = null;
326-
useSolidColor = true;
320+
if ( background instanceof Color ) {
327321

328-
}
322+
backgroundMaterial.color.copy( background );
323+
scene.background = null;
324+
useSolidColor = true;
329325

330326
} else {
331327

src/extras/core/Curve.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as MathUtils from '../../math/MathUtils.js';
21
import { Vector2 } from '../../math/Vector2.js';
32
import { Vector3 } from '../../math/Vector3.js';
43
import { Matrix4 } from '../../math/Matrix4.js';
4+
import * as MathUtils from '../../math/MathUtils.js';
55

66
/**
77
* Extensible curve object.
@@ -242,7 +242,7 @@ class Curve {
242242
const pt1 = this.getPoint( t1 );
243243
const pt2 = this.getPoint( t2 );
244244

245-
const tangent = optionalTarget || ( ( pt1.isVector2 ) ? new Vector2() : new Vector3() );
245+
const tangent = optionalTarget || ( ( pt1 instanceof Vector2 ) ? new Vector2() : new Vector3() );
246246

247247
tangent.copy( pt2 ).sub( pt1 ).normalize();
248248

src/materials/Material.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
import {
2+
AddEquation,
3+
AlwaysStencilFunc,
4+
FlatShading,
5+
FrontSide,
6+
KeepStencilOp,
7+
LessEqualDepth,
8+
NormalBlending,
9+
OneMinusSrcAlphaFactor,
10+
SrcAlphaFactor
11+
} from '../constants.js';
12+
113
import { EventDispatcher } from '../core/EventDispatcher.js';
2-
import { FrontSide, FlatShading, NormalBlending, LessEqualDepth, AddEquation, OneMinusSrcAlphaFactor, SrcAlphaFactor, AlwaysStencilFunc, KeepStencilOp } from '../constants.js';
14+
import { Color } from '../math/Color.js';
15+
import { Vector3 } from '../math/Vector3.js';
316
import * as MathUtils from '../math/MathUtils.js';
417

518
let materialId = 0;
@@ -140,11 +153,11 @@ class Material extends EventDispatcher {
140153

141154
}
142155

143-
if ( currentValue && currentValue.isColor ) {
156+
if ( currentValue instanceof Color ) {
144157

145158
currentValue.set( newValue );
146159

147-
} else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
160+
} else if ( ( currentValue instanceof Vector3 ) && ( newValue && newValue instanceof Vector3 ) ) {
148161

149162
currentValue.copy( newValue );
150163

@@ -185,20 +198,20 @@ class Material extends EventDispatcher {
185198

186199
if ( this.name !== '' ) data.name = this.name;
187200

188-
if ( this.color && this.color.isColor ) data.color = this.color.getHex();
201+
if ( this.color instanceof Color ) data.color = this.color.getHex();
189202

190203
if ( this.roughness !== undefined ) data.roughness = this.roughness;
191204
if ( this.metalness !== undefined ) data.metalness = this.metalness;
192205

193206
if ( this.sheen !== undefined ) data.sheen = this.sheen;
194-
if ( this.sheenColor && this.sheenColor.isColor ) data.sheenColor = this.sheenColor.getHex();
207+
if ( this.sheenColor instanceof Color ) data.sheenColor = this.sheenColor.getHex();
195208
if ( this.sheenRoughness !== undefined ) data.sheenRoughness = this.sheenRoughness;
196-
if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex();
209+
if ( this.emissive instanceof Color ) data.emissive = this.emissive.getHex();
197210
if ( this.emissiveIntensity && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity;
198211

199-
if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex();
212+
if ( this.specular instanceof Color ) data.specular = this.specular.getHex();
200213
if ( this.specularIntensity !== undefined ) data.specularIntensity = this.specularIntensity;
201-
if ( this.specularColor && this.specularColor.isColor ) data.specularColor = this.specularColor.getHex();
214+
if ( this.specularColor instanceof Color ) data.specularColor = this.specularColor.getHex();
202215
if ( this.shininess !== undefined ) data.shininess = this.shininess;
203216
if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat;
204217
if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness;

src/materials/ShaderMaterial.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import { Material } from './Material.js';
2+
import { Color } from '../math/Color.js';
3+
import { Vector2 } from '../math/Vector2.js';
4+
import { Vector3 } from '../math/Vector3.js';
5+
import { Vector4 } from '../math/Vector4.js';
6+
import { Matrix3 } from '../math/Matrix3.js';
7+
import { Matrix4 } from '../math/Matrix4.js';
28
import { cloneUniforms, cloneUniformsGroups } from '../renderers/shaders/UniformsUtils.js';
39

410
import default_vertex from '../renderers/shaders/ShaderChunk/default_vertex.glsl.js';
@@ -110,42 +116,42 @@ class ShaderMaterial extends Material {
110116
value: value.toJSON( meta ).uuid
111117
};
112118

113-
} else if ( value && value.isColor ) {
119+
} else if ( value instanceof Color ) {
114120

115121
data.uniforms[ name ] = {
116122
type: 'c',
117123
value: value.getHex()
118124
};
119125

120-
} else if ( value && value.isVector2 ) {
126+
} else if ( value instanceof Vector2 ) {
121127

122128
data.uniforms[ name ] = {
123129
type: 'v2',
124130
value: value.toArray()
125131
};
126132

127-
} else if ( value && value.isVector3 ) {
133+
} else if ( value instanceof Vector3 ) {
128134

129135
data.uniforms[ name ] = {
130136
type: 'v3',
131137
value: value.toArray()
132138
};
133139

134-
} else if ( value && value.isVector4 ) {
140+
} else if ( value instanceof Vector4 ) {
135141

136142
data.uniforms[ name ] = {
137143
type: 'v4',
138144
value: value.toArray()
139145
};
140146

141-
} else if ( value && value.isMatrix3 ) {
147+
} else if ( value instanceof Matrix3 ) {
142148

143149
data.uniforms[ name ] = {
144150
type: 'm3',
145151
value: value.toArray()
146152
};
147153

148-
} else if ( value && value.isMatrix4 ) {
154+
} else if ( value instanceof Matrix4 ) {
149155

150156
data.uniforms[ name ] = {
151157
type: 'm4',

src/math/Color.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class Color {
5656

5757
constructor( r, g, b ) {
5858

59-
this.isColor = true;
59+
// @deprecated
60+
Object.defineProperty( this, 'isColor', { value: true } );
6061

6162
this.r = 1;
6263
this.g = 1;
@@ -75,7 +76,7 @@ class Color {
7576

7677
set( value ) {
7778

78-
if ( value && value.isColor ) {
79+
if ( value instanceof Color ) {
7980

8081
this.copy( value );
8182

src/math/Matrix3.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ class Matrix3 {
22

33
constructor() {
44

5-
Matrix3.prototype.isMatrix3 = true;
5+
// @deprecated
6+
Object.defineProperty( this, 'isMatrix3', { value: true } );
67

78
this.elements = [
89

0 commit comments

Comments
 (0)