Spring Boot AR/VR Backend | 3D Data API | Unity Integration Sinhala

Spring Boot AR/VR Backend | 3D Data API | Unity Integration Sinhala

අපි හැමෝම දන්නවා AR (Augmented Reality) සහ VR (Virtual Reality) කියන්නේ මේ වෙද්දි දවසින් දවස වේගයෙන් දියුණු වෙන technology ක්ෂේත්‍රයක් කියලා. Gaming, Education, Healthcare, Manufacturing වගේ ගොඩක් දේවල් වලට මේවා පාවිච්චි කරනවා. හැබැයි, ඕනෑම AR/VR application එකක් සාර්ථක වෙන්න නම්, ඒකට හොඳ, විශ්වාසදායක backend එකක් අත්‍යවශ්‍යයි. අපේ application එකේ තියෙන 3D models, user data, real-time updates වගේ දේවල් manage කරන්නේ මේ backend එකෙන් තමයි.

අද මේ guide එකෙන් අපි බලමු, අපේ ප්‍රියතම Java Framework එක වෙන Spring Boot පාවිච්චි කරලා කොහොමද AR/VR application එකකට data serve කරන්න පුළුවන් සරල API එකක් හදාගන්නේ කියලා. අපි විශේෂයෙන්ම focus කරන්නේ AR app එකක් ඇතුළේ display කරන්න අවශ්‍ය 3D object metadata ලබා දෙන API එකක් හදන එකට.

AR/VR Backends කියන්නේ මොකක්ද? 🤔

AR/VR applications කියන්නේ හුදෙක් clients විතරයි. ඒ කියන්නේ, ඒගොල්ලන්ට තමන්ගේ user interface එක හෝ 3D environment එක පෙන්වන්න පුළුවන්. හැබැයි, user profiles, game scores, application එක ඇතුළේ තියෙන 3D assets වල තොරතුරු (ලැබෙන තැන, ප්‍රමාණය, texture වගේ දේවල්) වගේ දේවල් ගබඩා කරගෙන, update කරගෙන, ඒ client application එකට අවශ්‍ය වෙලාවට ලබා දෙන්න backend එකක් අවශ්‍යයි.

උදාහරණයක් විදිහට, AR app එකක් user කෙනෙක්ගේ camera එකෙන් බැලුවම ඒ location එකේ තියෙන්න ඕන 3D models මොනවද කියලා backend එකෙන් අහලා තමයි ඒ දේවල් පෙන්වන්නේ. ඒ වගේම, multiplayer VR game එකකදී අනිත් players ලගේ movements, scores වගේ දේවල් real-time update වෙන්නත් backend එකක් අත්‍යවශ්‍යයි.

Spring Boot මේ වගේ backends හදන්න නියම choice එකක්. ඒකට හේතු කීපයක් තියෙනවා:

  • Speed of Development: Spring Boot වල තියෙන convention-over-configuration design එක නිසා ඉක්මනින්ම REST APIs හදන්න පුළුවන්.
  • Robustness & Scalability: Enterprise-level applications වලට පාවිච්චි කරන framework එකක් නිසා, scalability, security වගේ දේවල් ගැන කරදර වෙන්න ඕනේ නැහැ.
  • Vibrant Ecosystem: Database integrations, security, messaging වගේ දේවල් වලට විශාල libraries ප්‍රමාණයක් තියෙනවා.

අපේ උදාහරණය: 3D Object Data API 🚀

අද අපි හදන API එකේ ප්‍රධාන අරමුණ වෙන්නේ AR application එකකට අවශ්‍ය 3D objects වල metadata සපයන එක. හිතන්න, ඔයාගේ AR app එක user කෙනෙක්ට එයාලගේ අවට පරිසරය ස්කෑන් කරලා, specific objects (උදාහරණයක් විදිහට ගෙදර තියෙන බඩු) AR එකෙන් බලන්න පුළුවන් විදිහට හදලා තියෙනවා කියලා. මේ objects වල තොරතුරු (model එකේ URL එක, AR environment එකේ තියෙන්න ඕන position, scale වගේ දේවල්) backend එකෙන් තමයි ගන්න ඕනේ.

අපි හදන API එකේ end-point එක මේ වගේ එකක් වෙයි:

GET /api/v1/ar-objects/{id}

මේකට id එකක් දුන්නම, ඒ id එකට අදාළ 3D object එකේ විස්තර JSON format එකෙන් ලැබෙයි.

Spring Boot Project එකක් හදාගනිමු! 🛠️

අපි මුලින්ම Spring Boot project එකක් හදාගමු. මේකට Spring Initializr (start.spring.io) එකට යන්න. පහත විස්තර ටික add කරන්න:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.x (latest stable version එක තෝරගන්න)
  • Group: com.example
  • Artifact: ar-vr-backend
  • Packaging: Jar
  • Java: 17 (or your preferred version)
  • Dependencies:
    • Spring Web: RESTful APIs හදන්න.
    • Lombok: Boilerplate code (getters, setters, constructors) අඩු කරන්න.

ඉන්පස්සේ Generate button එක click කරලා project එක download කරගෙන, ඔයාගේ favorite IDE (IntelliJ IDEA, VS Code, Eclipse) එකෙන් extract කරලා open කරගන්න.

API එක Implement කරමු! 👨‍💻

දැන් අපි API එක හදමු. අපිට ප්‍රධාන වශයෙන් classes තුනක් අවශ්‍යයි:

  1. ArObject (Model): අපේ 3D object එක define කරන data structure එක.
  2. ArObjectService (Service): 3D object data provide කරන logic එක (දැනට අපි hardcoded data පාවිච්චි කරමු).
  3. ArObjectController (Controller): Client requests handle කරලා, ArObjectService එකෙන් data අරගෙන response එකක් විදිහට යවන එක.

1. ArObject Model එක

src/main/java/com/example/arvrbackend folder එක ඇතුළේ ArObject.java කියලා class එකක් හදමු.

package com.example.arvrbackend;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ArObject {
    private Long id;
    private String name;
    private String description;
    private String modelUrl; // URL to the 3D model file (e.g., .glb, .gltf, .obj)
    private double scaleX; // Scale factor for the object in AR environment
    private double scaleY;
    private double scaleZ;
    private double positionX; // Default position in AR environment (e.g., relative to marker)
    private double positionY;
    private double positionZ;
}

මෙහිදී, @Data, @AllArgsConstructor, @NoArgsConstructor කියන Lombok annotations පාවිච්චි කරලා අපි getters, setters, constructors වගේ දේවල් auto-generate කරගන්නවා. modelUrl එකට 3D model file එකට යන link එකක් (උදා: cloud storage එකක තියෙන .glb file එකක්) දෙන්න පුළුවන්. scale සහ position කියන්නේ AR environment එක ඇතුළේ object එකේ ප්‍රමාණය සහ පිහිටීම define කරන values.

2. ArObjectService එක

දැන් අපි ArObjectService.java කියලා class එකක් හදමු. මේකෙන් අපි demo එකට in-memory HashMap එකක් පාවිච්චි කරලා data ටිකක් හදාගන්නවා. Real world application එකකදී මේ data database එකකින් හෝ වෙනත් external service එකකින් ගන්නවා.

package com.example.arvrbackend;

import org.springframework.stereotype.Service;

import jakarta.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ArObjectService {

    private final Map<Long, ArObject> arObjects = new HashMap<>();

    @PostConstruct
    public void init() {
        // Demo data for AR objects
        arObjects.put(1L, new ArObject(
                1L, 
                "Coffee Mug", 
                "A ceramic coffee mug.",
                "https://example.com/models/coffee_mug.glb", 
                0.2, 0.2, 0.2,
                0.0, 0.0, -0.5
        ));
        arObjects.put(2L, new ArObject(
                2L, 
                "Chair", 
                "A modern office chair.",
                "https://example.com/models/office_chair.gltf", 
                0.8, 0.8, 0.8,
                0.5, 0.0, 0.0
        ));
        arObjects.put(3L, new ArObject(
                3L, 
                "Plant Pot", 
                "A decorative indoor plant.",
                "https://example.com/models/plant_pot.obj", 
                0.4, 0.4, 0.4,
                -0.5, 0.0, 0.5
        ));
    }

    public Optional<ArObject> getArObjectById(Long id) {
        return Optional.ofNullable(arObjects.get(id));
    }

    public List<ArObject> getAllArObjects() {
        return List.copyOf(arObjects.values());
    }
}

@Service annotation එකෙන් Spring Framework එකට කියනවා මේක service component එකක් කියලා. @PostConstruct method එකෙන් application එක startup වෙනකොට අපේ demo data ටික arObjects HashMap එකට load කරනවා. getArObjectById method එකෙන් තමයි අපි request කරන id එකට අදාළ ArObject එක ලබාගන්නේ.

3. ArObjectController එක

අවසානයට, අපේ ArObjectController.java class එක හදමු. මේක තමයි incoming HTTP requests handle කරන්නේ.

package com.example.arvrbackend;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/ar-objects")
public class ArObjectController {

    private final ArObjectService arObjectService;

    public ArObjectController(ArObjectService arObjectService) {
        this.arObjectService = arObjectService;
    }

    @GetMapping("/{id}")
    public ResponseEntity<ArObject> getArObjectById(@PathVariable Long id) {
        return arObjectService.getArObjectById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping
    public ResponseEntity<List<ArObject>> getAllArObjects() {
        return ResponseEntity.ok(arObjectService.getAllArObjects());
    }
}

@RestController annotation එකෙන් මේ class එක REST API controller එකක් කියලා Spring එකට කියනවා. @RequestMapping("/api/v1/ar-objects") එකෙන් කියන්නේ මේ controller එකේ හැම method එකකටම /api/v1/ar-objects prefix එකක් තියෙනවා කියලා. @GetMapping("/{id}") එකෙන් කියන්නේ GET request එකක් /api/v1/ar-objects/{id} ට ආවොත් මේ method එක run කරන්න කියලා. @PathVariable Long id කියන්නේ URL එකේ තියෙන id value එක method parameter එකක් විදිහට ගන්න එකයි.

අපි ResponseEntity එකක් return කරන්නේ, HTTP status codes (200 OK, 404 Not Found) වගේ දේවල් control කරන්න පුළුවන් නිසා.

API එක Test කරමු! ✅

දැන් ඔයාගේ Spring Boot application එක run කරන්න. ඔයාගේ IDE එකෙන් හෝ Maven/Gradle command line එකෙන් run කරන්න පුළුවන්:

./mvnw spring-boot:run

Application එක start වුණාට පස්සේ, ඔයාට Postman, Insomnia වගේ API client එකක් හෝ web browser එකක් පාවිච්චි කරලා අපේ API එක test කරන්න පුළුවන්.

GET http://localhost:8080/api/v1/ar-objects/1

මේ request එක යැව්වම ඔයාට මේ වගේ JSON response එකක් ලැබෙන්න ඕනේ:

{
    "id": 1,
    "name": "Coffee Mug",
    "description": "A ceramic coffee mug.",
    "modelUrl": "https://example.com/models/coffee_mug.glb",
    "scaleX": 0.2,
    "scaleY": 0.2,
    "scaleZ": 0.2,
    "positionX": 0.0,
    "positionY": 0.0,
    "positionZ": -0.5
}

අනිත් objects ගන්න id එක වෙනස් කරලා බලන්න. GET http://localhost:8080/api/v1/ar-objects කියන request එක යැව්වොත් සියලුම objects ලැයිස්තුවක් ලැබෙයි.

ඉදිරි ගමන සහ අභියෝග 💡

මේ tutorial එකෙන් අපි Spring Boot පාවිච්චි කරලා AR/VR backend එකක් හදන සරලම පියවරක් ඉගෙන ගත්තා. හැබැයි, real world AR/VR applications වලට තව ගොඩක් දේවල් අවශ්‍ය වෙනවා. ඒ අතරින් කීපයක්:

  • Real Database Integration: අපේ HashMap එක වෙනුවට PostgreSQL, MongoDB වගේ database එකක් connect කරලා data persistent කරන්න.
  • User Authentication & Authorization: Users ලා register කරලා, login කරලා, AR objects වලට access control කරන්න. (e.g., Spring Security)
  • File Storage for 3D Models: 3D models (GLB, GLTF, OBJ files) Amazon S3, Google Cloud Storage වගේ cloud storage එකක store කරලා, ඒවාට access කරන්න පුළුවන් modelUrls provide කරන එක.
  • Real-time Communication: Multiplayer AR/VR experiences වලට WebSocket, WebRTC වගේ technologies පාවිච්චි කරලා real-time data synchronization කරන්න.
  • Content Management System (CMS): AR objects create, update, delete කරන්න Admin panel එකක් හදන එක.
  • Optimization for Performance: Low-latency responses, efficient data streaming වගේ දේවල් AR/VR experiences වලට ගොඩක් වැදගත්.

නිගමනය 🌟

Spring Boot කියන්නේ AR/VR applications වලට powerful, scalable backends හදන්න පුළුවන් විශිෂ්ට framework එකක්. මේ tutorial එක ඔයාලට ඒ ගමනට හොඳ ආරම්භයක් ලබා දෙන්න ඇති කියලා හිතනවා. 3D object metadata සපයන සරල API එකක් හදලා, ඒක test කරලා බලන්න අපි ඉගෙන ගත්තා.

දැන් ඔයාට පුළුවන් මේ project එක තව දුරටත් develop කරගෙන යන්න. Database එකක් connect කරන්න, අලුත් API endpoints add කරන්න, හෝ මේ project එක Unity වගේ AR/VR client platform එකකට connect කරලා බලන්න. ඔයාගේ අත්දැකීම් කොහොමද? පහලින් comment එකක් දාලා අපිට කියන්න! මේ වගේ තවත් tutorials එක්ක නැවත හමුවෙමු!