File size: 2,930 Bytes
f0c79f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import numpy as np
import json
from typing import Dict, Any

def convert_mesh_to_threejs(mesh_data: Dict[str, Any], output_path: str):
    """
    Convert mesh data to THREE.js compatible JSON format.

    Args:
        mesh_data: Mesh data from SAM 3D Body
        output_path: Path to save the THREE.js compatible JSON
    """
    try:
        # Convert mesh data to THREE.js format
        threejs_data = {
            "metadata": {
                "version": 4.5,
                "type": "Object",
                "generator": "MLSE Player 3D Generator"
            },
            "geometries": [
                {
                    "uuid": "player-mesh",
                    "type": "BufferGeometry",
                    "data": {
                        "attributes": {
                            "position": {
                                "itemSize": 3,
                                "type": "Float32Array",
                                "array": mesh_data["vertices"].flatten().tolist()
                            },
                            "normal": {
                                "itemSize": 3,
                                "type": "Float32Array",
                                "array": mesh_data.get("normals", np.zeros_like(mesh_data["vertices"])).flatten().tolist()
                            },
                            "uv": {
                                "itemSize": 2,
                                "type": "Float32Array",
                                "array": mesh_data.get("uvs", np.zeros((len(mesh_data["vertices"]), 2))).flatten().tolist()
                            }
                        },
                        "index": {
                            "type": "Uint16Array",
                            "array": mesh_data["faces"].flatten().tolist()
                        }
                    }
                }
            ],
            "materials": [
                {
                    "uuid": "player-material",
                    "type": "MeshStandardMaterial",
                    "color": 0x8888ff,
                    "roughness": 0.5,
                    "metalness": 0.2,
                    "emissive": 0x000000,
                    "side": 2
                }
            ],
            "object": {
                "uuid": "player-object",
                "type": "Mesh",
                "name": "PlayerMesh",
                "geometry": "player-mesh",
                "material": "player-material",
                "position": [0, 0, 0],
                "quaternion": [0, 0, 0, 1],
                "scale": [1, 1, 1]
            }
        }

        # Write to file
        with open(output_path, 'w') as f:
            json.dump(threejs_data, f)

        return output_path

    except Exception as e:
        print(f"Error converting mesh to THREE.js format: {str(e)}")
        raise RuntimeError(f"Failed to convert mesh: {str(e)}")