Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/shaders update #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 98 additions & 96 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,116 +7,118 @@ var utils = require('./lib/utils')

var Base = THREE.BufferGeometry

module.exports = function createTextGeometry (opt) {
return new TextGeometry(opt)
module.exports = function createTextGeometry(opt) {
return new TextGeometry(opt)
}

function TextGeometry (opt) {
Base.call(this)
function TextGeometry(opt) {
Base.call(this)

if (typeof opt === 'string') {
opt = { text: opt }
}
if (typeof opt === 'string') {
opt = { text: opt }
}

// use these as default values for any subsequent
// calls to update()
this._opt = Object.assign({}, opt)
// use these as default values for any subsequent
// calls to update()
this._opt = Object.assign({}, opt)

// also do an initial setup...
if (opt) this.update(opt)
// also do an initial setup...
if (opt) this.update(opt)
}

inherits(TextGeometry, Base)

TextGeometry.prototype.update = function (opt) {
if (typeof opt === 'string') {
opt = { text: opt }
}

// use constructor defaults
opt = Object.assign({}, this._opt, opt)

if (!opt.font) {
throw new TypeError('must specify a { font } in options')
}

this.layout = createLayout(opt)

// get vec2 texcoords
var flipY = opt.flipY !== false

// the desired BMFont data
var font = opt.font

// determine texture size from font file
var texWidth = font.common.scaleW
var texHeight = font.common.scaleH

// get visible glyphs
var glyphs = this.layout.glyphs.filter(function (glyph) {
var bitmap = glyph.data
return bitmap.width * bitmap.height > 0
})

// provide visible glyphs for convenience
this.visibleGlyphs = glyphs

// get common vertex data
var positions = vertices.positions(glyphs)
var uvs = vertices.uvs(glyphs, texWidth, texHeight, flipY)
var indices = createIndices([], {
clockwise: true,
type: 'uint16',
count: glyphs.length
})

// update vertex data
this.setIndex(indices)
this.setAttribute('position', new THREE.BufferAttribute(positions, 2))
this.setAttribute('uv', new THREE.BufferAttribute(uvs, 2))

// update multipage data
if (!opt.multipage && 'page' in this.attributes) {
// disable multipage rendering
this.removeAttribute('page')
} else if (opt.multipage) {
// enable multipage rendering
var pages = vertices.pages(glyphs)
this.setAttribute('page', new THREE.BufferAttribute(pages, 1))
}
if (typeof opt === 'string') {
opt = { text: opt }
}

// use constructor defaults
opt = Object.assign({}, this._opt, opt)

if (!opt.font) {
throw new TypeError('must specify a { font } in options')
}

this.layout = createLayout(opt)

// get vec2 texcoords
var flipY = opt.flipY !== false

// the desired BMFont data
var font = opt.font

// determine texture size from font file
var texWidth = font.common.scaleW
var texHeight = font.common.scaleH

// get visible glyphs
var glyphs = this.layout.glyphs.filter(function (glyph) {
var bitmap = glyph.data
return bitmap.width * bitmap.height > 0
})

// provide visible glyphs for convenience
this.visibleGlyphs = glyphs

// get common vertex data
var positions = vertices.positions(glyphs)
var uvs = vertices.uvs(glyphs, texWidth, texHeight, flipY)
var indices = createIndices([], {
clockwise: true,
type: 'uint16',
count: glyphs.length,
})

// update vertex data
this.setIndex(indices)
this.setAttribute('position', new THREE.BufferAttribute(positions, 2))
this.setAttribute('uv', new THREE.BufferAttribute(uvs, 2))

// update multipage data
if (!opt.multipage && 'page' in this.attributes) {
// disable multipage rendering
this.removeAttribute('page')
} else if (opt.multipage) {
// enable multipage rendering
var pages = vertices.pages(glyphs)
this.setAttribute('page', new THREE.BufferAttribute(pages, 1))
}
}

TextGeometry.prototype.computeBoundingSphere = function () {
if (this.boundingSphere === null) {
this.boundingSphere = new THREE.Sphere()
}

var positions = this.attributes.position.array
var itemSize = this.attributes.position.itemSize
if (!positions || !itemSize || positions.length < 2) {
this.boundingSphere.radius = 0
this.boundingSphere.center.set(0, 0, 0)
return
}
utils.computeSphere(positions, this.boundingSphere)
if (isNaN(this.boundingSphere.radius)) {
console.error('THREE.BufferGeometry.computeBoundingSphere(): ' +
'Computed radius is NaN. The ' +
'"position" attribute is likely to have NaN values.')
}
if (this.boundingSphere === null) {
this.boundingSphere = new THREE.Sphere()
}

var positions = this.attributes.position.array
var itemSize = this.attributes.position.itemSize
if (!positions || !itemSize || positions.length < 2) {
this.boundingSphere.radius = 0
this.boundingSphere.center.set(0, 0, 0)
return
}
utils.computeSphere(positions, this.boundingSphere)
if (isNaN(this.boundingSphere.radius)) {
console.error(
'THREE.BufferGeometry.computeBoundingSphere(): ' +
'Computed radius is NaN. The ' +
'"position" attribute is likely to have NaN values.'
)
}
}

TextGeometry.prototype.computeBoundingBox = function () {
if (this.boundingBox === null) {
this.boundingBox = new THREE.Box3()
}

var bbox = this.boundingBox
var positions = this.attributes.position.array
var itemSize = this.attributes.position.itemSize
if (!positions || !itemSize || positions.length < 2) {
bbox.makeEmpty()
return
}
utils.computeBox(positions, bbox)
if (this.boundingBox === null) {
this.boundingBox = new THREE.Box3()
}

var bbox = this.boundingBox
var positions = this.attributes.position.array
var itemSize = this.attributes.position.itemSize
if (!positions || !itemSize || positions.length < 2) {
bbox.makeEmpty()
return
}
utils.computeBox(positions, bbox)
}
82 changes: 51 additions & 31 deletions shaders/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,56 @@ module.exports = function createBasicShader (opt) {
delete opt.precision
delete opt.opacity

return assign({
uniforms: {
opacity: { type: 'f', value: opacity },
map: { type: 't', value: map || new THREE.Texture() },
color: { type: 'c', value: new THREE.Color(color) }
const webgl2 = document.getElementsByTagName('canvas')[0].getContext('webgl2')

return assign(
{
uniforms: {
opacity: { value: opacity },
map: { value: map || new THREE.Texture() },
color: { value: new THREE.Color(color) }
},
vertexShader: `${
webgl2
? `#version 300 es

#define attribute in
#define varying out`
: ''
}
attribute vec2 uv;
attribute vec3 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

varying vec2 vUv;

void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}`,
fragmentShader: `${
webgl2
? `#version 300 es
#define varying in
out highp vec4 pc_fragColor;

#define texture2D texture
#define gl_FragColor pc_fragColor`
: ''
}
precision ${precision} float;
uniform float opacity;
uniform vec3 color;
uniform sampler2D map;

varying vec2 vUv;

void main() {
gl_FragColor = texture2D(map, vUv) * vec4(color, opacity);
${alphaTest === 0 ? '' : `if (gl_FragColor.a < ${alphaTest}) discard;`}
}`
},
vertexShader: [
'attribute vec2 uv;',
'attribute vec4 position;',
'uniform mat4 projectionMatrix;',
'uniform mat4 modelViewMatrix;',
'varying vec2 vUv;',
'void main() {',
'vUv = uv;',
'gl_Position = projectionMatrix * modelViewMatrix * position;',
'}'
].join('\n'),
fragmentShader: [
'precision ' + precision + ' float;',
'uniform float opacity;',
'uniform vec3 color;',
'uniform sampler2D map;',
'varying vec2 vUv;',

'void main() {',
' gl_FragColor = texture2D(map, vUv) * vec4(color, opacity);',
alphaTest === 0
? ''
: ' if (gl_FragColor.a < ' + alphaTest + ') discard;',
'}'
].join('\n')
}, opt)
opt
)
}
Loading