-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip taking snapshot and compare working
- Loading branch information
1 parent
6b8b07c
commit c0b084f
Showing
17 changed files
with
294 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
extends DclTestingTools | ||
class_name TestingTools | ||
|
||
func take_and_compare_snapshot(id: String, camera_position: Vector3, camera_target: Vector3, snapshot_frame_size: Vector2, tolerance: float, dcl_rpc_sender: DclRpcSender): | ||
prints("take_and_compare_snapshot", id, camera_position, camera_target, snapshot_frame_size, tolerance, dcl_rpc_sender) | ||
|
||
# TODO: make this configurable | ||
var hide_player := true | ||
var update_snapshot := false | ||
var create_snapshot_if_does_not_exist := true | ||
|
||
var snapshot_path := "user://snapshot_" + id.replace(" ", "_") + ".png" | ||
|
||
var existing_snapshot: Image = null | ||
if not update_snapshot and FileAccess.file_exists(snapshot_path): | ||
existing_snapshot = Image.load_from_file(snapshot_path) | ||
|
||
|
||
RenderingServer.set_default_clear_color(Color(0, 0, 0, 0)) | ||
var viewport = get_viewport() | ||
var camera = viewport.get_camera_3d() | ||
var previous_camera_position = camera.global_position | ||
var previous_camera_rotation = camera.global_rotation | ||
var previous_viewport_size = viewport.size | ||
|
||
viewport.size = snapshot_frame_size | ||
camera.global_position = camera_position | ||
camera.look_at(camera_target) | ||
|
||
get_node("/root/explorer").set_visible_ui(false) | ||
if hide_player: | ||
get_node("/root/explorer/Player").hide() | ||
|
||
await get_tree().process_frame | ||
await get_tree().process_frame | ||
await get_tree().process_frame | ||
|
||
var viewport_img := viewport.get_texture().get_image() | ||
|
||
get_node("/root/explorer").set_visible_ui(true) | ||
if hide_player: | ||
get_node("/root/explorer/Player").show() | ||
|
||
viewport.size = previous_viewport_size | ||
camera.global_position = previous_camera_position | ||
camera.global_rotation = previous_camera_rotation | ||
|
||
var similarity := 0.0 | ||
var updated := false | ||
|
||
if existing_snapshot != null: | ||
similarity = self.compute_image_similarity(existing_snapshot, viewport_img) | ||
prints("similarity factor ", similarity) | ||
|
||
if update_snapshot or (existing_snapshot == null and create_snapshot_if_does_not_exist): | ||
viewport_img.save_png(snapshot_path) | ||
updated = true | ||
|
||
dcl_rpc_sender.send({ | ||
"is_match": similarity >= tolerance, | ||
"similarity": similarity, | ||
"was_exist": existing_snapshot != null, | ||
"replaced": updated, | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
rust/decentraland-godot-lib/src/godot_classes/dcl_rpc_sender.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use godot::engine::RefCounted; | ||
use godot::prelude::*; | ||
|
||
use crate::dcl::scene_apis::RpcResultSender; | ||
use crate::dcl::TakeAndCompareSnapshotResponse; | ||
|
||
#[derive(GodotClass)] | ||
#[class(init, base=RefCounted)] | ||
pub struct DclRpcSender { | ||
sender: Option<RpcResultSender<Result<TakeAndCompareSnapshotResponse, String>>>, | ||
|
||
#[base] | ||
_base: Base<RefCounted>, | ||
} | ||
|
||
impl godot::builtin::meta::GodotConvert for TakeAndCompareSnapshotResponse { | ||
type Via = Dictionary; | ||
} | ||
|
||
impl FromGodot for TakeAndCompareSnapshotResponse { | ||
fn try_from_godot(via: Dictionary) -> Option<Self> { | ||
let is_match = via.get("is_match")?.to::<bool>(); | ||
let similarity = via.get("similarity")?.to::<f32>(); | ||
let was_exist = via.get("was_exist")?.to::<bool>(); | ||
let replaced = via.get("replaced")?.to::<bool>(); | ||
Some(Self { | ||
is_match, | ||
similarity, | ||
was_exist, | ||
replaced, | ||
}) | ||
} | ||
} | ||
|
||
impl DclRpcSender { | ||
pub fn set_sender( | ||
&mut self, | ||
sender: RpcResultSender<Result<TakeAndCompareSnapshotResponse, String>>, | ||
) { | ||
self.sender = Some(sender); | ||
} | ||
} | ||
|
||
#[godot_api] | ||
impl DclRpcSender { | ||
#[func] | ||
fn send(&mut self, response: Variant) { | ||
if let Some(sender) = self.sender.as_ref() { | ||
let response = response.try_to::<Dictionary>().unwrap(); | ||
let response = TakeAndCompareSnapshotResponse::from_godot(response); | ||
let sender: tokio::sync::oneshot::Sender< | ||
Result<TakeAndCompareSnapshotResponse, String>, | ||
> = sender.take(); | ||
let ret = sender.send(Ok(response)); | ||
|
||
match ret { | ||
Ok(_) => { | ||
tracing::info!("Response sent"); | ||
} | ||
Err(e) => { | ||
tracing::info!("Error sending response {:?}", e); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.