How animate material with procedural textures?

Everything related to 3D programming
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

How animate material with procedural textures?

Post by minimy »

Is it possible create an animated material to use textures created with createTexture(.
Using MaterialAnimation( only allows files type frame001.jpg.

The idea is to animate X frames in a material with procedural textures.

Does anyone know how?
An example for simple minds is appreciated. :mrgreen:

Thanks for help!
If translation=Error: reply="Sorry, Im Spanish": Endif
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: How animate material with procedural textures?

Post by miso »

As long as pseudo code is OK:
I made two method for this some time ago, both retested now and working (6.11 LTS beta 2 X64 Win7 OpenGL. I was lazy to upgrade).
All animation must have it's own coded timer to know when to change frame.

1'st : Using scrollmaterial to change the frame
- requires a single 4x4 tiled texture, 16 looping animation frames. (animated fire for billboards for example)
- Create the material with the single texture, set it's attributes.
- Scale up with scalematerial(material,4,4)
-Use scrollmaterial(material,frame_x.f,frame_y.f,#PB_Material_Fixed) to change the frames
-First Frame x=0.0 y=0.0 // <EDIT -0.125 to all coordinates required as an offset to be precise.>
-Second Frame x=0.25 y=0.0
-And so on. This can be adjusted to other tilings.

2'nd: Retexturing, requires any number of textures as frames. Animation happens on the last layer of the material.
-create your material, if there are more than one layer, add the first frame manually.
-When the animation timer triggers :
-Removemateriallayer(material) ;That only removes the last layer of the material
-Addmateriallayer(material,TextureID(newframe),materialmode,0)

I used this to add a caustic effect to an undersea terrain environment for example.

PS: I don't like pseudocoding, but I have time only for this.
My corresponding old demo is heavy with external media files, and I'm too lazy to extract an only text example.
Built in material animation seems pretty straightforward, though I was not using it. Can't remember why.
Last edited by miso on Sat Nov 23, 2024 8:47 pm, edited 5 times in total.
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: How animate material with procedural textures?

Post by SMaag »

There are some software createt material motion expamples here in the PB Forum.
All from pf shadoko.

chain animation: viewtopic.php?p=620040&hilit=pf+shadoko#p620040


The second one, the belt simulation I could not find. But I have the code

Belt Simulation

Code: Select all

InitEngine3D():InitSprite():InitKeyboard():InitMouse()

ExamineDesktops()
OpenWindow(0, 0,0, DesktopWidth(0)*0.8,DesktopHeight(0)*0.8, "CreateShader - [Esc] quit",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)

Add3DArchive(#PB_Compiler_Home + "examples\3D\Data\Textures", #PB_3DArchive_FileSystem)
Add3DArchive(GetCurrentDirectory(), #PB_3DArchive_FileSystem )
Parse3DScripts()


CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,2,-4):CameraLookAt(0,0,0,0)
CreateLight(0,$ffffff, -100, 100, 100)
AmbientColor($111111*8)
CameraBackColor(0,$444488)

CreateTexture(0,512,512)
StartDrawing(TextureOutput(0))
Box(0,0,512,512,$aaaaaa)
For j=0 To 1
  For i=0 To 17
    x=j*256+i
LineXY(x,16,240+x,256,$ff)
LineXY(x,511-16,240+x,256,$ff)
Next
Next
StopDrawing()
CreateMaterial(0,TextureID(0))
MaterialShininess(0,64,$ffffff)
MaterialFilteringMode(0,#PB_Material_Anisotropic)
MaterialCullingMode(0,#PB_Material_NoCulling)
ScrollMaterial(0,1,0,#PB_Material_Animated)

;{----------------------------------------------------------------- mesh conveyor belt
Structure f2:x.f:y.f:EndStructure
Global Dim lp.f2(1000)
Global n
Define.f x0,y0,r0,x1,y1,r1,x2,y2,r2,  la,  ai,af,a,da,u

Procedure.f lenght(*p0.f2,*p1.f2)
  Protected.f dx=*p1\x-*p0\x,dy=*p1\y-*p0\y
  ProcedureReturn Sqr(dx*dx+dy*dy)
EndProcedure

Procedure LpAdd(x.f,y.f)
  lp(n)\x=x
  lp(n)\y=y
  n+1
EndProcedure

Procedure arc(x.f,y.f,r.f,ai.f,af.f)
  Protected.f a,da=360/64
  a=ai
  While a=<af
    a+da  
    LpAdd(x+Cos(Radian(a))*r,y+Sin(Radian(a))*r)
  Wend
EndProcedure

x0= 1:y0= 0  :r0=0.3;  coordinates x,y , radius cylinder
x1=-1:y1= 0  :r1=0.3
x2= 0:y2=-0.5:r2=0.3
la=1.5 ;conveyor belt width

da=30
arc(x0,y0,r0,-90+da,90)
arc(x1,y1,r1,90,270-da)
arc(x2,y2,r2,270-da,270+da)
arc(x0,y0,r0,-90+da,-90+da)
CreateMesh(0)
For i=0 To n-1:i2=i*2
  If i:u+lenght(lp(i-1),lp(i)):EndIf
  MeshVertex(lp(i)\x,lp(i)\y, la/2,u,1,$ffffff,0,1,0)
  MeshVertex(lp(i)\x,lp(i)\y,-la/2,u,0,$ffffff,0,1,0)
  If i:MeshFace(i2-2,i2-1,i2+1,i2):EndIf
Next
FinishMesh(1)
NormalizeMesh(0)
ScaleMaterial(0,u/10,1) ; to match start and end of conveyor belt 
;}

CreateEntity(0,MeshID(0),MaterialID(0))


Define.f MouseX,Mousey,depx,depz,dist,val,blend

Repeat
	While WindowEvent():Wend
	ExamineKeyboard()
	ExamineMouse()
	;RotateEntity(0,0.2,0.2,0.2, #PB_Relative)
	
	RenderWorld()
	FlipBuffers()    
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)

User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: How animate material with procedural textures?

Post by minimy »

Thank you very much miso and Smaag!
Very good ideas, I like your idea of modifying the material, miso.
Yours Smaag, it's very good, but it's a single texture animation.
We all know that ph_shadoko is a master of 3D, I have learned a lot from his posts.
Thanks for the help friends!
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply