2018-07-28 21:21:05 +02:00
|
|
|
extends ViewportContainer
|
|
|
|
|
2019-10-18 00:40:47 +02:00
|
|
|
const CAMERA_DISTANCE_MIN = 1.0
|
|
|
|
const CAMERA_DISTANCE_MAX = 10.0
|
|
|
|
|
2019-12-10 06:54:10 +01:00
|
|
|
export var ui_path : String = "Preview3DUI"
|
|
|
|
|
2019-10-01 08:34:08 +02:00
|
|
|
onready var objects = $MaterialPreview/Preview3d/Objects
|
2019-09-29 10:07:32 +02:00
|
|
|
onready var current_object = objects.get_child(0)
|
|
|
|
|
2019-10-13 22:06:13 +02:00
|
|
|
onready var environments = $MaterialPreview/Preview3d/Environments
|
|
|
|
onready var current_environment = environments.get_child(0)
|
|
|
|
|
2019-10-14 20:42:11 +02:00
|
|
|
onready var camera_stand = $MaterialPreview/Preview3d/CameraPivot
|
|
|
|
onready var camera = $MaterialPreview/Preview3d/CameraPivot/Camera
|
|
|
|
|
2019-12-10 06:54:10 +01:00
|
|
|
onready var ui = get_node(ui_path)
|
|
|
|
|
|
|
|
signal need_update(me)
|
2019-09-29 10:07:32 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _ready() -> void:
|
2019-12-08 08:50:14 +01:00
|
|
|
var model_list : Array = []
|
2019-09-29 10:07:32 +02:00
|
|
|
for o in objects.get_children():
|
|
|
|
var m = o.get_surface_material(0)
|
|
|
|
o.set_surface_material(0, m.duplicate())
|
2019-12-08 08:50:14 +01:00
|
|
|
model_list.push_back(o.name)
|
2019-12-10 06:54:10 +01:00
|
|
|
ui.set_models(model_list)
|
2019-12-08 08:50:14 +01:00
|
|
|
var environment_list : Array = []
|
2019-10-13 22:06:13 +02:00
|
|
|
for e in environments.get_children():
|
2019-12-08 08:50:14 +01:00
|
|
|
environment_list.push_back(e.name)
|
2019-12-10 06:54:10 +01:00
|
|
|
ui.set_environments(environment_list)
|
2019-10-01 08:34:08 +02:00
|
|
|
$MaterialPreview/Preview3d/ObjectRotate.play("rotate")
|
2018-07-29 19:19:35 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _on_Model_item_selected(id) -> void:
|
2019-09-29 10:07:32 +02:00
|
|
|
current_object.visible = false
|
|
|
|
current_object = objects.get_child(id)
|
|
|
|
current_object.visible = true
|
2019-12-10 06:54:10 +01:00
|
|
|
emit_signal("need_update", [ self ])
|
2018-07-29 23:43:24 +02:00
|
|
|
|
2019-12-08 08:50:14 +01:00
|
|
|
func _on_Environment_item_selected(id) -> void:
|
|
|
|
current_environment.visible = false
|
|
|
|
current_environment = environments.get_child(id)
|
|
|
|
$MaterialPreview/Preview3d/CameraPivot/Camera.set_environment(current_environment.environment)
|
|
|
|
current_environment.visible = true
|
2018-08-07 09:31:41 +02:00
|
|
|
|
2019-12-08 08:50:14 +01:00
|
|
|
func _on_Rotate_toggled(button_pressed) -> void:
|
|
|
|
if button_pressed:
|
|
|
|
$MaterialPreview/Preview3d/ObjectRotate.play("rotate")
|
2018-08-07 09:31:41 +02:00
|
|
|
else:
|
2019-12-08 08:50:14 +01:00
|
|
|
$MaterialPreview/Preview3d/ObjectRotate.stop(false)
|
2019-09-29 21:53:32 +02:00
|
|
|
|
2019-12-08 08:50:14 +01:00
|
|
|
func get_materials() -> Array:
|
2020-03-12 07:51:01 +01:00
|
|
|
if current_object != null:
|
|
|
|
return [ current_object.get_surface_material(0) ]
|
|
|
|
return []
|
2019-12-08 08:50:14 +01:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func on_gui_input(event) -> void:
|
2019-09-29 21:53:32 +02:00
|
|
|
if event is InputEventMouseButton:
|
2019-10-16 15:08:21 +02:00
|
|
|
$MaterialPreview/Preview3d/ObjectRotate.stop(false)
|
2019-12-10 06:54:10 +01:00
|
|
|
ui.rotation_cancelled()
|
2019-10-14 20:42:11 +02:00
|
|
|
match event.button_index:
|
|
|
|
BUTTON_WHEEL_UP:
|
2019-10-18 00:40:47 +02:00
|
|
|
camera.translation.z = clamp(
|
|
|
|
camera.translation.z / (1.01 if event.shift else 1.1),
|
|
|
|
CAMERA_DISTANCE_MIN,
|
|
|
|
CAMERA_DISTANCE_MAX
|
|
|
|
)
|
2019-10-14 20:42:11 +02:00
|
|
|
BUTTON_WHEEL_DOWN:
|
2019-10-18 00:40:47 +02:00
|
|
|
camera.translation.z = clamp(
|
|
|
|
camera.translation.z * (1.01 if event.shift else 1.1),
|
|
|
|
CAMERA_DISTANCE_MIN,
|
|
|
|
CAMERA_DISTANCE_MAX
|
|
|
|
)
|
2019-09-29 21:53:32 +02:00
|
|
|
elif event is InputEventMouseMotion:
|
2019-10-14 20:42:11 +02:00
|
|
|
var motion = 0.01*event.relative
|
|
|
|
var camera_basis = camera.global_transform.basis
|
|
|
|
if event.shift:
|
|
|
|
if event.button_mask & BUTTON_MASK_LEFT:
|
|
|
|
objects.rotate(camera_basis.x.normalized(), motion.y)
|
|
|
|
objects.rotate(camera_basis.y.normalized(), motion.x)
|
|
|
|
elif event.button_mask & BUTTON_MASK_RIGHT:
|
|
|
|
objects.rotate(camera_basis.z.normalized(), motion.x)
|
|
|
|
else:
|
|
|
|
if event.button_mask & BUTTON_MASK_LEFT:
|
|
|
|
camera_stand.rotate(camera_basis.x.normalized(), -motion.y)
|
|
|
|
camera_stand.rotate(camera_basis.y.normalized(), -motion.x)
|
|
|
|
elif event.button_mask & BUTTON_MASK_RIGHT:
|
|
|
|
camera_stand.rotate(camera_basis.z.normalized(), -motion.x)
|