Quellcode durchsuchen

- add getCollider() on Cube.hx and Polygon.hx
- implements rayIntersection() on Bounds.hx

bstouls vor 9 Jahren
Ursprung
Commit
4e8e389010
4 geänderte Dateien mit 64 neuen und 7 gelöschten Zeilen
  1. 26 1
      h3d/col/Bounds.hx
  2. 11 0
      h3d/prim/Cube.hx
  3. 16 0
      h3d/prim/Polygon.hx
  4. 11 6
      samples/interact/Main.hx

+ 26 - 1
h3d/col/Bounds.hx

@@ -1,7 +1,7 @@
 package h3d.col;
 import hxd.Math;
 
-class Bounds {
+class Bounds implements RayCollider {
 
 	public var xMin : Float;
 	public var xMax : Float;
@@ -42,6 +42,31 @@ class Bounds {
 		return dd + rr - p.d*2;
 	}
 
+	public function rayIntersection( r : Ray, ?p : Point ) : Null<Point> {
+		var minTx = (xMin - r.px) / r.lx;
+		var minTy = (yMin - r.py) / r.ly;
+		var minTz = (zMin - r.pz) / r.lz;
+		var maxTx = (xMax - r.px) / r.lx;
+		var maxTy = (yMax - r.py) / r.ly;
+		var maxTz = (zMax - r.pz) / r.lz;
+
+		var realMinTx = Math.min(minTx, maxTx);
+		var realMinTy = Math.min(minTy, maxTy);
+		var realMinTz = Math.min(minTz, maxTz);
+		var realMaxTx = Math.max(minTx, maxTx);
+		var realMaxTy = Math.max(minTy, maxTy);
+		var realMaxTz = Math.max(minTz, maxTz);
+
+		var minmax = Math.min( Math.min(realMaxTx, realMaxTy), realMaxTz);
+		var maxmin = Math.max( Math.max(realMinTx, realMinTy), realMinTz);
+
+		if(minmax < maxmin)	return null;
+
+		if( p == null ) p = new Point();
+		p.set(r.px + maxmin * r.lx , r.py + maxmin * r.ly, r.pz + maxmin * r.lz);
+		return p;
+	}
+
 	/**
 	 * Check if the camera model-view-projection Matrix intersects with the Bounds. Returns -1 if outside, 0 if interests and 1 if fully inside.
 	 * @param	mvp : the model-view-projection matrix to test against

+ 11 - 0
h3d/prim/Cube.hx

@@ -3,8 +3,15 @@ import h3d.col.Point;
 
 class Cube extends Polygon {
 
+	var sizeX : Float;
+	var sizeY : Float;
+	var sizeZ : Float;
+
 	public function new( x = 1., y = 1., z = 1. )
 	{
+		this.sizeX = x;
+		this.sizeY = y;
+		this.sizeZ = z;
 		var p = [
 			new Point(0, 0, 0),
 			new Point(x, 0, 0),
@@ -55,4 +62,8 @@ class Cube extends Polygon {
 		];
 	}
 
+	override public function getCollider() : h3d.col.RayCollider {
+		return h3d.col.Bounds.fromValues(translatedX, translatedY, translatedZ, sizeX * scaled, sizeY * scaled, sizeZ * scaled);
+	}
+
 }

+ 16 - 0
h3d/prim/Polygon.hx

@@ -178,4 +178,20 @@ class Polygon extends Primitive {
 		return points.length;
 	}
 
+	override function getCollider() : h3d.col.RayCollider {
+		var vertexes = new haxe.ds.Vector(points.length * 3);
+		var indexes = new haxe.ds.Vector(idx.length);
+		var vid = 0;
+		for( p in points ) {
+			vertexes[vid++] = p.x;
+			vertexes[vid++] = p.y;
+			vertexes[vid++] = p.z;
+		}
+		for( i in 0...idx.length )
+			indexes[i] = idx[i];
+		var poly = new h3d.col.Polygon();
+		poly.addBuffers(vertexes, indexes);
+		return poly;
+	}
+
 }

+ 11 - 6
samples/interact/Main.hx

@@ -19,6 +19,7 @@ class Main extends hxd.App {
 
 	var rnd : hxd.Rand;
 	var light : h3d.scene.DirLight;
+	var obj : h3d.scene.Object;
 
 	function initInteract( i : h3d.scene.Interactive, m : h3d.scene.Mesh ) {
 		var beacon = null;
@@ -55,15 +56,15 @@ class Main extends hxd.App {
 		s3d.lightSystem.ambientLight.set(0.74, 0.74, 0.74);
 
 		rnd = new hxd.Rand(5);
-		for(i in 0...6) {
-			var c = new h3d.prim.Sphere(1,64,32);
+		for(i in 0...8) {
+			var c = if( rnd.random(2) == 0 ) new h3d.prim.Cube() else new h3d.prim.Sphere(1,64,32);
 			//c.unindex();
 			c.addNormals();
 			c.addUVs();
 			var m = new h3d.scene.Mesh(c, s3d);
-			m.x = rnd.srand() * 0.8;
-			m.y = rnd.srand() * 0.8;
-			m.scale(0.25 + rnd.rand() * 0.5);
+			m.x = rnd.srand() * 0.9;
+			m.y = rnd.srand() * 0.9;
+			m.scale(0.25 + rnd.rand() * 0.3);
 			m.material.mainPass.enableLights = true;
 			m.material.shadows = true;
 			var c = 0.3 + rnd.rand() * 0.3;
@@ -75,7 +76,7 @@ class Main extends hxd.App {
 		}
 
 		var cache = new h3d.prim.ModelCache();
-		var obj = cache.loadModel(hxd.Res.Model);
+		obj = cache.loadModel(hxd.Res.Model);
 		obj.scale(1 / 20);
 		obj.rotate(0,0,Math.PI / 2);
 		obj.y = 0.2;
@@ -119,6 +120,10 @@ class Main extends hxd.App {
 		};
 	}
 
+	override function update(dt:Float) {
+		obj.rotate(0, 0, 0.01 * dt);
+	}
+
 
 	static function main() {
 		hxd.Res.initEmbed();