decal.odin 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package decal
  2. import rl "vendor:raylib"
  3. import r3d "../r3d"
  4. import "core:math"
  5. main :: proc() {
  6. // Initialize window
  7. rl.InitWindow(800, 450, "[r3d] - Decal example")
  8. defer rl.CloseWindow()
  9. rl.SetTargetFPS(60)
  10. // Initialize R3D
  11. r3d.Init(rl.GetScreenWidth(), rl.GetScreenHeight())
  12. defer r3d.Close()
  13. // Create meshes
  14. plane := r3d.GenMeshPlane(5.0, 5.0, 1, 1)
  15. defer r3d.UnloadMesh(plane)
  16. sphere := r3d.GenMeshSphere(0.5, 64, 64)
  17. defer r3d.UnloadMesh(sphere)
  18. cylinder := r3d.GenMeshCylinder(0.5, 0.5, 1, 64)
  19. defer r3d.UnloadMesh(cylinder)
  20. material := r3d.GetDefaultMaterial()
  21. defer r3d.UnloadMaterial(material)
  22. material.albedo.color = rl.GRAY
  23. // Create decal
  24. decal := r3d.DECAL_BASE
  25. defer r3d.UnloadDecalMaps(decal)
  26. r3d.SetTextureFilter(.BILINEAR)
  27. decal.albedo = r3d.LoadAlbedoMap("./resources/images/decal.png", rl.WHITE)
  28. decal.normal = r3d.LoadNormalMap("./resources/images/decal_normal.png", 1.0)
  29. decal.normalThreshold = 45.0
  30. decal.fadeWidth = 20.0
  31. // Create data for instanced drawing
  32. instances := r3d.LoadInstanceBuffer(3, {.POSITION})
  33. positions := cast([^]rl.Vector3)r3d.MapInstances(instances, {.POSITION})
  34. positions[0] = {-1.25, 0, 1}
  35. positions[1] = {0, 0, 1}
  36. positions[2] = {1.25, 0, 1}
  37. r3d.UnmapInstances(instances, {.POSITION})
  38. // Setup environment
  39. env := r3d.GetEnvironment()
  40. env.ambient.color = {10, 10, 10, 255}
  41. // Create light
  42. light := r3d.CreateLight(.DIR)
  43. r3d.SetLightDirection(light, {0.5, -1, -0.5})
  44. r3d.SetShadowDepthBias(light, 0.005)
  45. r3d.EnableShadow(light)
  46. r3d.SetLightActive(light, true)
  47. // Setup camera
  48. camera: rl.Camera3D = {
  49. position = {0, 3, 3},
  50. target = {0, 0, 0},
  51. up = {0, 1, 0},
  52. fovy = 60,
  53. }
  54. // Capture mouse
  55. rl.DisableCursor()
  56. // Main loop
  57. for !rl.WindowShouldClose()
  58. {
  59. rl.UpdateCamera(&camera, rl.CameraMode.FREE)
  60. rl.BeginDrawing()
  61. rl.ClearBackground(rl.RAYWHITE)
  62. r3d.Begin(camera)
  63. r3d.DrawMesh(plane, material, {0, 0, 0}, 1.0)
  64. r3d.DrawMesh(sphere, material, {-1, 0.5, -1}, 1.0)
  65. r3d.DrawMeshEx(cylinder, material, {1, 0.5, -1}, rl.QuaternionFromEuler(0, 0, math.PI/2), {1, 1, 1})
  66. r3d.DrawDecal(decal, {-1, 1, -1}, 1.0)
  67. r3d.DrawDecalEx(decal, {1, 0.5, -0.5}, rl.QuaternionFromEuler(math.PI/2, 0, 0), {1.25, 1.25, 1.25})
  68. r3d.DrawDecalInstanced(decal, instances, 3)
  69. r3d.End()
  70. rl.EndDrawing()
  71. }
  72. }