Comparing version 0.0.29 to 0.0.30
{ | ||
"name": "ogl", | ||
"version": "0.0.29", | ||
"version": "0.0.30", | ||
"description": "WebGL Framework", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -263,3 +263,2 @@ <p align="center"> | ||
- [ ] Particle Depth Sort | ||
- [ ] Frustum culling | ||
- [ ] LODs (Level Of Detail) | ||
@@ -272,3 +271,3 @@ - [x] Polylines | ||
- [x] Sort Transparency | ||
- [ ] Load Hierarchy Animation | ||
- [x] Frustum culling | ||
@@ -307,2 +306,3 @@ ### Interaction | ||
- [ ] Blendshapes | ||
- [ ] Load Hierarchy Animation | ||
@@ -309,0 +309,0 @@ ### Stencil |
import {Geometry} from '../core/Geometry.js'; | ||
import {Vec3} from '../math/Vec3.js'; | ||
export class Cylinder extends Geometry | ||
{ | ||
export class Cylinder extends Geometry { | ||
constructor(gl, { | ||
radius = 0.5, | ||
radiusTop = 0.5, | ||
radiusBottom = 0.5, | ||
height = 1, | ||
radialSegments = 16, | ||
radialSegments = 8, | ||
heightSegments = 1, | ||
openEnded = false, | ||
thetaStart = 0, | ||
thetaLength = Math.PI * 2, | ||
attributes = {}, | ||
@@ -15,114 +18,94 @@ } = {}) { | ||
const hSegs = heightSegments; | ||
const num = (radialSegments + 1) * (heightSegments + 1) + 2; //2 centres: bottom and top cap | ||
const numTris = radialSegments * (2 + heightSegments * 2); //1 tri top, 1 tri bottom, 2 tris side face. | ||
const numIndices = numTris * 3; | ||
const tStart = thetaStart; | ||
const tLength = thetaLength; | ||
const numCaps = openEnded ? 0 : radiusBottom && radiusTop ? 2 : 1; | ||
const num = (rSegs + 1) * (hSegs + 1 + numCaps) + numCaps; | ||
const numIndices = rSegs * hSegs * 6 + numCaps * rSegs * 3; | ||
const position = new Float32Array(num * 3); | ||
const normal = new Float32Array(num * 3); | ||
const uv = new Float32Array(num * 2); | ||
const index = (num > 65536) ? new Uint32Array(numIndices) : new Uint16Array(numIndices); | ||
const index = num > 65536 ? new Uint32Array(numIndices) : new Uint16Array(numIndices); | ||
//attributes | ||
let i = 0; | ||
let x, y, z; | ||
let n = new Vec3(); | ||
//bot cap centre | ||
x = 0; | ||
y = (0 - 0.5) * height; | ||
z = 0; | ||
position[i * 3 + 0] = x; | ||
position[i * 3 + 1] = y; | ||
position[i * 3 + 2] = z; | ||
n.set(x, y, z).normalize(); | ||
normal[i * 3] = n.x; | ||
normal[i * 3 + 1] = n.y; | ||
normal[i * 3 + 2] = n.z; | ||
uv[i * 2] = 0; | ||
uv[i * 2 + 1] = 1; | ||
let bci = i; | ||
i++; | ||
//top cap centre | ||
x = 0; | ||
y = (1 - 0.5) * height; | ||
z = 0; | ||
position[i * 3 + 0] = x; | ||
position[i * 3 + 1] = y; | ||
position[i * 3 + 2] = z; | ||
n.set(x, y, z).normalize(); | ||
normal[i * 3] = n.x; | ||
normal[i * 3 + 1] = n.y; | ||
normal[i * 3 + 2] = n.z; | ||
uv[i * 2] = 0; | ||
uv[i * 2 + 1] = 0; | ||
let tci = i; | ||
i++; | ||
for (var ir = 0; ir < rSegs + 1; ir++) { //var is faster than let in for loops. | ||
let u = ir / rSegs; | ||
for (var iy = 0; iy < hSegs + 1; iy++) { //+1 to get top cap vertices | ||
let v = iy / hSegs; | ||
x = Math.cos(u * Math.PI * 2) * radius; | ||
y = (v - 0.5) * height; | ||
z = Math.sin(u * Math.PI * 2) * radius; | ||
position[i * 3 + 0] = x; | ||
position[i * 3 + 1] = y; | ||
position[i * 3 + 2] = z; | ||
n.set(x, y, z).normalize(); | ||
normal[i * 3] = n.x; | ||
normal[i * 3 + 1] = n.y; | ||
normal[i * 3 + 2] = n.z; | ||
uv[i * 2] = u; | ||
uv[i * 2 + 1] = 1 - v; | ||
let ii = 0; | ||
const indexArray = []; | ||
addHeight(); | ||
if (!openEnded) { | ||
if (radiusTop) addCap(true); | ||
if (radiusBottom) addCap(false); | ||
} | ||
function addHeight() { | ||
let x, y; | ||
const n = new Vec3(); | ||
const slope = (radiusBottom - radiusTop) / height; | ||
for (y = 0; y <= hSegs; y++) { | ||
const indexRow = []; | ||
const v = y / hSegs; | ||
const r = v * (radiusBottom - radiusTop) + radiusTop; | ||
for (x = 0; x <= rSegs; x++) { | ||
const u = x / rSegs; | ||
const theta = u * tLength + tStart; | ||
const sinTheta = Math.sin(theta); | ||
const cosTheta = Math.cos(theta); | ||
position.set([r * sinTheta, (0.5 - v) * height, r * cosTheta], i * 3); | ||
n.set(sinTheta, slope, cosTheta).normalize(); | ||
normal.set([n.x, n.y, n.z], i * 3); | ||
uv.set([u, 1 - v], i * 2); | ||
indexRow.push(i++); | ||
} | ||
indexArray.push(indexRow); | ||
} | ||
for (x = 0; x < rSegs; x++) { | ||
for (y = 0; y < hSegs; y++) { | ||
const a = indexArray[y][x]; | ||
const b = indexArray[y + 1][x]; | ||
const c = indexArray[y + 1][x + 1]; | ||
const d = indexArray[y][x + 1]; | ||
index.set([a, b, d, b, c, d], ii * 3); | ||
ii += 2; | ||
} | ||
} | ||
} | ||
function addCap(isTop) { | ||
let x; | ||
const r = isTop === true ? radiusTop : radiusBottom; | ||
const sign = isTop === true ? 1 : -1; | ||
const centerIndex = i; | ||
position.set([0, 0.5 * height * sign, 0], i * 3); | ||
normal.set([0, sign, 0], i * 3) | ||
uv.set([0.5, 0.5], i * 2); | ||
i++; | ||
for (x = 0; x <= rSegs; x++) { | ||
const u = x / rSegs; | ||
const theta = u * tLength + tStart; | ||
const cosTheta = Math.cos(theta); | ||
const sinTheta = Math.sin(theta); | ||
position.set([r * sinTheta, 0.5 * height * sign, r * cosTheta], i * 3); | ||
normal.set([0, sign, 0], i * 3); | ||
uv.set([cosTheta * 0.5 + 0.5, sinTheta * 0.5 * sign + 0.5], i * 2); | ||
i++; | ||
} | ||
} | ||
//indices | ||
let ii = 0; | ||
let hSegsAll = hSegs+1; | ||
for (var ir = 0; ir < rSegs; ir++) { //var is faster than let in for loops. | ||
let irn = ir + 1; | ||
//irn = irn < rSegs ? irn : 0; //wrap | ||
//all "2 +" are so that we start *after* bci & tci. | ||
//bot (1 tri: 0) | ||
index[ii * 3 + 0] = bci; | ||
index[ii * 3 + 1] = 2 + ir * hSegsAll; //bot of this radial segment | ||
index[ii * 3 + 2] = 2 + irn * hSegsAll; //bot of next radial segment | ||
ii++; | ||
//sides (2 tris each: 1..n-1) | ||
for (var iy = 0; iy < hSegs; iy++) { | ||
index[ii * 3 + 0] = 2 + ir * hSegsAll + (iy + 0); | ||
index[ii * 3 + 1] = 2 + ir * hSegsAll + (iy + 1); | ||
index[ii * 3 + 2] = 2 + irn * hSegsAll + (iy + 0); | ||
for (x = 0; x < rSegs; x++) { | ||
const j = centerIndex + x + 1; | ||
if (isTop) { | ||
index.set([j, j + 1, centerIndex], ii * 3); | ||
} else { | ||
index.set([j + 1, j, centerIndex], ii * 3); | ||
} | ||
ii++; | ||
index[ii * 3 + 0] = 2 + irn * hSegsAll + (iy + 0); | ||
index[ii * 3 + 1] = 2 + ir * hSegsAll + (iy + 1); | ||
index[ii * 3 + 2] = 2 + irn * hSegsAll + (iy + 1); | ||
ii++; | ||
} | ||
//top (1 tri: n) | ||
index[ii * 3 + 0] = 2 + irn * hSegsAll + hSegs; //bot of next radial segment | ||
index[ii * 3 + 1] = 2 + ir * hSegsAll + hSegs; //bot of this radial segment | ||
index[ii * 3 + 2] = tci; //opposite winding order vs. bot. | ||
ii++; | ||
} | ||
@@ -135,6 +118,6 @@ | ||
index: {data: index}, | ||
}); | ||
}); | ||
super(gl, attributes); | ||
} | ||
} | ||
} |
@@ -58,3 +58,3 @@ import {Geometry} from '../core/Geometry.js'; | ||
uv[i * 2] = ix / wSegs; | ||
uv[i * 2] = ix / wSegs; | ||
uv[i * 2 + 1] = 1 - iy / hSegs; | ||
@@ -68,3 +68,3 @@ | ||
index[ii * 6] = a; | ||
index[ii * 6] = a; | ||
index[ii * 6 + 1] = b; | ||
@@ -71,0 +71,0 @@ index[ii * 6 + 2] = d; |
@@ -54,2 +54,3 @@ import * as EulerFunc from './functions/EulerFunc.js'; | ||
this[2] = v[2]; | ||
this.onChange(); | ||
return this; | ||
@@ -56,0 +57,0 @@ } |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
836209
24232