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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@masatomakino/colorable-merged-model

v0.7.4

Published

Mesh and Edge lines in three.js that can change color after geometry is merged

Readme

colorable-merged-model

MIT License npm version GitHub

A TypeScript library for Three.js that provides colorable merged geometries with both mesh bodies and edge lines. This library allows dynamic color changes on individual geometries after they have been merged, which is essential for performance optimization in Three.js applications.

Features

  • Performance Optimization: Merge multiple geometries into single buffers for better rendering performance
  • Dynamic Color Control: Change colors of individual geometries even after merging
  • Animated Color Transitions: Built-in color animation system with customizable easing functions
  • Edge Line Support: Automatic edge line generation with configurable detail levels
  • TypeScript Support: Full TypeScript definitions for better development experience

Demo

Demo Page

Installation

npm install @masatomakino/colorable-merged-model

Quick Start

Here's a simple example to get you started:

import * as THREE from "three";
import {
  ColorableMergedView,
  ColorableMergedBodyMaterial,
  ColorableMergedEdgeMaterial,
  TweenableColorMap,
} from "@masatomakino/colorable-merged-model";

// Create a Three.js scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000,
);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

async function createColorableMergedModel() {
  // Create the main view with body and edge options
  const view = new ColorableMergedView({
    bodyOption: { color: [1, 1, 1, 0.2] }, // White with 20% opacity
    edgeOption: { color: [1, 1, 1, 0.8] }, // White with 80% opacity
  });

  // Create color maps for managing colors
  const bodyColors = new TweenableColorMap("colors");
  const edgeColors = new TweenableColorMap("colors");

  // Create some box geometries
  const geometry1 = new THREE.BoxGeometry(1, 1, 1);
  const geometry2 = new THREE.BoxGeometry(1, 1, 1);

  // Position the second box
  geometry2.translate(2, 0, 0);

  // Add geometries with unique IDs
  const id1 = 1;
  const id2 = 2;

  // Add colors for each geometry
  bodyColors.add([1, 0, 0, 0.2], id1); // Red body
  bodyColors.add([0, 1, 0, 0.2], id2); // Green body

  edgeColors.add([1, 0, 0, 0.8], id1); // Red edges
  edgeColors.add([0, 1, 0, 0.8], id2); // Green edges

  // Add geometries to the view
  await view.body?.geometryMerger.add(geometry1, bodyColors, id1);
  await view.body?.geometryMerger.add(geometry2, bodyColors, id2);

  await view.edge?.geometryMerger.add(geometry1, edgeColors, id1);
  await view.edge?.geometryMerger.add(geometry2, edgeColors, id2);

  // Merge all geometries
  await view.merge();

  // Apply materials
  if (view.body) {
    view.body.material = new ColorableMergedBodyMaterial(bodyColors);
  }
  if (view.edge) {
    view.edge.material = new ColorableMergedEdgeMaterial(edgeColors);
  }

  // Add to scene
  scene.add(view);

  // Animate colors (optional)
  setTimeout(() => {
    bodyColors.changeColor([0, 0, 1, 0.2], id1, { duration: 2000 }); // Change to blue
    edgeColors.changeColor([0, 0, 1, 0.8], id1, { duration: 2000 });
  }, 1000);

  return view;
}

// Initialize and render
createColorableMergedModel().then(() => {
  camera.position.z = 5;

  function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
  }
  animate();
});

API Reference

ColorableMergedView

The main container class that combines body and edge components.

const view = new ColorableMergedView({
  bodyOption?: {
    color: [number, number, number, number]; // RGBA values (0-1)
  };
  edgeOption?: {
    color: [number, number, number, number]; // RGBA values (0-1)
    edgeDetail?: number; // Default: 7
    useFastEdgesGeometry?: boolean;
  };
});

Key Methods:

  • merge(): Promise<void> - Merges all added geometries
  • getGeometryID(name: string): number - Extracts geometry ID from name (customizable)

Properties:

  • body?: ColorableMergedBody - Mesh body component
  • edge?: ColorableMergedEdge - Edge line component

TweenableColorMap

Manages colors and animations for geometries.

const colorMap = new TweenableColorMap("colors"); // "colors" is the uniform name in shaders

// Add a color for a geometry
colorMap.add([1, 0, 0, 1], geometryId); // Red color

// Change color with animation
colorMap.changeColor([0, 1, 0, 1], geometryId, {
  duration: 1000, // Animation duration in milliseconds
  easing: (t) => t, // Easing function
  now: Date.now(), // Start time
});

// Get color for a geometry
const color = colorMap.get(geometryId);

Materials

ColorableMergedBodyMaterial

Material for mesh bodies with shader-based color control.

const bodyMaterial = new ColorableMergedBodyMaterial(colorMap, {
  blending?: THREE.Blending;
  side?: THREE.Side;
});

ColorableMergedEdgeMaterial

Material for edge lines with shader-based color control.

const edgeMaterial = new ColorableMergedEdgeMaterial(colorMap);

Advanced Usage

Creating Multiple Geometries

async function createMultipleBoxes() {
  const view = new ColorableMergedView({
    bodyOption: { color: [1, 1, 1, 0.2] },
    edgeOption: { color: [1, 1, 1, 0.8] },
  });

  const bodyColors = new TweenableColorMap("colors");
  const edgeColors = new TweenableColorMap("colors");

  const promises = [];

  // Create a grid of boxes
  for (let x = 0; x < 5; x++) {
    for (let y = 0; y < 5; y++) {
      const geometry = new THREE.BoxGeometry(0.8, 0.8, 0.8);
      geometry.translate(x * 2 - 4, y * 2 - 4, 0);

      const id = x * 5 + y;
      const hue = (id / 25) * 360; // Different hue for each box
      const color = new THREE.Color().setHSL(hue / 360, 0.7, 0.5);

      bodyColors.add([color.r, color.g, color.b, 0.2], id);
      edgeColors.add([color.r, color.g, color.b, 0.8], id);

      promises.push(
        view.body?.geometryMerger.add(geometry, bodyColors, id),
        view.edge?.geometryMerger.add(geometry, edgeColors, id),
      );
    }
  }

  await Promise.all(promises);
  await view.merge();

  // Apply materials
  if (view.body) {
    view.body.material = new ColorableMergedBodyMaterial(bodyColors);
  }
  if (view.edge) {
    view.edge.material = new ColorableMergedEdgeMaterial(edgeColors);
  }

  return view;
}

Custom Geometry ID Extraction

By default, the library expects geometry names to follow the pattern {prefix}_{category}_{id}. You can customize this:

class CustomColorableMergedView extends ColorableMergedView {
  getGeometryID(name: string): number {
    // Custom logic to extract ID from geometry name
    const match = name.match(/customPattern_(\d+)/);
    return match ? parseInt(match[1]) : 0;
  }
}

Color Animation Events

Each geometry's color is managed by a TweenableColor instance from the @masatomakino/tweenable-color module. You can access individual TweenableColor instances to listen to animation events:

const colorMap = new TweenableColorMap("colors");
const geometryId = 1;

// Add a color for the geometry
colorMap.add([1, 0, 0, 1], geometryId);

// Get the TweenableColor instance for event handling
const tweenableColor = colorMap.get(geometryId);

// Start the animation
colorMap.changeColor([0, 1, 0, 1], geometryId, { duration: 2000 });

For detailed information about available events and event handling, please refer to the @masatomakino/tweenable-color documentation.

Performance Considerations

  • Geometry Merging: Always call view.merge() after adding all geometries and before applying materials
  • Batch Operations: Add multiple geometries in parallel using Promise.all() for better performance
  • Memory Management: Dispose of geometries after merging if they're no longer needed

Browser Support

This library supports modern browsers with WebGL support. For WebGPU features, ensure your target browsers support WebGPU.

License

MIT License - see the LICENSE file for details.