How do I make objects stop running code when its not in view

Godot Version

4.5

Question

Hey there! I’m making a large 3D landscape with trees that use a simple vertex shader to simulate wind. Though I don’t want the trees to run the shader script when the camera3D is not looking at them as to save performance, but I’m not sure how to pull that off. I am very new with GD script and still learning the fundamentals. Thanks a bunch in advance!

This is the shader I’m using in case it’s relevant;

shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_disabled, diffuse_burley, specular_schlick_ggx;

uniform vec4 albedo : source_color;
uniform sampler2D texture_albedo : source_color, filter_linear_mipmap, repeat_disable;
uniform float alpha_scissor_threshold : hint_range(0.0, 1.0, 0.001);
uniform ivec2 albedo_texture_size;
uniform float point_size : hint_range(0.1, 128.0, 0.1);

uniform float roughness : hint_range(0.0, 1.0);
uniform sampler2D texture_metallic : hint_default_white, filter_linear_mipmap, repeat_disable;
uniform vec4 metallic_texture_channel;
uniform sampler2D texture_roughness : hint_roughness_r, filter_linear_mipmap, repeat_disable;

uniform float specular : hint_range(0.0, 1.0, 0.01);
uniform float metallic : hint_range(0.0, 1.0, 0.01);

uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec3 uv2_scale;
uniform vec3 uv2_offset;

group_uniforms wind;
uniform sampler2D noise_tex;
uniform float wind_speed = .1;
uniform float wind_strength = .01;


void vertex() {
	UV = UV * uv1_scale.xy + uv1_offset.xy;

	vec3 GLOBAL_VERTEX = (MODEL_MATRIX * vec4(VERTEX, 1.)).xyz;

float offset = TIME * wind_speed;
float noise = texture(noise_tex, vec2(GLOBAL_VERTEX.x-offset)).r;
noise -= .5;
noise *= wind_strength;
VERTEX.xy += noise * length(VERTEX.y) * length(VERTEX.xz);

}

void fragment() {
	vec2 base_uv = UV;
	
	
	
	vec4 albedo_tex = texture(texture_albedo, base_uv);
	ALBEDO = albedo.rgb * albedo_tex.rgb;

	float metallic_tex = dot(texture(texture_metallic, base_uv), metallic_texture_channel);
	METALLIC = metallic_tex * metallic;
	SPECULAR = specular;

	vec4 roughness_texture_channel = vec4(1.0, 0.0, 0.0, 0.0);
	float roughness_tex = dot(texture(texture_roughness, base_uv), roughness_texture_channel);
	ROUGHNESS = roughness_tex * roughness;
	ALPHA *= albedo.a * albedo_tex.a;
	ALPHA_SCISSOR_THRESHOLD = alpha_scissor_threshold;
}

Godot automatically applies frustum culling (objects out of view won’t be rendered) so you don’t have to do anything. Here are some tips for 3D optimization:

Yooo that is amazing! That solves that issue haha

I will check out the documentation, thanks so much!

If you are still up for helping out, I would love to learn how to stop GD code from running while not in the camera view. Not the shader but the actual GD script ^^
Thanks again!

You can use process_mode and set it to PROCESS_MODE_DISABLED, this will stop processing the node entirely. However, I recommend using the VisibleOnScreenNotifier3D node instead and simply have some flags or boolean values that you toggle when the node is visible or not, so you can have more control over what specifically gets processed inside your node, and what won’t.

aah thanks so much! The VisibleOnScreen node works perfectly! I ended up going with the VisibleOnScreenEnabler which so far works exactly as i needed it to since the trees are just basic props. Thanks for the help!