i developing game android using opengl es 2.0 , have problem fragment shader drawing stars in background. i've got following code:
precision mediump float; varying vec2 transformed_position; float rand(vec2 co) { return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); } void main(void) { float distance = 10.0; float quantization_strength = 4.0; vec3 background_color = vec3(0.09, 0.0, 0.288); vec2 0 = vec2(0.0, 0.0); vec2 distance_vec = vec2(distance, distance); vec2 quantization_vec = vec2(quantization_strength, quantization_strength); vec2 new_vec = floor(transformed_position / quantization_vec) * quantization_vec; if(all(equal(mod(new_vec, distance_vec), zero))) { float rand_val = rand(new_vec); vec3 current_color = background_color * (1.0 + rand_val); gl_fragcolor = vec4(current_color.x, current_color.y, current_color.z, 1.0); } else { gl_fragcolor = vec4(background_color.x, background_color.y, background_color.z, 1.0 ); } }
my aim 'quantize' fragment coordinates, 'stars' not 1px in size, , light quantized pixels distant enough random amount. code, however, produces different results depending on executed. have used glsl sandbox (http://glsl.heroku.com), nexus 7 , htc desire s create comparison:
as can see, glsl sandbox produces dense grid many stars visible. on nexus 7 stars fewer , distributed along lines (which may not obvious on small image) - rand
function not work expected. desire s draws no stars @ all.
why rand
function work strangely on nexus 7 (if modify vector used dot product, stars distributed along lines @ different angle)? , might cause desire s not render stars?
i appreciate optimization tips shader, inexperienced glsl. or perhaps there better way draw 'stars' via fragment shader?
update
i changed code (i used http://glsl.heroku.com/e#9364.0 reference):
precision mediump float; varying highp vec2 transformed_position; highp float rand(vec2 co) { highp float = 1e3; highp float b = 1e-3; highp float c = 1e5; return fract(sin((co.x+co.y*a)*b)*c); } void main(void) { float size = 15.0; float prob = 0.97; lowp vec3 background_color = vec3(0.09, 0.0, 0.288); highp vec2 world_pos = transformed_position; vec2 pos = floor(1.0 / size * world_pos); float color = 0.0; highp float starvalue = rand(pos); if(starvalue > prob) { vec2 center = size * pos + vec2(size, size) * 0.5; float xy_dist = abs(world_pos.x - center.x) * abs(world_pos.y - center.y) / 5.0; color = 0.6 - distance(world_pos, center) / (0.5 * size) * xy_dist; } if(starvalue < prob || color < 0.0) { gl_fragcolor = vec4(background_color, 1.0); } else { float starintensity = fract(100.0 * starvalue); gl_fragcolor = vec4(background_color * (1.0 + color * 3.0 * starintensity), 1.0); } }
desire s gives me nice, uniformly distributed stars. problem nexus 7 still there. prob = 0.97, no stars displayed, , low prob = 0.01, appear sparsely placed along horizontal lines. why tegra 3 behave strangely?
Comments
Post a Comment