소스 검색

Fix capsule primitive seams.

clementlandrin 1 년 전
부모
커밋
3cdf86dd87
1개의 변경된 파일34개의 추가작업 그리고 44개의 파일을 삭제
  1. 34 44
      h3d/prim/Capsule.hx

+ 34 - 44
h3d/prim/Capsule.hx

@@ -5,27 +5,24 @@ class Capsule extends Polygon {
 
 	var ray : Float;
 	var length : Float;
-	var segsH : Int;
-	var segsW : Int;
+	var segs : Int;
 
-	public function new( ray = 1., length = 1., segsW = 8, segsH = 6 ) {
+	public function new( ray = 1., length = 1., segs = 8 ) {
 		this.ray = ray;
 		this.length = length;
-		this.segsH = segsH;
-		this.segsW = segsW;
+		this.segs = segs;
 
-		var dp = Math.PI / segsW;
+		var dp = Math.PI / segs;
 		var pts = [], idx = new hxd.IndexBuffer();
 		normals = [];
-		var portion = 0.51;
 		function halfSphere(offsetX : Float, offsetPhi : Float) {
 			var indexOffset = pts.length;
-			for( y in 0...segsH+1 ) {
-				var t = (y / segsH) * Math.PI;
+			for( y in 0...segs+1 ) {
+				var t = (y / segs) * Math.PI;
 				var st = Math.sin(t);
 				var pz = Math.cos(t);
 				var p = offsetPhi;
-				for( x in 0...segsW+1 ) {
+				for( x in 0...segs+1 ) {
 					var px = st * Math.cos(p);
 					var py = st * Math.sin(p);
 					pts.push(new Point(px * ray + offsetX, py * ray, pz * ray));
@@ -33,9 +30,9 @@ class Capsule extends Polygon {
 					p += dp;
 				}
 			}
-			for( y in 0...segsH ) {
-				for( x in 0...segsW ) {
-					inline function vertice(x, y) return x + y * (segsW + 1) + indexOffset;
+			for( y in 0...segs ) {
+				for( x in 0...segs ) {
+					inline function vertice(x, y) return x + y * (segs + 1) + indexOffset;
 					var v1 = vertice(x + 1, y);
 					var v2 = vertice(x, y);
 					var v3 = vertice(x, y + 1);
@@ -45,7 +42,7 @@ class Capsule extends Polygon {
 						idx.push(v2);
 						idx.push(v4);
 					}
-					if( y != segsH - 1 ) {
+					if( y != segs - 1 ) {
 						idx.push(v2);
 						idx.push(v3);
 						idx.push(v4);
@@ -53,36 +50,29 @@ class Capsule extends Polygon {
 				}
 			}
 		}
-		var ds = Math.PI / segsW;
-		var x0 = -length / 2.0;
-		var x1 = length / 2.0;
 		function cylinder() {
-			for( s in 0...segsW * 2 + 1 ) {
-				var a = s * ds;
-				var a2 = (s + 1) * ds;
-				var y = Math.cos(a) * ray, z = Math.sin(a) * ray;
-				var y2 = Math.cos(a2) * ray, z2 = Math.sin(a2) * ray;
-
-				var index = pts.length;
-				pts.push(new Point(x0, y, z));
-				pts.push(new Point(x0, y2, z2));
-				pts.push(new Point(x1, y, z));
-				pts.push(new Point(x1, y2, z2));
-	
-				var n0 = new Point(0.0, Math.cos(a), Math.sin(a));
-				var n1 = new Point(0.0, Math.cos(a2), Math.sin(a2));
-				normals.push(n0);
-				normals.push(n1);
-				normals.push(n0.clone());
-				normals.push(n1.clone());
-
-				idx.push(pts.length);
-				idx.push(pts.length + 1);
-				idx.push(pts.length + 3);
-
-				idx.push(pts.length + 0);
-				idx.push(pts.length + 3);
-				idx.push(pts.length + 2);
+			var indexOffset = pts.length;
+			for( y in 0...segs * 2 + 1 ) {
+				var t = y / segs * Math.PI;
+				var st = Math.sin(t);
+				var pz = Math.cos(t);
+				pts.push(new Point(-length * 0.5, st * ray, pz * ray));
+				pts.push(new Point(length * 0.5, st * ray, pz * ray));
+				normals.push(new Point(0.0, st, pz));
+				normals.push(new Point(0.0, st, pz));
+			}
+			for( x in 0...segs * 2 ) {
+				inline function vertice(i) return i + indexOffset;
+				var v0 = vertice(x * 2);
+				var v1 = vertice(x * 2 + 1);
+				var v2 = vertice(x * 2 + 2);
+				var v3 = vertice(x * 2 + 3);
+				idx.push(v0);
+				idx.push(v1);
+				idx.push(v2);
+				idx.push(v1);
+				idx.push(v3);
+				idx.push(v2);
 			}
 		}
 		halfSphere(-length * 0.5, Math.PI * 0.5);
@@ -104,7 +94,7 @@ class Capsule extends Polygon {
 		var s : Capsule = @:privateAccess engine.resCache.get(Capsule);
 		if( s != null )
 			return s;
-		s = new h3d.prim.Capsule(1, 1, 16, 16);
+		s = new h3d.prim.Capsule(1, 1, 16);
 		@:privateAccess engine.resCache.set(Capsule, s);
 		return s;
 	}