Problem with effect

  • Original effect:

    vec3 light = vec3(-1, -1, 1.5);
    vec3 light_color = vec3(1, 1, 1);
    
    vec3 sphere = vec3(0, 0, 2);
    float sphere_size = 1.3;
    vec3 sphere_color = vec3(1, 1, 1);
    
    float raySphere(vec3 rpos, vec3 rdir, vec3 sp, float radius, inout vec3 point, inout vec3 normal) {
    	radius = radius * radius;
    	float dt = dot(rdir, sp - rpos);
    	if (dt < 0.0) {
    		return -1.0;
    	}
    	vec3 tmp = rpos - sp;
    	tmp.x = dot(tmp, tmp);
    	tmp.x = tmp.x - dt*dt;
    	if (tmp.x >= radius) {
    		return -1.0;
    	}
    	dt = dt - sqrt(radius - tmp.x);
    	point = rpos + rdir * dt;
    	normal = normalize(point - sp);
    	return dt;
    }
    
    void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
    	light.xy = iMouse.xy / iResolution.xy * 2.0 - 1.0;
    	light.y = -light.y;
    	vec3 point; 
    	vec3 normal;
    	vec2 uv = fragCoord.xy / iResolution.xy * 2.0 - 1.0;
    	uv.x *= iResolution.x / iResolution.y;
    	uv.y = -uv.y;
    	vec3 ray = vec3(uv.x, uv.y, 1.0);
    	ray = normalize(ray);
    	fragColor = vec4(0.0);
    	float dist = raySphere(vec3(0.0), ray, sphere, sphere_size, point, normal);
    	
    	if (dist > 0.0) {
    		vec3 tmp = normalize(light - sphere);
    		float u = atan(normal.z, normal.x) / 3.1415*2.0 + iTime / 5.0;
    		float v = asin(normal.y) / 3.1415*2.0 + 0.5;
    		
    		normal = normalize(normal * texture(iChannel0, vec2(u, v)).xyz);
    		
    		u = atan(normal.z, normal.x) / 3.1415*2.0 + iTime / 5.0;
    		v = asin(normal.y) / 3.1415*2.0 + 0.5;
     float gg = clamp(dot(reflect(tmp, normal), ray), 0.0, 1.0);
     gg = pow(gg, 400.0)*5.0;
    		fragColor.xyz = vec3(dot(tmp, normal)) * light_color * sphere_color * texture(iChannel0, vec2(u, v)).xyz + gg;
    	}
    }
    
    /////////////////////////////////////////////////////////
    // NewPlugin
    
    //The current foreground texture co-ordinate
    varying mediump vec2 vTex;
    //The foreground texture sampler, to be sampled at vTex
    uniform lowp sampler2D samplerFront;
    //The current foreground rectangle being rendered
    uniform mediump vec2 srcStart;
    uniform mediump vec2 srcEnd;
    //The current foreground source rectangle being rendered
    uniform mediump vec2 srcOriginStart;
    uniform mediump vec2 srcOriginEnd;
    //The current foreground source rectangle being rendered, in layout 
    uniform mediump vec2 layoutStart;
    uniform mediump vec2 layoutEnd;
    //The background texture sampler used for background - blending effects
    uniform lowp sampler2D samplerBack;
    //The current background rectangle being rendered to, in texture co-ordinates, for background-blending effects
    uniform mediump vec2 destStart;
    uniform mediump vec2 destEnd;
    //The time in seconds since the runtime started. This can be used for animated effects
    uniform mediump float seconds;
    //The size of a texel in the foreground texture in texture co-ordinates
    uniform mediump vec2 pixelSize;
    //The current layer scale as a factor (i.e. 1 is unscaled)
    uniform mediump float layerScale;
    //The current layer angle in radians.
    uniform mediump float layerAngle;
    
    precision mediump float;
    
    float pixelWidth;
    float pixelHeight;
    vec2 iResolution = vec2( 1.0/pixelWidth, 1.0/pixelHeight);
    
    vec3 light = vec3(-1.0, -1.0, 1.5);
    vec3 light_color = vec3(1.0, 1.0, 1.0);
    vec3 sphere = vec3(0.0, 0.0, 3.0);
    float sphere_size = 1.3;
    vec3 sphere_color = vec3(1.0, 1.0, 1.0);
    
    float raySphere(vec3 rpos, vec3 rdir, vec3 sp, float radius, inout vec3 point, inout vec3 normal) 
    {
    	radius = radius * radius;
    	float dt = dot(rdir, sp - rpos);
    	if (dt < 0.0) 
    	{
    	 return -1.0;
    	}
    	vec3 tmp = rpos - sp;
    	tmp.x = dot(tmp, tmp);
    	tmp.x = tmp.x - dt*dt;
    	if (tmp.x >= radius) 
    	{
    		return -1.0;
    	}
    	dt = dt - sqrt(radius - tmp.x);
    	point = rpos + rdir * dt;
    	normal = normalize(point - sp);
    	return dt;
    }
     
    void main(void)
    {
    	light.xy = vec2(700.0, 500.0) / iResolution.xy * 2.0 - 1.0;
    	light.y = -light.y;
    	vec3 point; 
    	vec3 normal;
    	vec2 uv = vTex * 2.0 - 1.0;
    	//uv.x *= iResolution.x / iResolution.y;
    	uv.y = -uv.y;
    	vec3 ray = vec3(uv.x, uv.y, 1.0);
    	ray = normalize(ray);
    	vec4 fragColor = vec4(0.0);
    	float dist = raySphere(vec3(0.0), ray, sphere, sphere_size, point, normal); 
    	if (dist > 0.0) 
    	{
    		 vec3 tmp = normalize(light - sphere);
    		 float u = atan(normal.z, normal.x) / 3.1415*2.0 + seconds / 5.0;
    		 float v = asin(normal.y) / 3.1415*2.0 + 0.5;
    		 normal = normalize(normal * texture2D(samplerFront, vec2(u, v)).xyz);
    		 u = atan(normal.z, normal.x) / 3.1415*2.0 + seconds / 5.0;
    		 v = asin(normal.y) / 3.1415*2.0 + 0.5;
     float gg = clamp(dot(reflect(tmp, normal), ray), 0.0, 1.0);
     gg = pow(gg, 1600.0)*5.0;
    		 fragColor.xyz = vec3(dot(tmp, normal)) * light_color * sphere_color * texture2D(samplerFront, vec2(u, v)).xyz + gg;
    	 lowp float a = texture2D(samplerFront, vTex).a;
    	 gl_FragColor = vec4(fragColor.xyz, a);
    	} 
    	
    }
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I posted a possible solution on the Construct Community Discord. Using fract() and normalize of uv/Vtex with srcOrigin*.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)