From bb64cba8358b88f1fb4a532fc9e2c46684fb977f Mon Sep 17 00:00:00 2001 From: Leslie Date: Thu, 14 Feb 2019 18:40:39 +0800 Subject: [PATCH] change root index.js to index.ts, add some physics api, transform plane to ts --- cocos/3d/framework/index.ts | 7 +- cocos/3d/framework/physics/body.ts | 166 +++++++++++++++-------------- cocos/3d/primitive/plane.js | 75 ------------- cocos/3d/primitive/plane.ts | 124 +++++++++++++++++++++ cocos/3d/primitive/transform.ts | 28 +++++ index.js => index.ts | 1 - rollup/build-engine-cli.ts | 2 +- rollup/config.js | 2 +- 8 files changed, 245 insertions(+), 160 deletions(-) delete mode 100644 cocos/3d/primitive/plane.js create mode 100644 cocos/3d/primitive/plane.ts rename index.js => index.ts (98%) diff --git a/cocos/3d/framework/index.ts b/cocos/3d/framework/index.ts index cfabdc8433..eb60ab64c9 100644 --- a/cocos/3d/framework/index.ts +++ b/cocos/3d/framework/index.ts @@ -24,14 +24,14 @@ THE SOFTWARE. ****************************************************************************/ -import { CameraComponent } from './camera-component'; import AnimationComponent from './animation-component'; +import { CameraComponent } from './camera-component'; import LightComponent from './light-component'; import { ModelComponent } from './model-component'; +import { BoxColliderComponent, SphereColliderComponent, ColliderComponentBase } from './physics/collider-component'; +import { RigidBodyComponent } from './physics/rigid-body-component'; import SkinningModelComponent from './skinning-model-component'; import SkyboxComponent from './skybox-component'; -import { BoxColliderComponent, SphereColliderComponent } from './physics/collider-component'; -import { RigidBodyComponent } from './physics/rigid-body-component'; export * from './physics'; import ParticleSystemComponent from './particle/particle-system-component'; @@ -56,6 +56,7 @@ cc.SkinningModelComponent = SkinningModelComponent; cc.SkyboxComponent = SkyboxComponent; cc.RigidBodyComponent = RigidBodyComponent; +cc.ColliderComponentBase = ColliderComponentBase; cc.BoxColliderComponent = BoxColliderComponent; cc.SphereColliderComponent = SphereColliderComponent; cc.ParticleSystemComponent = ParticleSystemComponent; diff --git a/cocos/3d/framework/physics/body.ts b/cocos/3d/framework/physics/body.ts index 1f9a8c63aa..7b7ab856d5 100644 --- a/cocos/3d/framework/physics/body.ts +++ b/cocos/3d/framework/physics/body.ts @@ -1,10 +1,10 @@ import CANNON from 'cannon'; +import { Quat } from '../../../core/value-types'; import Vec3 from '../../../core/value-types/vec3'; import { vec3 } from '../../../core/vmath'; import { Node } from '../../../scene-graph/node'; import { PhysicsMaterial as PhysicsMaterial } from '../../assets/physics/material'; -import { setWrap, getWrap } from './util'; -import { Quat } from '../../../core/value-types'; +import { getWrap, setWrap } from './util'; export enum DataFlow { PUSHING, @@ -32,7 +32,9 @@ export class PhysicsBody { private _useGravity = true; - constructor() { + private _velocity = new Vec3(); + + constructor () { const cannonBodyOptions: CANNON.IBodyOptions = { mass: 0, // material: new CANNON.Material(''), @@ -50,16 +52,16 @@ export class PhysicsBody { }; } - public bind(node: Node) { + public bind (node: Node) { this._node = node; this.pullTransform(); } - public destroy() { + public destroy () { this._cannonBody.removeEventListener('collide', this._onCollidedListener); } - public _onAdded() { + public _onAdded () { this.pullTransform(); this._onWorldBeforeStepListener = this._onWorldBeforeStep.bind(this); @@ -69,7 +71,7 @@ export class PhysicsBody { this._cannonBody.world.addEventListener('postStep', this._onWorldPostStepListener); } - public _onRemoved() { + public _onRemoved () { if (this._cannonBody.world) { if (this._onWorldPostStepListener) { this._cannonBody.world.removeEventListener('postStep', this._onWorldPostStepListener); @@ -83,11 +85,11 @@ export class PhysicsBody { } } - public _getCannonBody() { + public _getCannonBody () { return this._cannonBody; } - public addShape(shape: PhysicsShape) { + public addShape (shape: PhysicsShape) { // if (this._node) { // shape.__debugNodeName = this._node.name; // } @@ -99,11 +101,11 @@ export class PhysicsBody { this.commitShapesUpdate(); } - public removeShape(shape: PhysicsShape) { + public removeShape (shape: PhysicsShape) { throw new Error(`not impl`); } - public getCenter(shape: PhysicsShape) { + public getCenter (shape: PhysicsShape) { const iShape = this._cannonBody.shapes.indexOf(shape._getCannonShape()); if (iShape >= 0) { const shapeOffset = this._cannonBody.shapeOffsets[iShape]; @@ -112,26 +114,32 @@ export class PhysicsBody { throw new Error(`shape not found.`); } - get velocity() { - return this._cannonBody.velocity; + get velocity () { + vec3.copy(this._velocity, this._cannonBody.velocity); + return this._velocity; } - get force() { + set velocity (value: Vec3) { + vec3.set(this._velocity, value.x, value.y, value.z); + vec3.copy(this._cannonBody.velocity, this._velocity); + } + + get force () { return this._cannonBody.force; } - public applyForce(force: Vec3, position?: Vec3) { + public applyForce (force: Vec3, position?: Vec3) { if (!position) { position = this._cannonBody.position; } this._cannonBody.applyForce(force, position); } - get material() { + get material () { return this._material; } - set material(value) { + set material (value) { this._material = value; if (!this._material) { return; @@ -140,11 +148,11 @@ export class PhysicsBody { this._cannonBody.material = this._material._getImpl(); } - get mass() { + get mass () { return this._cannonBody.mass; } - set mass(value) { + set mass (value) { this._cannonBody.mass = value; this._cannonBody.updateMassProperties(); if (this._cannonBody.type !== CANNON.Body.KINEMATIC) { @@ -153,11 +161,11 @@ export class PhysicsBody { } } - get isKinematic() { + get isKinematic () { return this._cannonBody.type === CANNON.Body.KINEMATIC; } - set isKinematic(value) { + set isKinematic (value) { if (value) { this._cannonBody.type = CANNON.Body.KINEMATIC; } else { @@ -166,11 +174,11 @@ export class PhysicsBody { this._onBodyTypeUpdated(); } - get useGravity() { + get useGravity () { return this._useGravity; } - set useGravity(value) { + set useGravity (value) { this._useGravity = value; if (this._useGravity) { this._cannonBody.world.removeEventListener('beforeStep', this._cancelGravityListener); @@ -179,40 +187,40 @@ export class PhysicsBody { } } - get freezeRotation() { + get freezeRotation () { return this._cannonBody.fixedRotation; } - set freezeRotation(value) { + set freezeRotation (value) { this._cannonBody.fixedRotation = value; this._cannonBody.updateMassProperties(); } - get linearDamping() { + get linearDamping () { return this._cannonBody.linearDamping; } - set linearDamping(value) { + set linearDamping (value) { this._cannonBody.linearDamping = value; } - get angularDamping() { + get angularDamping () { return this._cannonBody.angularDamping; } - set angularDamping(value) { + set angularDamping (value) { this._cannonBody.angularDamping = value; } - get isTrigger() { + get isTrigger () { return this._cannonBody.collisionResponse; } - set isTrigger(value) { + set isTrigger (value) { this._cannonBody.collisionResponse = value; } - get dataFlow() { + get dataFlow () { return this._dataflow; } @@ -221,24 +229,24 @@ export class PhysicsBody { * @param {number} group The group which this body will be put into. * @param {number} mask The groups which this body can collide with. */ - public setCollisionFilter(group: number, mask: number) { + public setCollisionFilter (group: number, mask: number) { this._cannonBody.collisionFilterGroup = group; this._cannonBody.collisionFilterMask = mask; } - public setWorldPosition(position: Vec3) { + public setWorldPosition (position: Vec3) { this._cannonBody.position.set(position.x, position.y, position.z); } - public setWorldScale(scale: Vec3) { + public setWorldScale (scale: Vec3) { this._pullScale(scale); } - public setWorldRotation(rotation: Quat) { + public setWorldRotation (rotation: Quat) { this._cannonBody.quaternion.set(rotation.x, rotation.y, rotation.z, rotation.w); } - public pullTransform() { + public pullTransform () { if (!this._node) { return; } @@ -254,7 +262,7 @@ export class PhysicsBody { * Is this body currently in contact with the specified body? * @param {CannonBody} body The body to test against. */ - public isInContactWith(body: PhysicsBody) { + public isInContactWith (body: PhysicsBody) { if (!this._cannonBody.world) { return false; } @@ -263,21 +271,21 @@ export class PhysicsBody { this._cannonBody.id, body._cannonBody.id) > 0; } - public commitShapesUpdate() { + public commitShapesUpdate () { this._cannonBody.updateBoundingRadius(); } - private _onCollided(event: CANNON.ICollisionEvent) { + private _onCollided (event: CANNON.ICollisionEvent) { // console.log(`Collided {${getWrap(event.body)._node.name}} and {${getWrap(event.target)._node.name}}.`); } - private _onWorldBeforeStep(event: CANNON.IEvent) { + private _onWorldBeforeStep (event: CANNON.IEvent) { if (this._dataflow === DataFlow.PULLING) { this._pull(); } } - private _onWorldPostStep(event: CANNON.IEvent) { + private _onWorldPostStep (event: CANNON.IEvent) { if (this._dataflow === DataFlow.PUSHING) { this._push(); } @@ -286,7 +294,7 @@ export class PhysicsBody { /** * Pull node's transform information into rigidbody. */ - private _pull() { + private _pull () { if (!this._node) { return; } @@ -298,7 +306,7 @@ export class PhysicsBody { this.pullTransform(); } - private _pullScale(scale: Vec3) { + private _pullScale (scale: Vec3) { let shapeUpdated = false; this._shapes.forEach((shape) => { let calcShapeOffset = false; @@ -329,7 +337,7 @@ export class PhysicsBody { /** * Push the rigidbody's transform information back to node. */ - private _push() { + private _push () { if (!this._node) { return; } @@ -342,15 +350,15 @@ export class PhysicsBody { } } - private _resetBodyTypeAccordingMess() { + private _resetBodyTypeAccordingMess () { if (this.mass <= 0) { this._cannonBody.type = CANNON.Body.STATIC; } else { - this._cannonBody.type = CANNON.Body.DYNAMIC + this._cannonBody.type = CANNON.Body.DYNAMIC; } } - private _onBodyTypeUpdated() { + private _onBodyTypeUpdated () { if (this._cannonBody.type !== CANNON.Body.STATIC) { this._dataflow = DataFlow.PUSHING; } else { @@ -360,62 +368,62 @@ export class PhysicsBody { } export class PhysicsShape { + + public get center () { + return this._center; + } + + public set center (value) { + vec3.copy(this._center, value); + } + + public get scale () { + return this._scale; + } + + public set scale (value) { + vec3.copy(this._scale, value); + this._onShapeParamUpdated(); + } + + public _centerChanged = true; // public __debugNodeName: string = ''; private _scale: Vec3 = new Vec3(1.0, 1.0, 1.0); private _center: Vec3 = new Vec3(0, 0, 0); - public _centerChanged = true; - - public get center() { - return this._center; - } - - public set center(value) { - vec3.copy(this._center, value); - } - - constructor(private _cannonShape: CANNON.Shape) { + constructor (private _cannonShape: CANNON.Shape) { setWrap(this._cannonShape, this); } - public get scale() { - return this._scale; - } - - public set scale(value) { - vec3.copy(this._scale, value); - this._onShapeParamUpdated(); - } - - public _getCannonShape() { + public _getCannonShape () { return this._cannonShape as T; } - protected _onShapeParamUpdated() { + protected _onShapeParamUpdated () { } } export class PhysicsBoxShape extends PhysicsShape { private _size: Vec3; - constructor(size: Vec3) { + constructor (size: Vec3) { super(new CANNON.Box(new CANNON.Vec3(size.x / 2, size.y / 2, size.z / 2))); this._size = new Vec3(size.x, size.y, size.z); } - public get size() { + public get size () { return this._size; } - public set size(value) { + public set size (value) { vec3.copy(this._size, value); this._onShapeParamUpdated(); } - protected _onShapeParamUpdated() { + protected _onShapeParamUpdated () { const shape = this._getCannonShape(); if (!shape.halfExtents) { shape.halfExtents = new CANNON.Vec3(); @@ -434,22 +442,22 @@ export class PhysicsBoxShape extends PhysicsShape { export class PhysicsSphereShape extends PhysicsShape { private _radius: number; - constructor(radius: number) { + constructor (radius: number) { super(new CANNON.Sphere(radius)); this._radius = radius; } - public get radius() { + public get radius () { return this._radius; } - public set radius(value) { + public set radius (value) { this._radius = value; this._onShapeParamUpdated(); } - protected _onShapeParamUpdated() { + protected _onShapeParamUpdated () { const shape = this._getCannonShape(); shape.radius = this._radius * maxComponent(this.scale); // shape.updateBoundingSphereRadius(); @@ -458,6 +466,6 @@ export class PhysicsSphereShape extends PhysicsShape { } } -function maxComponent(v: { x: number, y: number, z: number }) { +function maxComponent (v: { x: number, y: number, z: number }) { return Math.max(v.x, Math.max(v.y, v.z)); } diff --git a/cocos/3d/primitive/plane.js b/cocos/3d/primitive/plane.js deleted file mode 100644 index 0b2828a0d2..0000000000 --- a/cocos/3d/primitive/plane.js +++ /dev/null @@ -1,75 +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 c00 = vec3.create(0, 0, 0); -let c10 = vec3.create(0, 0, 0); -let c01 = vec3.create(0, 0, 0); - -/** - * @param {Number} width - * @param {Number} length - * @param {Object} opts - * @param {Number} opts.widthSegments - * @param {Number} opts.lengthSegments - */ -export default function (width = 10, length = 10, opts = {}) { - let uSegments = opts.widthSegments !== undefined ? opts.widthSegments : 10; - let vSegments = opts.lengthSegments !== undefined ? opts.lengthSegments : 10; - - let hw = width * 0.5; - let hl = length * 0.5; - - let positions = []; - let normals = []; - let uvs = []; - let indices = []; - let minPos = vec3.create(-hw, 0, -hl); - let maxPos = vec3.create(hw, 0, hl); - let boundingRadius = Math.sqrt(width * width + length * length); - - vec3.set(c00, -hw, 0, hl); - vec3.set(c10, hw, 0, hl); - vec3.set(c01, -hw, 0, -hl); - - for (let y = 0; y <= vSegments; y++) { - for (let x = 0; x <= uSegments; x++) { - let u = x / uSegments; - let v = y / vSegments; - - vec3.lerp(temp1, c00, c10, u); - vec3.lerp(temp2, c00, c01, v); - vec3.sub(temp3, temp2, c00); - vec3.add(r, temp1, temp3); - - positions.push(r.x, r.y, r.z); - normals.push(0, 1, 0); - uvs.push(u, v); - - if ((x < uSegments) && (y < vSegments)) { - let useg1 = uSegments + 1; - let a = x + y * useg1; - let b = x + (y + 1) * useg1; - let c = (x + 1) + (y + 1) * useg1; - let d = (x + 1) + y * useg1; - - indices.push(a, d, b); - indices.push(d, c, b); - } - } - } - - return { - positions, - normals, - uvs, - indices, - minPos, - maxPos, - boundingRadius - }; -} diff --git a/cocos/3d/primitive/plane.ts b/cocos/3d/primitive/plane.ts new file mode 100644 index 0000000000..7b5ede6391 --- /dev/null +++ b/cocos/3d/primitive/plane.ts @@ -0,0 +1,124 @@ +import { Vec3 } from '../../core/value-types'; +import { vec3 } from '../../core/vmath'; +import { applyDefaultGeometryOptions, IGeometry, IGeometryOptions } from './define'; + +interface IPlaneOptions extends RecursivePartial { + /** + * Plane extent on X-axis. + */ + width: number; + + /** + * Plane extent on Z-axis. + */ + length: number; + + /** + * Segment count on X-axis. + */ + widthSegments: number; + + /** + * Segment count on Z-axis. + */ + lengthSegments: number; +} + +function applyDefaultPlaneOptions (options?: RecursivePartial): IPlaneOptions { + options = applyDefaultGeometryOptions(options); + options.width = 10; + options.length = 10; + options.widthSegments = 10; + options.lengthSegments = 10; + return options as IPlaneOptions; +} + +const temp1 = vec3.create(0, 0, 0); +const temp2 = vec3.create(0, 0, 0); +const temp3 = vec3.create(0, 0, 0); +const r = vec3.create(0, 0, 0); +const c00 = vec3.create(0, 0, 0); +const c10 = vec3.create(0, 0, 0); +const c01 = vec3.create(0, 0, 0); + +/** + * This function generates a plane on XOZ plane with positive Y direction. + * @param options Options. + */ +export default function (options?: IPlaneOptions): IGeometry { + const normalizedOptions = applyDefaultPlaneOptions(options); + + const { + width, + length, + widthSegments: uSegments, + lengthSegments: vSegments, + } = normalizedOptions; + + const hw = width * 0.5; + const hl = length * 0.5; + + const positions: number[] = []; + const uvs: number[] = []; + const indices: number[] = []; + const minPos = new Vec3(-hw, 0, -hl); + const maxPos = new Vec3(hw, 0, hl); + const boundingRadius = Math.sqrt(width * width + length * length); + + vec3.set(c00, -hw, 0, hl); + vec3.set(c10, hw, 0, hl); + vec3.set(c01, -hw, 0, -hl); + + for (let y = 0; y <= vSegments; y++) { + for (let x = 0; x <= uSegments; x++) { + const u = x / uSegments; + const v = y / vSegments; + + vec3.lerp(temp1, c00, c10, u); + vec3.lerp(temp2, c00, c01, v); + vec3.sub(temp3, temp2, c00); + vec3.add(r, temp1, temp3); + + positions.push(r.x, r.y, r.z); + if (normalizedOptions.includeUV) { + uvs.push(u, v); + } + + if ((x < uSegments) && (y < vSegments)) { + const useg1 = uSegments + 1; + const a = x + y * useg1; + const b = x + (y + 1) * useg1; + const c = (x + 1) + (y + 1) * useg1; + const d = (x + 1) + y * useg1; + + indices.push(a, d, b); + indices.push(d, c, b); + } + } + } + + const result: IGeometry = { + positions, + indices, + minPos, + maxPos, + boundingRadius, + }; + + if (normalizedOptions.includeNormal) { + const nVertex = (vSegments + 1) * (uSegments + 1); + const normals = new Array(3 * nVertex); + result.normals = normals; + for (let i = 0; i < nVertex; ++i) { + normals[i * 3 + 0] = 0; + normals[i * 3 + 1] = 1; + normals[i * 3 + 2] = 0; + } + } + + if (normalizedOptions.includeUV) { + result.uvs = uvs; + } + + return result; +} diff --git a/cocos/3d/primitive/transform.ts b/cocos/3d/primitive/transform.ts index 57eb6bd39f..f5e536cafa 100644 --- a/cocos/3d/primitive/transform.ts +++ b/cocos/3d/primitive/transform.ts @@ -44,3 +44,31 @@ export function scale (geometry: IGeometry, value: { x?: number; y?: number; z?: geometry.boundingRadius = Math.max(Math.max(x, y), z); return geometry; } + +export function wireframed (geometry: IGeometry) { + const { indices } = geometry; + if (!indices) { + return geometry; + } + + const offsets = [[0, 1], [1, 2], [2, 0]]; + const lines: number[] = []; + const lineIDs = {}; + + for (let i = 0; i < indices.length; i += 3) { + for (let k = 0; k < 3; ++k) { + const i1 = indices[i + offsets[k][0]]; + const i2 = indices[i + offsets[k][1]]; + + // check if we already have the line in our lines + const id = (i1 > i2) ? ((i2 << 16) | i1) : ((i1 << 16) | i2); + if (lineIDs[id] === undefined) { + lineIDs[id] = 0; + lines.push(i1, i2); + } + } + } + + geometry.indices = lines; + return geometry; + } diff --git a/index.js b/index.ts similarity index 98% rename from index.js rename to index.ts index f301a466cc..4bb2994381 100644 --- a/index.js +++ b/index.ts @@ -38,7 +38,6 @@ cc.renderer = renderer; export * from './cocos/assets'; export * from './cocos/load-pipeline'; export * from './cocos/components'; -export * from './cocos/animation'; export * from './cocos/3d'; export * from './cocos/gfx'; export * from './cocos/pipeline'; diff --git a/rollup/build-engine-cli.ts b/rollup/build-engine-cli.ts index 53edb0ca0a..cf8ba62718 100644 --- a/rollup/build-engine-cli.ts +++ b/rollup/build-engine-cli.ts @@ -19,7 +19,7 @@ const globalDefs = getGlobalDefs(yargs.argv.platform as Platform, flags); build({ compress: yargs.argv.compress as (boolean | undefined), globalDefines: globalDefs, - inputPath: './index.js', + inputPath: './index.ts', outputPath: yargs.argv.destination as string, sourcemap: yargs.argv.sourcemap as (boolean | undefined), }).then( diff --git a/rollup/config.js b/rollup/config.js index 58c99afb04..b907d75a2a 100644 --- a/rollup/config.js +++ b/rollup/config.js @@ -11,7 +11,7 @@ const moduleName = 'cc_modular'; fsJetpack.dir(dest); export default { - input: './index.js', + input: './index.ts', output: [{ file: `${dest}/${file}.js`,