Git Product home page Git Product logo

svg-mesh-3d's Introduction

svg-mesh-3d

stable

[

](http://mattdesl.github.io/svg-mesh-3d/)

(ThreeJS demo) - (source)

A high-level module to convert a SVG <path> string into a 3D triangulated mesh. Best suited for silhouettes, like font icon SVGs.

Built on top of cdt2d by @mikolalysenko, and various other modules.

Install

npm install svg-mesh-3d --save

Example

Here is an example using a simple path:

var svgMesh3d = require('svg-mesh-3d')

var path = 'M305.214,374.779c2.463,0,3.45,0.493...'
var mesh = svgMesh3d(path)

The returned mesh is a 3D indexed "simplicial complex" that can be used in ThreeJS, StackGL, etc. It uses arrays for vectors and follows the format:

{
  positions: [ [x1, y1, z1], [x2, y2, z2], ... ],
  cells: [ [a, b, c], [d, e, f] ]
}

Or, to load a mesh in the browser from an SVG file:

var loadSvg = require('load-svg')
var parsePath = require('extract-svg-path').parse
var svgMesh3d = require('svg-mesh-3d')

loadSvg('svg/logo.svg', function (err, svg) {
  if (err) throw err

  var svgPath = parsePath(svg)
  var mesh = svgMesh3d(svgPath, {
    delaunay: false,
    scale: 4
  })
})

Demos

Source:

To run the demos from source:

git clone https://github.com/mattdesl/svg-mesh-3d.git
cd svg-mesh-3d

# install dependencies
npm install

# ThreeJS demo
npm run start

# Canvas2D demo
npm run 2d

Usage

mesh = svgMesh3d(svgPath, [opt])

Triangulates the svgPath string into a 3D simplicial complex. The positions in the 3D mesh are normalized to a -1.0 ... 1.0 range.

Options:

  • delaunay (default true)
    • whether to use Delaunay triangulation
    • Delaunay triangulation is slower, but looks better
  • clean (default true)
    • whether to run the mesh through clean-pslg
    • slower, but often needed for correct triangulation
  • simplify (default 0)
    • a number, the distance threshold for simplication before triangulation (in pixel space)
    • higher number can produce faster triangulation
  • scale (default 1)
    • a positive number, the scale at which to approximate the curves from the SVG paths
    • higher number leads to smoother corners, but slower triangulation
  • normalize (default true) a boolean, whether to normalize the positions to -1 .. 1
  • randomization (default 0)
    • a positive number, the amount of extra points to randomly add within the bounding box before triangulation
    • higher number can lead to a nicer aesthetic, but slower triangulation

Other options are passed to cdt2d, such as exterior (default false) and interior (default true).

See Also

License

MIT, see LICENSE.md for details.

svg-mesh-3d's People

Contributors

mattdesl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svg-mesh-3d's Issues

min file

It would be nice to have a link to a minified file too

Change shader color

Hi @mattdesl great job with this library.

I was wondering if it possible to change the color of the triangulated mesh: I've tried to change it in ShaderMaterial but I always have a #ff0000 output in browser.

Thank you in advance,
Marco

Add support for converting svg color fills

I'm trying without much success to convert contours with solid fills into triangulated meshes, while retaining the fill from the original contours.
I've been trying to achieve this with the Tess2 library but cannot get it to work 100% of the time.
Any ideas how this could be implemented?

Buddle.js

Hi,
your svg-mesh-3d is great but if i download it, not work because miss the buddle.js file.
Is correct?

Is possible to have this file not minimized?

ThKs a lot

holes in the mesh

The AFrame plugin
https://github.com/7dir/aframe-svgfile-component/
uses this svg-mesh-3d library to get SVG into Three.js geometry.

I encountered the problem that overlapping paths creates holes in the mesh.

overlaptest.svg.txt
hole

The AFrame plugin is a bit rough because it simple extracts all the "d=" attributes from the SVG and aggreagtes them together into one long string, which it passes to svg-mesh-3d. So this includes plenty of M and Z commands. I suspect it's these M and Z commands that are confusing svg-mesh-3d.

I changed the AFrame plugin to treat each svg separately, passing them one at a time to svg-mesh-3d and getting a mesh back, then creating one Three.js Object3d for each . This works perfectly.

So I think there's a bug in how svg-mesh-3d handles complex "d" strings with M and Z's.

problem with SVG without closed paths

I am trying to render the following SVG, which does not have closed paths. But, the result I am getting is very different. Can you help me out, please?

My svg:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180.97 289.14"><defs><style>.cls-1,.cls-2,.cls-3,.cls-4{fill:none;stroke:#010101;}.cls-1,.cls-2,.cls-3{stroke-linejoin:round;}.cls-2{stroke-width:0.5px;stroke-dasharray:1.98 0.99;}.cls-3{stroke-width:0.75px;}</style></defs><title>ALine Mini</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-1" d="M149.83,12.14S139,66.77,155.41,82.79"/><line class="cls-1" x1="118.18" y1="0.47" x2="150.26" y2="12.3"/><path class="cls-1" d="M31.14,12.14S42,66.77,25.56,82.79"/><line class="cls-1" x1="62.79" y1="0.47" x2="30.71" y2="12.3"/><path class="cls-2" d="M147.34,11.34c-1.83,10.83-10.33,60.33,7.83,74.5"/><path class="cls-2" d="M33.67,11.18c2.5,14.5,9.33,60.67-7.5,74.08"/><line class="cls-3" x1="118.01" y1="3.86" x2="148.83" y2="15.14"/><line class="cls-3" x1="62.96" y1="3.86" x2="32.14" y2="15.14"/><path class="cls-4" d="M155.35,82.42C153.4,95.5,146,118,146,145.17c0,9.59,3.88,22,7.18,35.57C159.92,208.42,180.48,289,180.48,289"/><path class="cls-4" d="M.48,289s20.44-80.6,27.3-108.28c3.36-13.54,7.15-26,7.17-35.57C35,118,27.57,95.5,25.62,82.42"/><path class="cls-3" d="M27.17,181.3a409.82,409.82,0,0,0,63.5,5,389,389,0,0,0,62.53-5"/></g></g></svg>

My result attached-

screen shot 2017-03-02 at 6 19 30 am

bundle size

The bundle size is fairly large (101kb).

Biggest dependencies:

  • clean-pslg 81kb
  • cdt2d 15kb

Taking a look in disc, some heavy hitters are box-intersect, bn.js, and of course buffer.

/cc @mikolalysenko - what do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.