Hi All,
Assuming we have a scene with a sprite3D and an orthographic camera. How could we calculate the value for "size" of the camera so that the texture in sprite3d look exactly the same size as it is in external image viewer (like Paint, or Photos). If you are familiar with Unity3D, it is relevant to Pixel Perfect topic but I cannot find any similar formula in Godot.
Thanks.

  • extends Spatial
    
    onready var film = get_node("Film")
    
    func _ready():
    	screen_to_pixels()
    	get_tree().get_root().connect("size_changed", self, "screen_to_pixels")
    	
    func screen_to_pixels():
    	var window_height = get_viewport().size.y
    	var view_size = window_height / 100.0
    	film.size = view_size

Well 3D objects don't really have a "pixel" size. They are vector points in arbitrary space. While you can certainly convert them, it still probably won't be pixel perfect unless you disable filtering on the image textures, and then are very careful that both the sprite and the camera are only moved at integer values. I can look into it, though I think it might make for a choppy experience.

The equation is:

extends Spatial

onready var image = get_node("Image")

func _ready():
	var fit_to_height = 100.0 / (image.texture.get_height())
	image.scale = Vector3.ONE * fit_to_height

That is to fit to to the full height of the widow/screen. If you need to fit it to a portion you would get the window/screen dimensions and use that in the calculations. Give me one moment.

Here is the updated script so it will be actual pixel size on the screen.

extends Spatial

onready var image = get_node("Image")

func _ready():
	var window_height = get_viewport().size.y
	var image_scale = 100.0 / window_height
	image.scale = Vector3.ONE * image_scale

    cybereality
    Hi guy,
    Many thanks for your useful information.
    Meanwhile, my concern is about the value for "size" property of the camera, not the size of the the sprite/texture.

    I got your point regarding disabling the filter when importing texture, assuming the windows size is 1280x720 px. Is there any formula to calculate the right value for camera size? (so that it looks the same as viewed in the external image viewer)
    Thanks.

    extends Spatial
    
    onready var film = get_node("Film")
    
    func _ready():
    	screen_to_pixels()
    	get_tree().get_root().connect("size_changed", self, "screen_to_pixels")
    	
    func screen_to_pixels():
    	var window_height = get_viewport().size.y
    	var view_size = window_height / 100.0
    	film.size = view_size

      cybereality
      Hi, really appreciate for your hint. I follow you guide and figure out the correct formula.
      For anyone who go here by Google, assuming we set Keep Aspect is "Keep Height", then the correct ortho-camera size is:
      ortho_camera_size = screen_width * pixel_size / 2
      screen_width is the width of the screen in pixel (can get with get_viewport().size.x);
      pixel_size is the property of the Sprite3D node (default is 0.01), be aware to set the same value for all Sprite3D nodes
      2 is here because the size of camera is the numbers of units we can see through half of the view port width (or height, depending on which dimension you keep aspect)
      Example: if the screen_width is 1920 pixels, the pixel_size of the Sprite3D is 0.01, then the ortho_camera_size is 1920 * 0.01 / 2 = 9.6