From fc71386a674e20c1cffbbf74e652229ca30c9443 Mon Sep 17 00:00:00 2001 From: Leslie Date: Fri, 18 Jan 2019 18:04:43 +0800 Subject: [PATCH 1/2] add scale, circle to primitive --- cocos/3d/primitive/box.ts | 2 +- cocos/3d/primitive/circle.ts | 52 +++++++++++++++++++++++++++++++++ cocos/3d/primitive/define.ts | 19 +++++++++++- cocos/3d/primitive/index.js | 3 +- cocos/3d/primitive/quad.js | 46 ----------------------------- cocos/3d/primitive/quad.ts | 47 +++++++++++++++++++++++++++++ cocos/3d/primitive/transform.ts | 22 ++++++++++++++ 7 files changed, 142 insertions(+), 49 deletions(-) create mode 100644 cocos/3d/primitive/circle.ts delete mode 100644 cocos/3d/primitive/quad.js create mode 100644 cocos/3d/primitive/quad.ts diff --git a/cocos/3d/primitive/box.ts b/cocos/3d/primitive/box.ts index e7e17f0d38..e67755f41c 100644 --- a/cocos/3d/primitive/box.ts +++ b/cocos/3d/primitive/box.ts @@ -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 { /** * Box extent on X-axis. */ diff --git a/cocos/3d/primitive/circle.ts b/cocos/3d/primitive/circle.ts new file mode 100644 index 0000000000..4a48433adb --- /dev/null +++ b/cocos/3d/primitive/circle.ts @@ -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 { + options = applyDefaultGeometryOptions(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): IGeometry { + const normalizedOptions = applyDefaultCircleOptions(options); + const segments = normalizedOptions.segments; + + const positions = new Array(3 * (segments + 1)); + positions.push(0, 0, 0); + const indices = new Array(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; +} diff --git a/cocos/3d/primitive/define.ts b/cocos/3d/primitive/define.ts index 9199a5dc36..b4f555e759 100644 --- a/cocos/3d/primitive/define.ts +++ b/cocos/3d/primitive/define.ts @@ -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 ( + options?: RecursivePartial): GeometryOptions { + options = options || {}; + if (options.includeNormal === undefined) { + options.includeNormal = true; + } + if (options.includeUV === undefined) { + options.includeUV = true; + } + return options as GeometryOptions; } export interface IGeometry { diff --git a/cocos/3d/primitive/index.js b/cocos/3d/primitive/index.js index f7e2132075..9d77900f62 100644 --- a/cocos/3d/primitive/index.js +++ b/cocos/3d/primitive/index.js @@ -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'; \ No newline at end of file +export { default as circle } from './circle'; +export { translate, scale } from './transform'; \ No newline at end of file diff --git a/cocos/3d/primitive/quad.js b/cocos/3d/primitive/quad.js deleted file mode 100644 index 503702c5a0..0000000000 --- a/cocos/3d/primitive/quad.js +++ /dev/null @@ -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 - }; -} diff --git a/cocos/3d/primitive/quad.ts b/cocos/3d/primitive/quad.ts new file mode 100644 index 0000000000..a142a6cd8d --- /dev/null +++ b/cocos/3d/primitive/quad.ts @@ -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; +} diff --git a/cocos/3d/primitive/transform.ts b/cocos/3d/primitive/transform.ts index 20bfe9f3c8..bd286a4395 100644 --- a/cocos/3d/primitive/transform.ts +++ b/cocos/3d/primitive/transform.ts @@ -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; +} From 9793ef88b39eb949e3818b16dd78959edf8bfa7b Mon Sep 17 00:00:00 2001 From: Leslie Date: Fri, 18 Jan 2019 18:11:56 +0800 Subject: [PATCH 2/2] fix --- cocos/3d/primitive/circle.ts | 16 ++++++++++------ globals.d.ts | 6 ++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cocos/3d/primitive/circle.ts b/cocos/3d/primitive/circle.ts index 4a48433adb..eb20f37dc5 100644 --- a/cocos/3d/primitive/circle.ts +++ b/cocos/3d/primitive/circle.ts @@ -21,7 +21,9 @@ export default function circle (options?: RecursivePartial | ICi const segments = normalizedOptions.segments; const positions = new Array(3 * (segments + 1)); - positions.push(0, 0, 0); + positions[0] = 0; + positions[1] = 0; + positions[2] = 0; const indices = new Array(1 + segments * 2); indices[0] = 0; const step = Math.PI * 2 / segments; @@ -29,11 +31,13 @@ export default function circle (options?: RecursivePartial | ICi 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); + const p = (iSegment + 1) * 3; + positions[p + 0] = x; + positions[p + 1] = y; + positions[p + 2] = 0; + const i = iSegment * 2; + indices[1 + (i)] = iSegment + 1; + indices[1 + (i + 1)] = iSegment + 2; } if (segments > 0) { indices[indices.length - 1] = 1; diff --git a/globals.d.ts b/globals.d.ts index 52edcb9b28..163998848c 100644 --- a/globals.d.ts +++ b/globals.d.ts @@ -40,3 +40,9 @@ declare const CC_SUPPORT_JIT: boolean; declare const cc: any; declare const _cc: any; + +type RecursivePartial = { + [P in keyof T]?: + T[P] extends Array ? Array> : + T[P] extends ReadonlyArray ? ReadonlyArray> : RecursivePartial; +}; \ No newline at end of file