Godot Version
4
Question
Hello everyone,
A few days ago, I shared a script that downloads textures from a URL and applies them as an override material. The script works perfectly in a standalone application, but I’ve run into an issue: the textures do not load in the browser at all.
I’ve included the source code below. Does anyone have any ideas on what might be causing this problem?
Thanks in advance for your help!
extends Node
@onready var http_request = $HTTPRequest
@onready var prefab_1 = preload("res://Bilder/rahmen_2.tscn")
@onready var prefab_2 = preload("res://Bilder/rahmen_3.tscn")
#load an image from the given url
func load_image_from_url(image_url: String) -> ImageTexture:
var error = $HTTPRequest.request(image_url)
if error != OK:
print("Fehler bei der Anfrage: ", error)
return null
var result = await $HTTPRequest.request_completed
var response_code = result[1]
var body = result[3]
if response_code == 200:
var image = Image.new()
var load_error = image.load_jpg_from_buffer(body)
if load_error == OK:
print("Image loaded. Width: ", image.get_width(), " Height: ", image.get_height())
var texture = ImageTexture.create_from_image(image)
print("Texture loaded")
return texture
else:
print("Error while loading the image: ", load_error)
return null
else:
print("HTTP Request error: ", response_code)
return null
func set_texture(target_node : Node3D, texture: ImageTexture, target: String):
if target_node != null:
var image_plane = target_node.find_child("Plane") as MeshInstance3D
if image_plane != null:
var material = StandardMaterial3D.new()
material.albedo_texture = texture
image_plane.set_surface_override_material(0, material)
else:
print("Plane node dont exist.")
else:
print("Target node dont exist.")
func load_prefab(location : String, type : int) -> Node3D:
var root = self.get_parent()
var targetNode = root.find_child(location) as Node3D
if targetNode != null:
var transform = targetNode.transform
print("Removing node " + location)
root.remove_child.call_deferred(targetNode)
if type == 1:
var node = prefab_1.instantiate()
root.add_child.call_deferred(node)
print("add node " + targetNode.name)
node.name = targetNode.name
node.transform = transform
return node
if type == 2:
var node = prefab_2.instantiate()
root.add_child.call_deferred(node)
print("add node " + targetNode.name)
node.name = targetNode.name
node.transform = transform
return node
return null
func set_url_target(target : Node3D, targetName : String, url : String):
target.set_meta("URL", url)
func existNode(name : String):
var root = self.get_parent()
if root.find_child(name) != null:
return true
else:
return false
func _ready():
# Pfad zur JSON-Datei
var file_path = "res://Data/Room.json"
var json_as_text = FileAccess.get_file_as_string(file_path)
var json_as_dict = JSON.parse_string(json_as_text)
if json_as_dict:
for item in json_as_dict["images"]:
if existNode(item["id"]):
print("Loading prefab for " + item["id"])
var imageNode = load_prefab(item["id"], item["type"])
print("Prefab for position " + item["id"] + " loaded")
print("Loading image from URL:", item["image"])
var image = await load_image_from_url(item["image"])
if image != null and imageNode != null:
set_texture(imageNode, image, item["id"])
set_url_target(imageNode, item["id"], item["url"])
print("Texture set")
else:
print("Error while loading texture")
else:
print("Error while parsing the json file")