2017-03-05 15:47:28 +01:00
/*************************************************************************/
/* resource_importer_obj.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2017-03-05 15:47:28 +01:00
/*************************************************************************/
2022-01-03 21:27:34 +01:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2017-03-05 15:47:28 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2017-02-03 04:08:50 +01:00
# include "resource_importer_obj.h"
2021-06-11 14:51:48 +02:00
# include "core/io/file_access.h"
2018-09-11 18:13:45 +02:00
# include "core/io/resource_saver.h"
2021-09-21 03:24:31 +02:00
# include "scene/3d/importer_mesh_instance_3d.h"
2020-03-26 22:49:16 +01:00
# include "scene/3d/mesh_instance_3d.h"
# include "scene/3d/node_3d.h"
2021-09-21 03:24:31 +02:00
# include "scene/resources/importer_mesh.h"
2017-02-03 04:08:50 +01:00
# include "scene/resources/mesh.h"
# include "scene/resources/surface_tool.h"
2017-07-18 02:05:38 +02:00
uint32_t EditorOBJImporter : : get_import_flags ( ) const {
return IMPORT_SCENE ;
2017-02-03 04:08:50 +01:00
}
2022-05-13 15:04:37 +02:00
static Error _parse_material_library ( const String & p_path , HashMap < String , Ref < StandardMaterial3D > > & material_map , List < String > * r_missing_deps ) {
2022-03-23 10:08:58 +01:00
Ref < FileAccess > f = FileAccess : : open ( p_path , FileAccess : : READ ) ;
ERR_FAIL_COND_V_MSG ( f . is_null ( ) , ERR_CANT_OPEN , vformat ( " Couldn't open MTL file '%s', it may not exist or not be readable. " , p_path ) ) ;
2017-02-03 04:08:50 +01:00
2019-09-15 06:01:52 +02:00
Ref < StandardMaterial3D > current ;
2017-07-18 02:05:38 +02:00
String current_name ;
String base_path = p_path . get_base_dir ( ) ;
while ( true ) {
String l = f - > get_line ( ) . strip_edges ( ) ;
2017-02-03 04:08:50 +01:00
2017-07-18 02:05:38 +02:00
if ( l . begins_with ( " newmtl " ) ) {
//vertex
2017-02-03 04:08:50 +01:00
2017-07-18 02:05:38 +02:00
current_name = l . replace ( " newmtl " , " " ) . strip_edges ( ) ;
2021-06-18 00:03:09 +02:00
current . instantiate ( ) ;
2017-11-25 16:32:02 +01:00
current - > set_name ( current_name ) ;
2017-07-18 02:05:38 +02:00
material_map [ current_name ] = current ;
} else if ( l . begins_with ( " Ka " ) ) {
//uv
2019-11-07 09:44:15 +01:00
WARN_PRINT ( " OBJ: Ambient light for material ' " + current_name + " ' is ignored in PBR " ) ;
2017-02-03 04:08:50 +01:00
2017-07-18 02:05:38 +02:00
} else if ( l . begins_with ( " Kd " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
Vector < String > v = l . split ( " " , false ) ;
ERR_FAIL_COND_V ( v . size ( ) < 4 , ERR_INVALID_DATA ) ;
Color c = current - > get_albedo ( ) ;
c . r = v [ 1 ] . to_float ( ) ;
c . g = v [ 2 ] . to_float ( ) ;
c . b = v [ 3 ] . to_float ( ) ;
current - > set_albedo ( c ) ;
} else if ( l . begins_with ( " Ks " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
Vector < String > v = l . split ( " " , false ) ;
ERR_FAIL_COND_V ( v . size ( ) < 4 , ERR_INVALID_DATA ) ;
float r = v [ 1 ] . to_float ( ) ;
float g = v [ 2 ] . to_float ( ) ;
float b = v [ 3 ] . to_float ( ) ;
float metalness = MAX ( r , MAX ( g , b ) ) ;
current - > set_metallic ( metalness ) ;
} else if ( l . begins_with ( " Ns " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
Vector < String > v = l . split ( " " , false ) ;
ERR_FAIL_COND_V ( v . size ( ) ! = 2 , ERR_INVALID_DATA ) ;
float s = v [ 1 ] . to_float ( ) ;
current - > set_metallic ( ( 1000.0 - s ) / 1000.0 ) ;
} else if ( l . begins_with ( " d " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
Vector < String > v = l . split ( " " , false ) ;
ERR_FAIL_COND_V ( v . size ( ) ! = 2 , ERR_INVALID_DATA ) ;
float d = v [ 1 ] . to_float ( ) ;
Color c = current - > get_albedo ( ) ;
c . a = d ;
current - > set_albedo ( c ) ;
if ( c . a < 0.99 ) {
2019-09-15 06:01:52 +02:00
current - > set_transparency ( StandardMaterial3D : : TRANSPARENCY_ALPHA ) ;
2017-07-18 02:05:38 +02:00
}
} else if ( l . begins_with ( " Tr " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
Vector < String > v = l . split ( " " , false ) ;
ERR_FAIL_COND_V ( v . size ( ) ! = 2 , ERR_INVALID_DATA ) ;
float d = v [ 1 ] . to_float ( ) ;
Color c = current - > get_albedo ( ) ;
c . a = 1.0 - d ;
current - > set_albedo ( c ) ;
if ( c . a < 0.99 ) {
2019-09-15 06:01:52 +02:00
current - > set_transparency ( StandardMaterial3D : : TRANSPARENCY_ALPHA ) ;
2017-07-18 02:05:38 +02:00
}
} else if ( l . begins_with ( " map_Ka " ) ) {
//uv
2019-11-07 09:44:15 +01:00
WARN_PRINT ( " OBJ: Ambient light texture for material ' " + current_name + " ' is ignored in PBR " ) ;
2017-07-18 02:05:38 +02:00
} else if ( l . begins_with ( " map_Kd " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
String p = l . replace ( " map_Kd " , " " ) . replace ( " \\ " , " / " ) . strip_edges ( ) ;
2018-09-17 04:23:40 +02:00
String path ;
2021-06-03 15:41:22 +02:00
if ( p . is_absolute_path ( ) ) {
2018-09-17 04:23:40 +02:00
path = p ;
} else {
2022-08-30 02:34:01 +02:00
path = base_path . path_join ( p ) ;
2018-09-17 04:23:40 +02:00
}
2017-07-18 02:05:38 +02:00
2019-06-11 20:43:37 +02:00
Ref < Texture2D > texture = ResourceLoader : : load ( path ) ;
2017-07-18 02:05:38 +02:00
if ( texture . is_valid ( ) ) {
2019-09-15 06:01:52 +02:00
current - > set_texture ( StandardMaterial3D : : TEXTURE_ALBEDO , texture ) ;
2017-09-30 01:38:27 +02:00
} else if ( r_missing_deps ) {
2017-07-18 02:05:38 +02:00
r_missing_deps - > push_back ( path ) ;
}
} else if ( l . begins_with ( " map_Ks " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
String p = l . replace ( " map_Ks " , " " ) . replace ( " \\ " , " / " ) . strip_edges ( ) ;
2018-09-17 04:23:40 +02:00
String path ;
2021-06-03 15:41:22 +02:00
if ( p . is_absolute_path ( ) ) {
2018-09-17 04:23:40 +02:00
path = p ;
} else {
2022-08-30 02:34:01 +02:00
path = base_path . path_join ( p ) ;
2018-09-17 04:23:40 +02:00
}
2017-07-18 02:05:38 +02:00
2019-06-11 20:43:37 +02:00
Ref < Texture2D > texture = ResourceLoader : : load ( path ) ;
2017-07-18 02:05:38 +02:00
if ( texture . is_valid ( ) ) {
2019-09-15 06:01:52 +02:00
current - > set_texture ( StandardMaterial3D : : TEXTURE_METALLIC , texture ) ;
2017-09-30 01:38:27 +02:00
} else if ( r_missing_deps ) {
2017-07-18 02:05:38 +02:00
r_missing_deps - > push_back ( path ) ;
}
} else if ( l . begins_with ( " map_Ns " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
String p = l . replace ( " map_Ns " , " " ) . replace ( " \\ " , " / " ) . strip_edges ( ) ;
2018-09-17 04:23:40 +02:00
String path ;
2021-06-03 15:41:22 +02:00
if ( p . is_absolute_path ( ) ) {
2018-09-17 04:23:40 +02:00
path = p ;
} else {
2022-08-30 02:34:01 +02:00
path = base_path . path_join ( p ) ;
2018-09-17 04:23:40 +02:00
}
2017-07-18 02:05:38 +02:00
2019-06-11 20:43:37 +02:00
Ref < Texture2D > texture = ResourceLoader : : load ( path ) ;
2017-07-18 02:05:38 +02:00
if ( texture . is_valid ( ) ) {
2019-09-15 06:01:52 +02:00
current - > set_texture ( StandardMaterial3D : : TEXTURE_ROUGHNESS , texture ) ;
2017-09-30 01:38:27 +02:00
} else if ( r_missing_deps ) {
2017-07-18 02:05:38 +02:00
r_missing_deps - > push_back ( path ) ;
}
} else if ( l . begins_with ( " map_bump " ) ) {
//normal
ERR_FAIL_COND_V ( current . is_null ( ) , ERR_FILE_CORRUPT ) ;
String p = l . replace ( " map_bump " , " " ) . replace ( " \\ " , " / " ) . strip_edges ( ) ;
2022-08-30 02:34:01 +02:00
String path = base_path . path_join ( p ) ;
2017-02-03 04:08:50 +01:00
2019-06-11 20:43:37 +02:00
Ref < Texture2D > texture = ResourceLoader : : load ( path ) ;
2017-07-18 02:05:38 +02:00
if ( texture . is_valid ( ) ) {
2019-09-15 06:01:52 +02:00
current - > set_feature ( StandardMaterial3D : : FEATURE_NORMAL_MAPPING , true ) ;
current - > set_texture ( StandardMaterial3D : : TEXTURE_NORMAL , texture ) ;
2017-09-30 01:38:27 +02:00
} else if ( r_missing_deps ) {
2017-07-18 02:05:38 +02:00
r_missing_deps - > push_back ( path ) ;
}
} else if ( f - > eof_reached ( ) ) {
break ;
}
}
return OK ;
2017-02-03 04:08:50 +01:00
}
2020-03-17 07:33:00 +01:00
static Error _parse_obj ( const String & p_path , List < Ref < Mesh > > & r_meshes , bool p_single_mesh , bool p_generate_tangents , bool p_optimize , Vector3 p_scale_mesh , Vector3 p_offset_mesh , List < String > * r_missing_deps ) {
2022-03-23 10:08:58 +01:00
Ref < FileAccess > f = FileAccess : : open ( p_path , FileAccess : : READ ) ;
ERR_FAIL_COND_V_MSG ( f . is_null ( ) , ERR_CANT_OPEN , vformat ( " Couldn't open OBJ file '%s', it may not exist or not be readable. " , p_path ) ) ;
2017-07-18 02:05:38 +02:00
Ref < ArrayMesh > mesh ;
2021-06-18 00:03:09 +02:00
mesh . instantiate ( ) ;
2017-02-03 04:08:50 +01:00
2017-09-30 01:38:27 +02:00
bool generate_tangents = p_generate_tangents ;
2018-02-12 11:36:40 +01:00
Vector3 scale_mesh = p_scale_mesh ;
2020-01-11 09:21:38 +01:00
Vector3 offset_mesh = p_offset_mesh ;
2020-12-02 02:40:47 +01:00
int mesh_flags = 0 ;
2018-07-01 00:58:37 +02:00
2017-02-03 04:08:50 +01:00
Vector < Vector3 > vertices ;
Vector < Vector3 > normals ;
Vector < Vector2 > uvs ;
String name ;
2022-05-13 15:04:37 +02:00
HashMap < String , HashMap < String , Ref < StandardMaterial3D > > > material_map ;
2017-07-18 02:05:38 +02:00
2017-02-03 04:08:50 +01:00
Ref < SurfaceTool > surf_tool = memnew ( SurfaceTool ) ;
surf_tool - > begin ( Mesh : : PRIMITIVE_TRIANGLES ) ;
2017-07-18 02:05:38 +02:00
String current_material_library ;
String current_material ;
String current_group ;
2020-12-12 13:06:59 +01:00
uint32_t smooth_group = 0 ;
bool smoothing = true ;
2017-02-03 04:08:50 +01:00
while ( true ) {
String l = f - > get_line ( ) . strip_edges ( ) ;
2018-07-30 02:37:55 +02:00
while ( l . length ( ) & & l [ l . length ( ) - 1 ] = = ' \\ ' ) {
String add = f - > get_line ( ) . strip_edges ( ) ;
l + = add ;
2021-12-09 10:42:46 +01:00
if ( add . is_empty ( ) ) {
2018-07-30 02:37:55 +02:00
break ;
}
}
2017-02-03 04:08:50 +01:00
if ( l . begins_with ( " v " ) ) {
//vertex
Vector < String > v = l . split ( " " , false ) ;
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( v . size ( ) < 4 , ERR_FILE_CORRUPT ) ;
2017-02-03 04:08:50 +01:00
Vector3 vtx ;
2020-01-11 09:21:38 +01:00
vtx . x = v [ 1 ] . to_float ( ) * scale_mesh . x + offset_mesh . x ;
vtx . y = v [ 2 ] . to_float ( ) * scale_mesh . y + offset_mesh . y ;
vtx . z = v [ 3 ] . to_float ( ) * scale_mesh . z + offset_mesh . z ;
2017-02-03 04:08:50 +01:00
vertices . push_back ( vtx ) ;
} else if ( l . begins_with ( " vt " ) ) {
//uv
Vector < String > v = l . split ( " " , false ) ;
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( v . size ( ) < 3 , ERR_FILE_CORRUPT ) ;
2017-02-03 04:08:50 +01:00
Vector2 uv ;
uv . x = v [ 1 ] . to_float ( ) ;
uv . y = 1.0 - v [ 2 ] . to_float ( ) ;
uvs . push_back ( uv ) ;
} else if ( l . begins_with ( " vn " ) ) {
//normal
Vector < String > v = l . split ( " " , false ) ;
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( v . size ( ) < 4 , ERR_FILE_CORRUPT ) ;
2017-02-03 04:08:50 +01:00
Vector3 nrm ;
nrm . x = v [ 1 ] . to_float ( ) ;
nrm . y = v [ 2 ] . to_float ( ) ;
nrm . z = v [ 3 ] . to_float ( ) ;
normals . push_back ( nrm ) ;
2017-02-23 09:28:09 +01:00
} else if ( l . begins_with ( " f " ) ) {
2017-02-03 04:08:50 +01:00
//vertex
Vector < String > v = l . split ( " " , false ) ;
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( v . size ( ) < 4 , ERR_FILE_CORRUPT ) ;
2017-02-03 04:08:50 +01:00
//not very fast, could be sped up
Vector < String > face [ 3 ] ;
face [ 0 ] = v [ 1 ] . split ( " / " ) ;
face [ 1 ] = v [ 2 ] . split ( " / " ) ;
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( face [ 0 ] . size ( ) = = 0 , ERR_FILE_CORRUPT ) ;
2018-07-30 02:37:55 +02:00
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( face [ 0 ] . size ( ) ! = face [ 1 ] . size ( ) , ERR_FILE_CORRUPT ) ;
2017-02-03 04:08:50 +01:00
for ( int i = 2 ; i < v . size ( ) - 1 ; i + + ) {
face [ 2 ] = v [ i + 1 ] . split ( " / " ) ;
2018-07-30 02:37:55 +02:00
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( face [ 0 ] . size ( ) ! = face [ 2 ] . size ( ) , ERR_FILE_CORRUPT ) ;
2017-02-03 04:08:50 +01:00
for ( int j = 0 ; j < 3 ; j + + ) {
int idx = j ;
2019-06-20 16:59:48 +02:00
if ( idx < 2 ) {
2017-02-03 04:08:50 +01:00
idx = 1 ^ idx ;
}
if ( face [ idx ] . size ( ) = = 3 ) {
int norm = face [ idx ] [ 2 ] . to_int ( ) - 1 ;
2020-05-14 16:41:43 +02:00
if ( norm < 0 ) {
2017-04-27 10:24:09 +02:00
norm + = normals . size ( ) + 1 ;
2020-05-14 16:41:43 +02:00
}
2017-09-30 01:38:27 +02:00
ERR_FAIL_INDEX_V ( norm , normals . size ( ) , ERR_FILE_CORRUPT ) ;
2020-12-02 02:40:47 +01:00
surf_tool - > set_normal ( normals [ norm ] ) ;
2017-02-03 04:08:50 +01:00
}
2021-12-09 10:42:46 +01:00
if ( face [ idx ] . size ( ) > = 2 & & ! face [ idx ] [ 1 ] . is_empty ( ) ) {
2017-02-03 04:08:50 +01:00
int uv = face [ idx ] [ 1 ] . to_int ( ) - 1 ;
2020-05-14 16:41:43 +02:00
if ( uv < 0 ) {
2017-04-27 10:24:09 +02:00
uv + = uvs . size ( ) + 1 ;
2020-05-14 16:41:43 +02:00
}
2017-09-30 01:38:27 +02:00
ERR_FAIL_INDEX_V ( uv , uvs . size ( ) , ERR_FILE_CORRUPT ) ;
2020-12-02 02:40:47 +01:00
surf_tool - > set_uv ( uvs [ uv ] ) ;
2017-02-03 04:08:50 +01:00
}
int vtx = face [ idx ] [ 0 ] . to_int ( ) - 1 ;
2020-05-14 16:41:43 +02:00
if ( vtx < 0 ) {
2017-04-27 10:24:09 +02:00
vtx + = vertices . size ( ) + 1 ;
2020-05-14 16:41:43 +02:00
}
2017-09-30 01:38:27 +02:00
ERR_FAIL_INDEX_V ( vtx , vertices . size ( ) , ERR_FILE_CORRUPT ) ;
2017-02-03 04:08:50 +01:00
Vector3 vertex = vertices [ vtx ] ;
2020-12-12 13:06:59 +01:00
if ( ! smoothing ) {
smooth_group + + ;
}
surf_tool - > set_smooth_group ( smooth_group ) ;
2017-02-03 04:08:50 +01:00
surf_tool - > add_vertex ( vertex ) ;
}
face [ 1 ] = face [ 2 ] ;
}
2017-07-18 02:05:38 +02:00
} else if ( l . begins_with ( " s " ) ) { //smoothing
2017-02-03 04:08:50 +01:00
String what = l . substr ( 2 , l . length ( ) ) . strip_edges ( ) ;
2020-12-12 13:06:59 +01:00
bool do_smooth ;
2020-05-14 16:41:43 +02:00
if ( what = = " off " ) {
2020-12-12 13:06:59 +01:00
do_smooth = false ;
2020-05-14 16:41:43 +02:00
} else {
2020-12-12 13:06:59 +01:00
do_smooth = true ;
}
if ( do_smooth ! = smoothing ) {
smooth_group + + ;
smoothing = do_smooth ;
2020-05-14 16:41:43 +02:00
}
2017-07-29 05:03:54 +02:00
} else if ( /*l.begins_with("g ") | | */ l . begins_with ( " usemtl " ) | | ( l . begins_with ( " o " ) | | f - > eof_reached ( ) ) ) { //commit group to mesh
//groups are too annoying
2017-07-18 02:05:38 +02:00
if ( surf_tool - > get_vertex_array ( ) . size ( ) ) {
//another group going on, commit it
if ( normals . size ( ) = = 0 ) {
2017-02-03 04:08:50 +01:00
surf_tool - > generate_normals ( ) ;
2017-07-18 02:05:38 +02:00
}
if ( generate_tangents & & uvs . size ( ) ) {
2017-02-03 04:08:50 +01:00
surf_tool - > generate_tangents ( ) ;
2017-07-18 02:05:38 +02:00
}
2017-02-03 04:08:50 +01:00
surf_tool - > index ( ) ;
2017-07-18 02:05:38 +02:00
2018-08-24 09:35:07 +02:00
print_verbose ( " OBJ: Current material library " + current_material_library + " has " + itos ( material_map . has ( current_material_library ) ) ) ;
print_verbose ( " OBJ: Current material " + current_material + " has " + itos ( material_map . has ( current_material_library ) & & material_map [ current_material_library ] . has ( current_material ) ) ) ;
2017-07-18 02:05:38 +02:00
if ( material_map . has ( current_material_library ) & & material_map [ current_material_library ] . has ( current_material ) ) {
surf_tool - > set_material ( material_map [ current_material_library ] [ current_material ] ) ;
}
2018-07-01 00:58:37 +02:00
mesh = surf_tool - > commit ( mesh , mesh_flags ) ;
2017-07-18 02:05:38 +02:00
2021-12-09 10:42:46 +01:00
if ( ! current_material . is_empty ( ) ) {
2017-07-18 02:05:38 +02:00
mesh - > surface_set_name ( mesh - > get_surface_count ( ) - 1 , current_material . get_basename ( ) ) ;
2021-12-09 10:42:46 +01:00
} else if ( ! current_group . is_empty ( ) ) {
2017-07-18 02:05:38 +02:00
mesh - > surface_set_name ( mesh - > get_surface_count ( ) - 1 , current_group ) ;
}
2018-08-24 09:35:07 +02:00
print_verbose ( " OBJ: Added surface : " + mesh - > surface_get_name ( mesh - > get_surface_count ( ) - 1 ) ) ;
2017-02-03 04:08:50 +01:00
surf_tool - > clear ( ) ;
surf_tool - > begin ( Mesh : : PRIMITIVE_TRIANGLES ) ;
2017-07-18 02:05:38 +02:00
}
if ( l . begins_with ( " o " ) | | f - > eof_reached ( ) ) {
2017-09-30 01:38:27 +02:00
if ( ! p_single_mesh ) {
mesh - > set_name ( name ) ;
r_meshes . push_back ( mesh ) ;
2021-06-18 00:03:09 +02:00
mesh . instantiate ( ) ;
2017-09-30 01:38:27 +02:00
current_group = " " ;
current_material = " " ;
}
2017-02-03 04:08:50 +01:00
}
2017-07-18 02:05:38 +02:00
if ( f - > eof_reached ( ) ) {
break ;
}
if ( l . begins_with ( " o " ) ) {
2017-02-03 04:08:50 +01:00
name = l . substr ( 2 , l . length ( ) ) . strip_edges ( ) ;
2017-07-18 02:05:38 +02:00
}
if ( l . begins_with ( " usemtl " ) ) {
current_material = l . replace ( " usemtl " , " " ) . strip_edges ( ) ;
}
if ( l . begins_with ( " g " ) ) {
current_group = l . substr ( 2 , l . length ( ) ) . strip_edges ( ) ;
}
} else if ( l . begins_with ( " mtllib " ) ) { //parse material
current_material_library = l . replace ( " mtllib " , " " ) . strip_edges ( ) ;
if ( ! material_map . has ( current_material_library ) ) {
2022-05-13 15:04:37 +02:00
HashMap < String , Ref < StandardMaterial3D > > lib ;
2021-11-07 03:12:36 +01:00
String lib_path = current_material_library ;
if ( lib_path . is_relative_path ( ) ) {
2022-08-30 02:34:01 +02:00
lib_path = p_path . get_base_dir ( ) . path_join ( current_material_library ) ;
2017-07-18 23:21:51 +02:00
}
2021-11-07 03:12:36 +01:00
Error err = _parse_material_library ( lib_path , lib , r_missing_deps ) ;
2017-07-18 02:05:38 +02:00
if ( err = = OK ) {
material_map [ current_material_library ] = lib ;
}
}
2017-02-03 04:08:50 +01:00
}
}
2017-09-30 01:38:27 +02:00
if ( p_single_mesh ) {
r_meshes . push_back ( mesh ) ;
}
return OK ;
}
2022-05-13 15:04:37 +02:00
Node * EditorOBJImporter : : import_scene ( const String & p_path , uint32_t p_flags , const HashMap < StringName , Variant > & p_options , int p_bake_fps , List < String > * r_missing_deps , Error * r_err ) {
2020-03-17 07:33:00 +01:00
List < Ref < Mesh > > meshes ;
2017-09-30 01:38:27 +02:00
2021-04-05 13:16:16 +02:00
Error err = _parse_obj ( p_path , meshes , false , p_flags & IMPORT_GENERATE_TANGENT_ARRAYS , false , Vector3 ( 1 , 1 , 1 ) , Vector3 ( 0 , 0 , 0 ) , r_missing_deps ) ;
2017-09-30 01:38:27 +02:00
if ( err ! = OK ) {
if ( r_err ) {
* r_err = err ;
}
2020-04-02 01:20:12 +02:00
return nullptr ;
2017-09-30 01:38:27 +02:00
}
2020-03-26 22:49:16 +01:00
Node3D * scene = memnew ( Node3D ) ;
2017-09-30 01:38:27 +02:00
2021-07-26 17:50:35 +02:00
for ( const Ref < Mesh > & m : meshes ) {
2021-09-21 03:24:31 +02:00
Ref < ImporterMesh > mesh ;
2021-06-18 00:03:09 +02:00
mesh . instantiate ( ) ;
2020-12-12 13:06:59 +01:00
for ( int i = 0 ; i < m - > get_surface_count ( ) ; i + + ) {
mesh - > add_surface ( m - > surface_get_primitive_type ( i ) , m - > surface_get_arrays ( i ) , Array ( ) , Dictionary ( ) , m - > surface_get_material ( i ) ) ;
}
2021-09-21 03:24:31 +02:00
ImporterMeshInstance3D * mi = memnew ( ImporterMeshInstance3D ) ;
2020-12-12 13:06:59 +01:00
mi - > set_mesh ( mesh ) ;
2021-07-16 05:45:57 +02:00
mi - > set_name ( m - > get_name ( ) ) ;
2021-10-21 16:46:07 +02:00
scene - > add_child ( mi , true ) ;
2017-09-30 01:38:27 +02:00
mi - > set_owner ( scene ) ;
}
2017-02-03 04:08:50 +01:00
2017-09-30 01:38:27 +02:00
if ( r_err ) {
* r_err = OK ;
2017-02-03 04:08:50 +01:00
}
2017-07-18 02:05:38 +02:00
return scene ;
2017-02-03 04:08:50 +01:00
}
2020-05-14 14:29:06 +02:00
2017-09-30 01:38:27 +02:00
void EditorOBJImporter : : get_extensions ( List < String > * r_extensions ) const {
r_extensions - > push_back ( " obj " ) ;
}
2017-07-18 02:05:38 +02:00
EditorOBJImporter : : EditorOBJImporter ( ) {
2017-02-03 04:08:50 +01:00
}
2020-05-14 14:29:06 +02:00
2017-09-30 01:38:27 +02:00
////////////////////////////////////////////////////
String ResourceImporterOBJ : : get_importer_name ( ) const {
return " wavefront_obj " ;
}
2020-05-14 14:29:06 +02:00
2017-09-30 01:38:27 +02:00
String ResourceImporterOBJ : : get_visible_name ( ) const {
return " OBJ As Mesh " ;
}
2020-05-14 14:29:06 +02:00
2017-09-30 01:38:27 +02:00
void ResourceImporterOBJ : : get_recognized_extensions ( List < String > * p_extensions ) const {
p_extensions - > push_back ( " obj " ) ;
}
2020-05-14 14:29:06 +02:00
2017-09-30 01:38:27 +02:00
String ResourceImporterOBJ : : get_save_extension ( ) const {
return " mesh " ;
}
2020-05-14 14:29:06 +02:00
2017-09-30 01:38:27 +02:00
String ResourceImporterOBJ : : get_resource_type ( ) const {
return " Mesh " ;
}
2020-12-02 02:40:47 +01:00
int ResourceImporterOBJ : : get_format_version ( ) const {
return 1 ;
}
2017-09-30 01:38:27 +02:00
int ResourceImporterOBJ : : get_preset_count ( ) const {
return 0 ;
}
2020-05-14 14:29:06 +02:00
2017-09-30 01:38:27 +02:00
String ResourceImporterOBJ : : get_preset_name ( int p_idx ) const {
return " " ;
}
2021-11-14 18:02:38 +01:00
void ResourceImporterOBJ : : get_import_options ( const String & p_path , List < ImportOption > * r_options , int p_preset ) const {
2017-09-30 01:38:27 +02:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " generate_tangents " ) , true ) ) ;
2018-02-12 11:36:40 +01:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : VECTOR3 , " scale_mesh " ) , Vector3 ( 1 , 1 , 1 ) ) ) ;
2020-01-11 09:21:38 +01:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : VECTOR3 , " offset_mesh " ) , Vector3 ( 0 , 0 , 0 ) ) ) ;
2018-07-01 00:58:37 +02:00
r_options - > push_back ( ImportOption ( PropertyInfo ( Variant : : BOOL , " optimize_mesh " ) , true ) ) ;
2017-09-30 01:38:27 +02:00
}
2020-05-14 14:29:06 +02:00
2022-05-13 15:04:37 +02:00
bool ResourceImporterOBJ : : get_option_visibility ( const String & p_path , const String & p_option , const HashMap < StringName , Variant > & p_options ) const {
2017-09-30 01:38:27 +02:00
return true ;
}
2022-05-13 15:04:37 +02:00
Error ResourceImporterOBJ : : import ( const String & p_source_file , const String & p_save_path , const HashMap < StringName , Variant > & p_options , List < String > * r_platform_variants , List < String > * r_gen_files , Variant * r_metadata ) {
2020-03-17 07:33:00 +01:00
List < Ref < Mesh > > meshes ;
2017-09-30 01:38:27 +02:00
2020-04-02 01:20:12 +02:00
Error err = _parse_obj ( p_source_file , meshes , true , p_options [ " generate_tangents " ] , p_options [ " optimize_mesh " ] , p_options [ " scale_mesh " ] , p_options [ " offset_mesh " ] , nullptr ) ;
2017-09-30 01:38:27 +02:00
ERR_FAIL_COND_V ( err ! = OK , err ) ;
ERR_FAIL_COND_V ( meshes . size ( ) ! = 1 , ERR_BUG ) ;
String save_path = p_save_path + " .mesh " ;
2022-06-03 01:33:42 +02:00
err = ResourceSaver : : save ( meshes . front ( ) - > get ( ) , save_path ) ;
2017-09-30 01:38:27 +02:00
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( err ! = OK , err , " Cannot save Mesh to file ' " + save_path + " '. " ) ;
2017-09-30 01:38:27 +02:00
r_gen_files - > push_back ( save_path ) ;
return OK ;
}
ResourceImporterOBJ : : ResourceImporterOBJ ( ) {
}