diff --git a/cocos/3d/framework/skinning-model-component.ts b/cocos/3d/framework/skinning-model-component.ts index ee0737bb9c..a4f6e0bbea 100644 --- a/cocos/3d/framework/skinning-model-component.ts +++ b/cocos/3d/framework/skinning-model-component.ts @@ -27,7 +27,7 @@ import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator'; import { mat4 } from '../../core/vmath'; import { GFXDevice, GFXFeature } from '../../gfx/device'; -import { SkinningModel } from '../../renderer/models/skinning-model'; +import { SkinningModel, __FORCE_USE_UNIFORM_STORAGE__ } from '../../renderer/models/skinning-model'; import { Node } from '../../scene-graph/node'; import { Material } from '../assets/material'; import Skeleton from '../assets/skeleton'; @@ -131,15 +131,16 @@ export default class SkinningModelComponent extends ModelComponent { } public _updateModelParams () { - super._updateModelParams(); + // Should bind skeleton before super create pso this._bindSkeleton(); + super._updateModelParams(); } protected _onMaterialModified (index: number, material: Material) { super._onMaterialModified(index, material); const device = _getGlobalDevice(); - const useJointTexture = device !== null && device.hasFeature(GFXFeature.TEXTURE_FLOAT) && false; + const useJointTexture = !__FORCE_USE_UNIFORM_STORAGE__ && device !== null && device.hasFeature(GFXFeature.TEXTURE_FLOAT); this.getMaterial(0, CC_EDITOR)!.setDefines({ CC_USE_SKINNING: true, CC_USE_JOINTS_TEXTURE: useJointTexture, diff --git a/cocos/3d/misc/utils.ts b/cocos/3d/misc/utils.ts index dc34b4371c..7b2e70722c 100644 --- a/cocos/3d/misc/utils.ts +++ b/cocos/3d/misc/utils.ts @@ -1,44 +1,10 @@ import { GFXAttributeName, GFXPrimitiveMode } from '../../gfx/define'; export { default as find } from '../../scene-graph/find'; -import { Texture2D } from '../../assets/texture-2d'; -import { Filter, PixelFormat, WrapMode } from '../../assets/texture-base'; import { Vec3 } from '../../core/value-types'; import { AttributeBaseType, AttributeType, IMeshStruct, IndexUnit, IPrimitive, IVertexAttribute, IVertexBundle, Mesh } from '../assets/mesh'; import { IGeometry } from '../primitive/define'; -/** - * - */ -export function createJointsTexture (skinning: { joints: any[]; }) { - const jointCount = skinning.joints.length; - - // Set jointsTexture. - // A squared texture with side length N(N > 1) multiples of 2 can store - // 2 ^ (2 * N - 2) matrices. - // We support most 1024 joints. - let size = 0; - if (jointCount > 1024) { - throw new Error('To many joints(more than 1024).'); - } else if (jointCount > 256) { - size = 64; - } else if (jointCount > 64) { - size = 32; - } else if (jointCount > 16) { - size = 16; - } else if (jointCount > 4) { - size = 8; - } else { - size = 4; - } - - const texture = new Texture2D(); - texture.create(size, size, PixelFormat.RGBA32F); - texture.setFilters(Filter.NEAREST, Filter.NEAREST); - texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); - return texture; -} - const copyAttribute = (attribute: IVertexAttribute) => Object.assign({}, attribute); export function createMesh (geometry: IGeometry) { // Collect attributes and calculate length of result vertex buffer. diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index e16a86d647..ae5a1230c4 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -1,4 +1,4 @@ -//import * as WebGLDeveloperTools from 'webgl-debug'; +import * as WebGLDeveloperTools from 'webgl-debug'; import { GFXBindingLayout, GFXBindingUnit, IGFXBindingLayoutInfo } from '../binding-layout'; import { GFXBuffer, GFXBufferSource, IGFXBufferInfo, IGFXIndirectBuffer } from '../buffer'; import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator'; @@ -254,11 +254,9 @@ export class WebGLGFXDevice extends GFXDevice { }; this._webGLRC = this._canvas.getContext('webgl', webGLCtxAttribs); - /* if (this._webGLRC && info.debug) { this._webGLRC = WebGLDeveloperTools.makeDebugContext(this._webGLRC, this._onWebGLError.bind(this)); } - */ } catch (err) { console.error(err); return false; diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 7945b1bf94..a9e917167c 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -1,9 +1,9 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. -import { IBlockInfo, IBlockMember, IDefineInfo, IShaderInfo } from '../../3d/assets/effect-asset'; +import { IBlockInfo, IBlockMember, IDefineInfo, IShaderInfo, ISamplerInfo } from '../../3d/assets/effect-asset'; import { GFXGetTypeSize, GFXShaderType } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; -import { GFXShader, GFXUniform, GFXUniformBlock } from '../../gfx/shader'; +import { GFXShader, GFXUniform, GFXUniformBlock, GFXUniformSampler } from '../../gfx/shader'; import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline'; import { SkinningUBO } from '../models/model-uniforms'; import { IDefineMap } from './effect'; @@ -87,6 +87,8 @@ class ProgramLib { } if (def.name === 'CC_USE_SKINNING') { tmpl.blocks = tmpl.blocks.concat(skinning); + } else if (def.name === 'CC_USE_JOINTS_TEXTURE') { + tmpl.samplers = tmpl.samplers.concat(jointsTexture); } offset += cnt; def._offset = offset; @@ -151,6 +153,7 @@ class ProgramLib { const globals = convertToBlockInfo(UBOGlobal.BLOCK); const locals = convertToBlockInfo(UBOLocal.BLOCK); const skinning = convertToBlockInfo(SkinningUBO.BLOCK); +const jointsTexture = convertToSamplerInfo(SkinningUBO.JOINT_TEXTURE); function convertToUniformInfo (uniform: GFXUniform): IBlockMember { return { @@ -177,5 +180,15 @@ function convertToBlockInfo (block: GFXUniformBlock): IBlockInfo { }; } +function convertToSamplerInfo (sampler: GFXUniformSampler): ISamplerInfo { + return { + name: sampler.name, + binding: sampler.binding, + defines: [], + type: sampler.type, + count: sampler.count + }; +} + const programLib = cc.programLib = new ProgramLib(); export { programLib }; diff --git a/cocos/renderer/models/model-uniforms.ts b/cocos/renderer/models/model-uniforms.ts index 16ef5e976f..dc7fc83596 100644 --- a/cocos/renderer/models/model-uniforms.ts +++ b/cocos/renderer/models/model-uniforms.ts @@ -15,6 +15,6 @@ export class SkinningUBO { }; public static JOINT_TEXTURE: GFXUniformSampler = { - binding: 28, name: 'CCJointTexture', type: GFXType.SAMPLER2D, count: 1, + binding: 28, name: 'cc_jointsTexture', type: GFXType.SAMPLER2D, count: 1, }; } diff --git a/cocos/renderer/models/skinning-model.ts b/cocos/renderer/models/skinning-model.ts index e734f8f7d6..14e7e0a9a7 100644 --- a/cocos/renderer/models/skinning-model.ts +++ b/cocos/renderer/models/skinning-model.ts @@ -1,7 +1,6 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. import Skeleton from '../../3d/assets/skeleton'; -import { createJointsTexture } from '../../3d/misc/utils'; import { Texture2D } from '../../assets/texture-2d'; import { mat4 } from '../../core/vmath'; import { GFXBuffer } from '../../gfx/buffer'; @@ -12,6 +11,7 @@ import { Node } from '../../scene-graph/node'; import { Model } from '../scene/model'; import { RenderScene } from '../scene/render-scene'; import { SkinningUBO } from './model-uniforms'; +import { PixelFormat, Filter, WrapMode } from '../../assets/texture-base'; const textureSizeBuffer = new Float32Array(4); @@ -34,6 +34,8 @@ function isUniformStorage (storage: JointStorage): storage is IJointUniformsStor return !('texture' in storage); } +export const __FORCE_USE_UNIFORM_STORAGE__ = false; + export class SkinningModel extends Model { private _jointStorage: JointStorage | null = null; private _skinningUBO: GFXBuffer; @@ -52,8 +54,8 @@ export class SkinningModel extends Model { public bindSkeleton (skeleton: Skeleton) { this._destroyJointStorage(); - if (this._device.hasFeature(GFXFeature.TEXTURE_FLOAT) && false) { - const jointsTexture = createJointsTexture(skeleton); + if (!__FORCE_USE_UNIFORM_STORAGE__ && this._device.hasFeature(GFXFeature.TEXTURE_FLOAT)) { + const jointsTexture = _createJointsTexture(skeleton); textureSizeBuffer[0] = jointsTexture.width; this._skinningUBO.update( textureSizeBuffer, SkinningUBO.JOINTS_TEXTURE_SIZE_OFFSET, textureSizeBuffer.byteLength); @@ -129,3 +131,32 @@ function _setJointMatrix (out: Float32Array, iMatrix: number, matrix: mat4) { out[16 * iMatrix + 14] = matrix.m14; out[16 * iMatrix + 15] = matrix.m15; } + +function _createJointsTexture (skinning: { joints: any[]; }) { + const jointCount = skinning.joints.length; + + // Set jointsTexture. + // A squared texture with side length N(N > 1) multiples of 2 can store + // 2 ^ (2 * N - 2) matrices. + // We support most 1024 joints. + let size = 0; + if (jointCount > 1024) { + throw new Error('To many joints(more than 1024).'); + } else if (jointCount > 256) { + size = 64; + } else if (jointCount > 64) { + size = 32; + } else if (jointCount > 16) { + size = 16; + } else if (jointCount > 4) { + size = 8; + } else { + size = 4; + } + + const texture = new Texture2D(); + texture.create(size, size, PixelFormat.RGBA32F); + texture.setFilters(Filter.NEAREST, Filter.NEAREST); + texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); + return texture; +} \ No newline at end of file diff --git a/package.json b/package.json index 7bb2f28519..7e5cabdfa4 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "vinyl-buffer": "1.0.0", "vinyl-source-stream": "1.0.0", "watchify": "^3.7.0", - "webgl-debug": "github:cocos-creator/webgl-debug#5f9cbdd", + "webgl-debug": "github:cocos-creator/webgl-debug#4e535ed", "yargs": "^12.0.5" } } diff --git a/rollup/build-engine.ts b/rollup/build-engine.ts index 4a437caf29..7586926293 100644 --- a/rollup/build-engine.ts +++ b/rollup/build-engine.ts @@ -66,6 +66,7 @@ async function _doBundle (options: IBuildOptions) { commonjs({ namedExports: { cannon: ['CANNON', 'Shape'], + 'webgl-debug': ['makeDebugContext', 'glEnumToString'], }, }), ]; diff --git a/rollup/config.js b/rollup/config.js index 1bc59f2ff8..58c99afb04 100644 --- a/rollup/config.js +++ b/rollup/config.js @@ -60,7 +60,8 @@ export default { commonjs({ namedExports: { - 'cannon': ['CANNON'] + 'cannon': ['CANNON'], + 'webgl-debug': ['makeDebugContext', 'glEnumToString'] } }), ]