new adaptations for gizmos (#3438)

* gizmos adapt

* working texture cube

* misc fixes

* gizmo lighting
This commit is contained in:
Yun Hsiao Wu
2018-10-31 11:54:58 +08:00
committed by Huabing.Xu
parent 0931e808c7
commit e540797785
12 changed files with 238 additions and 320 deletions
+48 -128
View File
@@ -23,138 +23,58 @@
THE SOFTWARE.
****************************************************************************/
// @ts-check
import { _decorator } from "../../core/data/index";
const { ccclass } = _decorator;
import Texture from '../../assets/CCTexture2D';
import Texture2D from '../../assets/CCTexture2D';
import gfx from '../../renderer/gfx';
import { gfxFilters, gfxWraps, gfxTextureFmts } from '../misc/mappings';
let _opts = {
images: [],
mipmap: true,
width: 2,
height: 2,
format: gfx.TEXTURE_FMT_RGBA8,
anisotropy: 1,
wrapS: gfx.WRAP_REPEAT,
wrapT: gfx.WRAP_REPEAT,
minFilter: gfx.FILTER_LINEAR,
magFilter: gfx.FILTER_LINEAR,
mipFilter: gfx.FILTER_LINEAR,
let isComplete = function(elements) {
return elements.reduce((acc, t) => acc = acc && t.complete, false);
};
function _updateOpts(out, texture) {
out.images = texture._images;
out.mipmap = texture._mipmap;
out.width = texture._width;
out.height = texture._height;
out.format = gfxTextureFmts[texture._format];
out.anisotropy = texture._anisotropy;
out.wrapS = gfxWraps[texture._wrapS];
out.wrapT = gfxWraps[texture._wrapT];
out.minFilter = gfxFilters[texture._minFilter];
out.magFilter = gfxFilters[texture._magFilter];
out.mipFilter = gfxFilters[texture._mipFilter];
}
export default class TextureCube extends Texture2D {
export class TextureCube extends Texture {
constructor(device, width = 2, height = 2, fmt = 'rgba8') {
super(device);
//
this._images = [];
this._mipmap = true;
this._width = width;
this._height = height;
this._format = fmt;
this._anisotropy = 1;
this._wrapS = 'repeat';
this._wrapT = 'repeat';
this._minFilter = 'linear';
this._magFilter = 'linear';
this._mipFilter = 'linear';
}
destroy() {
this._texture.destory();
return super.destroy();
}
setImages(imgs) {
this._images = imgs;
this.resize(imgs[0][0].width, imgs[0][0].height);
}
resize(width, height) {
this._width = width;
this._height = height;
}
get width() {
return this._width;
}
get height() {
return this._height;
}
set mipmap(val) {
this._mipmap = val;
}
get mipmap() {
return this._mipmap;
}
set anisotropy(val) {
this._anisotropy = val;
}
get anisotropy() {
return this._anisotropy;
}
set wrapS(val) {
this._wrapS = val;
}
get wrapS() {
return this._wrapS;
}
set wrapT(val) {
this._wrapT = val;
}
get wrapT() {
return this._wrapT;
}
set minFilter(val) {
this._minFilter = val;
}
get minFilter() {
return this._minFilter;
}
set magFilter(val) {
this._magFilter = val;
}
get magFilter() {
return this._magFilter;
}
set mipFilter(val) {
this._mipFilter = val;
}
get mipFilter() {
return this._mipFilter;
}
commit() {
_updateOpts(_opts, this);
if (!this._texture) {
this._texture = new gfx.TextureCube(this._device, _opts);
} else {
this._texture.update(_opts);
constructor() {
super();
this._ctor = gfx.TextureCube;
}
handleLoadedTexture () {
let image = this._image[0];
if (!image || !image.width || !image.height)
return;
this.width = image.width;
this.height = image.height;
this._updateTexture([this._image]);
//dispatch load event to listener.
this.loaded = true;
this.emit("load");
this._image.forEach(img => {
if (cc.macro.CLEANUP_IMAGE_CACHE && image instanceof HTMLImageElement) {
// wechat game platform will cache image parsed data,
// so image will consume much more memory than web, releasing it
img.src = "";
// Release image in loader cache
cc.loader.removeItem(img.id);
}
});
}
initWithElement (element) {
if (!element) return;
this._image = element;
if (CC_WECHATGAME || CC_QQPLAY || isComplete(element)
|| element[0] instanceof HTMLCanvasElement) {
this.handleLoadedTexture();
return;
}
element.forEach(() => addEventListener('load', () => {
if (isComplete(element)) this.handleLoadedTexture();
}));
element.forEach(() => addEventListener('error', err => {
cc.warnID(3119, err.message);
}));
}
this._images = [];
}
}
+2 -14
View File
@@ -4,13 +4,13 @@
"stages": ["transparent"],
"passes": [
{
"program": "unlit",
"program": "gizmo",
"cullMode": "none",
"depthTest": false,
"depthWrite": false,
"blend": true,
"blendEq": "add",
"blendSrc": "one",
"blendSrc": "srcAlpha",
"blendDst": "oneMinusSrcAlpha",
"blendAlphaEq": "add",
"blendSrcAlpha": "one",
@@ -25,18 +25,6 @@
"color": {
"type": "color4",
"value": [1, 1, 1, 1]
},
"mainTiling": {
"type": "vec2",
"value": [1, 1]
},
"mainOffset": {
"type": "vec2",
"value": [0, 0]
},
"mainTexture": {
"type": "sampler2D",
"value": null
}
}
}
+45 -73
View File
@@ -4,10 +4,9 @@ import * as primitives from '../primitive';
import Mesh from '../assets/mesh';
import Material from '../assets/material';
import Texture2D from '../../assets/CCTexture2D';
// import TextureCube from '../assets/texture-cube';
import TextureCube from '../assets/texture-cube';
import Effect from '../../renderer/core/effect';
// import Technique from '../../renderer/core/technique';
// import Sprite from '../assets/sprite';
import Sprite from '../assets/sprite';
import { vec3 } from '../../core/vmath';
import effectJsons from './effects/index';
@@ -36,69 +35,51 @@ export default function (device) {
defaultTexture.setWrapMode(Texture2D.WrapMode.REPEAT, Texture2D.WrapMode.REPEAT);
defaultTexture._uuid = 'default-texture';
defaultTexture.initWithElement(canvas);
// defaultTexture.mipmap = true;
// defaultTexture.wrapS = 'repeat';
// defaultTexture.wrapT = 'repeat';
// defaultTexture.setImage(0, canvas);
// defaultTexture.commit();
// defaultTexture._uuid = 'default-texture';
// defaultTexture._loaded = true;
// default-texture-cube
// let defaultTextureCube = new TextureCube(device);
// defaultTextureCube.setImages(
// [[canvas, canvas, canvas, canvas, canvas, canvas]]
// );
// defaultTextureCube.commit();
// defaultTextureCube._uuid = 'default-texture-cube';
// defaultTextureCube._loaded = true;
let defaultTextureCube = new TextureCube(device);
defaultTextureCube._uuid = 'default-texture-cube';
defaultTextureCube.initWithElement(
[canvas, canvas, canvas, canvas, canvas, canvas]
);
// // black texture canvas fill
// canvas.width = canvas.height = 2;
// context.fillStyle = '#000';
// context.fillRect(0, 0, 2, 2);
// black texture canvas fill
canvas.width = canvas.height = 2;
context.fillStyle = '#000';
context.fillRect(0, 0, 2, 2);
// // black-texture
// let blackTexture = new Texture2D(device);
// blackTexture.mipmap = false;
// blackTexture.wrapS = 'repeat';
// blackTexture.wrapT = 'repeat';
// blackTexture.minFilter = 'nearest';
// blackTexture.magFilter = 'nearest';
// blackTexture.setImage(0, canvas);
// blackTexture.commit();
// blackTexture._uuid = 'black-texture';
// blackTexture._loaded = true;
// black-texture
let blackTexture = new Texture2D(device);
blackTexture.setMipmap(false);
blackTexture.setFilters(Texture2D.Filter.NEAREST, Texture2D.Filter.NEAREST);
blackTexture.setWrapMode(Texture2D.WrapMode.REPEAT, Texture2D.WrapMode.REPEAT);
blackTexture._uuid = 'black-texture';
defaultTexture.initWithElement(canvas);
// // white texture canvas fill
// canvas.width = canvas.height = 2;
// context.fillStyle = '#fff';
// context.fillRect(0, 0, 2, 2);
// white texture canvas fill
canvas.width = canvas.height = 2;
context.fillStyle = '#fff';
context.fillRect(0, 0, 2, 2);
// // white-texture
// let whiteTexture = new Texture2D(device);
// whiteTexture.mipmap = false;
// whiteTexture.wrapS = 'repeat';
// whiteTexture.wrapT = 'repeat';
// whiteTexture.minFilter = 'nearest';
// whiteTexture.magFilter = 'nearest';
// whiteTexture.setImage(0, canvas);
// whiteTexture.commit();
// whiteTexture._uuid = 'white-texture';
// whiteTexture._loaded = true;
// white-texture
let whiteTexture = new Texture2D(device);
blackTexture.setMipmap(false);
blackTexture.setFilters(Texture2D.Filter.NEAREST, Texture2D.Filter.NEAREST);
blackTexture.setWrapMode(Texture2D.WrapMode.REPEAT, Texture2D.WrapMode.REPEAT);
whiteTexture._uuid = 'white-texture';
defaultTexture.initWithElement(canvas);
// // ============================
// // builtin sprites
// // ============================
// ============================
// builtin sprites
// ============================
// // default-sprites
// let defaultSprite = new Sprite();
// defaultSprite._texture = whiteTexture;
// defaultSprite.width = whiteTexture.width;
// defaultSprite.height = whiteTexture.height;
// defaultSprite.commit();
// defaultSprite._uuid = 'default-sprite';
// defaultSprite._loaded = true;
// default-sprites
let defaultSprite = new Sprite();
defaultSprite.texture = whiteTexture;
defaultSprite.width = whiteTexture.width;
defaultSprite.height = whiteTexture.height;
defaultSprite._uuid = 'default-sprite';
defaultSprite.commit();
// ============================
// builtin meshes
@@ -175,16 +156,8 @@ export default function (device) {
let effects = {};
for (let i = 0; i < effectJsons.length; ++i) {
let effectJson = effectJsons[i];
// let effect = new Effect();
let effect = {};
effect._name = effectJson.name;
effect._uuid = `builtin-effect-${effectJson.name}`;
// effect._loaded = true;
effect.techniques = effectJson.techniques;
effect.properties = effectJson.properties;
effect.defines = effectJson.defines;
effect.dependencies = effectJson.dependencies ? effectJson.dependencies : [];
let effect = effectJsons[i];
effect._uuid = `builtin-effect-${effect.name}`;
effects[effect._uuid] = effect;
Effect.registerEffect(effect);
}
@@ -205,13 +178,12 @@ export default function (device) {
// materials[mat._uuid] = mat;
// });
//
return Object.assign({
[defaultTexture._uuid]: defaultTexture,
// [defaultTextureCube._uuid]: defaultTextureCube,
// [blackTexture._uuid]: blackTexture,
// [whiteTexture._uuid]: whiteTexture,
// [defaultSprite._uuid]: defaultSprite,
[defaultTextureCube._uuid]: defaultTextureCube,
[blackTexture._uuid]: blackTexture,
[whiteTexture._uuid]: whiteTexture,
[defaultSprite._uuid]: defaultSprite,
[cubeMesh._uuid]: cubeMesh,
[sphereMesh._uuid]: sphereMesh,
[cylinderMesh._uuid]: cylinderMesh,
-1
View File
@@ -87,7 +87,6 @@ let ModelShadowCastingMode = Enum({
*/
@ccclass('cc.ModelComponent')
@executionOrder(100)
@executeInEditMode
@menu('Components/ModelComponent')
@executeInEditMode
export default class ModelComponent extends RenderableComponent {
+61 -75
View File
@@ -25,7 +25,7 @@
****************************************************************************/
import EventTarget from '../core/event/event-target';
import Tex_GFX from '../renderer/gfx/texture-2d';
import gfx from '../renderer/gfx';
import IDGenerator from '../core/utils/id-generator';
import Enum from '../core/value-types/enum';
import {addon} from '../core/utils/js';
@@ -292,7 +292,7 @@ export default class Texture2D extends Asset {
_wrapT = WrapMode.CLAMP_TO_EDGE;
@property
_anisotropy = 0;
_anisotropy = 1;
static PixelFormat = PixelFormat;
@@ -353,6 +353,7 @@ export default class Texture2D extends Asset {
*/
this.height = 0;
this._ctor = gfx.Texture2D;
this._texture = null;
if (CC_EDITOR) {
@@ -463,6 +464,60 @@ export default class Texture2D extends Asset {
}
}
_updateTexture(images) {
let opts = _getSharedOptions();
opts.images = images;
opts.width = this.width;
opts.height = this.height;
opts.hasMipmap = this._hasMipmap;
opts.format = this._format;
opts.premultiplyAlpha = this._premultiplyAlpha;
opts.anisotropy = this._anisotropy;
opts.flipY = this._flipY;
opts.minFilter = FilterIndex[this._minFilter];
opts.magFilter = FilterIndex[this._magFilter];
opts.mipFilter = FilterIndex[this._mipFilter];
opts.wrapS = this._wrapS;
opts.wrapT = this._wrapT;
if (!this._texture) {
this._texture = new this._ctor(cc.game._renderContext, opts);
}
else {
this._texture.update(opts);
}
}
/**
* !#en
* Handler of texture loaded event.
* Since v2.0, you don't need to invoke this function, it will be invoked automatically after texture loaded.
* !#zh 贴图加载事件处理器。v2.0 之后你将不在需要手动执行这个函数,它会在贴图加载成功之后自动执行。
* @method handleLoadedTexture
* @param {Boolean} [premultiplied]
*/
handleLoadedTexture () {
if (!this._image || !this._image.width || !this._image.height)
return;
this.width = this._image.width;
this.height = this._image.height;
this._updateTexture([this._image]);
//dispatch load event to listener.
this.loaded = true;
this.emit("load");
if (cc.macro.CLEANUP_IMAGE_CACHE && this._image instanceof HTMLImageElement) {
// wechat game platform will cache image parsed data,
// so image will consume much more memory than web, releasing it
this._image.src = "";
// Release image in loader cache
cc.loader.removeItem(this._image.id);
}
}
/**
* !#en
* Init with HTML element.
@@ -504,30 +559,12 @@ export default class Texture2D extends Asset {
* @return {Boolean}
*/
initWithData (data, pixelFormat, pixelsWidth, pixelsHeight) {
var opts = _getSharedOptions();
opts.image = data;
// webgl texture 2d uses images
opts.images = [opts.image];
opts.hasMipmap = this._hasMipmap;
opts.premultiplyAlpha = this._premultiplyAlpha;
opts.anisotropy = this._anisotropy;
opts.flipY = this._flipY;
opts.minFilter = FilterIndex[this._minFilter];
opts.magFilter = FilterIndex[this._magFilter];
opts.mipFilter = FilterIndex[this._mipFilter];
opts.wrapS = this._wrapS;
opts.wrapT = this._wrapT;
opts.format = pixelFormat;
opts.width = pixelsWidth;
opts.height = pixelsHeight;
if (!this._texture) {
this._texture = new Tex_GFX(cc.game._renderContext, opts);
}
else {
this._texture.update(opts);
}
this.width = pixelsWidth;
this.height = pixelsHeight;
this._format = pixelFormat;
this._updateTexture([data]);
this.loaded = true;
this.emit("load");
return true;
@@ -607,57 +644,6 @@ export default class Texture2D extends Asset {
return this._hasMipmap || false;
}
/**
* !#en
* Handler of texture loaded event.
* Since v2.0, you don't need to invoke this function, it will be invoked automatically after texture loaded.
* !#zh 贴图加载事件处理器。v2.0 之后你将不在需要手动执行这个函数,它会在贴图加载成功之后自动执行。
* @method handleLoadedTexture
* @param {Boolean} [premultiplied]
*/
handleLoadedTexture () {
if (!this._image || !this._image.width || !this._image.height)
return;
this.width = this._image.width;
this.height = this._image.height;
let opts = _getSharedOptions();
opts.image = this._image;
// webgl texture 2d uses images
opts.images = [opts.image];
opts.width = this.width;
opts.height = this.height;
opts.hasMipmap = this._hasMipmap;
opts.format = this._format;
opts.premultiplyAlpha = this._premultiplyAlpha;
opts.anisotropy = this._anisotropy;
opts.flipY = this._flipY;
opts.minFilter = FilterIndex[this._minFilter];
opts.magFilter = FilterIndex[this._magFilter];
opts.mipFilter = FilterIndex[this._mipFilter];
opts.wrapS = this._wrapS;
opts.wrapT = this._wrapT;
if (!this._texture) {
this._texture = new Tex_GFX(cc.game._renderContext, opts);
}
else {
this._texture.update(opts);
}
//dispatch load event to listener.
this.loaded = true;
this.emit("load");
if (cc.macro.CLEANUP_IMAGE_CACHE && this._image instanceof HTMLImageElement) {
// wechat game platform will cache image parsed data,
// so image will consume much more memory than web, releasing it
this._image.src = "";
// Release image in loader cache
cc.loader.removeItem(this._image.id);
}
}
/**
* !#en
* Description of cc.Texture2D.
+1 -1
View File
@@ -773,7 +773,7 @@ var game = {
let builtins = this._builtins = initBuiltins(device);
this._renderer.setBuiltins({
defaultTexture: builtins['default-texture']._texture,
defaultTextureCube: null,
defaultTextureCube: builtins['default-texture-cube']._texture,
});
}
// renderer.initWebGL(localCanvas, opts);
+41 -24
View File
@@ -84,14 +84,14 @@ class VertexBuffer {
}
if (CC_EDITOR) {
let _type2fn = {
[enums.ATTR_TYPE_INT8]: 'getInt8',
[enums.ATTR_TYPE_UINT8]: 'getUint8',
[enums.ATTR_TYPE_INT16]: 'getInt16',
[enums.ATTR_TYPE_UINT16]: 'getUint16',
[enums.ATTR_TYPE_INT32]: 'getInt32',
[enums.ATTR_TYPE_UINT32]: 'getUint32',
[enums.ATTR_TYPE_FLOAT32]: 'getFloat32'
let _typeMap = {
[enums.ATTR_TYPE_INT8]: 'Int8',
[enums.ATTR_TYPE_UINT8]: 'Uint8',
[enums.ATTR_TYPE_INT16]: 'Int16',
[enums.ATTR_TYPE_UINT16]: 'Uint16',
[enums.ATTR_TYPE_INT32]: 'Int32',
[enums.ATTR_TYPE_UINT32]: 'Uint32',
[enums.ATTR_TYPE_FLOAT32]: 'Float32'
};
let littleEndian = (function() {
let buffer = new ArrayBuffer(2);
@@ -99,28 +99,39 @@ if (CC_EDITOR) {
// Int16Array uses the platform's endianness.
return new Int16Array(buffer)[0] === 256;
})();
let getAttrAt = function(data, idx, fmt, out = []) {
let createAttrCache = function(fmt, data) {
if (!data) return [];
let vcount = data.byteLength / fmt.stride;
let bytes = fmt.bytes / fmt.num;
let fname = `get${_typeMap[fmt.type]}`;
let result = new Array(vcount * fmt.num);
for (let i = 0; i < vcount; i++) {
let offset = i * fmt.stride + fmt.offset;
for (let j = 0; j < fmt.num; j++)
result[i * fmt.num + j] = data[fname](offset + j * bytes, littleEndian);
}
return result;
};
let writeAttrBack = function(fmt, attrData, data) {
let vcount = attrData.length / fmt.num;
let bytes = fmt.bytes / fmt.num;
let fname =`set${_typeMap[fmt.type]}`;
for (let i = 0; i < vcount; i++) {
let offset = i * fmt.stride + fmt.offset;
for (let j = 0; j < fmt.num; j++) {
data[fname](offset + j * bytes, attrData[i * fmt.num + j], littleEndian);
}
}
};
VertexBuffer.getAttrAt = function(data, idx, fmt, out = []) {
let offset = idx * fmt.stride + fmt.offset;
let bytes = fmt.bytes / fmt.num;
let fname = _type2fn[fmt.type];
let fname = `get${_typeMap[fmt.type]}`;
for (let i = 0; i < fmt.num; i++) {
out[i] = data[fname](offset + i * bytes, littleEndian);
}
return out;
};
let createAttrCache = function(vb, data, attr) {
if (!data) return [];
let fmt = vb._format.element(attr);
let vcount = data.byteLength / fmt.stride;
let result = new Array(vcount * fmt.num);
let t = new Array(fmt.num);
for (let i = 0; i < vcount; i++) {
getAttrAt(data, i, fmt, t);
for (let j = 0; j < fmt.num; j++)
result[i * fmt.num + j] = t[j];
}
return result;
};
VertexBuffer.prototype.do_update = VertexBuffer.prototype.update;
VertexBuffer.prototype.update = function(offset, data) {
this.do_update(offset, data);
@@ -130,7 +141,13 @@ if (CC_EDITOR) {
data = new DataView(data);
else if (ArrayBuffer.isView(data) && !(data instanceof DataView))
data = new DataView(data.buffer, data.byteOffset, data.byteLength);
this[enums.ATTR_POSITION] = createAttrCache(this, data, enums.ATTR_POSITION);
this.data = data; this.offset = offset;
this[enums.ATTR_POSITION] = createAttrCache(this._format.element(enums.ATTR_POSITION), data);
};
VertexBuffer.prototype.updateAttr = function(attr, data) {
if (!data) data = this[attr];
writeAttrBack(this._format.element(attr), data, this.data);
this.do_update(this.offset, this.data);
};
}
@@ -0,0 +1,16 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
#include <gamma-correction.frag>
uniform vec4 color;
varying vec3 normal_w;
void main () {
vec3 N = normalize(normal_w);
vec3 L = normalize(vec3(1, 2, 3));
vec3 diffuse = color.rgb * (0.2 + max(0.0, dot(N, L)) * 0.8);
gl_FragColor = linearToGammaSpaceRGBA(vec4(diffuse, color.a));
}
@@ -0,0 +1,21 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
attribute vec3 a_position;
attribute vec3 a_normal;
uniform mat4 model;
uniform mat4 viewProj;
uniform mat3 normalMatrix;
varying vec3 normal_w;
void main () {
vec4 pos = vec4(a_position, 1);
pos = viewProj * model * pos;
vec4 normal = vec4(a_normal, 0);
normal_w = normalMatrix * normal.xyz;
gl_Position = pos;
}
+1
View File
@@ -53,6 +53,7 @@ Layers._nextAvailable = 8;
Layers.Default = (1 << 0);
Layers.IgnoreRaycast = (1 << 1);
Layers.Gizmos = (1 << 2);
Layers.PanPlanes = (1 << 3);
// masks
Layers.All = Layers.makeExclusiveMask([Layers.Gizmos]);
+1 -2
View File
@@ -10,8 +10,7 @@ let dest = './bin';
let file = 'cocos-3d.dev';
let moduleName = 'cocos3d';
// clear directory
fsJetpack.dir(dest, { empty: true });
fsJetpack.dir(dest);
module.exports = {
input: './index.js',
+1 -2
View File
@@ -11,8 +11,7 @@ let name = 'mappings';
let sourcemap = true;
let globals = {};
// clear directory
fsJetpack.dir(dest, { empty: true });
fsJetpack.dir(dest);
module.exports = {
input: './cocos/3d/misc/offline-mappings.js',