• Stars
    star
    2,147
  • Rank 21,492 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mesh replacement for THREE.Line

MeshLine

Mesh replacement for THREE.Line

Instead of using GL_LINE, it uses a strip of triangles billboarded. Some examples:

Demo Graph Spinner SVG Shape Shape

  • Demo: play with the different settings of materials
  • Graph: example of using MeshLine to plot graphs
  • Spinner: example of dynamic MeshLine with texture
  • SVG: example of MeshLine rendering SVG Paths
  • Shape: example of MeshLine created from a mesh
  • Birds: example of MeshLine.advance() by @caramelcode (Jared Sprague) and @mwcz (Michael Clayton)

How to use

  • Include script
  • Create an array of 3D coordinates
  • Create a MeshLine and assign the points
  • Create a MeshLineMaterial
  • Use MeshLine and MeshLineMaterial to create a THREE.Mesh

Include the script

Include script after THREE is included

<script src="THREE.MeshLine.js"></script>

or use npm to install it

npm i three.meshline

and include it in your code (don't forget to require three.js)

const THREE = require('three');
const MeshLine = require('three.meshline').MeshLine;
const MeshLineMaterial = require('three.meshline').MeshLineMaterial;
const MeshLineRaycast = require('three.meshline').MeshLineRaycast;

or

import * as THREE from 'three';
import { MeshLine, MeshLineMaterial, MeshLineRaycast } from 'three.meshline';
Create an array of 3D coordinates

First, create the list of numbers that will define the 3D points for the line.

const points = [];
for (let j = 0; j < Math.PI; j += (2 * Math.PI) / 100) {
  points.push(Math.cos(j), Math.sin(j), 0);
}

MeshLine also accepts a BufferGeometry looking up the vertices in it.

const points = [];
for (let j = 0; j < Math.PI; j += 2 * Math.PI / 100) {
  points.push(new THREE.Vector3(Math.cos(j), Math.sin(j), 0));
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new MeshLine();
line.setGeometry(geometry);
Create a MeshLine and assign the points

Once you have that, you can create a new MeshLine, and call .setPoints() passing the list of points.

const line = new MeshLine();
line.setPoints(points);

Note: .setPoints accepts a second parameter, which is a function to define the width in each point along the line. By default that value is 1, making the line width 1 * lineWidth in the material.

// p is a decimal percentage of the number of points
// ie. point 200 of 250 points, p = 0.8
line.setPoints(geometry, p => 2); // makes width 2 * lineWidth
line.setPoints(geometry, p => 1 - p); // makes width taper
line.setPoints(geometry, p => 2 + Math.sin(50 * p)); // makes width sinusoidal
Create a MeshLineMaterial

A MeshLine needs a MeshLineMaterial:

const material = new MeshLineMaterial(OPTIONS);

By default it's a white material of width 1 unit.

MeshLineMaterial has several attributes to control the appereance of the MeshLine:

  • map - a THREE.Texture to paint along the line (requires useMap set to true)
  • useMap - tells the material to use map (0 - solid color, 1 use texture)
  • alphaMap - a THREE.Texture to use as alpha along the line (requires useAlphaMap set to true)
  • useAlphaMap - tells the material to use alphaMap (0 - no alpha, 1 modulate alpha)
  • repeat - THREE.Vector2 to define the texture tiling (applies to map and alphaMap - MIGHT CHANGE IN THE FUTURE)
  • color - THREE.Color to paint the line width, or tint the texture with
  • opacity - alpha value from 0 to 1 (requires transparent set to true)
  • alphaTest - cutoff value from 0 to 1
  • dashArray - the length and space between dashes. (0 - no dash)
  • dashOffset - defines the location where the dash will begin. Ideal to animate the line.
  • dashRatio - defines the ratio between that is visible or not (0 - more visible, 1 - more invisible).
  • resolution - THREE.Vector2 specifying the canvas size (REQUIRED)
  • sizeAttenuation - makes the line width constant regardless distance (1 unit is 1px on screen) (0 - attenuate, 1 - don't attenuate)
  • lineWidth - float defining width (if sizeAttenuation is true, it's world units; else is screen pixels)

If you're rendering transparent lines or using a texture with alpha map, you should set depthTest to false, transparent to true and blending to an appropriate blending mode, or use alphaTest.

Use MeshLine and MeshLineMaterial to create a THREE.Mesh

Finally, we create a mesh and add it to the scene:

const mesh = new THREE.Mesh(line, material);
scene.add(mesh);

You can optionally add raycast support with the following.

mesh.raycast = MeshLineRaycast;

Declarative use

THREE.meshline can be used declaritively. This is how it would look like in react-three-fiber. You can try it live here.

import { extend, Canvas } from 'react-three-fiber'
import { MeshLine, MeshLineMaterial, MeshLineRaycast } from 'three.meshline'

extend({ MeshLine, MeshLineMaterial })

function Line({ points, width, color }) {
  return (
    <Canvas>
      <mesh raycast={MeshLineRaycast}>
        <meshLine attach="geometry" points={points} />
        <meshLineMaterial
          attach="material"
          transparent
          depthTest={false}
          lineWidth={width}
          color={color}
          dashArray={0.05}
          dashRatio={0.95}
        />
      </mesh>
    </Canvas>
  )
}

Dynamic line widths can be set along each point using the widthCallback prop.

<meshLine attach='geometry' points={points} widthCallback={pointWidth => pointWidth * Math.random()} />

TODO

  • Better miters
  • Proper sizes

Support

Tested successfully on

  • Chrome OSX, Windows, Android
  • Firefox OSX, Windows, Anroid
  • Safari OSX, iOS
  • Internet Explorer 11 (SVG and Shape demo won't work because they use Promises)
  • Opera OSX, Windows

References

License

MIT licensed

Copyright (C) 2015-2016 Jaume Sanchez Elias, http://www.clicktorelease.com

More Repositories

1

ccapture.js

A library to capture canvas-based animations at a fixed framerate
JavaScript
3,419
star
2

Wagner

Effects composer for three.js
JavaScript
1,041
star
3

looper

Looperepool
JavaScript
954
star
4

polygon-shredder

The polygon shredder that takes many cubes and turns them into confetti
JavaScript
807
star
5

ShaderEditorExtension

Google Chrome DevTools extension to live edit WebGL GLSL shaders
JavaScript
607
star
6

FaceMeshFaceGeometry

FaceMeshFaceGeometry for FaceMesh
JavaScript
381
star
7

WebVR-Extension

Chrome DevTools extension to emulate WebVR API
JavaScript
301
star
8

android-lens-blur-depth-extractor

JavaScript
291
star
9

virtual-webcam

JavaScript
268
star
10

rstats

rStats
JavaScript
243
star
11

sketch

Explorations on cross-hatching, engraving, and similar non-photorealistic rendering.
JavaScript
222
star
12

codevember-2021

JavaScript
203
star
13

THREE.CubemapToEquirectangular

Export an equirectangular panorama image from a three.js scene
JavaScript
188
star
14

WebAudioExtension

Google Chrome DevTools extension to view and hopefully interact with the routing graph of Web Audio API
JavaScript
183
star
15

ThreeJSEditorExtension

Three.js Editor Extension for Google Chrome
JavaScript
182
star
16

THREE.DecalGeometry

Decals for three.js
JavaScript
149
star
17

fuck-2020

A browser demo to send away 2020.
JavaScript
140
star
18

codevember-2017

JavaScript
139
star
19

GSVPano.js

Google Street View Panorama Util
JavaScript
136
star
20

THREE.AugmentedConsole.js

Augmented methods for console.log
JavaScript
134
star
21

THREE.FBOHelper

FrameBuffer Object inspector for three.js
JavaScript
127
star
22

genuary-2022

JavaScript
114
star
23

cross-hatching

HTML
108
star
24

codevember-2016

JavaScript
90
star
25

cruciform

cru·ci·form
JavaScript
88
star
26

PanomNom.js

Panorama fetching tools in JavaScript
JavaScript
86
star
27

codevember-2022

JavaScript
83
star
28

spherical-environment-mapping

Spherical Environment Mapping GLSL Shader
JavaScript
79
star
29

CSS3DClouds

CSS 3D Clouds
HTML
78
star
30

Maf.js

Maf
JavaScript
77
star
31

Storyline.js

Storyline - generic sequencer for JavaScript projects
JavaScript
75
star
32

THREE.UpdatableTexture

JavaScript
71
star
33

depth-player

Depth player for Android Lens Blur images
JavaScript
61
star
34

SolariDisplay

A Split-Flap Display with CSS and JavaScript
JavaScript
57
star
35

THREE.EquirectangularToCubemap

Convert an equirectangular panorama into a cubemap texture
JavaScript
57
star
36

ShaderEditor

Live GLSL WebGL Shader Editor
JavaScript
55
star
37

vertex-displacement-noise-3d-webgl-glsl-three-js

JavaScript
47
star
38

webxr-trails

JavaScript
41
star
39

ConcatSoundLoader

Concatenated Sound Files Loader | Typed Arrays and Web Audio API
HTML
35
star
40

perlin-experiments

HTML
33
star
41

sdf-physics

SDF Physics
JavaScript
33
star
42

npr-shading

JavaScript
28
star
43

infinite-gift

Christmas Experiment 2018
JavaScript
28
star
44

THREE.ConstantSpline

Constant-time B-Spline
HTML
27
star
45

serviceworker-svg

ServiceWorker-SVG
HTML
26
star
46

makio-torus

JavaScript
26
star
47

baubles

generative baubles
JavaScript
24
star
48

AugmentedCompileShader.js

Augmented WebGLRenderingContext.prototype.compileShader
JavaScript
24
star
49

css-filters-devtools-extension

CSS Filters DevTools Extension
JavaScript
22
star
50

PerfMeter

JavaScript
22
star
51

FloatingShinyKnot

JavaScript
22
star
52

futurejs-2014

Web Audio presentation for Future JS Barcelona 2014
HTML
22
star
53

chrome-sketchtab

New tab extension with a code editor
JavaScript
21
star
54

THREE.GamepadControls

Camera controls for three.js using the Gamepad API
JavaScript
21
star
55

THREE.Text

THREE.TextMesh for three.js
JavaScript
19
star
56

solskogen-2020

JavaScript
18
star
57

resonate-2015

Workshop contents | Resonate 2015
JavaScript
18
star
58

shadow-mapping

Shadow mapping in GLSL
JavaScript
17
star
59

scotlandjs-2014

JavaScript
16
star
60

snipsnap

SnipSnap: Snippet Snapshot tool
CSS
16
star
61

GLStateCache.js

State cache for WebGL Rendering Context
JavaScript
15
star
62

unmute

UI for Chrome autoplay policy
JavaScript
15
star
63

mira-lab-2013

Contents for MIRA Lab 2013 Workshop
15
star
64

THREE.ShaderTexture

ShaderTexture, a helper to use procedurally generated texture
JavaScript
13
star
65

lines-on-sphere

JavaScript
13
star
66

genuary-2023

JavaScript
12
star
67

explore-with-me

Explore with me - Christmas Experiments 2017
HTML
12
star
68

halloween-2016

JavaScript
10
star
69

shield-vfx

JavaScript
10
star
70

dual-boids

Dual Boids
JavaScript
10
star
71

3dwebfest-2016

Ambient effect for 3DWebFest
JavaScript
9
star
72

halloween-2022

JavaScript
8
star
73

relooper

more loops, old loops, new loops
JavaScript
8
star
74

WebVR-Recorder

WebVR motion recorder and player
JavaScript
7
star
75

BlockyEarth

JavaScript
7
star
76

speech-jammer

Speech Jammer using getUserMedia and Web Audio API
JavaScript
7
star
77

graphicalweb-2016

JavaScript
6
star
78

linkognito

JavaScript
5
star
79

voxel-volume

JavaScript
5
star
80

getting-started-threejs

JavaScript
5
star
81

google-images-view-image-button

Adds a View Image button to Google Images results page
JavaScript
5
star
82

WebVR-Physics

WebVR Physics experiment
JavaScript
5
star
83

FullStackFest-2015

The Road to WebVR - FullStackFest 2015
JavaScript
5
star
84

list-issues

List all issues from Github repos from the logged in user
JavaScript
5
star
85

audio-cover

JavaScript
4
star
86

scotlandjs-2015

ScotlandJS 2015 - Slides and Demo
HTML
4
star
87

bumpy-metaballs

JavaScript
4
star
88

HexViewerExtension

JavaScript
3
star
89

adventofcode2021

JavaScript
3
star
90

powermate-input

Exploration using a Powermate as input device in the browser
JavaScript
3
star
91

wu2021-python

Python
2
star
92

adventofcode2023

JavaScript
2
star
93

floating-shiny-knot

Floating Shiny Knot
JavaScript
2
star
94

canvas-capture

JavaScript
2
star
95

service-worker-require

require() support with service worker
HTML
2
star
96

AdventOfCode2019

Advent of Code 2019
JavaScript
2
star
97

soundcloud-pi

Solution to souncloud challenge
PHP
2
star
98

webxr

JavaScript
2
star
99

nonocache-sw.js

JavaScript
2
star
100

get-into

JavaScript
2
star