Switch texture layers with fade

Blitz3D Forums/Blitz3D Programming/Switch texture layers with fade

RustyKristi(Posted 2016) [#1]
I wonder what's the best possible technique to switch diffuse texture layers with fade. Sort of changing or selecting a texture pattern.

How it could be done without any performance effect? My last fade based timer eats up performance and it's kind of disappointing. :/


Bobysait(Posted 2016) [#2]
I don't think it's possible without performance loss.

You can use a grey mask in blend Add (whyte part of the mask will mask the texture, while dark pixels will let the first texture being visible) to mask a part of the first texture
Then add the second texture on top (in blend mode 2)
But, it will only mask the first texture, the second texture will be visible everywhere

To get a real mask method, you have to use a second surface on top of the first, then, you'll need to use wether an alpha texture or an entityblend mode or vertexalpha to mask parts of the second surface.


Dan(Posted 2016) [#3]
Without performance loss you can either prerender the effect on a imagebuffer, or loading the animation which displays the desired effect.


RustyKristi(Posted 2016) [#4]
To get a real mask method, you have to use a second surface on top of the first, then, you'll need to use wether an alpha texture or an entityblend mode or vertexalpha to mask parts of the second surface.


thjanks bobysait. technically that's my first method, copying the mesh with a different texture and using entity alpha with fade. I was thinking if and how it can be done with texture layers :/

@Dan

Can you give a simple example?


RemiD(Posted 2016) [#5]

technically that's my first method, copying the mesh with a different texture and using entity alpha with fade



@RustyKristi>>that's a good idea, but maybe use copyentity instead of copymesh, especially if it is a rigged skinned mesh.
you may also need to scale down a little the entity with the new texture so that you are sure that it is behind and that there is no z-fighting. Then, once the entity with the old texture is alpha 0, scale the entity with the new texture to the "normal" scale.

I have to try that.


RustyKristi(Posted 2016) [#6]
yes sorry I meant to use copyentity not mesh. weirded out why for a simple fade on ~700 poly model, the fps drops to ~10-20fps. I'm running at stable 60fps with other models on the scene so it goes to 40-50fps when I focus on the model being faded.


RemiD(Posted 2016) [#7]
Maybe try to scale down the mesh with the new texture (or scale up the mesh with the old texture) a little more, because when there are many vertices/triangles at the same position with the same orientation, or when too many triangles intersect, it takes more time to render.


RustyKristi(Posted 2016) [#8]
Same result, got the other mesh scaled down to 0.99 and same fps drop when I look at model :/


RemiD(Posted 2016) [#9]
Are there partially transparent triangles/texels in your texture/mesh ? This may cause slowdowns too.


RustyKristi(Posted 2016) [#10]
apparently it's a solid model RemiD. what could be other causes for this simple routine?


RemiD(Posted 2016) [#11]
i don't know... Maybe somebody who has some experience with directx/opengl will have an idea ?


As mentioned previously, when many vertices/triangles have the same position/orientation or many triangles intersect each other, it causes slowdowns (takes more time to render). So maybe it is that...


RustyKristi(Posted 2016) [#12]
Ok thanks for the added info RemiD, still curious though.


Bobysait(Posted 2016) [#13]
CopyEntity copies the entity and all its hierarchy. So if you had a camera attached to it, it will duplicate it too (and leads to a additive superimposed renderworld)
Maybe check if your entity has no extra hierarchy


RemiD(Posted 2016) [#14]
Maybe post a simplified code example so that we can see if there is a problem and test it on our computers.


RustyKristi(Posted 2016) [#15]
I can't post the whole code but the flow is just typical so here it is.

1 LoadMesh
2 CopyEntity and scale to 0.99, load different texture
3 CreateTimer to slowly fade the copied entity alpha 0 and 1 back and forth.

that's all.

@Bobysait

Nothing attached, just plain 3d mesh under 1k polys/tris. I haven't tried even using a lower poly but will do that next and post the results.


RemiD(Posted 2016) [#16]
Here is my try at it :
;before the mainloop :
Global OutterRenderer = CopyEntity(XMeshWithTextureA)
;EntityColor(OutterRenderer,255,000,255)
PositionEntity(OutterRenderer,0,0,0,True)
ShowEntity(OutterRenderer)
Global OutterA# = 0.99

Global InnerRenderer = CopyEntity(XMeshWithTextureB)
;EntityColor(InnerRenderer,000,255,255)
PositionEntity(InnerRenderer,0,0,0,True) 
HideEntity(InnerRenderer)

;during the mainloop :
  If( KeyHit(44)=1 )
   State=-1
   OutterA = 0.99
   EntityAlpha(OutterRenderer,OutterA)
   EntityAlpha(InnerRenderer,1.0)
   ShowEntity(OutterRenderer)
   ShowEntity(InnerRenderer)
  Else If( KeyHit(45)=1 )
   State=+1
   OutterA = 0.01
   EntityAlpha(OutterRenderer,OutterA)
   EntityAlpha(InnerRenderer,1.0)
   ShowEntity(OutterRenderer)
   ShowEntity(InnerRenderer)
  EndIf

  If( State=-1 ) ;decrease alpha of outterrenderer
   OutterA = OutterA - 0.01
   If( OutterA < 0.01 )
    OutterA = 0.01
   EndIf
   EntityAlpha(OutterRenderer,OutterA)
   If( OutterA = 0.01 )
    HideEntity(OutterRenderer)
    ;EntityAlpha(InnerRenderer,1.0)
   EndIf
  Else If( State=+1 ) ;increase alpha of outterrenderer
   OutterA = OutterA + 0.01
   If( OutterA > 0.99 )
    OutterA = 0.99
   EndIf
   EntityAlpha(OutterRenderer,OutterA)
   If( OutterA = 0.99 )
    HideEntity(InnerRenderer)
    EntityAlpha(OutterRenderer,1.0)
   EndIf
  EndIf


seems to work well and fast enough (60fps with 2 meshes made of 1707vertices 2815triangles)

(during the procedure there are 2 meshes visible and rendered, obviously...)


RustyKristi(Posted 2016) [#17]
Ok great thanks. Now comparing that to my code, I don't hide my inner mesh since the outer one is the only one fading in and out. Could that be the problem here with framerate?

I will test it out later.


RemiD(Posted 2016) [#18]
I don't know but i have noticed that the outter mesh is rendered with graphics artifacts if the entityalpha of the inner mesh is not set. Weird but whatever, with this code it works perfectly.


Bobysait(Posted 2016) [#19]
Unless you're using a very very very old computer, then, no, it's not the problem ( or, you forgot to mention that the 1k polys model is drawn 1000 times ;) )

Even if the model is very poorly modelised and most of the triangles overlap, it's almost not noticeable. (it would really need a very old hardware to be limited to this low count of triangles)


RustyKristi(Posted 2016) [#20]
I will try this now with my exact test model and see how it goes.

Not old (i5/GTX gfx) as I have mentioned I'm getting low framerate on this part. I do have a frame limiter code set to 60fps, i don't know if that counts.


RemiD(Posted 2016) [#21]
@RustyKristi>>have you tested my code ? Does it work well on your computer ?


RustyKristi(Posted 2016) [#22]
Yes almost the same as mine but it should be timer based because the transition time will not be the same across different pc setup. If I use FLIP 0 then it's not a problem.

Maybe there's some frame rate hit coz there are other meshes around and some have alpha on them, idk. thank you for this test code.


RemiD(Posted 2016) [#23]

Maybe there's some frame rate hit coz there are other meshes around and some have alpha on them


yes maybe.