How to implement 8bit sprite stroke shader – cocos2d-x


Nice results (rockman / mega man) from this shader :+1:

basically you just need to look at the fragment shader part of the unity shader

// If outline is enabled and there is a pixel, try to draw an outline.
	if (_Outline > 0 && c.a != 0) {
		// Get the neighbouring four pixels.
		fixed4 pixelUp = tex2D(_MainTex, IN.texcoord + fixed2(0, _MainTex_TexelSize.y));
		fixed4 pixelDown = tex2D(_MainTex, IN.texcoord - fixed2(0, _MainTex_TexelSize.y));
		fixed4 pixelRight = tex2D(_MainTex, IN.texcoord + fixed2(_MainTex_TexelSize.x, 0));
		fixed4 pixelLeft = tex2D(_MainTex, IN.texcoord - fixed2(_MainTex_TexelSize.x, 0));

		// If one of the neighbouring pixels is invisible, we render an outline.
		if (pixelUp.a * pixelDown.a * pixelRight.a * pixelLeft.a == 0) {
			c.rgba = fixed4(1, 1, 1, 1) * _OutlineColor;
		}
	}

	c.rgb *= c.a;

And then you just tweak the outline shader from cpp test to become like the unity’s one
(using up, down, right, left textureCoordinate offsets instead of ‘radius’).
Do note that texelSize does not come built-in for glsl, you will have to pass it in as a uniform.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *