Skip to content

Commit

Permalink
add hit test mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
hikipuro committed Sep 8, 2018
1 parent f0170d7 commit d01e526
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
20 changes: 20 additions & 0 deletions src/HitTest.ts
@@ -0,0 +1,20 @@
import * as Tea from "./tea/Tea";

export class HitTest extends Tea.Script {
update(): void {
var mouse = this.mouse;
if (mouse.isDown(0)) {
//console.log("mouse down", mouse.x, mouse.y);
var p2 = new Tea.Vector3(mouse.x, mouse.y, 0);
var ray = this.scene.mainCamera.screenPointToRay(p2);
var r = this.object3d.getComponent(Tea.MeshRenderer);
if (r.bounds.collideRay(ray)) {
console.log("HitTest: true");
r.material.color = Tea.Color.red;
} else {
console.log("HitTest: false");
r.material.color = Tea.Color.white;
}
}
}
}
11 changes: 7 additions & 4 deletions src/Main.ts
Expand Up @@ -3,6 +3,7 @@ import { TestScript } from "./TestScript";
import { TestScript2 } from "./TestScript2";
import { Rotate } from "./Rotate";
import { CameraRotate } from "./CameraRotate";
import { HitTest } from "./HitTest";

export class Main {
app: Tea.App;
Expand Down Expand Up @@ -164,7 +165,6 @@ export class Main {

var cube = Tea.Object3D.createPrimitive(this.app, Tea.PrimitiveType.Cube);
//cube.name = "cube";
//cube.position.x = 2;
cube.position.y = 2;
//cube.position.z = -5;
//cube.scale.y = -1;
Expand All @@ -173,6 +173,7 @@ export class Main {
//cube.rotation.y = Tea.radians(20);
//cube.addScript(new Rotate());
//cube.getComponent(Tea.Renderer).material.mainTexture = renderTexture;
cube.addComponent(HitTest);
scene.appendChild(cube);

var cube2 = Tea.Object3D.createPrimitive(this.app, Tea.PrimitiveType.Cube);
Expand Down Expand Up @@ -242,8 +243,8 @@ export class Main {
//plane.renderer.wireframe = true;
//plane.position.z = -9;
//plane.position.y = -2;
plane.rotation.eulerAngles = new Tea.Vector3(-20, 0, 0);
//plane.rotation.eulerAngles = new Tea.Vector3(20, 180, 0);
//plane.rotation.eulerAngles = new Tea.Vector3(-20, 0, 0);
plane.rotation.eulerAngles = new Tea.Vector3(20, 180, 0);
//plane.addScript(script);
//plane.scale.x = 10;
//plane.scale.y = 10;
Expand All @@ -261,6 +262,8 @@ export class Main {
ps.settings.stencil.fail = Tea.ShaderStencilOp.Keep;
ps.settings.stencil.zfail = Tea.ShaderStencilOp.Replace;
ps.settings.stencil.zpass = Tea.ShaderStencilOp.Replace;

plane.addComponent(HitTest);
scene.appendChild(plane);

console.log("bounds", plane.getComponent(Tea.MeshFilter).mesh.bounds);
Expand Down Expand Up @@ -289,7 +292,7 @@ export class Main {
cs.settings.stencil.fail = Tea.ShaderStencilOp.Keep;
cs.settings.stencil.zfail = Tea.ShaderStencilOp.Keep;
cs.settings.stencil.zpass = Tea.ShaderStencilOp.Keep;
capsule.addComponent(TestScript);
//capsule.addComponent(TestScript);
scene.appendChild(capsule);

//console.log("capsule", capsule.localToWorldMatrix.toString());
Expand Down
8 changes: 8 additions & 0 deletions src/tea/collisions/Line.ts
Expand Up @@ -9,6 +9,14 @@ export class Line {
this.direction = new Tea.Vector3();
}

toString(): string {
return Tea.StringUtil.format(
"{ point: {0}, direction: {1} }",
this.point.toString(),
this.direction.toString()
);
}

distance(line: Line): number {
var n = this.direction.cross(line.direction).normalized;
if (Tea.Mathf.approximately(n.magnitude, 0)) {
Expand Down
2 changes: 1 addition & 1 deletion src/tea/components/Camera.ts
Expand Up @@ -195,7 +195,7 @@ export class Camera extends Component {
var far = this.screenToWorldPoint(p);
return new Tea.Ray(
near,
far.sub(near).normalized
far.sub$(near).normalize$()
);
}

Expand Down
13 changes: 8 additions & 5 deletions src/tea/components/MeshRenderer.ts
Expand Up @@ -25,7 +25,15 @@ export class MeshRenderer extends Renderer {
var mesh = component.mesh;
var bounds = mesh.bounds.clone();
bounds.center.add$(this.object3d.position);

var rotation = this.object3d.rotation;
bounds.extents.applyQuaternion(rotation);
var extents = bounds.extents;
extents[0] = Math.abs(extents[0]);
extents[1] = Math.abs(extents[1]);
extents[2] = Math.abs(extents[2]);
bounds.extents.scale$(this.object3d.scale);

this._mesh = mesh;
this._bounds = bounds;
} else {
Expand All @@ -51,11 +59,6 @@ export class MeshRenderer extends Renderer {
super.render(camera);

var mesh = this._mesh;
if (mesh.hasColors) {
this._uniforms.uniform1i("useColor", 1);
} else {
this._uniforms.uniform1i("useColor", 0);
}
if (this.receiveShadows) {
this._uniforms.uniform1i("receiveShadows", 1);
} else {
Expand Down
39 changes: 39 additions & 0 deletions src/tea/math/Bounds.ts
Expand Up @@ -42,4 +42,43 @@ export class Bounds {
toString(): string {
return JSON.stringify(this);
}

collideRay(ray: Tea.Ray): boolean {
var line = new Tea.Line();
line.point = ray.origin;
line.direction = ray.direction;
return this.collideLine(line);
}

collideLine(line: Tea.Line): boolean {
var min = this.min, max = this.max;
var p = line.point;
var d = line.direction;
var early = -Number.MAX_VALUE;
var late = Number.MAX_VALUE;

for (var i = 0; i < 3; i++) {
if (d[i] === 0) {
continue;
}
var id = 1.0 / d[i];
var near = (min[i] - p[i]) * id;
var far = (max[i] - p[i]) * id;
if (near > far) {
var tmp = near;
near = far;
far = tmp;
}
if (near > early) {
early = near;
}
if (far < late) {
late = far;
}
if (early > late) {
return false;
}
}
return true;
}
}
1 change: 0 additions & 1 deletion src/tea/shaders/ShaderSources.ts
Expand Up @@ -92,7 +92,6 @@ export module ShaderSources {
uniform vec2 _ShadowTex_ST;
uniform vec2 uv_NormalTex;
uniform vec2 _NormalTex_ST;
uniform bool useColor;
uniform bool receiveShadows;
varying vec3 vNormal;
Expand Down

0 comments on commit d01e526

Please sign in to comment.