понедельник, 5 ноября 2012 г.

2D Light Demo


Hi. Not a complete tutorial today - just a demo of what I've been working on. Click anywhere for displacement effect.

                 
Here you can tweak sliders to add multiple lights. There're many interesting things I met when worked on stuff.

1. The animation you see was created in Flash IDE. And I have to find a way to show this animation with stage3D not changing source at all. The most challenging part was to get transformations of "bones". Translation and scale was not a problem, but skew transform is something tough. Adobe help gives a wrong description of it's own skew in Matrix class. Maybe next tutorial will be about skew transform.

2. For light effect I used simplified normal mapping from my previous tutorial. Normal map was created with crazybump programm. In demo I normalize vector to light only in vertex shader. Althought it's not correct it's visually the same as normalizing vector in pixel shader. Tangent and bitangent vectors are constant for every triangle and in vertex shader they multiplied by full transform of the triangle, i.e. scale and skew (but without translation). LightX light is specular light. As can be seen there're no difference betwee normal map and specular map, but the last uses more agal instructions.

3. I used following algorithm for demo:

a) set vertex data for background.
b) set background texture.
c) render background to texture.
d) collect vertex data for left animation (yes it's happens every frame).
e) set vertex data for left animation.
f) set texture and normal map for left animation.
g) render left animation with normal mapping shader to same texture as background was rendered.
h) repeat steps d-g for right animation (althought in this particular demo they same).
i) set vertex data for displacement effect.
j) set map texture for displacement effect.
k) render displacement effect in separate texture.
l) finally render texture with background and animations with displacement effect to back buffer.

A lot of steps. The most sad part that I need to rebuild vertex buffers for animations every frame and send it to GPU. Changing textures are also very bad. On my good desktop computers and "middle age" notebook I got pretty good performance - 60/60 fps. But on older notebooks the situation is very, VERY bad (. Passing textures and vertex buffers are EXTREMELLY slow. For example, sending spritesheet texture and normal map takes 15 ms! In release player. With error checking setting to false. It's awful.

That's all. If you have a question, feel free to ask.

2 комментария: