bindpose as mat4 (#4738)

This commit is contained in:
Yun Hsiao Wu
2019-06-27 18:35:01 +08:00
committed by Weiliang Wang
parent d6fb2dc72b
commit f6d6aab06b
10 changed files with 156 additions and 123 deletions
+91 -65
View File
@@ -1,65 +1,91 @@
/*
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { Asset } from '../../assets/asset';
import { ccclass, property } from '../../core/data/class-decorator';
import { Node } from '../../scene-graph/node';
/**
* 骨骼资源。
* 骨骼资源记录了每个关节(相对于`SkinningModelComponent.SkinningRoot`)的路径以及它的绑定姿势矩阵。
*/
@ccclass('cc.Skeleton')
export class Skeleton extends Asset {
@property([String])
private _joints: string[] = [];
@property([Node])
private _bindposes: Node[] = [];
/**
* 所有关节的绑定姿势矩阵。该数组的长度始终与 `this.joints` 的长度相同。
*/
get bindposes () {
return this._bindposes;
}
set bindposes (value) {
this._bindposes = value;
}
/**
* 所有关节的路径。该数组的长度始终与 `this.bindposes` 的长度相同。
*/
get joints () {
return this._joints;
}
set joints (value) {
this._joints = value;
}
}
cc.Skeleton = Skeleton;
/*
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { Asset } from '../../assets/asset';
import { ccclass, property } from '../../core/data/class-decorator';
import { CCString } from '../../core/data/utils/attribute';
import { Mat4, Quat, Vec3 } from '../../core/value-types';
import { mat4 } from '../../core/vmath';
interface IBindTRS {
position: Vec3;
rotation: Quat;
scale: Vec3;
}
/**
* 骨骼资源。
* 骨骼资源记录了每个关节(相对于`SkinningModelComponent.SkinningRoot`)的路径以及它的绑定姿势矩阵。
*/
@ccclass('cc.Skeleton')
export class Skeleton extends Asset {
@property([CCString])
private _joints: string[] = [];
@property([Node])
private _bindposes: Mat4[] | null = null;
private _bindTRS: IBindTRS[] | null = null;
/**
* 所有关节的绑定姿势矩阵。该数组的长度始终与 `this.joints` 的长度相同。
*/
get bindposes () {
return this._bindposes;
}
set bindposes (value) {
this._bindposes = value;
if (!value) { this._bindTRS = null; return; }
this._bindTRS = value.map((m) => {
const position = new Vec3();
const rotation = new Quat();
const scale = new Vec3();
mat4.toRTS(m, rotation, position, scale);
return { position, rotation, scale };
});
}
get bindTRS () {
return this._bindTRS;
}
/**
* 所有关节的路径。该数组的长度始终与 `this.bindposes` 的长度相同。
*/
get joints () {
return this._joints;
}
set joints (value) {
this._joints = value;
}
public onLoaded () {
this.bindposes = this._bindposes;
}
}
cc.Skeleton = Skeleton;
+2 -2
View File
@@ -91,8 +91,8 @@ export class AudioSourceComponent extends Component {
* https://www.chromium.org/audio-video/autoplay
* @zh
* 是否启用自动播放。 <br>
* 请注意,根据最新的自动播放策略,大部分自动播放仅在收到用户指令后生效。 <br>
* 参看:https://www.chromium.org/audio-video/autoplay
* 请注意,根据最新的自动播放策略,现在大部分时候,自动播放只会在第一次收到用户输入后生效。 <br>
* @see https://www.chromium.org/audio-video/autoplay
*/
@property
set playOnAwake (val) {
+5 -3
View File
@@ -25,7 +25,8 @@
import { Texture2D } from '../../assets';
import { Filter, PixelFormat } from '../../assets/asset-enum';
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
import { Vec2 } from '../../core/value-types';
import { CCString } from '../../core/data/utils/attribute';
import { Mat4, Vec2 } from '../../core/value-types';
import { vec2 } from '../../core/vmath';
import { GFXFormat } from '../../gfx/define';
import { GFXAttributeName, GFXBufferTextureCopy, GFXFormatInfos } from '../../gfx/define';
@@ -154,7 +155,7 @@ export class AvatarModelComponent extends SkinningModelComponent {
this._combinedTexSize = size;
}
@property({ type: String })
@property({ type: CCString })
get albedoMapName (): string {
return this._albedoMapName;
}
@@ -300,6 +301,7 @@ export class AvatarModelComponent extends SkinningModelComponent {
if (!lca) { console.warn('illegal skinning roots'); return; }
// merge joints accordingly
const skeleton = new Skeleton();
skeleton.bindposes = [];
for (const unit of this._avatarUnits) {
if (!unit || !unit.skeleton || !unit.skinningRoot) { continue; }
const partial = unit.skeleton;
@@ -309,7 +311,7 @@ export class AvatarModelComponent extends SkinningModelComponent {
const idx = skeleton.joints.findIndex((p) => p === path);
if (idx >= 0) { continue; }
skeleton.joints.push(path);
skeleton.bindposes.push(partial.bindposes[i]);
skeleton.bindposes.push(partial.bindposes && partial.bindposes[i] || new Mat4());
}
}
// sort the array to be more cache-friendly
@@ -25,10 +25,11 @@
import { Texture2D } from '../../assets';
import { Filter, PixelFormat } from '../../assets/asset-enum';
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
import { Vec2 } from '../../core/value-types';
import { CCString } from '../../core/data/utils/attribute';
import { Mat4, Vec2 } from '../../core/value-types';
import { vec2 } from '../../core/vmath';
import { GFXFormat, GFXType } from '../../gfx/define';
import { GFXAttributeName, GFXBufferTextureCopy, GFXFormatInfos } from '../../gfx/define';
import { GFXFormat, GFXType } from '../../gfx/define';
import { GFXDevice } from '../../gfx/device';
import { IGFXAttribute } from '../../gfx/input-assembler';
import { Node } from '../../scene-graph';
@@ -111,7 +112,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
@property
public atlasSize: number = 1024;
@property({ type: [String] })
@property({ type: [CCString] })
public batchableTextureNames: string[] = [];
@property({ type: [SkinningModelUnit] })
public units: SkinningModelUnit[] = [];
@@ -216,6 +217,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
if (!lca) { console.warn('illegal skinning roots'); return; }
// merge joints accordingly
const skeleton = new Skeleton();
skeleton.bindposes = [];
for (const unit of this.units) {
if (!unit || !unit.skeleton || !unit.skinningRoot) { continue; }
const partial = unit.skeleton;
@@ -225,7 +227,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
const idx = skeleton.joints.findIndex((p) => p === path);
if (idx >= 0) { continue; }
skeleton.joints.push(path);
skeleton.bindposes.push(partial.bindposes[i]);
skeleton.bindposes.push(partial.bindposes && partial.bindposes[i] || new Mat4());
}
}
// sort the array to be more cache-friendly
@@ -237,8 +239,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
skeleton.joints = skeleton.joints.map((_, idx, arr) => arr[idxMap[idx]]);
skeleton.bindposes = skeleton.bindposes.map((_, idx, arr) => arr[idxMap[idx]]);
// apply
// @ts-ignore
super.skeleton = skeleton;
this.skeleton = skeleton;
}
public cookMeshes () {
+3 -5
View File
@@ -389,12 +389,10 @@ function calculateBoneSpaceBounds (mesh: Mesh, skeleton: Skeleton) {
continue;
}
const bindpose = skeleton.bindposes[refJointIndex];
const jointBounds = result[refJointIndex];
if (skeleton.bindposes) { vec3.transformMat4(v3_2, v3_1, skeleton.bindposes[refJointIndex]); }
else { vec3.copy(v3_2, v3_1); }
vec3.multiply(v3_2, v3_1, bindpose.scale);
vec3.transformQuat(v3_2, v3_2, bindpose.rotation);
vec3.add(v3_2, v3_2, bindpose.position);
const jointBounds = result[refJointIndex];
jointBounds.hasValue = true;
vec3.min(jointBounds.min, jointBounds.min, v3_2);
vec3.max(jointBounds.max, jointBounds.max, v3_2);
+1 -1
View File
@@ -720,7 +720,7 @@ export class mat4 {
const m10 = m3_1.m08 = mat.m10;
out.x = Math.sqrt(m00 * m00 + m01 * m01 + m02 * m02);
out.y = Math.sqrt(m04 * m04 + m05 * m05 + m06 * m06);
out.y = Math.sqrt(m08 * m08 + m09 * m09 + m10 * m10);
out.z = Math.sqrt(m08 * m08 + m09 * m09 + m10 * m10);
// account for refections
if (mat3.determinant(m3_1) < 0) { out.x *= -1; }
return out;
+21 -22
View File
@@ -244,61 +244,60 @@ export abstract class GFXInputAssembler extends GFXObject {
/**
* @en
* update VB data on the fly.
* @param vbuffer - an ArrayBuffer containing the full VB
* @param attr - name of the attribute to update (default names are specified in GFXAttributeName)
* @param data - the new VB attribute data to be uploaded
* @example
* ```typescript
* // get VB array buffer from mesh, better to cache this somewhere convenient
* const vbInfo = mesh.struct.vertexBundles[0].data;
* const vbuffer = mesh.data.buffer.slice(vbInfo.offset, vbInfo.offset + vbInfo.length);
* const submodel = someModelComponent.model.getSubModel(0);
* // say the new positions is stored in 'data' as a plain array
* submodel.inputAssembler.updateVertexBuffer(vbuffer, cc.GFXAttributeName.ATTR_POSITION, data);
* ```
* @zh
* 根据顶点属性名称更新顶点缓冲数据。
* @param vbuffer 缓冲数据源。
* @param attr 属性名。
* @param data 更新数据。
* @param index 要更新第几号顶点缓冲?(默认为 0)
* @param final 是否立即更新?(默认为 true,需要一次修改多个属性时可以使用)
* @example
* ```typescript
* // get VB array buffer from mesh, better to cache this somewhere convenient
* const vbInfo = mesh.struct.vertexBundles[mesh.struct.primitives[0].vertexBundelIndices[0]].view;
* const vbuffer = mesh.data.buffer.slice(vbInfo.offset, vbInfo.offset + vbInfo.length);
* const submodel = someModelComponent.model.getSubModel(0);
* // say the new positions is stored in 'data' as a plain array
* submodel.inputAssembler.updateVertexBuffer(vbuffer, cc.GFXAttributeName.ATTR_POSITION, data);
* ```
*/
public updateVertexAttr (vbuffer: GFXBufferSource, attr: string, data: number[]) {
public updateVertexAttr (vbuffer: GFXBufferSource, attr: string, data: number[], index = 0, final = true) {
let offset = 0;
let format = GFXFormat.UNKNOWN;
for (const a of this._attributes) {
if (a.name === attr) { format = a.format; break; }
offset += GFXFormatInfos[a.format].size;
}
const vb = this._vertexBuffers[0];
const vb = this._vertexBuffers[index];
if (!format || !vb) { return; }
writeBuffer(new DataView(vbuffer as ArrayBuffer), data, format, offset, vb.stride);
vb.update(vbuffer);
if (final) { vb.update(vbuffer, 0, vb.stride * vb.count); }
}
/**
* @en
* update IB data on the fly.
* need to call submodel.updateCommandBuffer after this if index count changed
* need to call `submodel.updateCommandBuffer` after this if index count changed
* @zh
* 更新索引缓冲数据,如果索引数量改变了,需要在此函数后调用 `submodel.updateCommandBuffer`。
* @param ibuffer 缓冲数据源。
* @param data 更新数据。
* @example
* ```typescript
* // get IB array buffer from mesh, better to cache this somewhere convenient
* const ibInfo = mesh.struct.primitives[0].indices.range;
* const ibInfo = mesh.struct.primitives[0].indexView;
* const ibuffer = mesh.data.buffer.slice(ibInfo.offset, ibInfo.offset + ibInfo.length);
* const submodel = someModelComponent.model.getSubModel(0);
* submodel.inputAssembler.updateIndexBuffer(ibuffer, [0, 1, 2]);
* submodel.updateCommandBuffer(); // index count changed
* ```
* @zh
* 更新索引缓冲数据。
* @param ibuffer 缓冲数据源。
* @param data 更新数据。
*/
public updateIndexBuffer (ibuffer: GFXBufferSource, data: number[]) {
const count = this._indexCount;
const ib = this._indexBuffer;
if (!count || !ib) { return; }
writeBuffer(new DataView(ibuffer as ArrayBuffer), data, GFXFormat[`R${ib.stride * 8}UI`]);
ib.update(ibuffer);
ib.update(ibuffer, 0, ib.stride * ib.count);
this._indexCount = data.length;
}
}
+24 -17
View File
@@ -156,17 +156,24 @@ export class SkinningModel extends Model {
if (!super.updateUBOs() || !this._skeleton) { return false; }
const len = this._joints.length;
for (let i = 0; i < len; ++i) {
const cur = this._joints[i]; cur.update();
const bindpose = this._skeleton.bindposes[i];
if (this._skeleton.bindTRS) { // TODO: pre-apply this into animation clip
for (let i = 0; i < len; ++i) {
const cur = this._joints[i]; cur.update();
const bindpose = this._skeleton.bindTRS[i];
vec3.multiply(v3_1, bindpose.position, cur.scale);
vec3.transformQuat(v3_1, v3_1, cur.rotation);
vec3.add(v3_1, v3_1, cur.position);
quat.multiply(qt_1, cur.rotation, bindpose.rotation);
vec3.multiply(v3_2, cur.scale, bindpose.scale);
vec3.multiply(v3_1, bindpose.position, cur.scale);
vec3.transformQuat(v3_1, v3_1, cur.rotation);
vec3.add(v3_1, v3_1, cur.position);
quat.multiply(qt_2, cur.rotation, bindpose.rotation);
vec3.multiply(v3_2, cur.scale, bindpose.scale);
this.updateJointData(i, v3_1, qt_1, v3_2, i === 0);
this.updateJointData(i, v3_1, qt_2, v3_2);
}
} else {
for (let i = 0; i < len; ++i) {
const cur = this._joints[i]; cur.update();
this.updateJointData(i, cur.position, cur.rotation, cur.scale);
}
}
this.commitJointData();
@@ -210,25 +217,25 @@ export class SkinningModel extends Model {
}
// Dual Quaternion Skinning
protected updateJointDataDQS (idx: number, pos: Vec3, rot: Quat, scale: Vec3, first = false) {
protected updateJointDataDQS (idx: number, pos: Vec3, rot: Quat, scale: Vec3) {
if (!this._jointsMedium) { return; }
const out = this._jointsMedium.nativeData;
const base = 12 * idx;
// sign consistency
if (first) { quat.copy(qt_0, rot); }
if (idx === 0) { quat.copy(qt_0, rot); }
else if (quat.dot(qt_0, rot) < 0) { quat.scale(rot, rot, -1); }
// conversion
quat.set(qt_2, pos.x, pos.y, pos.z, 0);
quat.scale(qt_2, quat.multiply(qt_2, qt_2, rot), 0.5);
quat.set(qt_1, pos.x, pos.y, pos.z, 0);
quat.scale(qt_1, quat.multiply(qt_1, qt_1, rot), 0.5);
// upload
out[base + 0] = rot.x;
out[base + 1] = rot.y;
out[base + 2] = rot.z;
out[base + 3] = rot.w;
out[base + 4] = qt_2.x;
out[base + 5] = qt_2.y;
out[base + 6] = qt_2.z;
out[base + 7] = qt_2.w;
out[base + 4] = qt_1.x;
out[base + 5] = qt_1.y;
out[base + 6] = qt_1.z;
out[base + 7] = qt_1.w;
out[base + 8] = scale.x;
out[base + 9] = scale.y;
out[base + 10] = scale.z;
+1 -1
View File
@@ -6,7 +6,7 @@ import { GFXCommandBuffer } from '../../gfx/command-buffer';
import { GFXCommandBufferType } from '../../gfx/define';
import { GFXInputAssembler } from '../../gfx/input-assembler';
import { GFXPipelineState } from '../../gfx/pipeline-state';
import { IInternalBindingInst, IRenderObject, UBOShadow } from '../../pipeline/define';
import { IInternalBindingInst, UBOShadow } from '../../pipeline/define';
import { DirectionalLight } from './directional-light';
import { Model } from './model';
import { RenderScene } from './render-scene';
+1 -1
View File
@@ -186,7 +186,7 @@ export class PlanarShadowInfo {
@property
protected _distance = 0;
@property
protected _shadowColor = new Color(76, 76, 76, 255);
protected _shadowColor = new Color(0, 0, 0, 76);
protected _resource: PlanarShadows | null = null;