Skip to content

Commit

Permalink
更换设备之前的提交
Browse files Browse the repository at this point in the history
  • Loading branch information
QinChengMaoXian authored and QinChengMaoXian committed Jun 20, 2019
1 parent b4f2223 commit 4e2cf19
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"line-counter.includes": [
"src/**"
]
}
14 changes: 14 additions & 0 deletions src/geometry/LineGeometry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Geometry } from "../graphics/Geometry";

/**
* 线段Geometry
*/
export class LinesGeometry extends Geometry {

protected _lineSegments = [];

constructor() {
super()
}

}
12 changes: 12 additions & 0 deletions src/graphics/Geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { Bounding } from '../bounding/Bounding';
import { AABB } from '../bounding/AABB'
import { Vector3 } from '../math/Vector3';

/**
* 绘制模式
*/
export enum DrawMode {
POINTS = CGE.POINTS,
LINES = CGE.LINES,
Expand All @@ -16,6 +19,9 @@ export enum DrawMode {
TRIANGLE_FAN = CGE.TRIANGLE_FAN,
}

/**
* 绘制参数
*/
export interface DrawParameter {
mode: DrawMode;
count: number;
Expand All @@ -29,7 +35,13 @@ export class Geometry extends GraphicsObject {
protected _buffers: Buffer[] = [];
protected _indexBuffer: Buffer;

/**
* position Attribute的引用,用于快速get position结构
*/
protected _posAttrib: Attribute;
/**
* position Buffer的引用,用于快速get position数据
*/
protected _posBuffer: Buffer;

constructor() {
Expand Down
2 changes: 1 addition & 1 deletion src/math/Line.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Vector3 } from "./Vector3";

export class Line {
export class LineSegment {

public start: Vector3;
public end: Vector3;
Expand Down
98 changes: 98 additions & 0 deletions src/math/Ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export class Ray {
return target.copy(this.dir).mul(t).addAt(this.origin);
}

/**
* 射线到线段的距离的平方
* @param v1
* @param v2
* @param optionalPointOnRay
* @param optionalPointOnSegment
*/
public distanceSqToSegment(v1: Vector3, v2: Vector3, optionalPointOnRay?: Vector3, optionalPointOnSegment?: Vector3): number { return }

/**
* 射线到点的距离平方
* @param point
Expand Down Expand Up @@ -193,6 +202,95 @@ Ray.prototype.isIntersectBox = function () {
}
}();

Ray.prototype.distanceSqToSegment = function() {

const segCenter = new Vector3();
const segDir = new Vector3();
const diff = new Vector3();

return function(v0: Vector3, v1: Vector3, optionalPointOnRay?: Vector3, optionalPointOnSegment?: Vector3) {
// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h
// It returns the min distance between the ray and the segment
// defined by v0 and v1
// It can also set two optional targets :
// - The closest point on the ray
// - The closest point on the segment
segCenter.copy(v0).addAt(v1).mul(0.5);
segDir.copy(v1).subAt(v0).normalize();
diff.copy(this.origin).subAt(segCenter);

let segExtent = v0.distanceTo(v1) * 0.5;
let a01 = -this.direction.dot(segDir);
let b0 = diff.dot(this.direction);
let b1 = -diff.dot(segDir);
let c = diff.lengthSquare();
let det = Math.abs(1 - a01 * a01);
let s0, s1, sqrDist, extDet;

if (det > 0) {
// The ray and segment are not parallel.
s0 = a01 * b1 - b0;
s1 = a01 * b0 - b1;
extDet = segExtent * det;

if (s0 >= 0) {
if (s1 >= - extDet) {
if (s1 <= extDet) {
// region 0
// Minimum at interior points of ray and segment.
let invDet = 1 / det;
s0 *= invDet;
s1 *= invDet;
sqrDist = s0 * (s0 + a01 * s1 + 2 * b0) + s1 * (a01 * s0 + s1 + 2 * b1) + c;
} else {
// region 1
s1 = segExtent;
s0 = Math.max(0, -(a01 * s1 + b0));
sqrDist = -s0 * s0 + s1 * (s1 + 2 * b1) + c;
}
} else {
// region 5
s1 = -segExtent;
s0 = Math.max(0, -(a01 * s1 + b0));
sqrDist = -s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
}
} else {
if (s1 <= - extDet) {
// region 4
s0 = Math.max(0, -(-a01 * segExtent + b0));
s1 = (s0 > 0) ? -segExtent : Math.min( Math.max(-segExtent, -b1), segExtent);
sqrDist = -s0 * s0 + s1 * (s1 + 2 * b1) + c;
} else if (s1 <= extDet) {
// region 3
s0 = 0;
s1 = Math.min(Math.max(-segExtent, -b1), segExtent);
sqrDist = s1 * (s1 + 2 * b1) + c;
} else {
// region 2
s0 = Math.max(0, -(a01 * segExtent + b0));
s1 = (s0 > 0) ? segExtent : Math.min(Math.max(-segExtent, -b1), segExtent);
sqrDist = -s0 * s0 + s1 * (s1 + 2 * b1) + c;
}
}
} else {
// Ray and segment are parallel.
s1 = (a01 > 0) ? -segExtent : segExtent;
s0 = Math.max(0, -(a01 * s1 + b0));
sqrDist = -s0 * s0 + s1 * (s1 + 2 * b1) + c;
}

if (optionalPointOnRay) {
optionalPointOnRay.copy(this.direction).mul(s0).addAt(this.origin);
}

if (optionalPointOnSegment) {
optionalPointOnSegment.copy(segDir).mul(s1).addAt(segCenter);
}

return sqrDist;
}
}()

Ray.prototype.distanceSqToPoint = function () {
const v = new Vector3

Expand Down
3 changes: 2 additions & 1 deletion src/util/RenderCulling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type SpotLightProxy = Object3DProxy<SpotLight>;
/**
* 渲染剔除
* TODO想办法优化这一对东西;
* 还是算了放弃吧,优化毛线啊。
*/
export class RenderCulling {

Expand Down Expand Up @@ -253,4 +254,4 @@ export class RenderCulling {
break;
}
}
}
}

0 comments on commit 4e2cf19

Please sign in to comment.