用户:小苏打_查看:22 回复:10 评论:22 创建时间:2023-02-04T22:37:30

function dsin(n) {
return Math.sin(Math.PI * n / 180);
}
function dcos(n) {
return Math.cos(Math.PI * n / 180);
}
var vector3 = /** @class */ (function () {
function vector3(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
vector3.prototype.add = function (v) {
return new vector3(this.x + v.x, this.y + v.y, this.z + v.z);
};
vector3.prototype.reduce = function (v) {
return new vector3(this.x - v.x, this.y - v.y, this.z - v.z);
};
vector3.prototype.mul = function (n) {
return new vector3(this.x * n, this.y * n, this.z * n);
};
return vector3;
}());
var matrix3 = /** @class */ (function () {
function matrix3(ihat, jhat, khat) {
this.ihat = ihat;
this.jhat = jhat;
this.khat = khat;
}
matrix3.prototype.linearTransformation = function (v) {
return new vector3(0, 0, 0)
.add(this.ihat.mul(v.x))
.add(this.jhat.mul(v.y))
.add(this.khat.mul(v.z));
};
matrix3.prototype.mul = function (m) {
return new matrix3(m.linearTransformation(this.ihat), m.linearTransformation(this.jhat), m.linearTransformation(this.khat));
};
matrix3.prototype.rotate = function (x, y, z) {
return this
.mul(new matrix3(new vector3(1, 0, 0), new vector3(0, dcos(x), dsin(x)), new vector3(0, -dsin(x), dcos(x))))
.mul(new matrix3(new vector3(dcos(y), 0, dsin(y)), new vector3(0, 1, 0), new vector3(-dsin(y), 0, dcos(y))))
.mul(new matrix3(new vector3(dcos(z), dsin(z), 0), new vector3(-dsin(z), dcos(z), 0), new vector3(0, 0, 1)));
};
return matrix3;
}());
var BASE_MATRIX = new matrix3(new vector3(1, 0, 0), new vector3(0, 1, 0), new vector3(0, 0, 1));
let ezRouteEntityTemplate = {
TEMPLATE_ROATITION: new Box3Quaternion(0, 1, 0, 0),
/**
* 设置模型旋转
* @param x{Number} x轴
* @param y{Number} y轴
* @param z{Number} z轴
*/
setRotation(x, y, z) {
this.meshOrientation = this.TEMPLATE_ROATITION
.rotateX(x * Math.PI / 180)
.rotateY(y * Math.PI / 180)
.rotateZ(z * Math.PI / 180);
},
/**
* 添加模型旋转
* @param x{Number} x轴
* @param y{Number} y轴
* @param z{Number} z轴
*/
addRotation(x, y, z) {
this.meshOrientation = this.meshOrientation
.rotateX(x * Math.PI / 180)
.rotateY(y * Math.PI / 180)
.rotateZ(z * Math.PI / 180);
},
/**
* 带有动画效果的模型旋转
* @param x{Number} x轴
* @param y{Number} y轴
* @param z{Number} z轴
* @param duringMs{Number} 持续时间
*/
async showRotation(x, y, z, duringMs, ease=Box3Easing.SINE) {
return new Promise((reslove, rej)=>{
let last = this.meshOrientation
.rotateX(x * Math.PI / 180)
.rotateY(y * Math.PI / 180)
.rotateZ(z * Math.PI / 180);
let animation = this.animate([
{ meshOrientation: last, duration: 1, easeIn:ease},
{ meshOrientation: this.meshOrientation, duration: 0 },
]);
animation.play({
duration: 16 * duringMs / 1000,
iterations: 1,
direction: Box3AnimationDirection.WRAP_REVERSE
});
animation.onFinish(() => {
reslove()
});
})
}
}
/**
* 为实体添加一些好东西
*/
function bind(entity) {
Object.assign(entity, ezRouteEntityTemplate);
entity.setRotation(0, 0, 0);
}
/**
* 旋转建筑
* 注意: start的每一项都要比end的**数值小**,并且copyto**不能位于**即将旋转的区域内。造成一切建筑的毁坏作者概不负责。
* @param start{Box3Vector3} 区域开始
* @param end{Box3Vector3} 区域结束
* @param origin{Box3Vector3} 区域原点
* @param copyto{Box3Vector3} 旋转后的新原点
* @param x{Number} x轴旋转角
* @param y{Number} y轴旋转角
* @param z{Number} z轴旋转角
*/
function rotateConstructor(start, end, origin, copyto, x, y, z){
let ro = new vector3(origin.x, origin.y, origin.z);
let rc = new vector3(copyto.x, copyto.y, copyto.z);
let rm = BASE_MATRIX.rotate(x, y, z);
for(let x=start.x; x<=end.x; x++){
for(let y=start.y; y<=end.y; y++){
for(let z=start.z; z<end.z; z++){
let tmp = rm.linearTransformation(new vector3(x, y, z).reduce(ro)).add(rc);
voxels.setVoxelId(tmp.x, tmp.y, tmp.z, voxels.getVoxelId(x, y, z));
}
}
}
}
module.exports = {
bind,
rotateConstructor
}
点赞1
评论