Using viewport texture as uniform in shader

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

Hi, I’m trying to pass a viewport texture to my shader(spatial). I’ve try things like set resource local to scene, viewport update mode to ‘always’ but it doesnt help. I know there is a way to do this with code: turn viewport texture to ImageTexture and pass to shader. But it’s bad to do this every frame. Question is the thing works fine with a standard SpatialMaterial so what do i have to add to my shader to get it?

Here is the shader code:

shader_type spatial;

uniform sampler2D albedo : hint_albedo;
uniform sampler2D normalmap : hint_normal;

uniform sampler2D heightmap;
uniform vec2 size = vec2(512, 512);
uniform float amplitude = 15.0;

float get_height(vec2 pos) {
	pos -= size/2.0;
	pos /= size;
	return amplitude * texture(heightmap, pos).r;
}

void vertex() {
	VERTEX.y = get_height(VERTEX.xz);
	
	TANGENT = normalize(vec3(1.0, get_height(VERTEX.xz + vec2(1.0, 0.0)) - VERTEX.y, 0.0));
	BINORMAL = normalize(vec3(0.0, get_height(VERTEX.xz + vec2(0.0, 1.0)) - VERTEX.y, 1.0));
	NORMAL = cross(BINORMAL, TANGENT);
	
}

void fragment() {
	ALBEDO = texture(albedo, UV * 10.0).rgb;
	NORMALMAP = texture(normalmap, UV * 10.0).rgb;
}