Skip to content

Commit

Permalink
Added basic GLTF support (using wojtekpil's code)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Feb 2, 2024
1 parent d55c2c6 commit f4d1880
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 40 deletions.
40 changes: 40 additions & 0 deletions addons/material_maker/mesh_loader/mesh_loader.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
extends RefCounted

static func load_gltf_mesh(file_path) -> ArrayMesh:
# Contributed by wojtekpil
# This will load gltf mesh and return #1 Mesh of #1 MeshInstance
# Material Editing will work only on first surface too. This should be easy to
# fix with MeshInstance.material_overwrite instaed of get_surface_override_material
# Probably can be improved later to combine/support all Meshes in GTLF
var gltf : GLTFDocument = GLTFDocument.new()
var gltf_state : GLTFState = GLTFState.new()
if not FileAccess.file_exists(file_path):
printerr("GLTF/GLB file does not exists or is not accesible")
return null
var glb_filebytes = FileAccess.get_file_as_bytes(file_path)
if glb_filebytes == null:
printerr("GLTF/GLB file data is null")
return null
var err = gltf.append_from_buffer(glb_filebytes, "", gltf_state)
if err != OK:
printerr("Failure during parsing GLTF/GLB buffer")
return null
var node: Node = gltf.generate_scene(gltf_state)
if node is MeshInstance3D:
return node.mesh
var mesh_instances : Array[Node] = node.find_children("*", "MeshInstance3D", true, false)
if mesh_instances.size() == 0:
printerr("GLTF/GLB does not include any meshes")
return null
return mesh_instances[0].mesh

static func load_mesh(path : String) -> ArrayMesh:
if path == null:
return null
var ext : String = path.get_extension()
if ext.matchn("obj"):
var obj_loader = load("res://addons/material_maker/mesh_loader/obj_loader.gd")
return obj_loader.load_obj_file(path)
elif ext.matchn("glb") or ext.matchn("gltf"):
return load_gltf_mesh(path)
return null
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extends Object
extends RefCounted

static func load_obj_file(path : String) -> ArrayMesh:
if path == null:
Expand Down
82 changes: 69 additions & 13 deletions material_maker/icons/icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 10 additions & 8 deletions material_maker/panels/paint/paint.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ var remote_node = null
var brush_parameters : Dictionary = {
brush_size = 50.0,
brush_hardness = 0.5,
pattern_scale = 10.0,
pattern_angle = 0.0
}
var pattern_scale : float = 10.0
var pattern_angle : float = 0.0

var idmap_filename : String = ""
var mask : ImageTexture
Expand Down Expand Up @@ -403,10 +403,12 @@ func handle_stroke_input(ev : InputEvent, painting_mode : int = PAINTING_MODE_VI
$VSplitContainer/HSplitContainer/Painter/Options/OptionsPanel/Brush/BrushHardness.set_value(brush_parameters.brush_hardness)
elif ev.is_command_or_control_pressed():
reset_stroke()
pattern_scale += ev.relative.x*0.1
pattern_scale = clamp(pattern_scale, 0.1, 25.0)
pattern_angle += fmod(ev.relative.y*0.01, 2.0*PI)
painter.update_brush_params( { pattern_scale=pattern_scale, pattern_angle=pattern_angle } )
brush_parameters.pattern_scale += ev.relative.x*0.1
brush_parameters.pattern_scale = clamp(brush_parameters.pattern_scale, 0.1, 25.0)
brush_parameters.pattern_angle = 0.5+(brush_parameters.pattern_angle+ev.relative.y*0.01)/TAU
brush_parameters.pattern_angle = TAU*(brush_parameters.pattern_angle-floor(brush_parameters.pattern_angle)-0.5)
painter.update_brush_params( { pattern_scale=brush_parameters.pattern_scale, pattern_angle=brush_parameters.pattern_angle } )
$VSplitContainer/HSplitContainer/Painter/Options/OptionsPanel/Brush/BrushAngle.set_value(brush_parameters.pattern_angle*57.2957795131)
elif current_tool == MODE_FREEHAND_DOTS or current_tool == MODE_FREEHAND_LINE:
paint(mouse_position, get_pressure(ev), ev.tilt, painting_mode)
last_tilt = ev.tilt
Expand Down Expand Up @@ -758,8 +760,8 @@ func load_project(file_name) -> bool:
var test_json_conv = JSON.new()
test_json_conv.parse(f.get_as_text())
var data = test_json_conv.get_data()
var obj_loader = load("res://material_maker/tools/obj_loader/obj_loader.gd").new()
var mesh : Mesh = obj_loader.load_obj_file(data.model)
var mesh_loader = load("res://addons/material_maker/mesh_loader/mesh_loader.gd").new()
var mesh : Mesh = mesh_loader.load_mesh(data.model)
if mesh == null:
return false
model_path = data.model
Expand Down
Loading

0 comments on commit f4d1880

Please sign in to comment.