Ooops
May 30, 2020, 04:13:41 AM

Author Topic: [bb] SpriteMesh instead of Blitz3d Sprite by RemiD [ 1+ years ago ]  (Read 1609 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : SpriteMesh instead of Blitz3d Sprite
Author : RemiD
Posted : 1+ years ago

Description : Shows how to use a sprite mesh instead of a Blitz3d sprite

Code :
Code: BlitzBasic
  1. Global GWidth% = 800
  2. Global GHeight% = 600
  3. Global GColors% = 32
  4. Global GMode% = 2
  5. Graphics3D(GWidth,GHeight,GColors,GMode)
  6.  
  7. SeedRnd(MilliSecs())
  8.  
  9. Global Camera = CreateCamera()
  10. CameraRange(Camera,0.1,10000)
  11.  
  12. Origine = CreateCube()
  13. ScaleMesh(Origine,0.01/2,0.01/2,0.01/2)
  14. EntityColor(Origine,255,000,000)
  15. EntityFX(Origine,1)
  16.  
  17. Global DebugFeet = CreatePivot()  
  18.  
  19. Global DebugEyes = CreatePivot()
  20. PositionEntity(DebugEyes,EntityX(DebugFeet,True),EntityY(DebugFeet,True),EntityZ(DebugFeet,True))
  21. RotateEntity(DebugEyes,EntityPitch(DebugFeet,True),EntityYaw(DebugFeet,True),EntityRoll(DebugFeet,True))
  22. MoveEntity(DebugEyes,0,1.7,0.25)
  23. EntityParent(DebugEyes,DebugFeet,True)
  24.  
  25. DebugMesh = CreateCube()
  26. ScaleMesh(DebugMesh,0.5/2,1.8/2,0.25/2)
  27. PositionMesh(DebugMesh,0,1.8/2,0)
  28. PositionEntity(DebugMesh,EntityX(DebugFeet,True),EntityY(DebugFeet,True),EntityZ(DebugFeet,True))
  29. RotateEntity(DebugMesh,EntityPitch(DebugFeet,True),EntityYaw(DebugFeet,True),EntityRoll(DebugFeet,True))
  30. EntityParent(DebugMesh,DebugFeet,True)
  31. EntityColor(DebugMesh,000,000,255)
  32.  
  33. Global MXSpeed# = 0
  34. Global MYSpeed# = 0
  35. Global DebugFeetYaw# = 0
  36. Global DebugEyesPitch# = 0
  37.  
  38. Const CEditView% = 1
  39. Const CDebugView% = 2
  40. Global CameraMode% = 0
  41. CameraMode = CDebugView
  42. EntityParent(Camera,0)
  43. PositionEntity(Camera,EntityX(DebugEyes,True),EntityY(DebugEyes,True),EntityZ(DebugEyes,True))
  44. RotateEntity(Camera,EntityPitch(DebugEyes,True),EntityYaw(DebugEyes,True),EntityRoll(DebugEyes,True))  
  45. EntityParent(Camera,DebugEyes,True)
  46.  
  47. PositionEntity(DebugFeet,0,0,-5)
  48.  
  49. ;ViewMode
  50. ;1: fixed (sprite always faces camera - Default)
  51. ;2: free (sprite is independent of camera)
  52. ;3: upright1 (sprite always faces camera, but rolls with camera as well, unlike mode no.1)
  53. ;4: upright2 (sprite always remains upright. Gives a 'billboard' effect. Good For trees, spectators etc.)
  54. ViewMode% = 1
  55.  
  56. SpriteMesh = CreateMesh()
  57. Surface = CreateSurface(SpriteMesh)
  58. AddVertex(Surface, -0.5, 0.5, 0.0, 0.000, 0.000)
  59. AddVertex(Surface, 0.5, 0.5, 0.0, 1.000, 0.000)
  60. AddVertex(Surface, -0.5, -0.5, 0.0, 0.000, 1.000)
  61. AddVertex(Surface, 0.5, -0.5, 0.0, 1.000, 1.000)
  62. AddTriangle(Surface,0,1,2)
  63. AddTriangle(Surface,2,1,3)
  64. UpdateNormals(SpriteMesh)
  65. PositionEntity(SpriteMesh,1,0,0)
  66. EntityFX(SpriteMesh,1)
  67.  
  68. SpriteOriginal = CreateSprite()
  69. ScaleSprite(SpriteOriginal,0.5,0.5)
  70. SpriteViewMode(SpriteOriginal,ViewMode)  
  71. PositionEntity(SpriteOriginal,-1,0,0)
  72.  
  73. SmileyTexDiffuse = CreateTexture(128,128)
  74. SetBuffer(TextureBuffer(SmileyTexDiffuse))
  75. ClsColor(125,125,125)
  76. Cls()
  77. Color(255,255,000)
  78. Oval(0,0,128,128,1)
  79. Color(001,001,001)
  80. Oval(33,20,20,40)
  81. Oval(73,20,20,40)
  82. Plot(60,75)
  83. Plot(66,75)
  84. Line(63,95,43,95)
  85. Line(43,95,33,80)
  86. Line(64,95,84,95)
  87. Line(84,95,94,80)
  88. SetBuffer(BackBuffer())
  89. TextureBlend(SmileyTexDiffuse,2)
  90. EntityTexture(SpriteMesh,SmileyTexDiffuse,0,0)
  91. EntityTexture(SpriteOriginal,SmileyTexDiffuse,0,0)
  92.  
  93. DLight = CreateLight(1)
  94. LightColor(DLight,255,255,255)
  95. PositionEntity(DLight,0,1024,-1024)
  96. RotateEntity(DLight,45,0,0)
  97. AmbientLight(125,125,125)
  98.  
  99. Repeat
  100.  
  101.  MainLoopStart% = MilliSecs()
  102.  
  103.  Cls()
  104.  
  105.  CameraModeSelect()
  106.  
  107.  DebugUpdate()
  108.  
  109.  If(ViewMode = 1)
  110.   RotateEntity(SpriteMesh,EntityPitch#(Camera,True),EntityYaw#(Camera,True),EntityRoll#(Camera,True))
  111.  ElseIf(ViewMode = 2)            
  112.   ;
  113.  ElseIf(ViewMode = 3)
  114.   RotateEntity(SpriteMesh,EntityPitch#(Camera,True),EntityYaw#(Camera,True),0)
  115.  ElseIf(ViewMode = 4)            
  116.   RotateEntity(SpriteMesh,0,EntityYaw#(Camera,True),0)
  117.  EndIf
  118.  
  119.  If(KeyDown(2)>0)  
  120.   Wireframe(True)  
  121.  Else  
  122.   Wireframe(False)  
  123.  EndIf
  124.  
  125.  SetBuffer(BackBuffer())
  126.  RenderWorld()
  127.  
  128.  Color(255,255,255)
  129.  Text(0,0,"Tris : "+TrisRendered())
  130.  Text(0,20,"FPS = "+FPS)
  131.  
  132.  Flip(True)
  133.  
  134.  MainLoopTime% = MilliSecs() - MainLoopStart   
  135.  
  136.  FPS = 1000/MainLoopTime
  137.  
  138. Until(KeyDown(1)>0)
  139.  
  140. End
  141.  
  142. Function CameraModeSelect()
  143.  ;Select between DebugView and EditView
  144.  If(KeyHit(15)>0)
  145.   If(CameraMode = CDebugView)
  146.    CameraMode = CEditView
  147.    EntityParent(Camera,0)
  148.   ElseIf(CameraMode = CEditView)
  149.    CameraMode = CDebugView
  150.    EntityParent(Camera,0)
  151.    PositionEntity(Camera,EntityX(DebugEyes,True),EntityY(DebugEyes,True),EntityZ(DebugEyes,True))
  152.    RotateEntity(Camera,EntityPitch(DebugEyes,True),EntityYaw(DebugEyes,True),EntityRoll(DebugEyes,True))  
  153.    EntityParent(Camera,DebugEyes,True)
  154.   EndIf
  155.  EndIf
  156. End Function
  157.  
  158. Function DebugUpdate()
  159.  If(CameraMode = CDebugView)
  160.   MXSpeed = MouseXSpeed()  
  161.   MYSpeed = MouseYSpeed()  
  162.   MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2)
  163.   DebugEyesPitch = DebugEyesPitch+MYSpeed*0.1
  164.   If(DebugEyesPitch > 89)
  165.    DebugEyesPitch = 89
  166.   EndIf
  167.   If(DebugEyesPitch < -89)
  168.    DebugEyesPitch = -89
  169.   EndIf
  170.   DebugFeetYaw = DebugFeetYaw-MXSpeed*0.1  
  171.   RotateEntity(DebugEyes,DebugEyesPitch,0,0)
  172.   RotateEntity(DebugFeet,0,DebugFeetYaw,0)  
  173.   If(KeyDown(42)>0)
  174.    SpeedCoeff# = 10.0
  175.   ElseIf(KeyDown(29)>0)
  176.    SpeedCoeff# = 0.1
  177.   Else
  178.    SpeedCoeff# = 1.0
  179.   EndIf
  180.   If(KeyDown(17)>0)  
  181.    MoveEntity(DebugFeet,0,0,0.1*SpeedCoeff)  
  182.   EndIf  
  183.   If(KeyDown(31)>0)  
  184.    MoveEntity(DebugFeet,0,0,-0.1*SpeedCoeff)  
  185.   EndIf  
  186.   If(KeyDown(30)>0)  
  187.    MoveEntity(DebugFeet,-0.1*SpeedCoeff,0,0)  
  188.   EndIf  
  189.   If(KeyDown(32)>0)  
  190.    MoveEntity(DebugFeet,0.1*SpeedCoeff,0,0)  
  191.   EndIf  
  192.   If(KeyDown(16)>0)  
  193.    MoveEntity(DebugFeet,0,-0.1*SpeedCoeff,0)  
  194.    EndIf
  195.   If(KeyDown(18)>0)  
  196.    MoveEntity(DebugFeet,0,0.1*SpeedCoeff,0)  
  197.   EndIf
  198.   If(KeyHit(201)>0)
  199.    MoveEntity(Camera,0,0,0.1)
  200.   EndIf
  201.   If(KeyHit(209)>0)
  202.    MoveEntity(Camera,0,0,-0.1)
  203.   EndIf
  204.   If(KeyDown(44)>0)
  205.    TurnEntity(Camera,0,0,-1)
  206.   EndIf
  207.   If(KeyDown(45)>0)
  208.    TurnEntity(Camera,0,0,1)
  209.   EndIf
  210.  ElseIf(CameraMode = CEditView)
  211.   MXSpeed = MouseXSpeed()  
  212.   MYSpeed = MouseYSpeed()  
  213.   ;MoveMouse(GWidth/2,GHeight/2)
  214.  EndIf
  215. End Function


Comments :


_PJ_(Posted 1+ years ago)

 I think this should be more accurate in terms of how the 'standard' SpriteViewMode works:
Code: [Select]
;Sprite Overload

;1: Sprite changes its pitch And yaw values To face camera, but doesn't roll. (Default)
;2: Sprite does Not change either its pitch, yaw Or roll values.
;3: Sprite changes its yaw And pitch To face camera, And changes its roll value To match cameras.
;4: Sprite changes its yaw value To face camera, but Not its pitch value, And changes its roll value To match cameras.

; Note:
; ClearWorld will destroy sprite meshes, but not the TYPES.
; EntityClass will not return "Sprite" results: Type Handle = "Entity does not exist"
; EntityClass will not return "Sprite" results: Quad Handle = "Mesh"
; RotateSprite is only valid for SpriteViewMode 1 or 2

;Necessary Additions

Type SPRITES
Field SpriteMesh
Field ViewMode
End Type

Function UpdateSprite(Sprite,Camera=0)
;When using Sprites of Mode 1,3 or 4 This must be used befrore every Render
;For every CAMERA for every SPRITE
;Whenever that camera moves or rotates wrt the sprite
If (Not(Sprite))
Return
End If

Local This.SPRITES=Object.SPRITES(Sprite)
If (This=Null)
Return
End If

Local Quad=ThisSpriteMesh

If (Not(Quad))
FreeSprite(Sprite)
Else

Local P#=EntityPitch#(Quad,True)
Local Y#=EntityYaw#(Quad,True)+DeltaYaw(Quad,Camera)
Local R#=EntityRoll#(Quad,True)

Local Mode=ThisViewMode
If (Mode<>2)
If (Mode=1)
P=P+DeltaPitch#(Quad,Camera)
Else
R=EntityRoll#(Camera,True)
End If
RotateEntity Quad,P,Y,R,True
End If

End If

End Function

;Override Functions

Function LoadSprite(FilePath$, Flags=1, Parent=0)
If (FileType(FilePath=1))
Local Texture=LoadTexture(FilePath,Flags)
If (Texture)
Local This.SPRITES=New SPRITES
ThisSpriteMesh=CreateQuad(Parent)
EntityTexture ThisSpriteMesh,Texture
FreeTexture Texture
ThisViewMode=1
;It might be adviseable to call UpdateSprite() here
Return Handle(This.SPRITES)
End If
End If
Return 0
End Function

Function FreeSprite(Sprite)
; Use this instead of FreeEntity to ensure Sprites are effectively removed from memory
If (Not(Sprite))
Return
End If

Local This.SPRITES=Object.SPRITES(Sprite)
If (This.SPRITES=Null)
Return
End If

If (ThisSpriteMesh)
FreeEntity ThisSpriteMesh
ThisSpriteMesh=0
ThisViewMode=0
End If
Delete This
End Function

Function HandleSprite(Sprite, X#=0.5, Y#=0.5)
Local Quad=GetSpriteMesh(Sprite)
If (Quad)
PositionMesh Quad,X*MeshWidth(Quad),Y*MeshHeight(Quad),0.5*MeshDepth(Quad)
End If
End Function

Function RotateSprite(Sprite, Angle#)
Local Quad=GetSpriteMesh(Sprite)
If (Quad)
TurnEntity Quad,0,0,Angle,False
End If
End Function

Function ScaleSprite(Sprite, X#, Y#)
Local Quad=GetSpriteMesh(Sprite)
If (Quad)
ScaleMesh Quad,X,Y,1.0
End If
End Function

Function SpriteViewMode(Sprite, Mode=2)
Mode=((Mode>0)*(Mode<5)*Mode)+(2*((Mode<1)+(Mode>4)))

If (Not(Sprite))
Return
End If
Local This.SPRITES=Object.SPRITES(Sprite)

If (ThisSpriteMesh)
ThisViewMode=Mode
;It might be adviseable to call UpdateSprite() here
Else
FreeSprite(Sprite)
End If
End Function

; For Direct Manipulation & Helper functions

Function GetSpriteMesh(Sprite)
If (Not(Sprite))
Return 0
End If

Local This.SPRITES=Object.SPRITES(Sprite)
If (This.SPRITES=Null)
Return 0
End If

Return ThisSpriteMesh
End Function

Function CreateQuad(Parent=False)
Local Mesh=CreateMesh(Parent)
Local Surface=CreateSurface(Mesh)

AddVertex(Surface,-0.5,0.5,0,1,0)
VertexColor Surface,0,255,255,255
AddVertex(Surface,0.5,0.5,0,0,0)
VertexColor Surface,1,255,255,255
AddVertex(Surface,-0.5,-0.5,0,1,1)
VertexColor Surface,2,255,255,255
AddVertex(Surface,0.5,-0.5,0,0,1)
VertexColor Surface,3,255,255,255
AddTriangle(Surface,0,1,2)
AddTriangle(Surface,3,2,1)

UpdateNormals Mesh

Return Mesh
End Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal