返回
WebGL框架three.js源码解析之Vector2数学库
前端
2023-11-07 08:12:42
前言
three.js是一个流行的JavaScript库,用于创建和渲染3D图形。它提供了一系列强大的功能,可以帮助开发人员轻松构建复杂的3D场景。three.js的核心之一就是其数学库,其中包含了一组常用的数学工具,可以用于各种图形计算。
在three.js的数学库中,Vector2类是一个非常重要的类。它代表了一个二维向量,可以用于表示空间中的点、多边形的顶点、空间方位等。在three.js中,Vector2类被广泛用于各种图形计算,例如:
- 顶点位置计算
- 法线向量计算
- 纹理坐标计算
- 光照计算
- 碰撞检测等
Vector2类的源代码解析
Vector2类的源代码位于three.js库的src/math/Vector2.js
文件中。该文件的内容如下:
/**
* @author mrdoob / http://mrdoob.com/
* @author philogb / http://blog.thejit.org/
* @author egraether / http://egraether.com/
* @author zz85 / http://www.lab4games.net/zz85/blog
*/
'use strict';
THREE.Vector2 = function ( x, y ) {
this.x = x || 0;
this.y = y || 0;
};
THREE.Vector2.prototype = {
constructor: THREE.Vector2,
isVector2: true,
set: function ( x, y ) {
this.x = x;
this.y = y;
return this;
},
setScalar: function ( scalar ) {
this.x = scalar;
this.y = scalar;
return this;
},
setX: function ( x ) {
this.x = x;
return this;
},
setY: function ( y ) {
this.y = y;
return this;
},
clone: function () {
return new this.constructor( this.x, this.y );
},
copy: function ( v ) {
this.x = v.x;
this.y = v.y;
return this;
},
add: function ( v, w ) {
if ( w !== undefined ) {
console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
return this.addVectors( v, w );
}
this.x += v.x;
this.y += v.y;
return this;
},
addScalar: function ( s ) {
this.x += s;
this.y += s;
return this;
},
addVectors: function ( a, b ) {
this.x = a.x + b.x;
this.y = a.y + b.y;
return this;
},
sub: function ( v, w ) {
if ( w !== undefined ) {
console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
return this.subVectors( v, w );
}
this.x -= v.x;
this.y -= v.y;
return this;
},
subScalar: function ( s ) {
this.x -= s;
this.y -= s;
return this;
},
subVectors: function ( a, b ) {
this.x = a.x - b.x;
this.y = a.y - b.y;
return this;
},
multiply: function ( v ) {
this.x *= v.x;
this.y *= v.y;
return this;
},
multiplyScalar: function ( scalar ) {
this.x *= scalar;
this.y *= scalar;
return this;
},
divide: function ( v ) {
this.x /= v.x;
this.y /= v.y;
return this;
},
divideScalar: function ( scalar ) {
return this.multiplyScalar( 1 / scalar );
},
applyMatrix3: function ( m ) {
var x = this.x, y = this.y;
var e = m.elements;
this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ];
this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ];
return this;
},
negate: function () {
this.x = - this.x;
this.y = - this.y;
return this;
},
dot: function ( v ) {
return this.x * v.x + this.y * v.y;
},
lengthSq: function () {
return this.x * this.x + this.y * this.y;
},
length: function () {
return Math.sqrt( this.x * this.x + this.y * this.y );
},
normalize: function () {
return this.divideScalar( this.length() );
},
manhattanLength: function () {
return Math.abs( this.x ) + Math.abs( this.y );
},
setLength: function ( length ) {
return this.normalize().multiplyScalar( length );
},
lerp: function ( v, alpha ) {
this.x += ( v.x - this.x ) * alpha;
this.y += ( v.y - this.y ) * alpha;
return this;
},
lerpVectors: function ( v1, v2, alpha ) {
this.x = v1.x + ( v2.x - v1.x ) * alpha;
this.y = v1.y + ( v2.y - v1.y ) * alpha;
return this;
},
equals: function ( v ) {
return ( ( v.x === this.x ) && ( v.y === this.y ) );
},
fromArray: function ( array, offset ) {
if ( offset === undefined ) offset = 0;
this.x = array[ offset ];
this.y = array[ offset + 1 ];
return this;
},
toArray: function ( array, offset ) {
if ( array === undefined ) array = [];
if ( offset === undefined ) offset = 0;
array[ offset ] = this.x;
array[ offset + 1 ] = this.y;
return array;
},
fromBufferAttribute: function ( attribute, index, offset ) {
if ( offset !== undefined ) {
console.warn( 'THREE.Vector2: offset has been removed from .fromBufferAttribute().' );
}
this.x = attribute.getX( index );
this.y = attribute.getY( index );
return this;
},
toJSON: function () {
return {
x: this.x,
y: this.y
};
}
};
从源代码可以看出,Vector2类是一个非常简单的类,它只包含了一些基本的操作方法,例如:
set()
: 设置向量的值setX()
: 设置向量的x分量setY()
: 设置向量的y分量clone()
: 克隆向量copy()
: 复制向量的值add()
: 将向量与另一个向量相加addScalar()
: 将向量与一个标量相加addVectors()
: 将两个向量相加sub()
: 将向量与另一个向量相减subScalar()
: 将向量与一个标量相减subVectors()
: 将两个向量相减multiply()
: 将向量与另一个向量相乘multiplyScalar()
: 将向量与一个标量相乘divide()
: 将向量与另一个向量相除