浏览代码

Spline: add recompute tangents

LeoVgr 9 月之前
父节点
当前提交
2da0af882b
共有 2 个文件被更改,包括 57 次插入2 次删除
  1. 55 0
      hrt/prefab/l3d/Spline.hx
  2. 2 2
      hrt/prefab/l3d/SplineMesh.hx

+ 55 - 0
hrt/prefab/l3d/Spline.hx

@@ -292,6 +292,28 @@ class Spline extends hrt.prefab.Object3D {
 		return samples[samples.length - 1].length;
 	}
 
+	public function recomputeTangents() {
+		for (idx in 1...points.length) {
+			var tan : h3d.Vector;
+			if (idx == points.length - 1) {
+				tan = (points[idx].pos - points[idx - 1].pos).normalized();
+				points[idx].tangentIn = tan * -1.;
+				points[idx].tangentOut = tan;
+				continue;
+			}
+
+			if (idx == 1) {
+				tan = (points[1].pos - points[0].pos).normalized();
+				points[0].tangentIn = tan * -1.;
+				points[0].tangentOut = tan;
+			}
+
+			tan = (points[idx + 1].pos - points[idx - 1].pos).normalized();
+			points[idx].tangentIn = tan * -1;
+			points[idx].tangentOut = tan;
+		}
+	}
+
 
 	public function addPoint(?idx : Int, ?point : SplinePoint) {
 		var newPoint = point;
@@ -657,9 +679,41 @@ class Spline extends hrt.prefab.Object3D {
 					<div class="points-container">
 					</div>
 				</div>
+				<div align="center">
+					<input type="button" value="Recompute Tangents" class="recompute" />
+				</div>
 			</div>
 		');
 
+		var recomputeTangent = props.find(".recompute");
+		recomputeTangent.click(function(_) {
+			var prevPoints = [ for (p in points) p.save() ];
+			recomputeTangents();
+			this.updateInstance();
+			refreshHandles();
+			var newPoints = [ for (p in points) p.save() ];
+			ctx.properties.undo.change(Custom(function(undo) {
+				if (undo) {
+					points = [];
+					for (obj in prevPoints) {
+						var p = new SplinePoint();
+						p.load(obj);
+						points.push(p);
+					}
+				}
+				else {
+					points = [];
+					for (obj in newPoints) {
+						var p = new SplinePoint();
+						p.load(obj);
+						points.push(p);
+					}
+				}
+				this.updateInstance();
+				refreshHandles();
+			}));
+		});
+
 		var editModeButton = props.find(".editModeButton");
 		editModeButton.click(function(_) {
 			if (!enabled) return;
@@ -712,6 +766,7 @@ class Spline extends hrt.prefab.Object3D {
 		return b;
 	}
 
+
 	function refreshPointList(ctx: hide.prefab.EditContext) {
 		var pointsContainer = ctx.properties.element.find(".points-container");
 		pointsContainer.empty();

+ 2 - 2
hrt/prefab/l3d/SplineMesh.hx

@@ -153,7 +153,7 @@ class SplineMesh extends hrt.prefab.Object3D {
 				uv += curPos.distance(prevPos);
 				var tangent = samples[i].tangentOut.normalized();
 				var angle = hxd.Math.acos( tangent.dot(new h3d.Vector(1.0, 0.0, 0.0)) );
-				if ( tangent.dot(new h3d.Vector(0.0, 1.0, 0.0)) < 0.0)
+				if (tangent.dot(new h3d.Vector(0.0, 1.0, 0.0)) < 0.0)
 					angle *= -1.0;
 				var trs = new h3d.Matrix();
 				trs.initRotationAxis(new h3d.Vector(0.0, 0.0, 1.0), angle);
@@ -246,7 +246,7 @@ class SplineMesh extends hrt.prefab.Object3D {
 		return {
 			icon : "arrows-v",
 			name : "SplineMesh",
-			allowParent: (p) -> Std.isOfType(p, Spline) || p.parent == null
+			allowParent : (p) -> Std.isOfType(p, Spline) || p.parent == null
 		};
 	}
 	#end