From 430156af5f635cfdba8b46cd1cb596b869ce15a9 Mon Sep 17 00:00:00 2001 From: Leslie Date: Thu, 10 Jan 2019 11:06:42 +0800 Subject: [PATCH] restructure primitive/box to support wireframed box --- cocos/3d/primitive/box.js | 130 ------------------------- cocos/3d/primitive/box.ts | 174 ++++++++++++++++++++++++++++++++++ cocos/3d/primitive/defines.ts | 76 +++++++++++++++ 3 files changed, 250 insertions(+), 130 deletions(-) delete mode 100644 cocos/3d/primitive/box.js create mode 100644 cocos/3d/primitive/box.ts create mode 100644 cocos/3d/primitive/defines.ts diff --git a/cocos/3d/primitive/box.js b/cocos/3d/primitive/box.js deleted file mode 100644 index e9aa1b0873..0000000000 --- a/cocos/3d/primitive/box.js +++ /dev/null @@ -1,130 +0,0 @@ -'use strict'; - -import { vec3 } from '../../core/vmath'; - -let temp1 = vec3.create(0, 0, 0); -let temp2 = vec3.create(0, 0, 0); -let temp3 = vec3.create(0, 0, 0); -let r = vec3.create(0, 0, 0); -let c0 = vec3.create(0, 0, 0); -let c1 = vec3.create(0, 0, 0); -let c2 = vec3.create(0, 0, 0); -let c3 = vec3.create(0, 0, 0); -let c4 = vec3.create(0, 0, 0); -let c5 = vec3.create(0, 0, 0); -let c6 = vec3.create(0, 0, 0); -let c7 = vec3.create(0, 0, 0); - -/** - * @param {Number} width - * @param {Number} height - * @param {Number} length - * @param {Object} opts - * @param {Number} opts.widthSegments - * @param {Number} opts.heightSegments - * @param {Number} opts.lengthSegments - */ -export default function (width = 1, height = 1, length = 1, opts = {}) { - let ws = opts.widthSegments !== undefined ? opts.widthSegments : 1; - let hs = opts.heightSegments !== undefined ? opts.heightSegments : 1; - let ls = opts.lengthSegments !== undefined ? opts.lengthSegments : 1; - let inv = opts.invWinding !== undefined ? opts.invWinding : false; - - let hw = width * 0.5; - let hh = height * 0.5; - let hl = length * 0.5; - - let corners = [ - vec3.set(c0, -hw, -hh, hl), - vec3.set(c1, hw, -hh, hl), - vec3.set(c2, hw, hh, hl), - vec3.set(c3, -hw, hh, hl), - vec3.set(c4, hw, -hh, -hl), - vec3.set(c5, -hw, -hh, -hl), - vec3.set(c6, -hw, hh, -hl), - vec3.set(c7, hw, hh, -hl), - ]; - - let faceAxes = [ - [ 2, 3, 1 ], // FRONT - [ 4, 5, 7 ], // BACK - [ 7, 6, 2 ], // TOP - [ 1, 0, 4 ], // BOTTOM - [ 1, 4, 2 ], // RIGHT - [ 5, 0, 6 ] // LEFT - ]; - - let faceNormals = [ - [ 0, 0, 1 ], // FRONT - [ 0, 0, -1 ], // BACK - [ 0, 1, 0 ], // TOP - [ 0, -1, 0 ], // BOTTOM - [ 1, 0, 0 ], // RIGHT - [ -1, 0, 0 ] // LEFT - ]; - - let positions = []; - let normals = []; - let uvs = []; - let indices = []; - let minPos = vec3.create(-hw, -hh, -hl); - let maxPos = vec3.create(hw, hh, hl); - let boundingRadius = Math.sqrt(hw * hw + hh * hh + hl * hl); - - function _buildPlane (side, uSegments, vSegments) { - let u, v; - let ix, iy; - let offset = positions.length / 3; - let faceAxe = faceAxes[side]; - let faceNormal = faceNormals[side]; - - for (iy = 0; iy <= vSegments; iy++) { - for (ix = 0; ix <= uSegments; ix++) { - u = ix / uSegments; - v = iy / vSegments; - - vec3.lerp(temp1, corners[faceAxe[0]], corners[faceAxe[1]], u); - vec3.lerp(temp2, corners[faceAxe[0]], corners[faceAxe[2]], v); - vec3.sub(temp3, temp2, corners[faceAxe[0]]); - vec3.add(r, temp1, temp3); - - positions.push(r.x, r.y, r.z); - normals.push(faceNormal[0], faceNormal[1], faceNormal[2]); - uvs.push(u, v); - - if ((ix < uSegments) && (iy < vSegments)) { - let useg1 = uSegments + 1; - let a = ix + iy * useg1; - let b = ix + (iy + 1) * useg1; - let c = (ix + 1) + (iy + 1) * useg1; - let d = (ix + 1) + iy * useg1; - - if (inv) { - indices.push(offset + a, offset + b, offset + d); - indices.push(offset + d, offset + b, offset + c); - } else { - indices.push(offset + a, offset + d, offset + b); - indices.push(offset + b, offset + d, offset + c); - } - } - } - } - } - - _buildPlane(0, ws, hs); // FRONT - _buildPlane(4, ls, hs); // RIGHT - _buildPlane(1, ws, hs); // BACK - _buildPlane(5, ls, hs); // LEFT - _buildPlane(3, ws, ls); // BOTTOM - _buildPlane(2, ws, ls); // TOP - - return { - positions, - normals, - uvs, - indices, - minPos, - maxPos, - boundingRadius - }; -} diff --git a/cocos/3d/primitive/box.ts b/cocos/3d/primitive/box.ts new file mode 100644 index 0000000000..0152d15079 --- /dev/null +++ b/cocos/3d/primitive/box.ts @@ -0,0 +1,174 @@ +import { GFXPrimitiveMode } from '../../gfx/define'; +import { IGeometry, IGeometryOptions, normalizeGeometryOptions, WindingOrder } from './defines'; + +interface IBoxOptions extends IGeometryOptions { + /** + * Box extent on X-axis. + */ + length: number; + + /** + * Box extent on Z-axis. + */ + width: number; + + /** + * Box extent on Y-axis. + */ + height: number; +} + +/** + * This function generates a box with specified extents and centered at origin, + * but may emplaced through `center` option). + * @param options Options. + */ +export default function box (options: IBoxOptions): IGeometry { + normalizeGeometryOptions(options); + + const right = options.length / 2; + const left = -right; + const top = options.height / 2; + const bottom = -top; + const near = options.width / 2; + const far = -near; + const center = options.center ? { + x: options.center.x || 0, + y: options.center.y || 0, + z: options.center.z || 0, + } : { + x: 0, + y: 0, + z: 0, + }; + + const vertices: number[] = []; + const addVertex = (x: number, y: number, z: number) => { + vertices.push(x + center.x, y + center.y, z + center.z); + }; + addVertex(left, top, near); + addVertex(left, bottom, near); + addVertex(right, bottom, near); + addVertex(right, top, near); + addVertex(left, top, far); + addVertex(left, bottom, far); + addVertex(right, bottom, far); + addVertex(right, top, far); + + let indices: number[] = []; + let normals: number[] | undefined; + let primitiveType: GFXPrimitiveMode = GFXPrimitiveMode.POINT_LIST; + if (options.wireframed) { + primitiveType = GFXPrimitiveMode.LINE_LIST; + indices = indices.concat([ + 0, 1, 1, 2, 2, 3, 3, 0, // Front quad + 4, 5, 5, 6, 6, 7, 7, 4, // Back quad + 0, 4, 1, 5, 2, 6, 3, 7, // Side lines + ]); + } else { + primitiveType = GFXPrimitiveMode.TRIANGLE_LIST; + type FaceNormal = [ number, number, number ]; + type FaceQuad = [ + number, // Left top + number, // Left bottom + number, // Right bottom + number // Right top + ]; + interface IFaceInfo { + quad: FaceQuad; + normal: FaceNormal; + } + interface IFaceInfos { + front: IFaceInfo; + right: IFaceInfo; + back: IFaceInfo; + left: IFaceInfo; + top: IFaceInfo; + bottom: IFaceInfo; + } + + const addTri = (i: number, j: number, k: number) => { + if (options.windingOrder === WindingOrder.CounterClockwise) { + indices.push(i, j, k); + } else { + indices.push(i, k, j); + } + }; + + const addQuad = (faceQuad: FaceQuad) => { + const lt = faceQuad[0]; + const lb = faceQuad[1]; + const rb = faceQuad[2]; + const rt = faceQuad[3]; + addTri(lt, lb, rb); + addTri(lt, rb, rt); + }; + + const addNormal = (faceNormal: FaceNormal) => { + const x = faceNormal[0]; + const y = faceNormal[1]; + const z = faceNormal[2]; + if (options.windingOrder === WindingOrder.CounterClockwise) { + normals!.push(x, y, z); + } else { + normals!.push(-x, -y, -z); + } + }; + + const addFace = (face: IFaceInfo) => { + addQuad(face.quad); + if (normals) { + addNormal(face.normal); + } + }; + + const faceInfos: IFaceInfos = { + front: { + quad: [0, 1, 2, 3], + normal: [0, 0, 1], + }, + right: { + quad: [3, 2, 6, 7], + normal: [1, 0, 0], + }, + back: { + quad: [7, 6, 5, 4], + normal: [0, 0, -1], + }, + left: { + quad: [4, 5, 1, 0], + normal: [-1, 0, 0], + }, + top: { + quad: [4, 0, 3, 7], + normal: [0, 1, 0], + }, + bottom: { + quad: [1, 5, 6, 2], + normal: [0, -1, 0], + }, + }; + + if (options.includeNormal) { + normals = []; + } + addFace(faceInfos.front); + addFace(faceInfos.right); + addFace(faceInfos.back); + addFace(faceInfos.left); + addFace(faceInfos.top); + addFace(faceInfos.bottom); + } + + const result: IGeometry = { + primitiveType, + vertices, + indices, + }; + + if (normals) { + result.normals = normals; + } + + return result; +} diff --git a/cocos/3d/primitive/defines.ts b/cocos/3d/primitive/defines.ts new file mode 100644 index 0000000000..9a3edcd280 --- /dev/null +++ b/cocos/3d/primitive/defines.ts @@ -0,0 +1,76 @@ +import { GFXPrimitiveMode } from '../../gfx/define'; + +export enum WindingOrder { + /** + * Clockwise. + */ + Clockwise, + + /** + * Counter-clockwise. + */ + CounterClockwise, +} + +export interface IGeometryOptions { + /** + * Whether generated wireframed geometry. Default to false. + */ + wireframed?: boolean; + + /** + * The winding order. Default to counter-clockwise. + */ + windingOrder?: WindingOrder; + + /** + * Whether to include normal. Default to true. + */ + includeNormal?: boolean; + + /** + * The center of the geometry. Default to (0, 0, 0). + */ + center?: { + x?: number; + y?: number; + z?: number; + }; +} + +export function normalizeGeometryOptions (options: IGeometryOptions) { + options.wireframed = options.wireframed || false; + options.windingOrder = options.windingOrder === undefined ? + WindingOrder.CounterClockwise : options.windingOrder; + options.includeNormal = options.includeNormal === undefined ? + true : options.includeNormal; + if (!options.center) { + options.center = {}; + } + options.center.x = options.center.x || 0; + options.center.y = options.center.y || 0; + options.center.z = options.center.z || 0; + return options; +} + +export interface IGeometry { + /** + * Topology of the geometry vertices. + */ + primitiveType: GFXPrimitiveMode; + + /** + * Vertex positions. + */ + vertices: number[]; + + /** + * Gemetry indices, if one needs indexed-draw. + */ + indices?: number[]; + + /** + * Vertex normals. + */ + normals?: number[]; +}