`
silence19841230
  • 浏览: 6342 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一直在寻找的as水纹特效(非gpu渲染。。。)

    博客分类:
  • as3
 
阅读更多

一直在寻找的as水纹特效(非gpu渲染。。。)

 

 

/*
Copyright (c) 2008 NascomASLib Contributors.  See:
    http://code.google.com/p/nascomaslib

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package
{
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.display.BlendMode;
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.filters.ConvolutionFilter;
    import flash.filters.DisplacementMapFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    
    /** 
     * 
     * The Rippler class creates an effect of rippling water on a source DisplayObject.
     * 
     * @example The following code takes a DisplayObject on the stage and adds a ripple to it, assuming source is a DisplayObject already on the stage.
     * 
     *     <listing version="3.0">
     *         import be.nascom.flash.graphics.Rippler;
     *         
     *         // create a Rippler instance to impact source, with a strength of 60 and a scale of 6.
     *         // The source can be any DisplayObject on the stage, such as a Bitmap or MovieClip object.
     *         var rippler : Rippler = new Rippler(source, 60, 6);
     * 
     *         // create a ripple with size 20 and alpha 1 with origin on position (200, 50)
     *         rippler.drawRipple(100, 50, 20, 1);
     * </listing>
     * 
     * @author David Lenaerts
     * @mail david.lenaerts@nascom.be
     * 
      */
    public class Rippler
    {
        // The DisplayObject which the ripples will affect.
        private var _source : DisplayObject;
        
        // Two buffers on which the ripple displacement image will be created, and swapped.
        // Depending on the scale parameter, this will be smaller than the source
        private var _buffer1 : BitmapData;
        private var _buffer2 : BitmapData;
        
        // The final bitmapdata containing the upscaled ripple image, to match the source DisplayObject
        private var _defData : BitmapData;
        
        // Rectangle and Point objects created once and reused for performance
        private var _fullRect : Rectangle; 			// A buffer-sized Rectangle used to apply filters to the buffer
        private var _drawRect : Rectangle;			// A Rectangle used when drawing a ripple
        private var _origin : Point = new Point();	// A Point object to (0, 0) used for the DisplacementMapFilter as well as for filters on the buffer
        
        // The DisplacementMapFilter applied to the source DisplayObject
        private var _filter : DisplacementMapFilter;
        // A filter causing the ripples to grow
        private var _expandFilter : ConvolutionFilter;
        
        // Creates a colour offset to 0x7f7f7f so there is no image offset due to the DisplacementMapFilter
        private var _colourTransform : ColorTransform;
        
        // Used to scale up the buffer to the final source DisplayObject's scale
        private var _matrix : Matrix;
        
        // We only need 1/scale, so we keep it here
        private var _scaleInv : Number;
        
        /**
         * Creates a Rippler instance.
         * 
         * @param source The DisplayObject which the ripples will affect.
         * @param strength The strength of the ripple displacements.
         * @param scale The size of the ripples. In reality, the scale defines the size of the ripple displacement map (map.width = source.width/scale). Higher values are therefor also potentially faster.
         * 
         */
        public function Rippler(source : DisplayObject, strength : Number, scale : Number = 2)
        {
            var correctedScaleX : Number;
            var correctedScaleY : Number;
            
            _source = source;
            _scaleInv = 1/scale;
            
            // create the (downscaled) buffers and final (upscaled) image data, sizes depend on scale
            _buffer1 = new BitmapData(source.width*_scaleInv, source.height*_scaleInv, false, 0x000000);
            _buffer2 = new BitmapData(_buffer1.width, _buffer1.height, false, 0x000000);
            _defData = new BitmapData(source.width, source.height);
            
            // Recalculate scale between the buffers and the final upscaled image to prevent roundoff errors.
            correctedScaleX = _defData.width/_buffer1.width;
            correctedScaleY = _defData.height/_buffer1.height;
            
            // Create reusable objects
            _fullRect = new Rectangle(0, 0, _buffer1.width, _buffer1.height);
            _drawRect = new Rectangle();
            
            // Create the DisplacementMapFilter and assign it to the source
            _filter = new DisplacementMapFilter(_buffer1, _origin, BitmapDataChannel.BLUE, BitmapDataChannel.BLUE, strength, strength, "wrap");
            _source.filters = [_filter];
            
            // Create a frame-based loop to update the ripples
            _source.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
            
            // Create the filter that causes the ripples to grow.
            // Depending on the colour of its neighbours, the pixel will be turned white
            _expandFilter = new ConvolutionFilter(3, 3, [0.5, 1, 0.5, 1, 0, 1, 0.5, 1, 0.5], 3);
            
            // Create the colour transformation based on 
            _colourTransform = new ColorTransform(1, 1, 1, 1, 127, 127, 127);
            
            // Create the Matrix object
            _matrix = new Matrix(correctedScaleX, 0, 0, correctedScaleY);
            
        }
        
        /**
         * Initiates a ripple at a position of the source DisplayObject.
         * 
         * @param x The horizontal coordinate of the ripple origin.
         * @param y The vertical coordinate of the ripple origin.
         * @param size The size of the ripple diameter on first impact.
         * @param alpha The alpha value of the ripple on first impact.
         */
        public function drawRipple(x : int, y : int, size : int, alpha : Number) : void
        {
        	var half : int = size >> 1;		// We need half the size of the ripple
            var intensity : int = (alpha*0xff & 0xff)*alpha;	// The colour which will be drawn in the currently active buffer
            
            // calculate and draw the rectangle, having (x, y) in its centre
            _drawRect.x = (-half+x)*_scaleInv;	
            _drawRect.y = (-half+y)*_scaleInv;
            _drawRect.width = _drawRect.height = size*_scaleInv;
            _buffer1.fillRect(_drawRect, intensity);
        }
        
       	/**
       	 * Returns the actual ripple image.
       	 */
        public function getRippleImage() : BitmapData
        {
        	return _defData;
        }
        
        /**
         * Removes all memory occupied by this instance. This method must be called before discarding an instance.
         */
        public function destroy() : void
        {
            _source.removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
            _buffer1.dispose();
            _buffer2.dispose();
            _defData.dispose();
        }
        
        // the actual loop where the ripples are animated
        private function handleEnterFrame(event : Event) : void
        {
        	// a temporary clone of buffer 2
            var temp : BitmapData = _buffer2.clone();
            // buffer2 will contain an expanded version of buffer1
            _buffer2.applyFilter(_buffer1, _fullRect, _origin, _expandFilter);
            // by substracting buffer2's old image, buffer2 will now be a ring
            _buffer2.draw(temp, null, null, BlendMode.SUBTRACT, null, false);
            // scale up and draw to the final displacement map, and apply it to the filter
            _defData.draw(_buffer2, _matrix, _colourTransform, null, null, true);
            _filter.mapBitmap = _defData;
            _source.filters = [_filter];
            temp.dispose();
            // switch buffers 1 and 2
            switchBuffers();
        }
        
        // switch buffer 1 and 2, so that 
        private function switchBuffers() : void
        {
            var temp : BitmapData;
            temp = _buffer1;
            _buffer1 = _buffer2;
            _buffer2 = temp;
        }
    }
}
 

 

分享到:
评论
1 楼 mythl 2013-01-06  
那有没有找到GPU渲染的 as 水纹呢?

相关推荐

    水纹特效.e

    水纹特效.e

    水纹特效 文章:Cocos2dx特效 水纹特效1

    本文将深入探讨如何在Cocos2dx中实现水纹特效,这对于创建生动、逼真的游戏环境至关重要。水纹特效能够增加游戏的视觉吸引力,使玩家沉浸在更丰富的游戏体验中。 首先,我们需要了解水纹特效的基本原理。这种效果...

    C#实现的水纹特效代码封装

    在本文中,我们将深入探讨如何使用C#编程语言和GDI+库来实现水纹特效。C#是一种广泛应用于Windows桌面应用开发的语言,而GDI+是.NET Framework的一部分,提供了丰富的图形绘制功能,包括创建复杂的视觉效果。 首先...

    VC水纹特效.rar

    5. **OpenGL或DirectX**:作为图形库,它们提供了底层的图形渲染功能,用于在屏幕上绘制和更新水纹特效。VC开发者可能会使用这些库来实现水纹的实时渲染。 6. **Shader语言**:如GLSL(OpenGL着色器语言)或HLSL...

    易语言查表法水纹特效

    易语言查表法水纹特效是一种在编程中实现动态视觉效果的技术,主要应用于游戏开发、图形界面设计等领域。本文将详细解析这一技术的核心概念、实现原理以及如何在易语言中运用。 首先,我们要理解什么是“查表法”。...

    cocos2d特效研究 水纹特效11

    【cocos2d特效研究:水纹特效11】这篇内容主要探讨了如何在Cocos2dx游戏开发中实现水纹特效,以增强游戏的视觉吸引力和玩家的沉浸感。文章指出,游戏中的细节处理对于提升用户体验至关重要,比如在射击游戏中,当...

    Cocos2dx特效 水纹特效1

    在Cocos2d-x中,特效是提升游戏视觉体验的关键元素之一,水纹特效就是其中一种常见的动态效果,它能为游戏场景增添生动的水面波动感。本文将深入探讨Cocos2dx中的水纹特效及其实现方法。 首先,我们要理解水纹特效...

    易语言查表法水纹特效源码.rar

    在计算机图形学中,水纹特效是一种常见的视觉表现手法,常用于游戏、界面设计等领域,为用户带来更生动的视觉体验。查表法是实现这种特效的一种高效手段。 易语言是一种中文编程语言,旨在降低编程的门槛,让更多的...

    WPF 实现水纹特效

    在Windows Presentation Foundation (WPF) 中,实现水纹特效是一种增强用户界面动态视觉效果的方法。这种特效通常在鼠标滑过控件时触发,为用户提供一种交互式的体验,仿佛水面随着鼠标移动而产生波动。在本篇文章中...

    易语言查表法水纹特效源码.zip易语言项目例子源码下载

    在本压缩包“易语言查表法水纹特效源码.zip”中,包含了一个易语言项目例子,重点展示了如何使用查表法实现水纹特效。 查表法是程序设计中常用的一种优化技术,它通过预先计算好一系列值并存储在一个表格中,在运行...

    水纹特效模块

    水纹特效模块 优化帧数, 释放内存

    易语言查表法水纹特效源码

    在IT领域,编程技巧和特效实现是至关重要的技术之一,特别是在游戏开发、图形界面设计以及动画制作中。这里我们关注的是一个使用易语言实现的查表法水纹特效源码。易语言是一款中国本土开发的可视化编程环境,以其...

    VC++ 鼠标跟随 烟花、水纹特效

    VC++ 鼠标跟随 烟花、水纹特效,使用了OnTimer,OnPaint函数,鼠标跟踪,在鼠标经过处显示特效。 适用于初学者熟悉OnTimer,OnMouseMoveOn及OnPaint函数。

    HTML5 SVG鼠标点击按钮水纹特效代码4套提供下载.zip

    首先,SVG的水纹特效通常通过定义路径(`&lt;path&gt;`)或者圆形(`&lt;circle&gt;`)等形状,并结合CSS3动画来实现。当用户点击按钮时,通过JavaScript监听点击事件,触发相应的CSS类变化,进而改变SVG图形的属性,如半径、...

    e语言-易语言查表法水纹特效

    在本例程中,“e语言-易语言查表法水纹特效”是一个使用易语言编写的程序,它展示了如何通过查表法实现水波纹的视觉效果。 查表法,又称查找表技术,是一种在编程中常用的数据处理方法。它通过预先计算和存储一组...

    C# 实现水纹效果.zip

    在C#编程中,实现水纹效果是一种常见的图形渲染技术,通常用于增加用户界面的视觉吸引力。本项目“C# 实现水纹效果”提供了一种方法来在Windows Forms应用程序中创建动态的、类似水面波动的图形效果。下面将详细阐述...

    WPF加载和分页显示的图片列表,可多选,带水纹特效

    截图效果地址:https://blog.csdn.net/XING861000/article/details/80549069 WPF加载和分页显示的图片列表,可多选,带水纹特效,效果非常好,编译前可以自己修改默认加载图片路径

    NB水波特效|NB水波特效支持库

    NB水波特效是一种在软件开发中用于创建动态视觉效果的技术,尤其在游戏开发、界面设计等领域广泛应用。这个特效支持库提供了实现水波效果的工具和方法,使得开发者能够轻松地在他们的项目中集成这种生动的视觉元素。...

Global site tag (gtag.js) - Google Analytics