`
txf2004
  • 浏览: 7206269 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

【ZZ】使用Displacement Map Filter创建静态的扭曲效果

阅读更多
Create a Static Distortion Effect Using the Displacement Map Filter

Create a Static Distortion Effect Using the Displacement Map Filter


I’m a freelance designer specializing in motion graphics and interactive design. When I’m not working on client projects I make games over at The Pencil Farm .

Final Result Preview

Let's take a look at the final result we will be working towards:

Step 1: About Displacement Mapping

A displacement map works by using the color values from one image to alter the position of pixels in another image. This effect is often used to make a flat graphic 'wrap' around a dimensional image. We're going to use it here to distort a button so it looks like it's receiving static interference.

displacement example

You can read more about displacement mapping here .

Step 2: Set up Your Flash File

Create a new Flash file (ActionScript 3).Your movie settings will vary depending on your game. For this demo I'm setting up my movie as 500x300, black background, and 30 fps.

Flash file setup

Step 3: Create a Simple Button

Create a new Button symbol on the stage (Insert > New Symbol). Design the 4 states for your button. The exact design should be something that matches your game. Something glowy and semi-transparent works well with this effect. I used a font called Genetrix Square for mine, but you should use something that matches the look of your game.

Give your button an instance name of 'button1'.

button properties

Step 4: Test

If you save and test your movie (Control > Test Movie) now you should see your button on the stage responding to the mouse with the rollover states you designed. Like this:

Step 5: Create the JitteryButton Class

We need to add custom functionality to our button. We'll accomplish this by creating a new custom class and putting our simple button inside it.

Create a new ActionScript file named 'JitteryButton.as'. Save this file in the same directory as your main Flash file. Add this code to create the wrapper for our button:

  1. package {
  2. import flash.display.Sprite;
  3. import flash.display.SimpleButton;
  4. public class JitteryButton extends Sprite{
  5. private var myButton:SimpleButton; //holdsthereferencetooursimplebutton
  6. //CLASSCONSTRUCTOR
  7. public function JitteryButton(button:SimpleButton){
  8. myButton=button;//thebuttononthestagegetspassedin
  9. }
  10. }

All this code does so far is accept the simple button and store a reference to it. We'll add more functionality later.

Step 6: Create the Game Class

Create a new ActionScript file named 'Game.as'. This will be the document class for our movie. Save it in the same directory as the main Flash file.This code will add our custom button wrapper around the button on the stage:

  1. package {
  2. import flash.display.MovieClip;
  3. public class Game extends MovieClip{
  4. private var startButton:JitteryButton;
  5. //CLASSCONSTRUCTOR
  6. public function Game(){
  7. //createthejitterybuttonfromthesimplebuttononthestage
  8. startButton=new JitteryButton(button1);
  9. //addthenewbuttontothestage
  10. addChild(startButton);
  11. }
  12. }
  13. }

This code creates a new instance of our custom JitteryButton class and passes it the button on the stage ('button1').

Of course your document class will end up looking much different since it will have the code for your game in it. Here we're just concerned with the code for our button.

Back inside your Flash file set the document class to 'Game'. Remember, you don't include the file extension here.

set the document class

Step 7: Test Again

If you save and test your movie again at this point you should see exactly the same thing as when we tested in Step 4. The only difference is that now our code is set up to be able to add our custom behavior.

Step 8: Create the Displacement Map Image

We'll now create the image of the static pattern that we'll use to distort our button graphic.

Open Photoshop and create a new image filled with neutral grey (#808080). The image should be slightly wider than your button and about 3 or 4 times as high. My button is 277x56, and my image is 310x220.

We're starting with a neutral grey because that won't effect our image.

create a new image

Step 9: Add Noise

We'll now add a little bit of noise to our image. This won't be very noticeable in our static effect, but it gives it a bit of extra shimmer. You can skip this step if you like.

Duplicate the background layer and name the new layer 'Noise'. You should now have 2 layers filled with neutral grey. Select the new Noise layer and choose Filter > Noise > Add Noise. Set Amount to 120% and Distribution to Uniform. Check Monochromatic.

Hit OK.

Set the 'Noise' layer to 10% opacity.

add noise to the Noise layer

Step 10: Add Lines

Create a new layer called 'Lines'. Now use a 1px pencil brush to add some horizontal black and grey lines to the image.

Remember how these lines will effect our image: anything darker than neutral will shift our image in one direction and anything lighter will shift it in the other.

draw horizontal lines

Step 11: Save the Displacement Map Image

Choose File > Save for Web & Devices and save your image as an 8 color gif named 'staticMap.gif'.

save for web

Step 12:

Back in Flash, import the 'staticMap.gif' to your library (File > Import > Import to Library...). Open the linkage properties, check Export for ActionScript , and set the Class name to 'StaticMap'.

import the image

We can now reference this image in our code by using the StaticMap class name.

Step 13: Create the Displacement Map Filter

Add this function to your JitteryButton class:

  1. //createthedisplacementmapfilter
  2. private function createDMFilter():DisplacementMapFilter{
  3. var mapBitmap:BitmapData= new StaticMap(0,0); //usethebitmapdatafromourStaticMapimage
  4. var mapPoint:Point= new Point(0,0); //positionoftheStaticMapimageinrelationtoourbutton
  5. var channels:uint=BitmapDataChannel.RED; //whichcolortousefordisplacement
  6. var componentX:uint=channels;
  7. var componentY:uint=channels;
  8. var scaleX:Number=5; //theamountofhorizontalshift
  9. var scaleY:Number=1; //theamountofverticalshift
  10. var mode:String=DisplacementMapFilterMode.COLOR;
  11. var color:uint=0;
  12. var alpha:Number=0;
  13. return new DisplacementMapFilter(
  14. mapBitmap,
  15. mapPoint,
  16. componentX,
  17. componentY,
  18. scaleX,
  19. scaleY,
  20. mode,
  21. color,
  22. alpha);
  23. }

This function simply creates the Displacement Map Filter using the BitmapData from our StaticMap image. This doesn't need to be in it's own function, I'm just doing it for clarity.

In order for it to work we'll need to import these classes at the top of our JitteryButton class:

  1. import flash.display.BitmapData;
  2. import flash.display.BitmapDataChannel;
  3. import flash.filters.DisplacementMapFilter;
  4. import flash.filters.DisplacementMapFilterMode;
  5. import flash.geom.Point;

(You can learn more about the DisplacementMapFilter class in the AS3 documentation )

Step 14: Apply the Filter

We'll now create a variable to hold the filter. We apply it to the button by setting the button's 'filters' property to an array that contains our filter.

Here's the JitteryButton class so far (lines 18 and 25 are new):

  1. package {
  2. import flash.display.Sprite;
  3. import flash.display.SimpleButton;
  4. import flash.display.BitmapData;
  5. import flash.display.BitmapDataChannel;
  6. import flash.filters.DisplacementMapFilter;
  7. import flash.filters.DisplacementMapFilterMode;
  8. import flash.geom.Point;
  9. import caurina.transitions.Tweener;
  10. public class JitteryButton extends Sprite{
  11. private var myButton:SimpleButton;
  12. //createavariabletoholdthedisplacementmapfilter
  13. private var dmFilter:DisplacementMapFilter=createDMFilter();
  14. //CLASSCONSTRUCTOR
  15. public function JitteryButton(button:SimpleButton){
  16. myButton=button;
  17. //applythefiltertothebutton
  18. myButton.filters=new Array(dmFilter);
  19. }
  20. //createthedisplacementmapfilter
  21. private function createDMFilter():DisplacementMapFilter{
  22. var mapBitmap:BitmapData= new StaticMap(0,0); //usethebitmapdatafromourStaticMapimage
  23. var mapPoint:Point= new Point(0,0); //thisisthepositionoftheStaticMapimageinrelationtoourbutton
  24. var channels:uint=BitmapDataChannel.RED; //whichcolortousefordisplacement
  25. var componentX:uint=channels;
  26. var componentY:uint=channels;
  27. var scaleX:Number=5; //theamountofhorizontalshift
  28. var scaleY:Number=1; //theamountofverticalshift
  29. var mode:String=DisplacementMapFilterMode.COLOR;
  30. var color:uint=0;
  31. var alpha:Number=0;
  32. return new DisplacementMapFilter(
  33. mapBitmap,
  34. mapPoint,
  35. componentX,
  36. componentY,
  37. scaleX,
  38. scaleY,
  39. mode,
  40. color,
  41. alpha);
  42. }
  43. }
  44. }

Step 15: Test Again

If we save and test the file now we can see the displacement map filter being applied to our button:

You can see how the horizontal lines we drew in the StaticMap are shifting the pixels in our button left and right. The amount of the shift is dependent on the darkness of the lines in the image and the scaleX setting in our Displacement Map Filter.

Unfortunately, the static isn't animating so it looks pretty lame. Let's fix that now...

Step 16: Add the randRange Function

This is a simple function that returns a random integer within a specified range:

  1. //returnsarandomnumberbetweenthespecifiedrange(inclusive)
  2. private function randRange(min: int ,max: int ): int {
  3. var randomNum: int =Math.floor(Math.random()*(max-min+1))+min;
  4. return randomNum;
  5. }

I find it makes it a little easier to generate random values. We'll be randomizing a few different values for our static effect so it will come in handy.

Add it to your JitteryButton class.

Step 17: Animate the Displacement Map Filter

There are a couple of ways we can animate the static effect. The first will be to alter the amount of horizontal shift applied to our button. This is done through the scaleX property of the DisplacementMapFilter.

We can also animate the position of the StaticMap image in relation to our button. This will ensure that the same areas of the button aren't always getting shifted.

To animate the effect we'll add a function called 'displayStatic' that gets called every frame to update these two properties of the filter. Add this function to your JitteryButton class:

  1. //calledonENTER_FRAME
  2. private function displayStatic(e:Event): void {
  3. dmFilter.scaleX=randRange(fuzzMin,fuzzMax);
  4. dmFilter.mapPoint=new Point(0,randRange(0,-160));
  5. myButton.filters=new Array(dmFilter);
  6. }

The first line of this function randomizes the amount of horizontal shifting to a value between the variables fuzzMin and fuzzMax. Add these two variables to your JitteryButton class:

  1. private var fuzzMin: int =0;
  2. private var fuzzMax: int =2;

The second line of the displayStatic function randomizes the Y position of the StaticMap in relation to our button. We already told the filter to use our StaticMap image so we just need to update the position.

The third line just re-applies the filter to our button.

The last thing we need to do to get this animating is to add the ENTER_FRAME event listener. Add this line to the JitteryButton constructor function:

  1. //startdisplayingthestaticeffect
  2. addEventListener(Event.ENTER_FRAME,displayStatic);

And don't forget to import the Event class at the top of the JitteryButton file:

  1. import flash.events.Event;

Step 18: Test Again

If you save and test the movie now you'll see the effect is making our button shimmer and jump:

That's pretty cool, but we want the effect to react to the mouse as well. Onward...

Step 19: Adjust the Intensity of the Effect

We'll now add two functions to adjust the intensity of the jitter effect. We'll call the effect we currently have Low intensity so we'll add a setting for Medium and High intensity. Add these functions to your JitteryButton class:

  1. //increasetheintensityofthestatictoMEDIUM
  2. private function setStaticMedium(e:MouseEvent= null ): void {
  3. fuzzMin=2;
  4. fuzzMax=6;
  5. staticLength=randRange(8,12);
  6. }
  7. //increasetheintensityofthestatictoHIGH
  8. private function setStaticHigh(e:MouseEvent= null ): void {
  9. fuzzMin=12;
  10. fuzzMax=25;
  11. staticLength=12;
  12. }

You can see that we're adjusting the intensity by setting the values of the fuzzMin and fuzzMax variables. This way our displayStatic function will use these new values when it sets the horizontal shift of the filter.

We also added a variable called staticLength. We'll use this to set how long the more intense effect should last (the number of frames) before returning to low intensity. Add this variable to your JitteryButton class and modify your displayStatic function like so:

  1. private var staticLength: int ;
  2. //calledonENTER_FRAME
  3. private function displayStatic(e:Event): void {
  4. dmFilter.scaleX=randRange(fuzzMin,fuzzMax);
  5. dmFilter.mapPoint=new Point(0,randRange(0,-160));
  6. myButton.filters=new Array(dmFilter);
  7. staticLength--;
  8. if (staticLength<=0){
  9. fuzzMin=0;
  10. fuzzMax=2;
  11. }
  12. }

This new code decrements the staticLength variable and resets fuzzMin and fuzzMax to the low intensity values once the value of staticLength reaches zero.

Step 20: Set up the Button Rollover Handlers

To make our button react to the mouse we need to add two mouse event listeners and an event handler function for each.

Add the mouse listeners in the constructor function of your JitteryButton class:

  1. //addtherollovereventlistenerstothebutton
  2. myButton.addEventListener(MouseEvent.ROLL_OVER,onButtonRollOver);
  3. myButton.addEventListener(MouseEvent.ROLL_OUT,onButtonRollOut);

Now create the two event handlers that are referenced in those two new lines. These also go in the JitteryButton class:

  1. //calledonbuttonROLL_OVER
  2. private function onButtonRollOver(e:MouseEvent): void {
  3. setStaticHigh();
  4. }
  5. //calledonbuttonROLL_OUT
  6. private function onButtonRollOut(e:MouseEvent): void {
  7. setStaticMedium();
  8. }

To make this all work we'll have to import the MouseEvent class at the top of our JitteryButton file:

  1. import flash.events.MouseEvent;

Now when our button detects a ROLL_OVER event it will call the event handler which in turn calls our setStaticHigh function. This function increases the values of fuzzMin and fuzzMax (used for setting the horizontal shift) for the duration specified by the staticLength variable.

Step 21: Add the Scale Effect

We could stop here. Our effect is animating nicely and reacts to the mouse rollovers. I still feel like something is missing here though. Let's add a little scaling effect.

You'll need to download the Tweener Library for this step if you don't already have it. Place the 'caurina' folder in your project directory and import the Tweener classes at the top of your JitteryButton file:

  1. import caurina.transitions.Tweener;

Tweener allows us to add some nice scaling effects with only a couple lines of code. We can add one line to each of our rollover event handlers:

  1. //calledonbuttonROLL_OVER
  2. private function onButtonRollOver(e:MouseEvent): void {
  3. Tweener.addTween(myButton,{scaleX:1.1,time:.5,transition:"easeOutElastic" });
  4. setStaticHigh();
  5. }
  6. //calledonbuttonROLL_OOUT
  7. private function onButtonRollOut(e:MouseEvent): void {
  8. Tweener.addTween(myButton,{scaleX:1,time:.5,transition:"easeOutElastic" });
  9. setStaticMedium();
  10. }

Here we're adding an animation to the rollover handler that scales the button's scaleX property to 110% over .5 seconds. We're using an elastic transition type to give it that bouncy feel. In the rollout handler we're doing the same thing in reverse, scaling it back to 100%.

You can learn more about how to use Tweener in the Tweener documentation .

Step 22: Add Sound

The last thing we need to do make this effect complete is to add some sound. I made my sound effect in Garage Band. You can make your own or try to find one online.

Once you have one you like, import it into your library and set the linkage to export as 'StaticSound'.

To add it to our JitteryButton we first need to import the Sound class:

  1. import flash.media.Sound;

Then we'll initialize it (add this line just before the constructor function):

  1. private var staticSound:Sound= new StaticSound();

Inside the rollover handler we'll tell the sound to play:

  1. //calledonbuttonROLL_OVER
  2. private function onButtonRollOver(e:MouseEvent): void {
  3. Tweener.addTween(myButton,{scaleX:1.1,time:.5,transition:"easeOutElastic" });
  4. setStaticHigh();
  5. staticSound.play();
  6. }

Now we're good to go. Test your movie and everything should be working. If your button or sound isn't working right check the source files to see my completed JitteryButton class.

Step 23: Add More Buttons

The cool thing about building this effect as a separate class that wraps our button is that we can easily reuse it on other buttons.

If you want to add more buttons to your game menu just create a new button and add it to the stage. Give it the instance name 'button2'. Then inside your document class (the 'Game.as' file) create a new JitteryButton and pass it the new button. Here's how that might look:

  1. package {
  2. import flash.display.MovieClip;
  3. public class Game extends MovieClip{
  4. private var startButton:JitteryButton;
  5. private var menuButton:JitteryButton;
  6. //CLASSCONSTRUCTOR
  7. public function Game(){
  8. //createthejitterybuttonsfromthesimplebuttonsonthestage
  9. startButton=new JitteryButton(button1);
  10. addChild(startButton);
  11. //addinganewbuttoniseasy!
  12. menuButton=new JitteryButton(button2);
  13. addChild(menuButton);
  14. }
  15. }
  16. }

Conclusion

You will almost certainly need to change this code a bit to get it to fit into the structure of your game. Hopefully this tutorial will give you a good starting point though.

If you want to change the look of this effect you can try using different types of images for your StaticMap graphic, and adjusting the values for fuzzMin and fuzzMax.

This is my first tutorial so let me know if there's anything I can do better next time. Thanks for reading!


Related Posts

分享到:
评论

相关推荐

    基于opengl和cg实现的displacement map

    本篇文章将详细讲解基于OpenGL和Cg实现的Displacement Map(置换贴图)技术,以及如何利用这些工具来创建出逼真的三维视觉效果。 首先,OpenGL是一个跨语言、跨平台的编程接口,用于生成二维和三维图形。它提供了...

    three.js-displacement-map:使用three.js的置换贴图的基本示例

    三.js-位移图 使用的置换贴图的基本。 演示在这里运行:

    threejs-displacement-map-scale:Three.js位移图示例

    标题“threejs-displacement-map-scale”暗示了我们讨论的核心内容,即如何在Three.js中使用位移图并调整其缩放比例。位移图是一种特殊类型的纹理,它可以改变模型表面的形状,创造出更复杂的几何细节,而无需增加...

    Normal/Displacement Map Generator-开源

    Normal/Displacement Map Generator 是一款开源软件,专为创建高质量的法线贴图和置换贴图而设计,为3D模型添加额外的细节和真实感,尤其适用于游戏应用程序和Blender 3D等专业3D编辑器。 法线贴图是一种特殊纹理,...

    AE常用特效自学必备教程集合.docx

    6. 创建Displacement Map:创建新的Composition,添加FRACTAL NOISE效果,调整对比度、亮度和offset参数以生成动态扭曲。将此新Composition作为Displacement Map效果的Map贴图。 7. 使用Colorama效果:添加Image ...

    AE层的Bulge效果.pdf

    在Adobe After Effects(简称AE)这款强大的视觉特效和动态图形软件中,Bulge效果是一种常用的特效,它可以模拟物体透过气泡或放大镜所呈现的凸凹或扭曲效果,为你的视频创作增添丰富的视觉表现力。这篇学习资料将...

    Displacement_filter.zip_位移追踪_检测位移_相机运动_高速相机 MATLAB

    最后,"Displacement_filter.m"这个文件很可能包含了整个位移追踪和滤波处理的MATLAB代码。这个脚本可能涵盖了从读取图像、特征检测、位移计算、滤波(例如使用低通滤波器去除噪声,提高追踪稳定性)到结果可视化等...

    AE常用特效自学必备教程集合.doc

    在原始文字层上添加`Displacement Map`效果,选择之前创建的`Fractal Noise P`层作为Map贴图,使用`luminance`作为加载通道。调整`Max Horizontal and Vertical`数值以获得不同的火焰形态。 **6. 调整色彩** 使用`...

    后期剪辑与特效之广告宣传片制作.pptx

    6. 应用特效,如Fill、Grow和Displacement Map,Fill用于改变图层的颜色,Grow则可以让元素逐渐扩大,而Displacement Map则可以利用一个图层的灰度信息来变形另一图层,实现复杂的表面纹理效果。 在实际的广告宣传...

    displacement.rar_matlab图像分析_sleptdb9_图像相关性_相关分析_相关性分析

    通过“displacement.rar”这个压缩包,我们可以看到包含了一个名为“displacement.m”的MATLAB脚本,该脚本很可能是用来执行图像相关性分析的。 MATLAB是一种广泛使用的编程环境,尤其在科学计算和工程应用中,包括...

    制作火焰特效文字_AE教程.pdf

    * 选择最初的笔画层,添加 Displacement Map 效果,并选择 FRACTAL NOISE COMP 作为 Map 帖图。 * 设置 luminance 作为载入的通道,并尝试改变 Max Horizontal and Vertical 的数值来尝试不同效果。 第五部分:添加...

    AE插件-专业置换贴图映射高级版 Displacer Pro v1.5 Win/Mac+使用教程

    The humble AE displacement map with more features + juiced up on the GPU. And it’s free. 插件特征: 平移,旋转和缩放位移 您所有喜欢的模式:色相,饱和度,亮度,红色等 强大的地图调整功能:伽玛,...

    aa.rar_displacement_位移响应_振型_振型分解_运动响应

    "aa.rar_displacement_位移响应_振型_振型分解_运动响应"这个压缩包文件内容似乎涉及了这一主题,尤其是如何利用振型分解法来计算结构的位移响应。以下是关于这个主题的详细解释。 位移响应是指在地震作用下,结构...

    Displacement_Mapping_Forsyth_Doggett

    在计算机图形学领域中,置换映射(Displacement Mapping)是一种用于为物体表面创建高度细节的技术。它通过改变表面的几何形状来创建凹凸效果,以此达到比传统的贴图技术更真实的效果。Tom Forsyth 和 Michael ...

    基于canvas使用three.js制作的立体水波纹效果

    在本文中,我们将深入探讨如何使用three.js库创建一个基于Canvas的立体水波纹效果。three.js是一个流行的JavaScript 3D库,它使得在Web浏览器中进行三维图形编程变得简单。我们将讨论涉及到的关键技术和算法,以及...

    大量AE插件的名称与用途的说明

    - Boris Displacement Map:根据指定的贴图进行位移。 - Boris Fast Flipper:快速翻转图像或层。 - Boris Polar Displacement:使用极坐标系统进行位移变换。 - Boris Ripple:创建波纹效果。 - Boris Vector ...

    一些3dmax贴图 木纹材质

    木纹贴图通常包括几种类型:颜色贴图(Color Map)定义了木材的颜色和斑纹,法线贴图(Bump Map或Normal Map)用于模拟表面的凹凸感,光泽贴图(Gloss Map)控制反射和高光,以及位移贴图(Displacement Map)可以...

    AE-特效-裂-

    例如,可以使用"Fractal Noise"(分形噪声)或者"Displacement Map"(位移贴图)来模拟裂纹的随机性和不规则性。通过调整这些特效的参数,如强度、频率、缩放等,可以定制裂纹的形状和外观。 3. **关键帧动画**:...

    unity贴图效果资源

    以及位移贴图(Displacement Map)用于根据贴图的灰度值改变模型表面的高度,从而增加细节。 在“花草树木模型”中,这些贴图效果的运用可以使植物看起来更加生动和真实。例如,颜色贴图可能包含各种绿色调,以反映...

Global site tag (gtag.js) - Google Analytics