对像素进行取整保证在不同分辨率下保持清晰

This commit is contained in:
yhh
2021-05-25 11:16:49 +08:00
parent da3ab02a8d
commit 6c44d38c10
8 changed files with 53 additions and 45 deletions

View File

@@ -2499,6 +2499,7 @@ declare module es {
* @param length * @param length
*/ */
static repeat(t: number, length: number): number; static repeat(t: number, length: number): number;
static floorToInt(f: number): number;
/** /**
* 将值绕一圈移动的助手 * 将值绕一圈移动的助手
* @param position * @param position

View File

@@ -5814,6 +5814,9 @@ var es;
MathHelper.repeat = function (t, length) { MathHelper.repeat = function (t, length) {
return t - Math.floor(t / length) * length; return t - Math.floor(t / length) * length;
}; };
MathHelper.floorToInt = function (f) {
return Math.trunc(Math.floor(f));
};
/** /**
* 将值绕一圈移动的助手 * 将值绕一圈移动的助手
* @param position * @param position
@@ -6814,10 +6817,10 @@ var es;
}; };
Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) { Rectangle.prototype.calculateBounds = function (parentPosition, position, origin, scale, rotation, width, height) {
if (rotation == 0) { if (rotation == 0) {
this.x = parentPosition.x + position.x - origin.x * scale.x; this.x = Math.trunc(parentPosition.x + position.x - origin.x * scale.x);
this.y = parentPosition.y + position.y - origin.y * scale.y; this.y = Math.trunc(parentPosition.y + position.y - origin.y * scale.y);
this.width = width * scale.x; this.width = Math.trunc(width * scale.x);
this.height = height * scale.y; this.height = Math.trunc(height * scale.y);
} }
else { else {
// 我们需要找到我们的绝对最小/最大值,并据此创建边界 // 我们需要找到我们的绝对最小/最大值,并据此创建边界
@@ -6841,13 +6844,13 @@ var es;
es.Vector2Ext.transformR(bottomLeft, this._transformMat, bottomLeft); es.Vector2Ext.transformR(bottomLeft, this._transformMat, bottomLeft);
es.Vector2Ext.transformR(bottomRight, this._transformMat, bottomRight); es.Vector2Ext.transformR(bottomRight, this._transformMat, bottomRight);
// 找出最小值和最大值,这样我们就可以计算出我们的边界框。 // 找出最小值和最大值,这样我们就可以计算出我们的边界框。
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x); var minX = Math.trunc(Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x));
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x); var maxX = Math.trunc(Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x));
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y); var minY = Math.trunc(Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y));
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y); var maxY = Math.trunc(Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y));
this.location = new es.Vector2(minX, minY); this.location = new es.Vector2(minX, minY);
this.width = maxX - minX; this.width = Math.trunc(maxX - minX);
this.height = maxY - minY; this.height = Math.trunc(maxY - minY);
} }
}; };
/** /**
@@ -6931,7 +6934,7 @@ var es;
* 获取这个矩形的哈希码 * 获取这个矩形的哈希码
*/ */
Rectangle.prototype.getHashCode = function () { Rectangle.prototype.getHashCode = function () {
return (this.x ^ this.y ^ this.width ^ this.height); return (Math.trunc(this.x) ^ Math.trunc(this.y) ^ Math.trunc(this.width) ^ Math.trunc(this.height));
}; };
Rectangle.prototype.clone = function () { Rectangle.prototype.clone = function () {
return new Rectangle(this.x, this.y, this.width, this.height); return new Rectangle(this.x, this.y, this.width, this.height);
@@ -6960,7 +6963,7 @@ var es;
*/ */
SubpixelFloat.prototype.update = function (amount) { SubpixelFloat.prototype.update = function (amount) {
this.remainder += amount; this.remainder += amount;
var motion = Math.floor(Math.trunc(this.remainder)); var motion = Math.trunc(this.remainder);
this.remainder -= motion; this.remainder -= motion;
amount = motion; amount = motion;
return amount; return amount;
@@ -7670,11 +7673,11 @@ var es;
} }
while (currentCell.x != lastCell.x || currentCell.y != lastCell.y) { while (currentCell.x != lastCell.x || currentCell.y != lastCell.y) {
if (tMaxX < tMaxY) { if (tMaxX < tMaxY) {
currentCell.x = Math.floor(es.MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX))); currentCell.x = Math.trunc(es.MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX)));
tMaxX += tDeltaX; tMaxX += tDeltaX;
} }
else { else {
currentCell.y = Math.floor(es.MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY))); currentCell.y = Math.trunc(es.MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY)));
tMaxY += tDeltaY; tMaxY += tDeltaY;
} }
cell = this.cellAtPosition(currentCell.x, currentCell.y); cell = this.cellAtPosition(currentCell.x, currentCell.y);
@@ -7790,7 +7793,7 @@ var es;
* @param y * @param y
*/ */
SpatialHash.prototype.cellCoords = function (x, y) { SpatialHash.prototype.cellCoords = function (x, y) {
return new es.Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize)); return new es.Vector2(es.MathHelper.floorToInt(x * this._inverseCellSize), es.MathHelper.floorToInt(y * this._inverseCellSize));
}; };
/** /**
* 获取世界空间x,y值的单元格。 * 获取世界空间x,y值的单元格。
@@ -11763,8 +11766,8 @@ var es;
RectangleExt.getClosestPointOnRectangleBorderToPoint = function (rect, point) { RectangleExt.getClosestPointOnRectangleBorderToPoint = function (rect, point) {
// 对于每个轴,如果该点在盒子外面,则将在盒子上,否则不理会它 // 对于每个轴,如果该点在盒子外面,则将在盒子上,否则不理会它
var res = es.Vector2.zero; var res = es.Vector2.zero;
res.x = es.MathHelper.clamp(point.x, rect.left, rect.right); res.x = es.MathHelper.clamp(Math.trunc(point.x), rect.left, rect.right);
res.y = es.MathHelper.clamp(point.y, rect.top, rect.bottom); res.y = es.MathHelper.clamp(Math.trunc(point.y), rect.top, rect.bottom);
// 如果点在矩形内我们需要将res推到边框因为它将在矩形内 // 如果点在矩形内我们需要将res推到边框因为它将在矩形内
if (rect.contains(res.x, res.y)) { if (rect.contains(res.x, res.y)) {
var dl = rect.x - rect.left; var dl = rect.x - rect.left;
@@ -11815,7 +11818,7 @@ var es;
if (pt.y > maxY) if (pt.y > maxY)
maxY = pt.y; maxY = pt.y;
} }
return this.fromMinMaxVector(new es.Vector2(minX, minY), new es.Vector2(maxX, maxY)); return this.fromMinMaxVector(new es.Vector2(Math.trunc(minX), Math.trunc(minY)), new es.Vector2(Math.trunc(maxX), Math.trunc(maxY)));
}; };
/** /**
* 缩放矩形 * 缩放矩形
@@ -11823,10 +11826,10 @@ var es;
* @param scale * @param scale
*/ */
RectangleExt.scale = function (rect, scale) { RectangleExt.scale = function (rect, scale) {
rect.x = rect.x * scale.x; rect.x = Math.trunc(rect.x * scale.x);
rect.y = rect.y * scale.y; rect.y = Math.trunc(rect.y * scale.y);
rect.width = rect.width * scale.x; rect.width = Math.trunc(rect.width * scale.x);
rect.height = rect.height * scale.y; rect.height = Math.trunc(rect.height * scale.y);
}; };
RectangleExt.translate = function (rect, vec) { RectangleExt.translate = function (rect, vec) {
rect.location.add(vec); rect.location.add(vec);

File diff suppressed because one or more lines are too long

View File

@@ -456,6 +456,10 @@ module es {
return t - Math.floor(t / length) * length; return t - Math.floor(t / length) * length;
} }
public static floorToInt(f: number) {
return Math.trunc(Math.floor(f));
}
/** /**
* 将值绕一圈移动的助手 * 将值绕一圈移动的助手
* @param position * @param position

View File

@@ -411,10 +411,10 @@ module es {
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2,
rotation: number, width: number, height: number) { rotation: number, width: number, height: number) {
if (rotation == 0) { if (rotation == 0) {
this.x = parentPosition.x + position.x - origin.x * scale.x; this.x = Math.trunc(parentPosition.x + position.x - origin.x * scale.x);
this.y = parentPosition.y + position.y - origin.y * scale.y; this.y = Math.trunc(parentPosition.y + position.y - origin.y * scale.y);
this.width = width * scale.x; this.width = Math.trunc(width * scale.x);
this.height = height * scale.y; this.height = Math.trunc(height * scale.y);
} else { } else {
// 我们需要找到我们的绝对最小/最大值,并据此创建边界 // 我们需要找到我们的绝对最小/最大值,并据此创建边界
let worldPosX = parentPosition.x + position.x; let worldPosX = parentPosition.x + position.x;
@@ -441,14 +441,14 @@ module es {
Vector2Ext.transformR(bottomRight, this._transformMat, bottomRight); Vector2Ext.transformR(bottomRight, this._transformMat, bottomRight);
// 找出最小值和最大值,这样我们就可以计算出我们的边界框。 // 找出最小值和最大值,这样我们就可以计算出我们的边界框。
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x); let minX = Math.trunc(Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x));
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x); let maxX = Math.trunc(Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x));
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y); let minY = Math.trunc(Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y));
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y); let maxY = Math.trunc(Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y));
this.location = new Vector2(minX, minY); this.location = new Vector2(minX, minY);
this.width = maxX - minX; this.width = Math.trunc(maxX - minX);
this.height = maxY - minY; this.height = Math.trunc(maxY - minY);
} }
} }
@@ -549,7 +549,7 @@ module es {
* 获取这个矩形的哈希码 * 获取这个矩形的哈希码
*/ */
public getHashCode(): number{ public getHashCode(): number{
return (this.x ^ this.y ^ this.width ^ this.height); return (Math.trunc(this.x) ^ Math.trunc(this.y) ^ Math.trunc(this.width) ^ Math.trunc(this.height));
} }
public clone(): Rectangle { public clone(): Rectangle {

View File

@@ -16,7 +16,7 @@ module es {
*/ */
public update(amount: number){ public update(amount: number){
this.remainder += amount; this.remainder += amount;
let motion = Math.floor(Math.trunc(this.remainder)); let motion = Math.trunc(this.remainder);
this.remainder -= motion; this.remainder -= motion;
amount = motion; amount = motion;
return amount; return amount;

View File

@@ -181,11 +181,11 @@ module es {
while (currentCell.x != lastCell.x || currentCell.y != lastCell.y){ while (currentCell.x != lastCell.x || currentCell.y != lastCell.y){
if (tMaxX < tMaxY){ if (tMaxX < tMaxY){
currentCell.x = Math.floor(MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX))); currentCell.x = Math.trunc(MathHelper.approach(currentCell.x, lastCell.x, Math.abs(stepX)));
tMaxX += tDeltaX; tMaxX += tDeltaX;
}else{ }else{
currentCell.y = Math.floor(MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY))); currentCell.y = Math.trunc(MathHelper.approach(currentCell.y, lastCell.y, Math.abs(stepY)));
tMaxY += tDeltaY; tMaxY += tDeltaY;
} }
@@ -286,7 +286,7 @@ module es {
* @param y * @param y
*/ */
public cellCoords(x: number, y: number): Vector2 { public cellCoords(x: number, y: number): Vector2 {
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize)); return new Vector2(MathHelper.floorToInt(x * this._inverseCellSize), MathHelper.floorToInt(y * this._inverseCellSize));
} }
/** /**

View File

@@ -265,8 +265,8 @@ module es {
public static getClosestPointOnRectangleBorderToPoint(rect: Rectangle, point: Vector2) { public static getClosestPointOnRectangleBorderToPoint(rect: Rectangle, point: Vector2) {
// 对于每个轴,如果该点在盒子外面,则将在盒子上,否则不理会它 // 对于每个轴,如果该点在盒子外面,则将在盒子上,否则不理会它
let res = es.Vector2.zero; let res = es.Vector2.zero;
res.x = MathHelper.clamp(point.x, rect.left, rect.right) res.x = MathHelper.clamp(Math.trunc(point.x), rect.left, rect.right)
res.y = MathHelper.clamp(point.y, rect.top, rect.bottom); res.y = MathHelper.clamp(Math.trunc(point.y), rect.top, rect.bottom);
// 如果点在矩形内我们需要将res推到边框因为它将在矩形内 // 如果点在矩形内我们需要将res推到边框因为它将在矩形内
if (rect.contains(res.x, res.y)) { if (rect.contains(res.x, res.y)) {
@@ -327,7 +327,7 @@ module es {
maxY = pt.y; maxY = pt.y;
} }
return this.fromMinMaxVector(new Vector2(minX, minY), new Vector2(maxX, maxY)); return this.fromMinMaxVector(new Vector2(Math.trunc(minX), Math.trunc(minY)), new Vector2(Math.trunc(maxX), Math.trunc(maxY)));
} }
/** /**
@@ -336,10 +336,10 @@ module es {
* @param scale * @param scale
*/ */
public static scale(rect: Rectangle, scale: Vector2) { public static scale(rect: Rectangle, scale: Vector2) {
rect.x = rect.x * scale.x; rect.x = Math.trunc(rect.x * scale.x);
rect.y = rect.y * scale.y; rect.y = Math.trunc(rect.y * scale.y);
rect.width = rect.width * scale.x; rect.width = Math.trunc(rect.width * scale.x);
rect.height = rect.height * scale.y; rect.height = Math.trunc(rect.height * scale.y);
} }
public static translate(rect: Rectangle, vec: Vector2) { public static translate(rect: Rectangle, vec: Vector2) {