How to make a camera similar in function to sketchfab?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zedsterr

Guys, I need some help with camera stuff. I’m not a programmer by any means. I’m an artist. Have you guys used sketchfab? What would be the easiest way for me to set up a camera like they have in sketchfab to build a 3d model viewer? I need to be able to move around, orbit, zoom, etc without using any gui to do this. I did try a couple scripts I saw on YouTube but none of them really had a plug and play kind of function. I hope I don’t sound like I’m being difficult. I would love to learn to set this up and k have an open mind to learning programming haha. Thank you in advance.

:bust_in_silhouette: Reply From: MagnusS

I just wrote a quick script that should do exactly what you want. Just add a spatial to your scene that has this script attached to it and one child named “CamRotHelper” that is also a spatial. This CamRotHelper then contains a camera node. The camera node should have a z position of 2.

Here’s the script. For now it only works with your mouse and exactly like on sketchfab but I could add touch functionality.

extends Spatial

const ZOOM_FACTOR = 1.2
const ROTATE_SPEED = 0.5
const PAN_SPEED = 0.005

func _ready():
	pass # Replace with function body.

func _input(event):
	if event is InputEventMouseMotion:
		if Input.is_mouse_button_pressed(BUTTON_LEFT):
			# Rotate
			self.rotate_y(deg2rad(event.get_relative().x * ROTATE_SPEED))
			$CamRotHelper.rotate_x(deg2rad(event.get_relative().y * ROTATE_SPEED))
			
			#Stop rotation over top or bottom
			var camera_rot = $CamRotHelper.rotation_degrees
			camera_rot.x = clamp(camera_rot.x, -90, 90)
			$CamRotHelper.rotation_degrees = camera_rot
		elif Input.is_mouse_button_pressed(BUTTON_MIDDLE) or Input.is_mouse_button_pressed(BUTTON_RIGHT):
			# Pan
			self.translate(Vector3(
				event.get_relative().x * PAN_SPEED,
				event.get_relative().y * PAN_SPEED,
				0.0
			))
	elif event is InputEventMouseButton and event.is_pressed():
		# Zoom
		if event.button_index == BUTTON_WHEEL_DOWN:
			self.scale_object_local(Vector3.ONE * (ZOOM_FACTOR))
		elif event.button_index == BUTTON_WHEEL_UP:
			self.scale_object_local(Vector3.ONE * (1/ZOOM_FACTOR))

Oh my GOD you are a genius dude! I’m going to have to buy you a beer when i get paid LOL. I have been spending days trying to learn how to do this stuff. How did you come up with this solution? This is awesome. I want to learn programming and have NO clue where to start.

zedsterr | 2019-07-11 00:19

Heck, if touch support isn’t too much to implement that could be awesome as well. You should totally publish this somewhere though or even throw it on youtube. It’s a shame godot doesnt have this as a built in sort of template.

zedsterr | 2019-07-11 00:23