Vec3 三维向量
这是一个客户端API
该API仅在客户端脚本使用
构造函数¶
属性¶
方法¶
反人类,不给set方法
第三方重写¶
S-C-Link_client中重写的Vector3
/**
* 三维向量
*/
class Vector3 {
x = 0;
y = 0;
z = 0;
/**
* 定义一个三维向量
* @param {number} x 三维向量`x`的值(左右方向)
* @param {number} y 三维向量`y`的值(竖直方向)
* @param {number} z 三维向量`z`的值(前后方向)
*/
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
set(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
clone() {
return new Vector3(this.x, this.y, this.z);
}
copy(v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
}
add(v) {
return new Vector3(this.x + v.x, this.y + v.y, this.z + v.z);
}
sub(v) {
return new Vector3(this.x - v.x, this.y - v.y, this.z - v.z);
}
mul(v) {
return new Vector3(this.x * v.x, this.y * v.y, this.z * v.z);
}
div(v) {
return new Vector3(this.x / v.x, this.y / v.y, this.z / v.z);
}
addEq(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
}
subEq(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
}
mulEq(v) {
this.x *= v.x;
this.y *= v.y;
this.z *= v.z;
return this;
}
divEq(v) {
this.x /= v.x;
this.y /= v.y;
this.z /= v.z;
return this;
}
pow(n) {
return new Vector3(Math.pow(this.x, n), Math.pow(this.y, n), Math.pow(this.z, n));
}
distance(v) {
return Math.sqrt(Math.pow(v.x - this.x, 2) + Math.pow(v.y - this.y, 2) + Math.pow(v.z - this.z, 2));
}
mag() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2));
}
min(v) {
return new Vector2(Math.min(this.x, v.x), Math.min(this.y, v.y), Math.min(this.z, v.z));
}
max(v) {
return new Vector2(Math.max(this.x, v.x), Math.max(this.y, v.y), Math.max(this.z, v.z));
}
/**
* 归一化函数
* @returns {Vector2}
*/
normalize() {
let max = Math.max(this.x, this.y, this.z);
return new Vector3(this.x / max, this.y / max, this.z / max);
}
scale(n) {
return new Vector3(this.x * n, this.y * n, this.z * n);
}
toString() {
return JSON.stringify(this);
}
towards(v) {
return new Vector3(v.x - this.x, v.y - this.y, v.z - this.z);
}
equals(v, tolerance = 0.0001) {
return Math.abs(v.x - this.x) <= tolerance && Math.abs(v.y - this.y) <= tolerance && Math.abs(v.z - this.z) <= tolerance;
}
lerp(v) {
return this.add(v).scale(0.5);
}
setVec3(vec3) {
vec3.x = this.x;
vec3.y = this.y;
vec3.z = this.z;
}
static fromVec3(v) {
return new Vector3(v.x, v.y, v.z);
}
}
S-C-Link_client中重写的RGBColor
/**
* RGB颜色
*/
class RGBColor {
r = 0;
g = 0;
b = 0;
constructor(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
set(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
copy(c) {
this.r = c.r;
this.g = c.g;
this.b = c.b;
}
clone() {
return new RGBColor(this.r, this.g, this.b);
}
add(c) {
return new RGBColor(this.r + c.r, this.g + c.g, this.b + c.b);
}
sub(c) {
return new RGBColor(this.r - c.r, this.g - c.g, this.b - c.b);
}
mul(c) {
return new RGBColor(this.r * c.r, this.g * c.g, this.b * c.b);
}
div(c) {
return new RGBColor(this.r / c.r, this.g / c.g, this.b / c.b);
}
addEq(c) {
this.r += c.r;
this.g += c.g;
this.b += c.b;
return this;
}
subEq(c) {
this.r -= c.r;
this.g -= c.g;
this.b -= c.b;
return this;
}
mulEq(c) {
this.r *= c.r;
this.g *= c.g;
this.b *= c.b;
return this;
}
divEq(c) {
this.r /= c.r;
this.g /= c.g;
this.b /= c.b;
return this;
}
scale(n) {
return new RGBColor(this.r * n, this.g * n, this.b * n);
}
toRGB255() {
return this.scale(255);
}
/**
* 归一化函数
* @returns {Vector2}
*/
normalize() {
let max = Math.max(this.r, this.g, this.b);
return new RGBColor(this.r / max, this.g / max, this.b / max);
}
setVec3(vec3) {
vec3.x = this.r;
vec3.y = this.g;
vec3.z = this.b;
}
equals(c, tolerance = 0.0001) {
return Math.abs(c.r - this.r) <= tolerance && Math.abs(c.g - this.g) <= tolerance && Math.abs(c.b - this.b) <= tolerance;
}
lerp(c) {
return this.add(c).scale(0.5);
}
toString() {
return JSON.stringify(this);
}
toRGBA() {
return new RGBAColor(this.r, this.g, this.b, 1);
}
static fromRGB255(r, g, b) {
return new RGBColor(r / 255, g / 255, b / 255);
}
/**
* 从十六进制转换颜色
* 要求有6位(不包括`#`),例子:`123456`、`AABBCC`、`#FEDCBA`
* @param {string} hex 十六进制颜色
*/
static fromHEX(hex) {
var s;
if (hex.length < 6)
throwError(`无效的HEX:${hex}`);
if (hex.startsWith('#'))
s = hex.slice(1);
else
s = hex;
return new RGBColor(parseInt(s.slice(0, 2), 16), parseInt(s.slice(2, 4), 16), parseInt(s.slice(4, 6), 16));
}
static fromRGB(c) {
return new RGBColor(c.r, c.g, c.b);
}
}
S-C-Link_client中重写的RGBAColor
/**
* RGBA颜色
*/
class RGBAColor {
r = 0;
g = 0;
b = 0;
a = 1;
constructor(r, g, b, a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
set(r, g, b, a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
copy(c) {
this.r = c.r;
this.g = c.g;
this.b = c.b;
this.a = c.a;
}
clone() {
return new RGBAColor(this.r, this.g, this.b, this.a);
}
add(c) {
return new RGBAColor(this.r + c.r, this.g + c.g, this.b + c.b, this.a + c.a);
}
sub(c) {
return new RGBAColor(this.r - c.r, this.g - c.g, this.b - c.b, this.a - c.a);
}
mul(c) {
return new RGBAColor(this.r * c.r, this.g * c.g, this.b * c.b, this.a * c.a);
}
div(c) {
return new RGBAColor(this.r / c.r, this.g / c.g, this.b / c.b, this.a / c.a);
}
addEq(c) {
this.r += c.r;
this.g += c.g;
this.b += c.b;
this.a += c.a;
return this;
}
subEq(c) {
this.r -= c.r;
this.g -= c.g;
this.b -= c.b;
this.a -= c.a;
return this;
}
mulEq(c) {
this.r *= c.r;
this.g *= c.g;
this.b *= c.b;
this.a *= c.a;
return this;
}
divEq(c) {
this.r /= c.r;
this.g /= c.g;
this.b /= c.b;
this.a /= c.a;
return this;
}
scale(n) {
return new RGBAColor(this.r * n, this.g * n, this.b * n, this.a * n);
}
toRGBA255() {
return this.scale(255);
}
/**
* 归一化函数
* @returns {Vector2}
*/
normalize() {
let max = Math.max(this.r, this.g, this.b, this.a);
return new RGBAColor(this.r / max, this.g / max, this.b / max, this.a / max);
}
setVec3(vec3) {
vec3.x = this.r;
vec3.y = this.g;
vec3.z = this.b;
}
equals(c, tolerance = 0.0001) {
return Math.abs(c.r - this.r) <= tolerance && Math.abs(c.g - this.g) <= tolerance && Math.abs(c.b - this.b) <= tolerance && Math.abs(c.a - this.a) <= tolerance;
}
lerp(c) {
return this.add(c).scale(0.5);
}
toString() {
return JSON.stringify(this);
}
toRGB() {
return new RGBColor(this.r, this.g, this.a);
}
static fromRGBA255(r, g, b, a) {
return new RGBAColor(r / 255, g / 255, b / 255, a / 255);
}
/**
* 从十六进制转换颜色
* 要求有8位(不包括`#`),例子:`12345678`、`AABBCCFF`、`#FEDCBA98`
* @param {string} hex 十六进制颜色
*/
static fromHEX(hex) {
var s;
if (hex.length < 8)
throwError(`无效的HEX:${hex}`);
if (hex.startsWith('#'))
s = hex.slice(1);
else
s = hex;
return new RGBAColor(parseInt(s.slice(0, 2), 16), parseInt(s.slice(2, 4), 16), parseInt(s.slice(4, 6), 16), parseInt(s.slice(6, 8), 16));
}
static fromRGBA(c) {
return new RGBAColor(c.r, c.g, c.b, c.a);
}
}
继承于的RGBColor(只修改构造函数 新增set)
class RGBColor extends Vec3{
constructor(r, g, b){
super([r, g, b]);
this.r = r;
this.g = g;
this.b = b;
}
set(r, g, b){
this.x = r;
this.y = g;
this.z = b;
return this;
}
get x() {
return this.r;
}
get y() {
return this.g;
}
get z() {
return this.b;
}
set x(value) {
this.r = value;
}
set y(value) {
this.g = value;
}
set z(value) {
this.b = value;
}
}