Achieve stage lighting and scratch card effects with Blendfunc

Source: Internet
Author: User
Tags addchild

"Turn" http://code.lovemiao.com/?p=136#more-136

Previously wrote a "Click Judgment of Irregular shape button", using Ccrendertexture to create a canvas, you can paint on the above, this time, the art students in the spirit of the programmer to toss the end of the attitude, put forward another wonderful demand, due to the original demand design trade secrets, Here are just a few examples of the same explanation.

A fringe Benefit map:

God horse? Haven't seen enough? You want to see someone else? Please take a look at the full text

To achieve the above effect, Glblendfunc is a good choice. Glblendfunc is a way to set up the image overlay function, is to draw a picture on the canvas, with the specified color-mixing mode, so that the drawing and the original canvas of the picture on the color mixing operation, to achieve a variety of color mixing effect. About the use of Glblendfunc online there are many tutorials, the specific calculation principle is not described here, can be found in Microsoft's documentation (official website document layout is painful): http://msdn.microsoft.com/en-us/library/ms537046, The implementation of the above effects is described below.

1. Layered Drawing

A total of three layers of the figure, the original beauty map at the bottom, the second layer is a black mask layer, the third layer is the light layer.

For example, the mask layer is a slightly transparent black pigment, the light layer is a black light-shaped area marked only with an alpha value (please forgive me for drawing with the mouse), if you want to expose the beauty from under the black Mask layer, the black Mask layer "cutout a hole", and then the "hole" mask layer covered in the original.

Ccrendertexture again, as a temporary canvas, the black mask layer is drawn on the canvas, and then with the light layer for the process of color processing, to get "hole" mask layer.

2. Select the appropriate Blendfunc

Blendfunc is set to the light layer, if the light layer in which alpha is greater than 0 pixels are called labeled pixels, the desired result is the greater the alpha value, the lower the alpha value after the process color operation, the greater the transparency of the corresponding place on the black mask layer, the alpha value of 0 pixels, Does not affect the black matte layer.

Here the Black Mask layer (actually already painted on the rendertexture) is destination color, the light layer is the source color, because the light layer only play the role of the marked area, only the alpha value is valid, and should not affect the final result of the RGB value , so srcfactor select Gl_zero, black Mask layer as destination Color, is also the provider of RGB map, need to light layer mark the location of "key off", so dstfactor choose Gl_one_minues_src_alpha.

You can now use ccrendertexture and glblendfunc to achieve the stage lighting effect. The key code is as follows:

voidblendfunc::updatetexture () {rendertexture->beginwithclear (0, 0, 0, 0); Sprfore-visit (); Sprmask-visit (); Rendertexture-end ();} voidblendfunc::initsprites () {//Initialize all of the graphsSPRBG =NewCcsprite (); SPRBG->initwithfile ("Images/back2.png"); Sprfore=NewCcsprite (); Sprfore->initwithfile ("Images/fore2.png"); Sprmask=NewCcsprite (); Sprmask->initwithfile ("Images/mask3.png"); //Initialize CcrendertextureConst Ccsize &size = sprbg->getcontentsize (); Rendertexture=Newccrendertexture (); Rendertexture-initwithwidthandheight (Size.width, Size.Height, kcctexture2dpixelformat_rgba8888); Rendertexture->setcontentsize (size);//ccrendertexture contentsize need to be set manually, otherwise it will affect the result of coordinate conversionCcpoint POS= CCP (250, 150);//just as an example, here is a place to set upRendertexture->setposition (POS); SPRBG-setposition (POS); Sprfore->ignoreanchorpointforposition (true);//Ccsprite The default anchor point at the center of the image, ccrendertexture the origin at the point where you need to ignore the effect of the anchor point on the Ccspriterendertexture-AddChild (Sprmask);    AddChild (SPRBG);     AddChild (rendertexture);    Ccblendfunc Blendfunc; BLENDFUNC.SRC=Gl_zero; BLENDFUNC.DST=Gl_one_minus_src_alpha; Sprmask-Setblendfunc (Blendfunc); Updatetexture ();}

The SPRBG and Rendertexture are added to the parent node, and the visit function is called recursively by the main loop, which can be drawn directly, but the Sprmask is added to the rendertexture, and the sprmask is not drawn. Unless the Rendertexture Autodraw open, but the latest Cocosd-x 2.2.1 is not recommended, so you need to rely on the Updatetexture function to modify the contents of the Rendertexture, Otherwise, the rendertexture is always a blank canvas. The reason to add Sprmask to rendertexture is that it allows Sprmask's onenter and OnExit functions to be called to ensure the integrity of the sprmask life cycle.

It is now possible to illuminate any one mm by modifying the location of the Sprmask. Since sprmask only needs to be redrawn when the position is modified, Rendertexture spends most of its time drawing texture that have been painted, which is the same performance overhead as drawing a normal picture.

Lighting effect introduced, unfortunately only a lamp, if in the Updatetexture, retain the original canvas content, on this basis in different positions on this sprmask, so on the canvas there are two lights on the effect, and so on, can have more than a lamp effect.

voidblendfunc::updatetexture () {rendertexture-begin (); Sprmovablemask-visit (); Rendertexture-end ();} voidblendfunc::initsprites () {//Code Ibid ........ Ccblendfunc Blendfunc; BLENDFUNC.SRC=Gl_zero; BLENDFUNC.DST=Gl_one_minus_src_alpha; Sprmovablemask-Setblendfunc (Blendfunc); Rendertexture->beginwithclear (0, 0, 0, 0); Sprfore-visit (); Rendertexture-end (); Sprmovablemask->onmoving = [ This]{updatetexture (); };}

What if I replaced the light mask with a rubber mask? Let the rubber mask move with the finger, a scratch card is born.

Achieve stage lighting and scratch card effects with Blendfunc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.