simple-model-viewer
v1.0.4
Published
A simple 3D model viewer for embedding into websites
Downloads
359
Maintainers
Readme
simple-model-viewer
A 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
📚 Table of Contents
🌟 Introduction
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.
🚀 Features
- 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.
Installation
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.
Using npm
This method is ideal if you're using bundlers like Webpack, Vite, or Parcel in your project.
Initialize Your Project
If you haven't already set up a project, start by creating a new directory and initializing it with npm:
mkdir my-3d-project cd my-3d-project npm init -y
Install Dependencies
npm install three lil-gui simple-model-viewer
🌐 Using CDN
This method is perfect for quick setups or integrating into static websites without a build process.
Include the Necessary Scripts
Add the following <script>
tags to your HTML file:
<!-- Include Three.js via CDN -->
<script src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<!-- Include lil-gui via CDN -->
<script src="https://unpkg.com/[email protected]/dist/lil-gui.min.js"></script>
<!-- Include simple-model-viewer UMD build via CDN -->
<script src="https://unpkg.com/[email protected]/dist/browser/model-viewer.browser.umd.js"></script>
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.
With npm and a Bundler
Project Structure
my-3d-project/
├── models/
│ ├── model1.glb
│ └── model2.glb
├── index.html
├── package.json
└── vite.config.js (if using Vite)
Create index.html
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Model Viewer Example</title>
<style>
#my-model-viewer {
width: 600px;
height: 600px;
}
</style>
</head>
<body>
<div id="my-model-viewer"></div>
<script type="module">
import ModelViewer from "simple-model-viewer";
const viewer = new ModelViewer({
containerId: "my-model-viewer",
modelsDirectory: "/models/",
models: ["sheep.glb", "chalice.glb", "ore.glb"],
});
</script>
</body>
</html>
Configure Your Bundler (Optional)
If you're using Vite, create a vite.config.js
:
// vite.config.js
import { defineConfig } from "vite";
export default defineConfig({
server: {
open: true,
},
build: {
outDir: "dist",
},
});
Run Your Project
Using Vite:
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.
With CDN
Create Your HTML File
<!-- cdn-example.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Simple Model Viewer CDN Example</title>
<style>
body {
margin: 0;
overflow: hidden;
}
#my-model-viewer {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div id="my-model-viewer"></div>
<!-- Include Three.js via CDN -->
<script src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<!-- Include lil-gui via CDN -->
<script src="https://unpkg.com/[email protected]/dist/lil-gui.min.js"></script>
<!-- Include simple-model-viewer UMD build via CDN -->
<script src="https://unpkg.com/[email protected]/dist/browser/model-viewer.browser.umd.js"></script>
<script>
// Initialize the ModelViewer
const viewer = new ModelViewer({
containerId: "my-model-viewer",
modelsDirectory: "/models/",
models: ["model1.glb", "model2.glb"],
showGui: true,
});
</script>
</body>
</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
:
npm install -g http-server
http-server .
Using Python's SimpleHTTPServer:
# 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. - modelsDirectory (
string
, required): The relative path to the directory containing your model files. - models (
array of strings
, required): An array of model filenames (e.g.,['model1.glb', 'model2.glb']
). - showGui (
boolean
, optional): Whether to display the GUI for tweaking settings. Default istrue
. - backgroundColor (
number
orstring
, optional): The background color of the scene. Default is0xffffff
(white). - enableControls (
boolean
, optional): Enable or disable user controls (orbiting, zooming). Default istrue
. - lighting (
object
, optional): Customize lighting settings.- ambientIntensity (
number
, optional): Intensity of ambient light. Default is0.5
.
- ambientIntensity (
- onModelLoad (
function
, optional): Callback function triggered when a model is successfully loaded.
Example:
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);
},
});
Examples
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
License
This project is licensed under the MIT License.