npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-model-viewer

v1.0.4

Published

A simple 3D model viewer for embedding into websites

Downloads

359

Readme

simple-model-viewer

License npm version

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

  1. Introduction
  2. Features
  3. Installation
  4. Quick Start
  5. Examples
  6. License

🌟 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.

  1. 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
  2. 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 is true.
  • backgroundColor (number or string, optional): The background color of the scene. Default is 0xffffff (white).
  • enableControls (boolean, optional): Enable or disable user controls (orbiting, zooming). Default is true.
  • lighting (object, optional): Customize lighting settings.
    • ambientIntensity (number, optional): Intensity of ambient light. Default is 0.5.
  • 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.