R3F Transform Plugin
A Vite plugin for React Three Fiber that enables scene editing capabilities. The plugin provides transform controls for meshes and automatically saves their transformations to a JSON file.
Features
- Transform controls for meshes (translate, rotate, scale)
- Auto-saves transformations to JSON file
- Mesh selection via clicking
- Transform data persistence across refreshes
- Mesh identification using unique names
Installation
npm install vite-plugin-r3f-transform
Setup
- Add the plugin to your Vite config:
import r3fTransform from 'vite-plugin-r3f-transform'
export default {
plugins: [r3fTransform()]
}
- Import and use the
DevTransformWrapper
in your R3F scene:
import { DevTransformWrapper } from 'virtual:r3f-transform'
function Scene() {
return (
<DevTransformWrapper>
<mesh name="uniqueName">
<boxGeometry />
<meshStandardMaterial />
</mesh>
</DevTransformWrapper>
)
}
Usage
Mesh Requirements
- Each mesh must have a unique
name
prop - Meshes without names will be ignored
- Duplicate names will trigger a warning
<mesh name="cube1">...</mesh>
<mesh name="cube2">...</mesh>
<mesh>...</mesh>
<mesh name="cube1">...</mesh>
<mesh name="cube1">...</mesh>
Transform Data Storage
Transformations are automatically saved to public/transform-data.json
in the following format:
{
"meshName1": {
"position": [x, y, z],
"rotation": [x, y, z],
"scale": [x, y, z]
},
"meshName2": {
"position": [x, y, z],
"rotation": [x, y, z],
"scale": [x, y, z]
}
}
Controls
- Click on a mesh to select it
- Use the transform controls to modify:
- Press
Escape
to deselect the current mesh
How It Works
- The plugin creates a virtual module that provides the
DevTransformWrapper
component - When a mesh is clicked, transform controls appear
- Changes are tracked and saved to
transform-data.json
- On scene load, saved transformations are applied to meshes based on their names
Limitations
- Relies on mesh names for identification
- Mesh names must be unique
Development
To modify or extend the plugin:
-
The plugin consists of two main parts:
- Vite plugin configuration
- DevTransformWrapper component
-
Key files:
vite-plugin-r3f-transform.js
: Main plugin filetransform-data.json
: Generated file storing transformations
Future Improvements
Potential areas for enhancement:
- UUID-based identification system
- Multi-selection support
- Undo/redo functionality
- Transform snapping
- Custom keyboard shortcuts
- Support for grouped transformations
- Transform history
- Export/import capabilities
Contributing
Feel free to submit issues and enhancement requests!
License
MIT License