فهرست منبع

Adding UVs on end caps

Bastiaan Olij 6 سال پیش
والد
کامیت
9703153396
1فایلهای تغییر یافته به همراه34 افزوده شده و 3 حذف شده
  1. 34 3
      modules/csg/csg_shape.cpp

+ 34 - 3
modules/csg/csg_shape.cpp

@@ -1666,6 +1666,24 @@ CSGBrush *CSGPolygon::_build_brush() {
 	Path *path = NULL;
 	Ref<Curve3D> curve;
 
+	// get bounds for our polygon
+	Vector2 final_polygon_min;
+	Vector2 final_polygon_max;
+	for (int i = 0; i < final_polygon.size(); i++) {
+		Vector2 p = final_polygon[i];
+		if (i == 0) {
+			final_polygon_min = p;
+			final_polygon_max = final_polygon_min;
+		} else {
+			if (p.x < final_polygon_min.x) final_polygon_min.x = p.x;
+			if (p.y < final_polygon_min.y) final_polygon_min.y = p.y;
+
+			if (p.x > final_polygon_max.x) final_polygon_max.x = p.x;
+			if (p.y > final_polygon_max.y) final_polygon_max.y = p.y;
+		}
+	}
+	Vector2 final_polygon_size = final_polygon_max - final_polygon_min;
+
 	if (mode == MODE_PATH) {
 		if (!has_node(path_node))
 			return NULL;
@@ -1757,6 +1775,10 @@ CSGBrush *CSGPolygon::_build_brush() {
 								v.z -= depth;
 							}
 							facesw[face * 3 + k] = v;
+							uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
+							if (i == 0) {
+								uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
+							}
 						}
 
 						smoothw[face] = false;
@@ -1888,6 +1910,7 @@ CSGBrush *CSGPolygon::_build_brush() {
 								Vector2 p = final_polygon[triangles[j + src[k]]];
 								Vector3 v = Vector3(p.x, p.y, 0);
 								facesw[face * 3 + k] = v;
+								uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
 							}
 
 							smoothw[face] = false;
@@ -1905,6 +1928,8 @@ CSGBrush *CSGPolygon::_build_brush() {
 								Vector2 p = final_polygon[triangles[j + src[k]]];
 								Vector3 v = Vector3(normali_n.x * p.x, p.y, normali_n.z * p.x);
 								facesw[face * 3 + k] = v;
+								uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
+								uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
 							}
 
 							smoothw[face] = false;
@@ -1990,10 +2015,10 @@ CSGBrush *CSGPolygon::_build_brush() {
 							};
 
 							Vector2 u[4] = {
-								Vector2(u1, 0),
 								Vector2(u1, 1),
-								Vector2(u2, 1),
-								Vector2(u2, 0)
+								Vector2(u1, 0),
+								Vector2(u2, 0),
+								Vector2(u2, 1)
 							};
 
 							// face 1
@@ -2036,6 +2061,7 @@ CSGBrush *CSGPolygon::_build_brush() {
 								Vector2 p = final_polygon[triangles[j + src[k]]];
 								Vector3 v = Vector3(p.x, p.y, 0);
 								facesw[face * 3 + k] = xf.xform(v);
+								uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
 							}
 
 							smoothw[face] = false;
@@ -2053,6 +2079,8 @@ CSGBrush *CSGPolygon::_build_brush() {
 								Vector2 p = final_polygon[triangles[j + src[k]]];
 								Vector3 v = Vector3(p.x, p.y, 0);
 								facesw[face * 3 + k] = xf.xform(v);
+								uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
+								uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
 							}
 
 							smoothw[face] = false;
@@ -2077,6 +2105,9 @@ CSGBrush *CSGPolygon::_build_brush() {
 			} else {
 				aabb.expand_to(facesw[i]);
 			}
+
+			// invert UVs on the Y-axis OpenGL = upside down
+			uvsw[i].y = 1.0 - uvsw[i].y;
 		}
 	}