January 26, 2021, 05:27:39 AM

Author Topic: [bmx] Blobby Object Plasma by ImaginaryHuman [ 1+ years ago ]  (Read 402 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Blobby Object Plasma
Author : ImaginaryHuman
Posted : 1+ years ago

Description : This simple but attractive effect is produced by the interactions of numerous blobby object particle fields. For a 1024x768 display I have set it up to use 60 blobby objects. Each field consumes a 512x512 area.

The blobby energy field, which is a gradient with a squared falloff, is rendered into a 2 dimensional array in the form of an image. The image is then pasted onto the screen with additive blending. The values of all overlapping fields add up and the curvature of their gradients produces a gently curved contour. This is done on 255 energy levels in 3 separate color components, whereby every level of energy is rendered and not just those beyond a threshold. This produces a plasma effect and the changes in color further animate the contours.

This code was originally a 512-byte program and has been re-expanded into more human-readable form. As a result, calculation of colors is done based on object coordinates and rotation angles, and you will notice the red component eventually goes out of range, resulting in a mainly blue-green palette. There are many better ways to generate and maintain colors but I was short on space originally.

This should run well on even mid-low-range graphics cards since it is mainly just drawing images with additive blending. Enjoy.


Code :
Code: BlitzMax
  1. 'Set these to what you want
  2. Const Objects:Int=60
  3. Const ScrWidth:Int=1024
  4. Const ScrHeight:Int=768
  5. Const RotationSpeed:Float=0.2
  6.  
  7. 'Open a screen and draw a blobby energy field with squared falloff
  8. Graphics ScrWidth,ScrHeight,32
  9. For Local Radius:Float=1 Until 512 Step 0.5
  10.     Local Energy:Float=(Radius*Radius)/1024.0
  11.     SetColor Energy,Energy,Energy
  12.     DrawOval Radius/2.0,Radius/2.0,512-Radius,512-Radius     'Try DrawRect
  13. Next
  14.  
  15. 'Turn it into an image/texture
  16. Local Image:TImage=CreateImage(512,512)
  17. GrabImage(Image,0,0)
  18.  
  19. 'Set up the positions and movement directions/speeds/angles of objects
  20. Local XPos:Float[Objects],YPos:Float[Objects]
  21. Local XMove:Float[Objects],YMove:Float[Objects]
  22. Local Rotation:Float[Objects]
  23. Local Obj:Int
  24. For Obj=0 Until Objects
  25.     XPos[Obj]=Rand(0,ScrWidth)
  26.     Ypos[Obj]=Rand(0,ScrHeight)
  27.     XMove[Obj]=(RndFloat()-0.5)*4
  28.     YMove[Obj]=(RndFloat()-0.5)*4
  29.     Rotation[Obj]=RndFloat()*360
  30. Next
  31.  
  32. 'Additive blending to combine blob fields
  33. SetBlend LIGHTBLEND
  34.  
  35. 'Do a demo
  36. Repeat
  37.     Cls
  38.  
  39.     For Obj=0 Until Objects
  40.         'Move the objects
  41.         XPos[Obj]:+XMove[Obj]
  42.         YPos[Obj]:+YMove[Obj]
  43.        
  44.         'Bounce the objects off the screen edges
  45.         If XPos[Obj]<0 or XPos[Obj]>ScrWidth Then XMove[Obj]=-XMove[Obj]
  46.         If YPos[Obj]<0 or YPos[Obj]>ScrHeight then YMove[Obj]=-YMove[Obj]
  47.        
  48.         'Rotate the objects around their corners
  49.         Rotation[Obj]:+RotationSpeed
  50.        
  51.         'Set object color based on coords/angles
  52.         SetColor 1024-(Rotation[Obj] Mod 256),512-XPos[Obj],512-YPos[Obj]
  53.        
  54.         'Draw the blobby object
  55.         SetRotation Rotation[Obj]
  56.         DrawImage Image,XPos[Obj],YPos[Obj]
  57.     Next
  58.  
  59.     Flip 1
  60. Until KeyHit(KEY_ESCAPE)


Comments :


Boiled Sweets(Posted 1+ years ago)

 Many thanks for this!


mindstorms(Posted 1+ years ago)

 Here is a blitz3d port of it...not a direct port, had to change some things, but it should look the same (I don't have blitzmax, but the screenshots look the same).  It uses an additive blended mesh with each "blob" being a seperate surface for ease of vertex manipulation :)
Code: [Select]
;Set these To what you want
Const Objects=19
Const ScrWidth=800
Const ScrHeight=600
SeedRnd(MilliSecs())

;Open a screen And draw a blobby Energy Field with squared falloff
Graphics3D ScrWidth,ScrHeight,0,2

cam = CreateCamera()
MoveEntity(cam,0,0,-1)

;create the image/texture...
Image=CreateTexture(512,512)
SetBuffer(TextureBuffer(Image))
For Radius#=1 To 512 Step .5
Energy#=(Radius*Radius)/1024
Color Energy,Energy,Energy
Oval Radius/2.0,Radius/2.0,512-Radius,512-Radius     ;Try Rect
Next
SetBuffer(BackBuffer())

;the magic :)
Global render2d=CreatePivot()
PositionEntity render2d,-Float(ScrWidth)/ScrHeight,1,0
ScaleEntity render2d,2.0/ScrWidth,-2.0/ScrWidth,1

mainmesh=CreateMesh(render2d)
EntityBlend(mainmesh,3)
Dim mesh(Objects)
Global brush=CreateBrush()
BrushTexture(brush,Image)
Dim vertsx(3)
Dim vertsy(3)
vertsx(0)=0  : vertsy(0)=0
vertsx(1)=512: vertsy(1)=0
vertsx(2)=512: vertsy(2)=512
vertsx(3)=0  : vertsy(3)=512

For i=0 To Objects-1
mesh(i)=CreateSurface(mainmesh)
AddVertex(mesh(i),vertsx(0),vertsy(0),0,0,0)
AddVertex(mesh(i),vertsx(1),vertsy(1),0,1,0,0)
AddVertex(mesh(i),vertsx(2),vertsy(2),0,1,1,0)
AddVertex(mesh(i),vertsx(3),vertsy(3),0,0,1,0)
AddTriangle(mesh(i),0,1,2)
AddTriangle(mesh(i),2,3,0)
Next

;Set up the positions And movement directions/speeds/angles of Objects
Dim XPos#(Objects)
Dim YPos#(Objects)
Dim XMove#(Objects)
Dim YMove#(Objects)
Dim Rotation#(Objects)
Dim RotationSpeed#(Objects)

For Obj=0 To Objects-1
XPos(Obj)=Rand(0,ScrWidth)
YPos(Obj)=Rand(0,ScrHeight)
XMove(Obj)=(Rnd(0,1)-0.5)*4
YMove(Obj)=(Rnd(0,1)-0.5)*4
Rotation(Obj)=Rnd(0,1)*360
RotationSpeed(Obj)=Rnd(-.2,.2)
Next

Global tempPivot=CreatePivot(render2d)

;Do a demo
Repeat
For Obj=0 To Objects-1
;Move the Objects
XPos(Obj)=XPos(Obj)+XMove(Obj)
YPos(Obj)=YPos(Obj)+YMove(Obj)

;Bounce the Objects off the screen edges
If XPos(Obj)<0 Or XPos(Obj)>ScrWidth Then XMove(Obj)=-XMove(Obj)
If YPos(Obj)<0 Or YPos(Obj)>ScrHeight Then YMove(Obj)=-YMove(Obj)

;Rotate the Objects around their corners
Rotation(Obj)=Rotation(Obj)+RotationSpeed(Obj)

;Set Object Color based on coords/angles
BrushColor(brush,1024-(Rotation(Obj) Mod 256),512-XPos(Obj),512-YPos(Obj))
PaintSurface(mesh(obj),brush)

;actually update
RotateEntity(tempPivot,0,0,Rotation(Obj))
PositionEntity(tempPivot,XPos(Obj),YPos(Obj),0)
For i=0 To 3
TFormPoint(vertsx(i),vertsy(i),0,tempPivot,render2d)
VertexCoords(mesh(Obj),i,TFormedX(),TFormedY(),0)
Next

Next

RenderWorld()
Flip
Until KeyHit(1)



ImaginaryHuman(Posted 1+ years ago)

 Cool. I can't see the Blitz3D version but the code is an interesting adaptation.You are welcome, too, Boiled Man. Enjoy.


Boiled Sweets(Posted 1+ years ago)

 @mindstorm - awesome.Screen saver in the pipeline :-) [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal