locked
Can't get CCClippingNode to work - has anyone? RRS feed

  • Question

  • User68877 posted

    Hi, I'm wondering if anyone can help me with this... basically I've got a sprite that I want to reveal by using a mask that get's updated upon certain actions in the game.

    So I'm using a CCDrawNode for the CCClippingNode stencil and a sprite for the content. No matter what I do the whole sprite gets drawn. Here's some example code which should only reveal a small rectangular area of the full image:

    CCSprite fullImage = new CCSprite("myphoto");
    
    CCDrawNode revealMask = new CCDrawNode();
    revealMask.DrawRect(new CCRect(100.0F, 100.0F, 300.0F, 200.0F), CCColor4B.White);
    
    CCClippingNode revealedImage = new CCClippingNode();
    revealedImage.ContentSize = ActualBackground.ContentSize;
    revealedImage.AnchorPoint = new CCPoint(0.5f, 0.5f);
    revealedImage.Stencil = revealMask;
    revealedImage.AddChild(fullImage);
    
    backgroundLayer.AddChild(revealedImage);
    

    I have also tried setting the CCClippingNode alpha threshold to 0.0 as this is what some COCOS2D examples use and this has no effect.

    I then tried downloading the CocosSharp source code from GitHub and compiled and ran the suite of tests that is included with it and I have to say that I don't think that the CCClippingNode tests are working - these don't seem to be clipping either. Is something broken in CocosSharp.

    I've tried using both the 1.2 and the newly updated 1.3 versions. I'm running on iOS using the simulator if that makes any difference.

    Any help will be gratefully appreciated, Adrian

    Monday, January 12, 2015 12:43 AM

Answers

  • User68494 posted

    Adrian

    The clipping node does not work on iOS or Mac at this time. I have set up an issue for tracking here.

    Thank you for reporting this.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, January 12, 2015 8:15 AM
  • User68494 posted

    Not sure at this time. We are tracking it and are working on Unified support.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, January 12, 2015 8:23 AM

All replies

  • User68494 posted

    Adrian

    The clipping node does not work on iOS or Mac at this time. I have set up an issue for tracking here.

    Thank you for reporting this.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, January 12, 2015 8:15 AM
  • User68877 posted

    Thanks for confirming this. I can live with this for a while as I can swap my main testing from iOS to Android. However this is quite key to my game, is this likely to be fixed in the next couple of months?

    Monday, January 12, 2015 8:20 AM
  • User68494 posted

    Not sure at this time. We are tracking it and are working on Unified support.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, January 12, 2015 8:23 AM
  • User68877 posted

    OK - thank you again. I'll try a different approach using a render texture to try and blend a draw node with the background image and save the resultant texture into a sprite. Unless the problem is to do with the BlendFunc as I think I need to use CCBlendFunc.Opaque for the mask and then CCBlendFunc.AlphaBlend for my background.

    Monday, January 12, 2015 8:32 AM
  • User68877 posted

    Now with the CocosSharp 1.3.2 update it is now possible to create a masked sprite using a draw node mask as follows:

        public static CCSprite CreateMaskedSprite(CCSprite image, CCDrawNode mask)
        {
            // see http://www.raywenderlich.com/4421/how-to-mask-a-sprite-with-cocos2d-1-0
    
            // remember current values to re-instate
            CCPoint     maskPosition        = mask.Position;
            CCPoint     maskAnchorPoint     = mask.AnchorPoint;
            CCBlendFunc maskBlendFunc       = mask.BlendFunc;
            CCPoint     imagePosition       = image.Position;
            CCPoint     imageAnchorPoint    = image.AnchorPoint;
            CCBlendFunc imageBlendFunc      = image.BlendFunc;
    
            // ensure we have setup what we need to correctly create masked sprite
            mask.Position       = CCPoint.Zero;
            mask.AnchorPoint    = CCPoint.AnchorLowerLeft;
            mask.BlendFunc      = new CCBlendFunc(CCOGLES.GL_ONE, CCOGLES.GL_ZERO);
            image.Position      = CCPoint.Zero;
            image.AnchorPoint   = CCPoint.AnchorLowerLeft;
            image.BlendFunc     = new CCBlendFunc(CCOGLES.GL_DST_ALPHA, CCOGLES.GL_ZERO);
    
            // create the masked image
            CCRenderTexture rt  = new CCRenderTexture(image.ContentSize, image.TextureRectInPixels.Size, CCSurfaceFormat.Color, CCDepthFormat.Depth24Stencil8);
            rt.Begin();
            mask.Visit();
            image.Visit();
            rt.End();
    
            // re-instate values
            mask.Position       = maskPosition;
            mask.AnchorPoint    = maskAnchorPoint;
            mask.BlendFunc      = maskBlendFunc;
            image.Position      = imagePosition;
            image.AnchorPoint   = imageAnchorPoint;
            image.BlendFunc     = imageBlendFunc;
    
            // return masked sprite
            return new CCSprite(rt.Sprite.Texture);
        }
    

    Hope that helps.

    Tuesday, March 17, 2015 10:38 AM