myroom-react
Version:
React component wrapper for MyRoom 3D scene
180 lines (140 loc) • 6.23 kB
Markdown
# MyRoom React Component - Implementation Notes
## Overview
This React component was created by analyzing the `myroom-system` project and its web component implementation. The goal was to create a React wrapper that provides the same functionality as the web component but in a more React-friendly way.
## Analysis of Original Web Component
The original web component (`MyRoomWebComponent.ts`) provides:
1. **3D Scene Management**: Uses Babylon.js with the `IntegratedBabylonScene` component
2. **Avatar System**: Customizable avatars with gender, parts, and colors
3. **Room Management**: Load and manage 3D rooms and furniture items
4. **Interactive Controls**: Camera controls, item selection, and manipulation
5. **Event Handling**: Scene ready, avatar changes, item selection, etc.
## React Component Structure
### Core Files Created:
1. **`src/components/MyRoomScene.tsx`** - Main React component wrapper
2. **`src/types/index.ts`** - TypeScript type definitions
3. **`src/utils/avatarUtils.ts`** - Avatar configuration utilities
4. **`src/components/MyRoomScene.css`** - Component styling
5. **`src/demo/MyRoomDemo.tsx`** - Full-featured demo component
6. **`src/examples/`** - Simple and advanced usage examples
### Key Features Implemented:
- ✅ **TypeScript Support**: Full type definitions for all props and events
- ✅ **React Hooks Integration**: Uses useState, useEffect, useRef, useImperativeHandle
- ✅ **Event Handling**: Scene ready, error handling, avatar changes, item selection
- ✅ **Responsive Design**: Mobile-friendly with CSS media queries
- ✅ **Accessibility**: Proper ARIA labels and keyboard navigation support
- ✅ **Modern Styling**: CSS with dark mode and high contrast support
- ✅ **Comprehensive Documentation**: README with usage examples
## Integration Requirements
### Current Status: Placeholder Implementation
The React component currently uses a placeholder for the `IntegratedBabylonScene` component. To complete the integration, you need to:
1. **Import the Real Component**: Replace the placeholder with the actual `IntegratedBabylonScene` from `myroom-system`
2. **Copy Dependencies**: Ensure all required Babylon.js components and utilities are available
3. **Asset Paths**: Verify that 3D model paths are correctly configured
4. **Build Configuration**: Set up proper bundling for the 3D assets
### Required Steps for Full Integration:
1. **Copy Core Components**:
```bash
# Copy the IntegratedBabylonScene and related components from myroom-system
cp -r myroom-system/src/shared/components/babylon/* myroom-react/src/components/babylon/
cp -r myroom-system/src/shared/hooks/* myroom-react/src/hooks/
cp -r myroom-system/src/shared/config/* myroom-react/src/config/
```
2. **Update Imports**:
```typescript
// In MyRoomScene.tsx, replace the placeholder with:
import { IntegratedBabylonScene } from './babylon/IntegratedBabylonScene';
```
3. **Copy Assets**:
```bash
# Copy 3D models and textures
cp -r myroom-system/public/models myroom-react/public/
cp -r myroom-system/public/manifest myroom-react/public/
```
4. **Update Package Dependencies**:
```json
{
"dependencies": {
"@babylonjs/core": "^7.31.1",
"@babylonjs/gui": "^7.31.1",
"@babylonjs/loaders": "^7.31.1",
"@babylonjs/materials": "^7.31.1",
"@babylonjs/post-processes": "^7.31.1"
}
}
```
## Usage Examples
### Basic Usage:
```tsx
import { MyRoomScene } from 'myroom-react';
function App() {
return (
<MyRoomScene
roomPath="/models/rooms/cate001/MR_KHROOM_0001.glb"
gender="male"
width="100%"
height="600px"
onSceneReady={(scene) => console.log('Scene ready:', scene)}
/>
);
}
```
### Advanced Usage:
```tsx
import { MyRoomScene, MyRoomSceneRef, AvatarConfig, LoadedItem } from 'myroom-react';
function App() {
const sceneRef = useRef<MyRoomSceneRef>(null);
const [avatarConfig, setAvatarConfig] = useState<AvatarConfig>(/* config */);
const [loadedItems, setLoadedItems] = useState<LoadedItem[]>([]);
return (
<MyRoomScene
ref={sceneRef}
roomPath="/models/rooms/cate001/MR_KHROOM_0001.glb"
avatarConfig={avatarConfig}
loadedItems={loadedItems}
onSceneReady={(scene, engine) => console.log('Ready')}
onAvatarChanged={(config) => setAvatarConfig(config)}
onItemSelected={(item) => console.log('Selected:', item)}
/>
);
}
```
## Key Differences from Web Component
1. **Props vs Attributes**: React uses props instead of HTML attributes
2. **Event Handling**: React events instead of DOM events
3. **State Management**: React hooks for state instead of class properties
4. **Styling**: CSS modules instead of shadow DOM styles
5. **TypeScript**: Full TypeScript support with proper type definitions
## Benefits of React Component
1. **Better Integration**: Seamless integration with React applications
2. **Type Safety**: Full TypeScript support with proper type checking
3. **Modern APIs**: Uses React hooks and modern patterns
4. **Better Performance**: React's virtual DOM and optimization
5. **Easier Testing**: Can be tested with React testing libraries
6. **Component Composition**: Can be easily composed with other React components
## Next Steps
1. **Complete Integration**: Replace placeholder with real Babylon.js component
2. **Testing**: Add unit tests and integration tests
3. **Documentation**: Add more examples and API documentation
4. **Performance**: Optimize rendering and memory usage
5. **Accessibility**: Add more accessibility features
6. **Internationalization**: Add i18n support if needed
## Build and Development
```bash
# Install dependencies
npm install
# Build the component
npm run build
# Development mode
npm run dev
# Clean build
npm run clean
```
## Distribution
The component is designed to be published as an npm package:
```bash
npm publish
```
This will make it available for other React applications to use via:
```bash
npm install myroom-react
```