Browse Source

Added option for UVs (and tangents) in adding sphere for ImmediateGeometry, closes #6398

(cherry picked from commit f31400c04d49aee2d2433b5404e2f17cf54da3a1)
Juan Linietsky 9 years ago
parent
commit
7178399548
2 changed files with 7 additions and 3 deletions
  1. 6 2
      scene/3d/immediate_geometry.cpp
  2. 1 1
      scene/3d/immediate_geometry.h

+ 6 - 2
scene/3d/immediate_geometry.cpp

@@ -102,7 +102,7 @@ DVector<Face3> ImmediateGeometry::get_faces(uint32_t p_usage_flags) const {
 
 
 
-void ImmediateGeometry::add_sphere(int p_lats,int p_lons,float p_radius) {
+void ImmediateGeometry::add_sphere(int p_lats, int p_lons, float p_radius, bool p_add_uv) {
 
 	for(int i = 1; i <= p_lats; i++) {
 		double lat0 = Math_PI * (-0.5 + (double) (i - 1) / p_lats);
@@ -132,6 +132,10 @@ void ImmediateGeometry::add_sphere(int p_lats,int p_lons,float p_radius) {
 			};
 
 #define ADD_POINT(m_idx)\
+	if (p_add_uv) {\
+		set_uv(Vector2(Math::atan2(v[m_idx].x,v[m_idx].z)/Math_PI * 0.5+0.5,v[m_idx].y*0.5+0.5));\
+		set_tangent(Plane(Vector3(-v[m_idx].z,v[m_idx].y,v[m_idx].x),1));		\
+	}\
 	set_normal(v[m_idx]);\
 	add_vertex(v[m_idx]*p_radius);
 
@@ -156,7 +160,7 @@ void ImmediateGeometry::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("set_uv","uv"),&ImmediateGeometry::set_uv);
 	ObjectTypeDB::bind_method(_MD("set_uv2","uv"),&ImmediateGeometry::set_uv2);
 	ObjectTypeDB::bind_method(_MD("add_vertex","pos"),&ImmediateGeometry::add_vertex);
-	ObjectTypeDB::bind_method(_MD("add_sphere","lats","lons","radius"),&ImmediateGeometry::add_sphere);
+	ObjectTypeDB::bind_method(_MD("add_sphere","lats","lons","radius","add_uv"),&ImmediateGeometry::add_sphere,DEFVAL(true));
 	ObjectTypeDB::bind_method(_MD("end"),&ImmediateGeometry::end);
 	ObjectTypeDB::bind_method(_MD("clear"),&ImmediateGeometry::clear);
 

+ 1 - 1
scene/3d/immediate_geometry.h

@@ -62,7 +62,7 @@ public:
 	void clear();
 
 
-	void add_sphere(int p_lats,int p_lons,float p_radius);
+	void add_sphere(int p_lats,int p_lons,float p_radius,bool p_add_uv=true);