From 17b5d9bda39acb08b23b123c5d7a3a9bb3aee5f4 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Wed, 12 Dec 2018 19:12:22 +0800 Subject: [PATCH] texture cube from texture2d (#3624) * texture cube from texture2d * fix format --- cocos/3d/assets/texture-cube.js | 44 +++++++++++++------- cocos/core/value-types/quat.js | 30 ++----------- cocos/renderer/renderers/forward-renderer.js | 6 +-- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/cocos/3d/assets/texture-cube.js b/cocos/3d/assets/texture-cube.js index a8872690f4..b32875e10b 100644 --- a/cocos/3d/assets/texture-cube.js +++ b/cocos/3d/assets/texture-cube.js @@ -29,6 +29,7 @@ import { ccclass, property } from '../../core/data/class-decorator'; import ImageAsset from '../../assets/image-asset'; /** + * @typedef {import("../../assets/CCTexture2D")} Texture2D * @typedef {import("../../assets/texture-base").ImageSource} ImageSource * @typedef {import("../../assets/texture-base").TextureUpdateOpts} TextureUpdateOpts * @typedef {import("../../assets/texture-base").HTMLImageSource} HTMLImageSource @@ -39,14 +40,14 @@ import ImageAsset from '../../assets/image-asset'; /** * @enum {number} */ -const FaceIndex = { - front: 5, - back: 4, - left: 1, +const FaceIndex = cc.Enum({ right: 0, + left: 1, top: 2, bottom: 3, -}; + back: 4, + front: 5 +}); @ccclass('cc.TextureCube') export default class TextureCube extends TextureBase { @@ -58,7 +59,7 @@ export default class TextureCube extends TextureBase { constructor() { super(); - + /** * @type {GFXTextureCube} */ @@ -138,7 +139,7 @@ export default class TextureCube extends TextureBase { /** * Sets the _mipmaps to specified value, and updates the width and height accordingly. * If available, synchronize the mipmap data to underlying texture. - * @param {Mipmap[]} mipmaps + * @param {Mipmap[]} mipmaps */ _setMipmaps(mipmaps) { this._mipmaps = mipmaps; @@ -159,11 +160,11 @@ export default class TextureCube extends TextureBase { const inc = () => { ++counter; if (counter === this._mipmaps.length * 6) { - this._updateMipmaps(this._mipmaps.map(mipmap => TextureCube._getMipmapSource(mipmap))); + this._updateMipmaps(this._mipmaps.map(mipmap => TextureCube._toMipmapArray(mipmap))); } }; mipmaps.forEach(mipmap => { - TextureCube._foreachFace(mipmap, face => { + TextureCube._forEachFace(mipmap, face => { if (face.loaded) { inc(); } else { @@ -177,7 +178,7 @@ export default class TextureCube extends TextureBase { /** * Updates mipmaps of the underlying texture. - * @param {ImageSource[][]} mipmapSources + * @param {ImageSource[][]} mipmapSources */ _updateMipmaps(mipmapSources) { const opts = this._getOpts(); @@ -190,11 +191,10 @@ export default class TextureCube extends TextureBase { } /** - * - * @param {Mipmap} mipmap + * @param {Mipmap} mipmap * @return {ImageSource[]} */ - static _getMipmapSource(mipmap) { + static _toMipmapArray(mipmap) { /** @type {ImageSource[]} */ const result = new Array(); result[FaceIndex.front] = mipmap.front.data; @@ -210,11 +210,27 @@ export default class TextureCube extends TextureBase { * @param {Mipmap} mipmap * @param {(face: ImageAsset) => void} callback */ - static _foreachFace(mipmap, callback) { + static _forEachFace(mipmap, callback) { for (let face in mipmap) { callback(mipmap[face]); } } + + /** + * @param {Texture2D[]} textures + */ + static fromTexture2DArray(textures) { + let res = [], mips = textures.length / 6; + for (let i = 0; i < mips; i++) { + res.push(textures.splice(0, 6).reduce((acc, img, i) => { + acc[FaceIndex[i]] = img.image; + return acc; + }, {})); + } + let cube = new TextureCube(); + cube.mipmaps = res; + return cube; + } } cc.TextureCube = TextureCube; diff --git a/cocos/core/value-types/quat.js b/cocos/core/value-types/quat.js index 01a790e47a..30dcfc5f36 100644 --- a/cocos/core/value-types/quat.js +++ b/cocos/core/value-types/quat.js @@ -25,6 +25,7 @@ import ValueType from './value-type'; import CCClass from '../data/class'; +import quat from '../vmath/quat'; /** * !#en Representation of 2D vectors and points. @@ -109,32 +110,9 @@ export default class Quat extends ValueType { return other && this.x === other.x && this.y === other.y && this.z === other.z && this.w === other.w; } - getRoll () { - var sinr = 2.0 * (this.w * this.x + this.y * this.z); - var cosr = 1.0 - 2.0 * (this.x * this.x + this.y * this.y); - return 180 * Math.atan2(sinr, cosr) / Math.PI; - } - - getPitch () { - var sinp = 2.0 * (this.w * this.y - this.z * this.x); - var pitch = sinp > 1 ? 1 : sinp; - pitch = sinp < -1 ? -1 : sinp; - pitch = 180 * Math.asin(pitch) / Math.PI; - return pitch; - } - - getYaw () { - var siny = 2.0 * (this.w * this.z + this.x * this.y); - var cosy = 1.0 - 2.0 * (this.y * this.y + this.z * this.z); - return 180 * Math.atan2(siny, cosy) / Math.PI; - } - getEulerAngles (out) { out = out || cc.v3(); - out.x = this.getRoll(); - out.y = this.getPitch(); - out.z = this.getYaw(); - return out; + return quat.toEuler(out, this); } lerp (to, ratio, out) { @@ -160,8 +138,8 @@ CCClass.fastDefine('cc.Quat', Quat, { x: 0, y: 0, z: 0, w: 1 }); * @param {Number} [w=1] * @return {Quat} */ -cc.quat = function quat (x, y, z, w) { +cc.quat = function(x, y, z, w) { return new Quat(x, y, z, w); }; -cc.Quat = Quat; \ No newline at end of file +cc.Quat = Quat; diff --git a/cocos/renderer/renderers/forward-renderer.js b/cocos/renderer/renderers/forward-renderer.js index a02b8f013f..12053c838c 100644 --- a/cocos/renderer/renderers/forward-renderer.js +++ b/cocos/renderer/renderers/forward-renderer.js @@ -251,13 +251,13 @@ export default class ForwardRenderer extends BaseRenderer { _drawSkinning(item) { let { model, defines } = item; - defines.USE_SKINNING = true; + defines['USE_SKINNING'] = true; if (model._jointsTexture != null) { - defines.USE_JOINTS_TEXTURE = true; + defines['USE_JOINTS_TEXTURE'] = true; this._device.setTexture('_u_jointsTexture', model._jointsTexture, this._allocTextureUnit()); this._device.setUniform('_u_jointsTextureSize', model._jointsTexture._width); } else if (model._jointsMatrixArray != null) { - defines.USE_JOINTS_TEXTURE = false; + defines['USE_JOINTS_TEXTURE'] = false; this._device.setUniform("_u_jointMatrices", model._jointsMatrixArray); }