three-line-2d
Advanced tools
Comparing version 1.0.9 to 1.1.0
184
index.js
@@ -1,111 +0,111 @@ | ||
var inherits = require('inherits') | ||
var getNormals = require('polyline-normals') | ||
var VERTS_PER_POINT = 2 | ||
var inherits = require('inherits'); | ||
var getNormals = require('polyline-normals'); | ||
var VERTS_PER_POINT = 2; | ||
var tmp = [0, 0] | ||
module.exports = function createLineMesh (THREE) { | ||
function LineMesh (path, opt) { | ||
if (!(this instanceof LineMesh)) { | ||
return new LineMesh(path, opt); | ||
} | ||
THREE.BufferGeometry.call(this); | ||
module.exports = function(THREE) { | ||
if (Array.isArray(path)) { | ||
opt = opt || {}; | ||
} else if (typeof path === 'object') { | ||
opt = path; | ||
path = []; | ||
} | ||
function LineMesh(path, opt) { | ||
if (!(this instanceof LineMesh)) | ||
return new LineMesh(path, opt) | ||
THREE.BufferGeometry.call(this) | ||
opt = opt || {}; | ||
if (Array.isArray(path)) { | ||
opt = opt||{} | ||
} else if (typeof path === 'object') { | ||
opt = path | ||
path = [] | ||
} | ||
this.addAttribute('position', new THREE.BufferAttribute(null, 3)); | ||
this.addAttribute('lineNormal', new THREE.BufferAttribute(null, 2)); | ||
this.addAttribute('lineMiter', new THREE.BufferAttribute(null, 1)); | ||
if (opt.distances) { | ||
this.addAttribute('lineDistance', new THREE.BufferAttribute(null, 1)); | ||
} | ||
if (typeof this.setIndex === 'function') { | ||
this.setIndex(new THREE.BufferAttribute(null, 1)); | ||
} else { | ||
this.addAttribute('index', new THREE.BufferAttribute(null, 1)); | ||
} | ||
this.update(path, opt.closed); | ||
} | ||
opt = opt||{} | ||
inherits(LineMesh, THREE.BufferGeometry); | ||
this._positions = new THREE.BufferAttribute(null, 3) | ||
this._normals = new THREE.BufferAttribute(null, 2) | ||
this._miters = new THREE.BufferAttribute(null, 1) | ||
this._indices = new THREE.BufferAttribute(null, 1) | ||
LineMesh.prototype.update = function (path, closed) { | ||
path = path || []; | ||
var normals = getNormals(path, closed); | ||
if (opt.distances) | ||
this._distances = new THREE.BufferAttribute(null, 1) | ||
if (closed) { | ||
path = path.slice(); | ||
path.push(path[0]); | ||
normals.push(normals[0]); | ||
} | ||
this.update(path, opt.closed) | ||
var attrPosition = this.getAttribute('position'); | ||
var attrNormal = this.getAttribute('lineNormal'); | ||
var attrMiter = this.getAttribute('lineMiter'); | ||
var attrDistance = this.getAttribute('lineDistance'); | ||
var attrIndex = typeof this.getIndex === 'function' ? this.getIndex() : this.getAttribute('index'); | ||
this.addAttribute('position', this._positions) | ||
this.addAttribute('lineNormal', this._normals) | ||
this.addAttribute('lineMiter', this._miters) | ||
this.addAttribute('index', this._indices) | ||
if (!attrPosition.array || | ||
(path.length !== attrPosition.array.length / 3 / VERTS_PER_POINT)) { | ||
var count = path.length * VERTS_PER_POINT; | ||
attrPosition.array = new Float32Array(count * 3); | ||
attrNormal.array = new Float32Array(count * 2); | ||
attrMiter.array = new Float32Array(count); | ||
attrIndex.array = new Uint16Array(Math.max(0, (path.length - 1) * 6)); | ||
if (opt.distances) | ||
this.addAttribute('lineDistance', this._distances) | ||
if (attrDistance) { | ||
attrDistance.array = new Float32Array(count); | ||
} | ||
} | ||
inherits(LineMesh, THREE.BufferGeometry) | ||
LineMesh.prototype.update = function(path, closed) { | ||
path = path||[] | ||
var normals = getNormals(path, closed) | ||
attrPosition.needsUpdate = true; | ||
attrNormal.needsUpdate = true; | ||
attrMiter.needsUpdate = true; | ||
if (attrDistance) { | ||
attrDistance.needsUpdate = true; | ||
} | ||
if (closed) { | ||
path = path.slice() | ||
path.push(path[0]) | ||
normals.push(normals[0]) | ||
} | ||
if (!this._positions.array || | ||
(path.length !== this._positions.array.length/3/VERTS_PER_POINT)) { | ||
var count = path.length * VERTS_PER_POINT | ||
this._positions.array = new Float32Array(count * 3) | ||
this._normals.array = new Float32Array(count * 2) | ||
this._miters.array = new Float32Array(count * 1) | ||
this._indices.array = new Uint16Array(Math.max(0, (path.length-1) * 6)) | ||
var index = 0; | ||
var c = 0; | ||
var dIndex = 0; | ||
var indexArray = attrIndex.array; | ||
if (this._distances) | ||
this._distances.array = new Float32Array(count * 1) | ||
} | ||
var useDist = Boolean(this._distances) | ||
path.forEach(function (point, pointIndex, list) { | ||
var i = index; | ||
indexArray[c++] = i + 0; | ||
indexArray[c++] = i + 1; | ||
indexArray[c++] = i + 2; | ||
indexArray[c++] = i + 2; | ||
indexArray[c++] = i + 1; | ||
indexArray[c++] = i + 3; | ||
this._positions.needsUpdate = true | ||
this._miters.needsUpdate = true | ||
this._normals.needsUpdate = true | ||
this._indices.needsUpdate = true | ||
if (useDist) | ||
this._distances.needsUpdate = true | ||
var index = 0, | ||
c = 0, | ||
dIndex = 0, | ||
indexArray = this._indices.array | ||
path.forEach(function(point, pointIndex, self) { | ||
var i = index | ||
indexArray[c++] = i + 0 | ||
indexArray[c++] = i + 1 | ||
indexArray[c++] = i + 2 | ||
indexArray[c++] = i + 2 | ||
indexArray[c++] = i + 1 | ||
indexArray[c++] = i + 3 | ||
attrPosition.setXYZ(index++, point[0], point[1], 0); | ||
attrPosition.setXYZ(index++, point[0], point[1], 0); | ||
this._positions.setXYZ(index++, point[0], point[1], 0) | ||
this._positions.setXYZ(index++, point[0], point[1], 0) | ||
if (attrDistance) { | ||
var d = pointIndex / (list.length - 1); | ||
attrDistance.setX(dIndex++, d); | ||
attrDistance.setX(dIndex++, d); | ||
} | ||
}); | ||
if (useDist) { | ||
var d = pointIndex/(self.length-1) | ||
this._distances.setX(dIndex++, d) | ||
this._distances.setX(dIndex++, d) | ||
} | ||
}, this) | ||
var nIndex = 0; | ||
var mIndex = 0; | ||
normals.forEach(function (n) { | ||
var norm = n[0]; | ||
var miter = n[1]; | ||
attrNormal.setXY(nIndex++, norm[0], norm[1]); | ||
attrNormal.setXY(nIndex++, norm[0], norm[1]); | ||
var nIndex = 0, | ||
mIndex = 0 | ||
normals.forEach(function(n) { | ||
var norm = n[0] | ||
var miter = n[1] | ||
this._normals.setXY(nIndex++, norm[0], norm[1]) | ||
this._normals.setXY(nIndex++, norm[0], norm[1]) | ||
attrMiter.setX(mIndex++, -miter); | ||
attrMiter.setX(mIndex++, miter); | ||
}); | ||
}; | ||
this._miters.setX(mIndex++, -miter) | ||
this._miters.setX(mIndex++, miter) | ||
}, this) | ||
} | ||
return LineMesh | ||
} | ||
return LineMesh; | ||
}; |
{ | ||
"name": "three-line-2d", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "lines expanded in a vertex shader", | ||
@@ -12,7 +12,11 @@ "main": "index.js", | ||
}, | ||
"semistandard": { | ||
"globals": [ | ||
"THREE" | ||
] | ||
}, | ||
"dependencies": { | ||
"as-number": "^1.0.0", | ||
"inherits": "^2.0.1", | ||
"polyline-normals": "^2.0.0", | ||
"xtend": "^4.0.0" | ||
"object-assign": "^4.0.1", | ||
"polyline-normals": "^2.0.0" | ||
}, | ||
@@ -22,14 +26,17 @@ "devDependencies": { | ||
"arc-to": "^1.0.0", | ||
"browserify": "^6.3.3", | ||
"domready": "^1.0.7", | ||
"garnish": "^2.1.0", | ||
"normalize-path-scale": "^1.1.1", | ||
"three": "^0.69.0", | ||
"three-orbit-viewer": "^69.2.2", | ||
"uglify-js": "^2.4.15", | ||
"wzrd": "^1.2.1" | ||
"as-number": "^1.0.0", | ||
"browserify": "^13.0.0", | ||
"budo": "^8.2.1", | ||
"normalize-path-scale": "^2.0.0", | ||
"orbit-controls": "^1.0.4", | ||
"raf-loop": "^1.1.3", | ||
"semistandard": "^7.0.5", | ||
"three": "^0.75.0", | ||
"three-orbit-viewer": "^69.3.0", | ||
"uglify-js": "^2.4.15" | ||
}, | ||
"scripts": { | ||
"test": "wzrd test/test.js | garnish", | ||
"build": "browserify test/test.js | uglifyjs -cm > bundle.js" | ||
"build": "browserify test/index.js | uglifyjs -cm > bundle.js", | ||
"start": "budo test/index.js:bundle.js --live", | ||
"test": "semistandard" | ||
}, | ||
@@ -36,0 +43,0 @@ "keywords": [ |
# three-line-2d | ||
[![unstable](http://badges.github.io/stability-badges/dist/unstable.svg)](http://github.com/badges/stability-badges) | ||
[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) | ||
@@ -11,15 +11,17 @@ [![img](http://i.imgur.com/7yGGXdd.png)](http://mattdesl.github.io/three-line-2d/) | ||
Works in ThreeJS r69-r75, and possibly newer versions. | ||
See [test.js](test/test.js) for a complete example, as well as other shader applications. | ||
```js | ||
var bezier = require('adaptive-bezier-curve') | ||
var Line = require('three-line-2d')(THREE) | ||
var BasicShader = require('three-line-2d/shaders/basic')(THREE) | ||
var bezier = require('adaptive-bezier-curve'); | ||
var Line = require('three-line-2d')(THREE); | ||
var BasicShader = require('three-line-2d/shaders/basic')(THREE); | ||
//build a smooth bezier curve in world units | ||
var quality = 5 | ||
var curve = bezier([0, 0], [0.5, 1], [1, 1], [2, 0], quality) | ||
var quality = 5; | ||
var curve = bezier([0, 0], [0.5, 1], [1, 1], [2, 0], quality); | ||
//create our geometry | ||
var curveGeometry = Line(curve) | ||
var curveGeometry = Line(curve); | ||
@@ -31,6 +33,6 @@ //create a material using a basic shader | ||
thickness: 0.3 | ||
})) | ||
})); | ||
var mesh = new THREE.Mesh(curveGeometry, mat) | ||
app.scene.add(mesh) | ||
var mesh = new THREE.Mesh(curveGeometry, mat); | ||
app.scene.add(mesh); | ||
``` | ||
@@ -44,3 +46,3 @@ | ||
Creates a new Line geometry from a 2D list of points. You can also omit the `path` and just pass an `opt` object to initially construct the geometry. Options: | ||
Creates a new Line geometry from a 2D list of points. Options: | ||
@@ -50,2 +52,4 @@ - `distances` if true, each vertex will also pass a `lineDistance` attribute to the vertex shader. This can be used to compute the U texture coordinate from the start of the line to its end. | ||
*Note:* You can also omit the `path` and just pass an `opt` object to initially construct the geometry. However, some versions of ThreeJS do not support dynamically growing vertex data, so it's best to pass an initial list of points with the maximum capacity of your desired line. | ||
#### `geometry.update(path[, closed])` | ||
@@ -55,4 +59,12 @@ | ||
## Changelog | ||
## 1.1.0 | ||
- Major code cleanup and new test with cleaner modules | ||
- Support r69-r75 | ||
- It seems like ThreeJS does not support dynamically growing geometry across all versions; use with care! | ||
## License | ||
MIT, see [LICENSE.md](http://github.com/mattdesl/three-line-2d/blob/master/LICENSE.md) for details. |
@@ -1,37 +0,50 @@ | ||
var xtend = require('xtend') | ||
var number = require('as-number') | ||
var assign = require('object-assign'); | ||
module.exports = function(THREE) { | ||
return function (opt) { | ||
opt = opt||{} | ||
module.exports = function (THREE) { | ||
return function (opt) { | ||
opt = opt || {}; | ||
var thickness = typeof opt.thickness === 'number' ? opt.thickness : 0.1; | ||
var opacity = typeof opt.opacity === 'number' ? opt.opacity : 1.0; | ||
var diffuse = opt.diffuse ? opt.diffuse : 0xffffff; | ||
var ret = xtend({ | ||
uniforms: { | ||
thickness: { type: 'f', value: number(opt.thickness, 0.1) }, | ||
opacity: { type: 'f', value: number(opt.opacity, 1.0) }, | ||
diffuse: { type: 'c', value: new THREE.Color(opt.diffuse) } | ||
}, | ||
attributes: { | ||
lineMiter: { type: 'f', value: 0 }, | ||
lineNormal: { type: 'v2', value: new THREE.Vector2() } | ||
}, | ||
vertexShader: [ | ||
"uniform float thickness;", | ||
"attribute float lineMiter;", | ||
"attribute vec2 lineNormal;", | ||
"void main() {", | ||
"vec3 pointPos = position.xyz + vec3(lineNormal * thickness/2.0 * lineMiter, 0.0);", | ||
"gl_Position = projectionMatrix * modelViewMatrix * vec4( pointPos, 1.0 );", | ||
"}" | ||
].join("\n"), | ||
fragmentShader: [ | ||
"uniform float opacity;", | ||
"uniform vec3 diffuse;", | ||
"void main() {", | ||
"gl_FragColor = vec4(diffuse, opacity);", | ||
"}" | ||
].join("\n") | ||
}, opt) | ||
return ret | ||
// remove to satisfy r73 | ||
delete opt.thickness; | ||
delete opt.opacity; | ||
delete opt.diffuse; | ||
delete opt.precision; | ||
var ret = assign({ | ||
uniforms: { | ||
thickness: { type: 'f', value: thickness }, | ||
opacity: { type: 'f', value: opacity }, | ||
diffuse: { type: 'c', value: new THREE.Color(diffuse) } | ||
}, | ||
vertexShader: [ | ||
'uniform float thickness;', | ||
'attribute float lineMiter;', | ||
'attribute vec2 lineNormal;', | ||
'void main() {', | ||
'vec3 pointPos = position.xyz + vec3(lineNormal * thickness / 2.0 * lineMiter, 0.0);', | ||
'gl_Position = projectionMatrix * modelViewMatrix * vec4(pointPos, 1.0);', | ||
'}' | ||
].join('\n'), | ||
fragmentShader: [ | ||
'uniform vec3 diffuse;', | ||
'uniform float opacity;', | ||
'void main() {', | ||
'gl_FragColor = vec4(diffuse, opacity);', | ||
'}' | ||
].join('\n') | ||
}, opt); | ||
var threeVers = (parseInt(THREE.REVISION, 10) || 0) | 0; | ||
if (threeVers < 72) { | ||
// Old versions need to specify shader attributes | ||
ret.attributes = { | ||
lineMiter: { type: 'f', value: 0 }, | ||
lineNormal: { type: 'v2', value: new THREE.Vector2() } | ||
}; | ||
} | ||
} | ||
return ret; | ||
}; | ||
}; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10245
3
7
139
66
12
1
+ Addedobject-assign@^4.0.1
+ Addedobject-assign@4.1.1(transitive)
- Removedas-number@^1.0.0
- Removedxtend@^4.0.0
- Removedas-number@1.0.0(transitive)
- Removedxtend@4.0.2(transitive)