add scale, circle to primitive

This commit is contained in:
Leslie
2019-01-18 18:04:43 +08:00
parent 58a1ea39b3
commit fc71386a67
7 changed files with 142 additions and 49 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import { Vec3 } from '../../core/value-types';
import { vec3 } from '../../core/vmath';
import { IGeometry, IGeometryOptions } from './define';
interface IBoxOptions extends IGeometryOptions {
interface IBoxOptions extends RecursivePartial<IGeometryOptions> {
/**
* Box extent on X-axis.
*/
+52
View File
@@ -0,0 +1,52 @@
import { GFXPrimitiveMode } from '../../gfx/define';
import { applyDefaultGeometryOptions, IGeometry, IGeometryOptions } from './define';
interface ICircleOptions extends IGeometryOptions {
// The segments. Default to 64.
segments: number;
}
function applyDefaultCircleOptions (options?: RecursivePartial<ICircleOptions>): ICircleOptions {
options = applyDefaultGeometryOptions<ICircleOptions>(options);
options.segments = 64;
return options as ICircleOptions;
}
/**
* Generate a circle with radius 1, centered at origin.
* @param options Options.
*/
export default function circle (options?: RecursivePartial<ICircleOptions> | ICircleOptions): IGeometry {
const normalizedOptions = applyDefaultCircleOptions(options);
const segments = normalizedOptions.segments;
const positions = new Array<number>(3 * (segments + 1));
positions.push(0, 0, 0);
const indices = new Array<number>(1 + segments * 2);
indices[0] = 0;
const step = Math.PI * 2 / segments;
for (let iSegment = 0; iSegment < segments; ++iSegment) {
const angle = step * iSegment;
const x = Math.cos(angle);
const y = Math.sin(angle);
const iVertex = (iSegment + 1) * 3;
positions[iVertex + 0] = x;
positions[iVertex + 0] = y;
positions[iVertex + 0] = 0;
indices.push(iSegment + 1, iSegment + 2);
}
if (segments > 0) {
indices[indices.length - 1] = 1;
}
const result: IGeometry = {
positions,
indices,
minPos: { x: 1, y: 1, z: 0 },
maxPos: { x: -1, y: -1, z: 0 },
boundingRadius: 1,
primitiveMode: GFXPrimitiveMode.TRIANGLE_FAN,
};
return result;
}
+18 -1
View File
@@ -4,7 +4,24 @@ export interface IGeometryOptions {
/**
* Whether to include normal. Default to true.
*/
includeNormal?: boolean;
includeNormal: boolean;
/**
* Whether to include uv. Default to true.
*/
includeUV: boolean;
}
export function applyDefaultGeometryOptions<GeometryOptions = IGeometryOptions> (
options?: RecursivePartial<IGeometryOptions>): GeometryOptions {
options = options || {};
if (options.includeNormal === undefined) {
options.includeNormal = true;
}
if (options.includeUV === undefined) {
options.includeUV = true;
}
return options as GeometryOptions;
}
export interface IGeometry {
+2 -1
View File
@@ -7,4 +7,5 @@ export { default as quad } from './quad';
export { default as sphere } from './sphere';
export { default as torus } from './torus';
export { default as capsule } from './capsule';
export { translate } from './transform';
export { default as circle } from './circle';
export { translate, scale } from './transform';
-46
View File
@@ -1,46 +0,0 @@
'use strict';
import { vec3 } from '../../core/vmath';
let positions = [
-0.5, -0.5, 0, // bottom-left
-0.5, 0.5, 0, // top-left
0.5, 0.5, 0, // top-right
0.5, -0.5, 0, // bottom-right
];
let normals = [
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
];
let uvs = [
0, 0,
0, 1,
1, 1,
1, 0,
];
let indices = [
0, 3, 1,
3, 2, 1
];
// TODO: ?
let minPos = vec3.create(-0.5, -0.5, 0);
let maxPos = vec3.create(0.5, 0.5, 0);
let boundingRadius = Math.sqrt(0.5 * 0.5 + 0.5 * 0.5);
export default function () {
return {
positions,
indices,
normals,
uvs,
minPos,
maxPos,
boundingRadius
};
}
+47
View File
@@ -0,0 +1,47 @@
'use strict';
import { applyDefaultGeometryOptions, IGeometry, IGeometryOptions } from './define';
/**
* Generate a quad with width and height both to 1, centered at origin.
* @param options Options.
*/
export default function quad (options?: IGeometryOptions): IGeometry {
const normalizedOptions = applyDefaultGeometryOptions(options);
const result: IGeometry = {
positions: [
-0.5, -0.5, 0, // bottom-left
-0.5, 0.5, 0, // top-left
0.5, 0.5, 0, // top-right
0.5, -0.5, 0, // bottom-right
],
indices: [
0, 3, 1,
3, 2, 1,
],
minPos: {
x: -0.5, y: -0.5, z: 0,
},
maxPos: {
x: 0.5, y: 0.5, z: 0,
},
boundingRadius: Math.sqrt(0.5 * 0.5 + 0.5 * 0.5),
};
if (normalizedOptions.includeNormal !== false) {
result.normals = [
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
];
}
if (normalizedOptions.includeUV !== false) {
result.uvs = [
0, 0,
0, 1,
1, 1,
1, 0,
];
}
return result;
}
+22
View File
@@ -21,3 +21,25 @@ export function translate (geometry: IGeometry, offset: { x?: number; y?: number
geometry.maxPos.z += z;
return geometry;
}
export function scale (geometry: IGeometry, value: { x?: number; y?: number; z?: number }) {
const x = value.x || 0;
const y = value.y || 0;
const z = value.z || 0;
const nVertex = Math.floor(geometry.positions.length / 3);
for (let iVertex = 0; iVertex < nVertex; ++iVertex) {
const iX = iVertex * 3;
const iY = iVertex * 3 + 1;
const iZ = iVertex * 3 + 2;
geometry.positions[iX] *= x;
geometry.positions[iY] *= y;
geometry.positions[iZ] *= z;
}
geometry.minPos.x *= x;
geometry.minPos.y *= y;
geometry.minPos.z *= z;
geometry.maxPos.x *= x;
geometry.maxPos.y *= y;
geometry.maxPos.z *= z;
return geometry;
}