sprite.odin 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package sprite
  2. import rl "vendor:raylib"
  3. import "core:math"
  4. import r3d "../r3d"
  5. get_texcoord_scale_offset :: proc(xFrameCount: int, yFrameCount: int, currentFrame: f32) -> (uvScale: rl.Vector2, uvOffset: rl.Vector2) {
  6. uvScale.x = 1.0 / f32(xFrameCount)
  7. uvScale.y = 1.0 / f32(yFrameCount)
  8. frameIndex := int(currentFrame + 0.5) % (xFrameCount * yFrameCount)
  9. frameX := frameIndex % xFrameCount
  10. frameY := frameIndex / xFrameCount
  11. uvOffset.x = f32(frameX) * uvScale.x
  12. uvOffset.y = f32(frameY) * uvScale.y
  13. return
  14. }
  15. main :: proc() {
  16. // Initialize window
  17. rl.InitWindow(800, 450, "[r3d] - Sprite example")
  18. defer rl.CloseWindow()
  19. rl.SetTargetFPS(60)
  20. // Initialize R3D
  21. r3d.Init(rl.GetScreenWidth(), rl.GetScreenHeight())
  22. defer r3d.Close()
  23. r3d.SetTextureFilter(.POINT)
  24. // Set background/ambient color
  25. env := r3d.GetEnvironment()
  26. env.background.color = {102, 191, 255, 255}
  27. env.ambient.color = {10, 19, 25, 255}
  28. env.tonemap.mode = .FILMIC
  29. // Create ground mesh and material
  30. meshGround := r3d.GenMeshPlane(200, 200, 1, 1)
  31. defer r3d.UnloadMesh(meshGround)
  32. matGround := r3d.GetDefaultMaterial()
  33. matGround.albedo.color = rl.GREEN
  34. // Create sprite mesh and material
  35. meshSprite := r3d.GenMeshQuad(1.0, 1.0, 1, 1, {0, 0, 1})
  36. defer r3d.UnloadMesh(meshSprite)
  37. meshSprite.shadowCastMode = .ON_DOUBLE_SIDED
  38. matSprite := r3d.GetDefaultMaterial()
  39. defer r3d.UnloadMaterial(matSprite)
  40. matSprite.albedo = r3d.LoadAlbedoMap("./resources/images/spritesheet.png", rl.WHITE)
  41. matSprite.billboardMode = .Y_AXIS
  42. // Setup spotlight
  43. light := r3d.CreateLight(.SPOT)
  44. r3d.LightLookAt(light, {0, 10, 10}, {0, 0, 0})
  45. r3d.SetLightRange(light, 64.0)
  46. r3d.EnableShadow(light)
  47. r3d.SetLightActive(light, true)
  48. // Setup camera
  49. camera: rl.Camera3D = {
  50. position = {0, 2, 5},
  51. target = {0, 0.5, 0},
  52. up = {0, 1, 0},
  53. fovy = 45,
  54. }
  55. // Bird data
  56. birdPos: rl.Vector3 = {0, 0.5, 0}
  57. birdDirX: f32 = 1.0
  58. // Main loop
  59. for !rl.WindowShouldClose()
  60. {
  61. // Update bird position
  62. birdPrev := birdPos
  63. time := f32(rl.GetTime())
  64. birdPos.x = 2.0 * math.sin_f32(time)
  65. birdPos.y = 1.0 + math.cos_f32(time * 4.0) * 0.5
  66. birdDirX = (birdPos.x - birdPrev.x >= 0.0) ? 1.0 : -1.0
  67. // Update sprite UVs
  68. // We multiply by the sign of the X direction to invert the uvScale.x
  69. currentFrame := 10.0 * time
  70. matSprite.uvScale, matSprite.uvOffset = get_texcoord_scale_offset(
  71. int(4 * birdDirX),
  72. 1,
  73. currentFrame,
  74. )
  75. rl.BeginDrawing()
  76. rl.ClearBackground(rl.RAYWHITE)
  77. // Draw scene
  78. r3d.Begin(camera)
  79. r3d.DrawMesh(meshGround, matGround, {0, -0.5, 0}, 1.0)
  80. r3d.DrawMesh(meshSprite, matSprite, {birdPos.x, birdPos.y, 0}, 1.0)
  81. r3d.End()
  82. rl.EndDrawing()
  83. }
  84. }