A simple 3D model viewer for embedding into websites
npm install simple-model-viewerA simple and lightweight 3D model viewer for quickly embedding interactive models into your websites. An easy method for showcasing portfolios, adding some flair to landing pages, or displaying product models without the hassle of extensive coding. Example
1. Introduction
2. Features
3. Installation
- Using npm
- Using CDN
4. Quick Start
- With npm and a Bundler
- With CDN
5. Examples
6. License
---
simple-model-viewer is designed for developers and creatives who want to effortlessly integrate 3D models into their websites. Whether you're building a portfolio to showcase your modeling work or enhancing your site's aesthetics with engaging and fun visuals, this library provides a straightforward solution without the complexity.
---
- Ease of Use: Minimal setup and configuration required.
- Lightweight: Optimized for quick loading without compromising performance.
- Interactive Controls: Rotate/ zoom your models using mouse controls.
- GUI Integration: Optional graphical user interface.
- Multiple Model Support: Display one or multiple models.
---
You can integrate simple-model-viewer into your project using npm for module bundler environments or via a CDN for direct inclusion in HTML files.
This method is ideal if you're using bundlers like Webpack, Vite, or Parcel in your project.
1. Initialize Your Project
If you haven't already set up a project, start by creating a new directory and initializing it with npm:
``bash`
mkdir my-3d-project
cd my-3d-project
npm init -y
2. Install Dependencies
``
npm install three lil-gui simple-model-viewer
This method is perfect for quick setups or integrating into static websites without a build process.
#### Include the Necessary Scripts
Add the following
`
_Note:_ Ensure that these scripts are included in the order shown to handle dependencies correctly.
š§ Quick Start
Choose the method that best fits your project setup.
#### Project Structure
``
my-3d-project/
āāā models/
ā āāā model1.glb
ā āāā model2.glb
āāā index.html
āāā package.json
āāā vite.config.js (if using Vite)
#### Create index.html
`html
`
#### Configure Your Bundler (Optional)
If you're using Vite, create a vite.config.js:
`javascript
// vite.config.js
import { defineConfig } from "vite";
export default defineConfig({
server: {
open: true,
},
build: {
outDir: "dist",
},
});
`
#### Run Your Project
##### Using Vite:
`bash`
npm install vite --save-dev
npx vite
##### Using a Bundler of Your Choice:
Follow the specific instructions for your bundler to serve or build your project.
#### View in Browser
Navigate to http://localhost:3000 (or the port your bundler specifies) to see the simple-model-viewer in action.
#### Create Your HTML File
`html
`
#### Serve Your Models
Ensure that the /models/ directory is accessible from your server and contains the .glb or .gltf files you want to display.
#### Open in Browser
Serve your HTML file using a simple HTTP server to bypass CORS restrictions (many browsers block local file imports).
##### Using http-server:
`bash`
npm install -g http-server
http-server .
##### Using Python's SimpleHTTPServer:
`bash`For Python 3.x
python -m http.server
Navigate to http://localhost:8080/cdn-example.html (or the port specified by your server) to view the example.
š ļø Configuration Options
When initializing the ModelViewer, you can customize its behavior using various options:
- containerId (string, required): The ID of the HTML element where the viewer will be rendered.string
- modelsDirectory (, required): The relative path to the directory containing your model files.array of strings
- models (, required): An array of model filenames (e.g., ['model1.glb', 'model2.glb']).boolean
- showGui (, optional): Whether to display the GUI for tweaking settings. Default is true.number
- backgroundColor ( or string, optional): The background color of the scene. Default is 0xffffff (white).boolean
- enableControls (, optional): Enable or disable user controls (orbiting, zooming). Default is true.object
- lighting (, optional): Customize lighting settings.number
- ambientIntensity (, optional): Intensity of ambient light. Default is 0.5.function
- onModelLoad (, optional): Callback function triggered when a model is successfully loaded.
Example:
`javascript`
const viewer = new ModelViewer({
containerId: "my-model-viewer",
modelsDirectory: "./models/",
models: ["model1.glb", "model2.glb"],
showGui: true,
backgroundColor: 0x000000, // Black background
enableControls: true,
lighting: {
ambientIntensity: 0.7,
},
onModelLoad: (gltf) => {
console.log("Model loaded:", gltf);
},
});
Two example set ups are provided in the examples` directory. You can see live examples for a portfolio here and for a landing page here
This project is licensed under the MIT License.