Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vulkan: ParticlesMaterial emission_color_texture causes unknown identifier error #58410

Closed
Tracked by #61067
h0lley opened this issue Feb 21, 2022 · 0 comments · Fixed by #62158
Closed
Tracked by #61067

Vulkan: ParticlesMaterial emission_color_texture causes unknown identifier error #58410

h0lley opened this issue Feb 21, 2022 · 0 comments · Fixed by #62158

Comments

@h0lley
Copy link

h0lley commented Feb 21, 2022

Godot version

4.0.alpha2

System information

Ubuntu, Vulkan, Nvidia

Issue description

When Godot converts a ParticlesMaterial that uses a emission_color_texture into a shader, it will put emission_tex_ofs into void process() when it is declared in void start(), resulting in an unknown identifier error.

Show example shader code
    1 | // NOTE: Shader automatically converted from Godot Engine 4.0.alpha2's ParticlesMaterial.
    2 | 
    3 | shader_type particles;
    4 | uniform vec3 direction;
    5 | uniform float spread;
    6 | uniform float flatness;
    7 | uniform float initial_linear_velocity_min;
    8 | uniform float initial_angle_min;
    9 | uniform float angular_velocity_min;
   10 | uniform float orbit_velocity_min;
   11 | uniform float linear_accel_min;
   12 | uniform float radial_accel_min;
   13 | uniform float tangent_accel_min;
   14 | uniform float damping_min;
   15 | uniform float scale_min;
   16 | uniform float hue_variation_min;
   17 | uniform float anim_speed_min;
   18 | uniform float anim_offset_min;
   19 | uniform float initial_linear_velocity_max;
   20 | uniform float initial_angle_max;
   21 | uniform float angular_velocity_max;
   22 | uniform float orbit_velocity_max;
   23 | uniform float linear_accel_max;
   24 | uniform float radial_accel_max;
   25 | uniform float tangent_accel_max;
   26 | uniform float damping_max;
   27 | uniform float scale_max;
   28 | uniform float hue_variation_max;
   29 | uniform float anim_speed_max;
   30 | uniform float anim_offset_max;
   31 | uniform float lifetime_randomness;
   32 | uniform sampler2D emission_texture_points : hint_black;
   33 | uniform int emission_texture_point_count;
   34 | uniform sampler2D emission_texture_color : hint_white;
   35 | uniform vec4 color_value : hint_color;
   36 | uniform vec3 gravity;
   37 | 
   38 | 
   39 | float rand_from_seed(inout uint seed) {
   40 | 	int k;
   41 | 	int s = int(seed);
   42 | 	if (s == 0)
   43 | 	s = 305420679;
   44 | 	k = s / 127773;
   45 | 	s = 16807 * (s - k * 127773) - 2836 * k;
   46 | 	if (s < 0)
   47 | 		s += 2147483647;
   48 | 	seed = uint(s);
   49 | 	return float(seed % uint(65536)) / 65535.0;
   50 | }
   51 | 
   52 | float rand_from_seed_m1_p1(inout uint seed) {
   53 | 	return rand_from_seed(seed) * 2.0 - 1.0;
   54 | }
   55 | 
   56 | uint hash(uint x) {
   57 | 	x = ((x >> uint(16)) ^ x) * uint(73244475);
   58 | 	x = ((x >> uint(16)) ^ x) * uint(73244475);
   59 | 	x = (x >> uint(16)) ^ x;
   60 | 	return x;
   61 | }
   62 | 
   63 | void start() {
   64 | 	uint base_number = NUMBER;
   65 | 	uint alt_seed = hash(base_number + uint(1) + RANDOM_SEED);
   66 | 	float angle_rand = rand_from_seed(alt_seed);
   67 | 	float scale_rand = rand_from_seed(alt_seed);
   68 | 	float hue_rot_rand = rand_from_seed(alt_seed);
   69 | 	float anim_offset_rand = rand_from_seed(alt_seed);
   70 | 	float pi = 3.14159;
   71 | 	float degree_to_rad = pi / 180.0;
   72 | 
   73 | 	int point = min(emission_texture_point_count - 1, int(rand_from_seed(alt_seed) * float(emission_texture_point_count)));
   74 | 	ivec2 emission_tex_size = textureSize(emission_texture_points, 0);
   75 | 	ivec2 emission_tex_ofs = ivec2(point % emission_tex_size.x, point / emission_tex_size.x);
   76 | 	float tex_angle = 0.0;
   77 | 	float tex_anim_offset = 1.0;
   78 | 	float spread_rad = spread * degree_to_rad;
   79 | 	if (RESTART_VELOCITY) {
   80 | 		float tex_linear_velocity = 1.0;
   81 | 		{
   82 | 			float angle1_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad;
   83 | 			angle1_rad += direction.x != 0.0 ? atan(direction.y, direction.x) : sign(direction.y) * (pi / 2.0);
   84 | 			vec3 rot = vec3(cos(angle1_rad), sin(angle1_rad), 0.0);
   85 | 			VELOCITY = rot * mix(initial_linear_velocity_min,initial_linear_velocity_max, rand_from_seed(alt_seed));
   86 | 		}
   87 | 	}
   88 | 	float base_angle = (tex_angle) * mix(initial_angle_min, initial_angle_max, angle_rand);
   89 | 	CUSTOM.x = base_angle * degree_to_rad;
   90 | 	CUSTOM.y = 0.0;
   91 | 	CUSTOM.w = (1.0 - lifetime_randomness * rand_from_seed(alt_seed));
   92 | 	CUSTOM.z = (tex_anim_offset) * mix(anim_offset_min, anim_offset_max, anim_offset_rand);
   93 | 	if (RESTART_POSITION) {
   94 | 		TRANSFORM[3].xyz = texelFetch(emission_texture_points, emission_tex_ofs, 0).xyz;
   95 | 	if (RESTART_VELOCITY) VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;
   96 | 	TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;
   97 | 	VELOCITY.z = 0.0;
   98 | 	TRANSFORM[3].z = 0.0;
   99 | 	}
  100 | }
  101 | 
  102 | void process() {
  103 | 	uint base_number = NUMBER;
  104 | 	uint alt_seed = hash(base_number + uint(1) + RANDOM_SEED);
  105 | 	float angle_rand = rand_from_seed(alt_seed);
  106 | 	float scale_rand = rand_from_seed(alt_seed);
  107 | 	float hue_rot_rand = rand_from_seed(alt_seed);
  108 | 	float anim_offset_rand = rand_from_seed(alt_seed);
  109 | 	float pi = 3.14159;
  110 | 	float degree_to_rad = pi / 180.0;
  111 | 
  112 | 	CUSTOM.y += DELTA / LIFETIME;
  113 | 	float tv = CUSTOM.y / CUSTOM.w;
  114 | 	float tex_linear_velocity = 1.0;
  115 | 	float tex_orbit_velocity = 1.0;
  116 | 	float tex_angular_velocity = 1.0;
  117 | 	float tex_linear_accel = 1.0;
  118 | 	float tex_radial_accel = 1.0;
  119 | 	float tex_tangent_accel = 1.0;
  120 | 	float tex_damping = 1.0;
  121 | 	float tex_angle = 1.0;
  122 | 	float tex_anim_speed = 1.0;
  123 | 	float tex_anim_offset = 1.0;
  124 | 	vec3 force = gravity;
  125 | 	vec3 pos = TRANSFORM[3].xyz;
  126 | 	pos.z = 0.0;
  127 | 	// apply linear acceleration
  128 | 	force += length(VELOCITY) > 0.0 ? normalize(VELOCITY) * tex_linear_accel * mix(linear_accel_min, linear_accel_max, rand_from_seed(alt_seed)) : vec3(0.0);
  129 | 	// apply radial acceleration
  130 | 	vec3 org = EMISSION_TRANSFORM[3].xyz;
  131 | 	vec3 diff = pos - org;
  132 | 	force += length(diff) > 0.0 ? normalize(diff) * tex_radial_accel * mix(radial_accel_min, radial_accel_max, rand_from_seed(alt_seed)) : vec3(0.0);
  133 | 	// apply tangential acceleration;
  134 | 	float tangent_accel_val = tex_tangent_accel * mix(tangent_accel_min, tangent_accel_max, rand_from_seed(alt_seed))
  135 | ;	force += length(diff.yx) > 0.0 ? vec3(normalize(diff.yx * vec2(-1.0, 1.0)), 0.0) * tangent_accel_val : vec3(0.0);
  136 | 	force += ATTRACTOR_FORCE;
  137 | 
  138 | 	// apply attractor forces
  139 | 	VELOCITY += force * DELTA;
  140 | 	// orbit velocity
  141 | 	float orbit_amount = tex_orbit_velocity * mix(orbit_velocity_min, orbit_velocity_max, rand_from_seed(alt_seed));
  142 | 	if (orbit_amount != 0.0) {
  143 | 	     float ang = orbit_amount * DELTA * pi * 2.0;
  144 | 	     mat2 rot = mat2(vec2(cos(ang), -sin(ang)), vec2(sin(ang), cos(ang)));
  145 | 	     TRANSFORM[3].xy -= diff.xy;
  146 | 	     TRANSFORM[3].xy += rot * diff.xy;
  147 | 	}
  148 | 	float dmp = mix(damping_min, damping_max, rand_from_seed(alt_seed));
  149 | 	if (dmp * tex_damping > 0.0) {
  150 | 		float v = length(VELOCITY);
  151 | 		float damp = tex_damping * dmp;
  152 | 		v -= damp * DELTA;
  153 | 		if (v < 0.0) {
  154 | 			VELOCITY = vec3(0.0);
  155 | 		} else {
  156 | 			VELOCITY = normalize(VELOCITY) * v;
  157 | 		}
  158 | 	}
  159 | 	float base_angle = (tex_angle) * mix(initial_angle_min, initial_angle_max, rand_from_seed(alt_seed));
  160 | 	base_angle += CUSTOM.y * LIFETIME * (tex_angular_velocity) * mix(angular_velocity_min,angular_velocity_max, rand_from_seed(alt_seed));
  161 | 	CUSTOM.x = base_angle * degree_to_rad;
  162 | 	CUSTOM.z = (tex_anim_offset) * mix(anim_offset_min, anim_offset_max, rand_from_seed(alt_seed)) + tv * tex_anim_speed * mix(anim_speed_min, anim_speed_max, rand_from_seed(alt_seed));
  163 | 	vec3 tex_scale = vec3(1.0);
  164 | 	float tex_hue_variation = 1.0;
  165 | 	float hue_rot_angle = (tex_hue_variation) * pi * 2.0 * mix(hue_variation_min, hue_variation_max, rand_from_seed(alt_seed));
  166 | 	float hue_rot_c = cos(hue_rot_angle);
  167 | 	float hue_rot_s = sin(hue_rot_angle);
  168 | 	mat4 hue_rot_mat = mat4(vec4(0.299, 0.587, 0.114, 0.0),
  169 | 			vec4(0.299, 0.587, 0.114, 0.0),
  170 | 			vec4(0.299, 0.587, 0.114, 0.0),
  171 | 			vec4(0.000, 0.000, 0.000, 1.0)) +
  172 | 		mat4(vec4(0.701, -0.587, -0.114, 0.0),
  173 | 			vec4(-0.299, 0.413, -0.114, 0.0),
  174 | 			vec4(-0.300, -0.588, 0.886, 0.0),
  175 | 			vec4(0.000, 0.000, 0.000, 0.0)) * hue_rot_c +
  176 | 		mat4(vec4(0.168, 0.330, -0.497, 0.0),
  177 | 			vec4(-0.328, 0.035,  0.292, 0.0),
  178 | 			vec4(1.250, -1.050, -0.203, 0.0),
  179 | 			vec4(0.000, 0.000, 0.000, 0.0)) * hue_rot_s;
  180 | 	COLOR = hue_rot_mat * color_value;
E 181-> 	COLOR *= texelFetch(emission_texture_color, emission_tex_ofs, 0);
  182 | 
  183 | 	TRANSFORM[0] = vec4(cos(CUSTOM.x), -sin(CUSTOM.x), 0.0, 0.0);
  184 | 	TRANSFORM[1] = vec4(sin(CUSTOM.x), cos(CUSTOM.x), 0.0, 0.0);
  185 | 	TRANSFORM[2] = vec4(0.0, 0.0, 1.0, 0.0);
  186 | 	float base_scale = mix(scale_min, scale_max, scale_rand);
  187 | 	base_scale = sign(base_scale) * max(abs(base_scale), 0.001);
  188 | 	TRANSFORM[0].xyz *= base_scale * sign(tex_scale.r) * max(abs(tex_scale.r), 0.001);
  189 | 	TRANSFORM[1].xyz *= base_scale * sign(tex_scale.g) * max(abs(tex_scale.g), 0.001);
  190 | 	TRANSFORM[2].xyz *= base_scale * sign(tex_scale.b) * max(abs(tex_scale.b), 0.001);
  191 | 	VELOCITY.z = 0.0;
  192 | 	TRANSFORM[3].z = 0.0;
  193 | 	if (CUSTOM.y > CUSTOM.w) {
  194 | 		ACTIVE = false;
  195 | 	}
  196 | }
  197 | 
  198 |

Steps to reproduce

  1. Create blank project
  2. Create 2D scene and add GPUParticles2D node
  3. Give it a ParticlesMaterial
  4. Select "Load Emission Mask":
    image
  5. Select a texture (e.g. icon.png) and tick "Capture from Pixel"
  6. Check log for errors:
:181 - Unknown identifier in expression: 'emission_tex_ofs'.
  Shader compilation failed.
  ./servers/rendering/renderer_rd/shader_rd.h:140 - Condition "!version" is true. Returning: RID()

Minimal reproduction project

skipping this as reproduction is trivial

@akien-mga akien-mga added this to the 4.0 milestone Feb 21, 2022
@Calinou Calinou changed the title Godot 4: ParticlesMaterial emission_color_texture causes unknown identifier error Vulkan: ParticlesMaterial emission_color_texture causes unknown identifier error Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants