All Projects → spite → Three.meshline

spite / Three.meshline

Licence: mit
Mesh replacement for THREE.Line

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Three.meshline

Magicshader
🔮 Tiny helper for three.js to debug and write shaders
Stars: ✭ 205 (-87.53%)
Mutual labels:  webgl, glsl, threejs, shader
Sky Shader
☀️ WebGL sky and sun shader editor
Stars: ✭ 90 (-94.53%)
Mutual labels:  webgl, glsl, threejs, shader
Glsl Godrays
This module implements a volumetric light scattering effect(godrays)
Stars: ✭ 155 (-90.57%)
Mutual labels:  webgl, glsl, shader
Three.phenomenon
⭐️ A tiny wrapper around three.js built for high-performance WebGL experiences.
Stars: ✭ 338 (-79.44%)
Mutual labels:  webgl, threejs, shader
Polygon Shredder
The polygon shredder that takes many cubes and turns them into confetti
Stars: ✭ 686 (-58.27%)
Mutual labels:  webgl, glsl, threejs
Aladino
🧞‍♂️ Your magic WebGL carpet
Stars: ✭ 225 (-86.31%)
Mutual labels:  webgl, glsl, shader
Sketch
Explorations on cross-hatching, engraving, and similar non-photorealistic rendering.
Stars: ✭ 136 (-91.73%)
Mutual labels:  webgl, glsl, threejs
Threejs Starter
Stars: ✭ 89 (-94.59%)
Mutual labels:  webgl, glsl, threejs
Curtainsjs
curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.
Stars: ✭ 1,039 (-36.8%)
Mutual labels:  webgl, glsl, shader
Solarsys
Realistic Solar System simulation with three.js
Stars: ✭ 49 (-97.02%)
Mutual labels:  webgl, glsl, threejs
Wagner
Effects composer for three.js
Stars: ✭ 930 (-43.43%)
Mutual labels:  webgl, glsl, threejs
Glsl Worley
Worley noise implementation for WebGL shaders
Stars: ✭ 66 (-95.99%)
Mutual labels:  webgl, glsl, shader
Glsl Grid
Draws an antialiased grid along the X/Y/Z direction of a mesh.
Stars: ✭ 57 (-96.53%)
Mutual labels:  webgl, glsl, shader
3d Game Shaders For Beginners
🎮 A step-by-step guide to implementing SSAO, depth of field, lighting, normal mapping, and more for your 3D game.
Stars: ✭ 11,698 (+611.56%)
Mutual labels:  webgl, glsl, shader
Sketch Threejs
Interactive sketches made with three.js.
Stars: ✭ 1,227 (-25.36%)
Mutual labels:  webgl, threejs
Godot Particle Dof
Bokeh-esque particle depth of field in Godot 3.0
Stars: ✭ 77 (-95.32%)
Mutual labels:  glsl, shader
Lol Heroes
LOL 3D gallery based on WebGL
Stars: ✭ 82 (-95.01%)
Mutual labels:  webgl, threejs
Threejs Es6 Webpack Starter
Three.js ES6 starter project with a sane webpack configuration
Stars: ✭ 85 (-94.83%)
Mutual labels:  glsl, threejs
Phenomenon Px
⚡️ The fastest way to create pixel shaders.
Stars: ✭ 77 (-95.32%)
Mutual labels:  webgl, shader
Three Forcegraph
Force-directed graph as a ThreeJS 3d object
Stars: ✭ 84 (-94.89%)
Mutual labels:  webgl, threejs

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 Geometry or BufferGeometry looking up the vertices in it.

const geometry = new THREE.Geometry();
for (let j = 0; j < Math.PI; j += 2 * Math.PI / 100) {
	const v = new THREE.Vector3(Math.cos(j), Math.sin(j), 0);
	geometry.vertices.push(v);
}
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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].