From 8bf7573dbaa5bdaefbc0f197b552f0d7ff08864e Mon Sep 17 00:00:00 2001 From: liteng <930372551@qq.com> Date: Fri, 7 Dec 2018 12:35:07 +0800 Subject: [PATCH] =?UTF-8?q?C#=E7=89=88three.js=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- THREE/Math/Box2.cs | 264 +++++++++++++++++++++++++++++++- THREE/Math/Matrix3.cs | 12 +- THREE/Math/Vector2.cs | 348 +++++++++++++++++++++++++++++++++++++++++- THREE/THREE.csproj | 3 - 4 files changed, 618 insertions(+), 9 deletions(-) diff --git a/THREE/Math/Box2.cs b/THREE/Math/Box2.cs index f436cc66..d6d5f4b3 100644 --- a/THREE/Math/Box2.cs +++ b/THREE/Math/Box2.cs @@ -6,7 +6,269 @@ using System.Threading.Tasks; namespace THREE.Math { - class Box2 + public class Box2 { + public Vector2 min; + public Vector2 max; + + public Box2(Vector2 min = null, Vector2 max = null) + { + this.min = (min != null) ? min : new Vector2(double.PositiveInfinity, double.NegativeInfinity); + this.max = (max != null) ? max : new Vector2(double.NegativeInfinity, double.NegativeInfinity); + } + + public Box2 set(Vector2 min, Vector2 max) + { + + this.min.copy(min); + this.max.copy(max); + + return this; + + }, + + setFromPoints: function(points ) + { + + this.makeEmpty(); + + for (var i = 0, il = points.length; i < il; i++) + { + + this.expandByPoint(points[i]); + + } + + return this; + + }, + + setFromCenterAndSize: function() + { + + var v1 = new Vector2(); + + return function setFromCenterAndSize(center, size) { + + var halfSize = v1.copy(size).multiplyScalar(0.5); + this.min.copy(center).sub(halfSize); + this.max.copy(center).add(halfSize); + + return this; + + }; + + } + (), + + clone: function() + { + + return new this.constructor().copy(this); + + }, + + copy: function(box ) + { + + this.min.copy(box.min); + this.max.copy(box.max); + + return this; + + }, + + makeEmpty: function() + { + + this.min.x = this.min.y = +Infinity; + this.max.x = this.max.y = -Infinity; + + return this; + + }, + + isEmpty: function() + { + + // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes + + return (this.max.x < this.min.x) || (this.max.y < this.min.y); + + }, + + getCenter: function(target ) + { + + if (target === undefined) + { + + console.warn('THREE.Box2: .getCenter() target is now required'); + target = new Vector2(); + + } + + return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5); + + }, + + getSize: function(target ) + { + + if (target === undefined) + { + + console.warn('THREE.Box2: .getSize() target is now required'); + target = new Vector2(); + + } + + return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min); + + }, + + expandByPoint: function(point ) + { + + this.min.min(point); + this.max.max(point); + + return this; + + }, + + expandByVector: function(vector ) + { + + this.min.sub(vector); + this.max.add(vector); + + return this; + + }, + + expandByScalar: function(scalar ) + { + + this.min.addScalar(-scalar); + this.max.addScalar(scalar); + + return this; + + }, + + containsPoint: function(point ) + { + + return point.x < this.min.x || point.x > this.max.x || + point.y < this.min.y || point.y > this.max.y ? false : true; + + }, + + containsBox: function(box ) + { + + return this.min.x <= box.min.x && box.max.x <= this.max.x && + this.min.y <= box.min.y && box.max.y <= this.max.y; + + }, + + getParameter: function(point, target ) + { + + // This can potentially have a divide by zero if the box + // has a size dimension of 0. + + if (target === undefined) + { + + console.warn('THREE.Box2: .getParameter() target is now required'); + target = new Vector2(); + + } + + return target.set( + (point.x - this.min.x) / (this.max.x - this.min.x), + (point.y - this.min.y) / (this.max.y - this.min.y) + ); + + }, + + intersectsBox: function(box ) + { + + // using 4 splitting planes to rule out intersections + + return box.max.x < this.min.x || box.min.x > this.max.x || + box.max.y < this.min.y || box.min.y > this.max.y ? false : true; + + }, + + clampPoint: function(point, target ) + { + + if (target === undefined) + { + + console.warn('THREE.Box2: .clampPoint() target is now required'); + target = new Vector2(); + + } + + return target.copy(point).clamp(this.min, this.max); + + }, + + distanceToPoint: function() + { + + var v1 = new Vector2(); + + return function distanceToPoint(point) { + + var clampedPoint = v1.copy(point).clamp(this.min, this.max); + return clampedPoint.sub(point).length(); + + }; + + } + (), + + intersect: function(box ) + { + + this.min.max(box.min); + this.max.min(box.max); + + return this; + + }, + + union: function(box ) + { + + this.min.min(box.min); + this.max.max(box.max); + + return this; + + }, + + translate: function(offset ) + { + + this.min.add(offset); + this.max.add(offset); + + return this; + + }, + + equals: function(box ) + { + + return box.min.equals(this.min) && box.max.equals(this.max); + + } } } diff --git a/THREE/Math/Matrix3.cs b/THREE/Math/Matrix3.cs index c3f12777..3306b0e9 100644 --- a/THREE/Math/Matrix3.cs +++ b/THREE/Math/Matrix3.cs @@ -6,7 +6,17 @@ using System.Threading.Tasks; namespace THREE.Math { - class Matrix3 + public class Matrix3 { + public double[] elements; + + public Matrix3() + { + this.elements = new double[] { + 1, 0, 0, + 0, 1, 0, + 0, 0, 1 + }; + } } } diff --git a/THREE/Math/Vector2.cs b/THREE/Math/Vector2.cs index 14388152..090de353 100644 --- a/THREE/Math/Vector2.cs +++ b/THREE/Math/Vector2.cs @@ -8,8 +8,8 @@ namespace THREE.Math { public class Vector2 { - public double x = 0; - public double y = 0; + public double x = 0.0; + public double y = 0.0; public Vector2(double x = 0, double y = 0) { @@ -41,7 +41,7 @@ namespace THREE.Math } } - public bool isVector2 = true; + public const bool isVector2 = true; public Vector2 Set(double x, double y) { @@ -111,7 +111,7 @@ namespace THREE.Math { if (w != null) { - // console.warn('THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.'); + Console.WriteLine("THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead."); return this.AddVectors(v, w); } @@ -136,5 +136,345 @@ namespace THREE.Math return this; } + + public Vector2 AddScaledVector(Vector2 v, double s) + { + this.x += v.x * s; + this.y += v.y * s; + + return this; + } + + public Vector2 Sub(Vector2 v, Vector2 w = null) + { + if (w != null) + { + + Console.WriteLine("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; + } + + public Vector2 SubScalar(double s) + { + this.x -= s; + this.y -= s; + + return this; + } + + public Vector2 SubVectors(Vector2 a, Vector2 b) + { + this.x = a.x - b.x; + this.y = a.y - b.y; + + return this; + } + + public Vector2 Multiply(Vector2 v) + { + this.x *= v.x; + this.y *= v.y; + + return this; + } + + public Vector2 MultiplyScalar(double scalar) + { + this.x *= scalar; + this.y *= scalar; + + return this; + } + + public Vector2 Divide(Vector2 v) + { + this.x /= v.x; + this.y /= v.y; + + return this; + } + + public Vector2 DivideScalar(double scalar) + { + return this.MultiplyScalar(1 / scalar); + } + + public Vector2 ApplyMatrix3(Matrix3 m) + { + double 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; + } + + public Vector2 Min(Vector2 v) + { + this.x = System.Math.Min(this.x, v.x); + this.y = System.Math.Min(this.y, v.y); + + return this; + } + + public Vector2 Max(Vector2 v) + { + this.x = System.Math.Max(this.x, v.x); + this.y = System.Math.Max(this.y, v.y); + + return this; + } + + public Vector2 Clamp(Vector2 min, Vector2 max) + { + // assumes min < max, componentwise + this.x = System.Math.Max(min.x, System.Math.Min(max.x, this.x)); + this.y = System.Math.Max(min.y, System.Math.Min(max.y, this.y)); + + return this; + } + + public Vector2 ClampScalar(Vector2 minVal, Vector2 maxVal) + { + var min = new Vector2(); + var max = new Vector2(); + + min.set(minVal, minVal); + max.set(maxVal, maxVal); + + return this.clamp(min, max); + } + + clampLength: function(min, max ) + { + + var length = this.length(); + + return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length))); + + }, + + floor: function() + { + + this.x = Math.floor(this.x); + this.y = Math.floor(this.y); + + return this; + + }, + + ceil: function() + { + + this.x = Math.ceil(this.x); + this.y = Math.ceil(this.y); + + return this; + + }, + + round: function() + { + + this.x = Math.round(this.x); + this.y = Math.round(this.y); + + return this; + + }, + + roundToZero: function() + { + + this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x); + this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y); + + 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; + + }, + + cross: function(v ) + { + + return this.x * v.y - this.y * v.x; + + }, + + lengthSq: function() + { + + return this.x * this.x + this.y * this.y; + + }, + + length: function() + { + + return Math.sqrt(this.x * this.x + this.y * this.y); + + }, + + manhattanLength: function() + { + + return Math.abs(this.x) + Math.abs(this.y); + + }, + + normalize: function() + { + + return this.divideScalar(this.length() || 1); + + }, + + angle: function() + { + + // computes the angle in radians with respect to the positive x-axis + + var angle = Math.atan2(this.y, this.x); + + if (angle < 0) angle += 2 * Math.PI; + + return angle; + + }, + + distanceTo: function(v ) + { + + return Math.sqrt(this.distanceToSquared(v)); + + }, + + distanceToSquared: function(v ) + { + + var dx = this.x - v.x, dy = this.y - v.y; + return dx * dx + dy * dy; + + }, + + manhattanDistanceTo: function(v ) + { + + return Math.abs(this.x - v.x) + Math.abs(this.y - v.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 ) + { + + return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1); + + }, + + 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; + + }, + + rotateAround: function(center, angle ) + { + + var c = Math.cos(angle), s = Math.sin(angle); + + var x = this.x - center.x; + var y = this.y - center.y; + + this.x = x * c - y * s + center.x; + this.y = x * s + y * c + center.y; + + return this; + + } } } diff --git a/THREE/THREE.csproj b/THREE/THREE.csproj index 41289b8d..5d7647c3 100644 --- a/THREE/THREE.csproj +++ b/THREE/THREE.csproj @@ -41,7 +41,6 @@ - @@ -50,7 +49,6 @@ - @@ -58,7 +56,6 @@ -