UNPKG

@senspark/ee

Version:

utility library for cocos creator

423 lines (422 loc) 15.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); var gl = require("gl-matrix"); var HsvMaterial_1 = require("./HsvMaterial"); var HsvShaderFrag_1 = require("./HsvShaderFrag"); var HsvShaderVert_1 = require("./HsvShaderVert"); var HsvUtils_1 = require("./HsvUtils"); var _a = cc._decorator, ccclass = _a.ccclass, disallowMultiple = _a.disallowMultiple, executeInEditMode = _a.executeInEditMode, menu = _a.menu, property = _a.property; /** * Finds the rendering node (_ccsg.Node). * @param view The view. */ var getRenderingNode = function (view) { if (view === undefined) { return undefined; } { var component = view.getComponent(cc.Sprite); if (component !== null) { return component._sgNode; } } { var component = view.getComponent(sp.Skeleton); if (component !== null && component._sgNode !== null) { return component._sgNode; } } { var component = view.getComponent(cc.Label); if (component !== null) { return component._sgNode; } } return undefined; }; var createRenderer = function (view) { if (cc.ENGINE_VERSION >= '2') { var renderer = new HsvRenderer_2_0_WebGL(); if (renderer.initialize(view)) { return renderer; } } else { if (cc.sys.isNative) { var renderer = new HsvRenderer_1_9_Native(); if (renderer.initialize(view)) { return renderer; } } else { var renderer = new HsvRenderer_1_9_WebGL(); if (renderer.initialize(view)) { return renderer; } } } return new NullHsvRenderer(); }; var NullHsvRenderer = /** @class */ (function () { function NullHsvRenderer() { } NullHsvRenderer.prototype.initialize = function (view) { return true; }; NullHsvRenderer.prototype.setEnabled = function (enabled) { }; NullHsvRenderer.prototype.updateMatrix = function (matrix) { }; NullHsvRenderer.prototype.updateMaterial = function () { }; return NullHsvRenderer; }()); // tslint:disable-next-line:class-name var HsvRenderer_1_9_WebGL = /** @class */ (function () { function HsvRenderer_1_9_WebGL() { } HsvRenderer_1_9_WebGL.prototype.isSupported = function () { return 'opengl' in cc.sys.capabilities; }; HsvRenderer_1_9_WebGL.prototype.initialize = function (view) { if (!this.isSupported()) { return false; } this.view = view; var program = new cc.GLProgram(); program.initWithString(HsvShaderVert_1.shader, HsvShaderFrag_1.shader); program.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION); program.addAttribute(cc.macro.ATTRIBUTE_NAME_COLOR, cc.macro.VERTEX_ATTRIB_COLOR); program.addAttribute(cc.macro.ATTRIBUTE_NAME_TEX_COORD, cc.macro.VERTEX_ATTRIB_TEX_COORDS); program.link(); program.updateUniforms(); this.program = program; return true; }; HsvRenderer_1_9_WebGL.prototype.setRenderingNode = function (node) { if (node === this.renderingNode) { return; } if (this.renderingNode !== undefined) { this.renderingNode.setShaderProgram(this.oldProgram); } this.renderingNode = node; if (this.renderingNode !== undefined) { this.oldProgram = this.renderingNode.getShaderProgram(); this.renderingNode.setShaderProgram(this.program); } }; HsvRenderer_1_9_WebGL.prototype.setEnabled = function (enabled) { if (enabled) { this.setRenderingNode(getRenderingNode(this.view)); } else { this.setRenderingNode(undefined); } }; HsvRenderer_1_9_WebGL.prototype.updateMatrix = function (matrix) { // Constantly update the current rendering node. this.setRenderingNode(getRenderingNode(this.view)); var array = Array.prototype.slice.call(matrix); var location = this.program.getUniformLocationForName('u_hsv'); this.program.use(); this.program.setUniformLocationWithMatrix4fv(location, array); }; HsvRenderer_1_9_WebGL.prototype.updateMaterial = function () { }; return HsvRenderer_1_9_WebGL; }()); // tslint:disable-next-line:class-name var HsvRenderer_1_9_Native = /** @class */ (function () { function HsvRenderer_1_9_Native() { } HsvRenderer_1_9_Native.prototype.isSupported = function () { return 'opengl' in cc.sys.capabilities; }; HsvRenderer_1_9_Native.prototype.initialize = function (view) { if (!this.isSupported()) { return false; } this.view = view; var program = new cc.GLProgram(); program.initWithString(HsvShaderVert_1.shader, HsvShaderFrag_1.shader); program.link(); program.updateUniforms(); this.programState = cc.GLProgramState.getOrCreateWithGLProgram(program); return true; }; HsvRenderer_1_9_Native.prototype.setRenderingNode = function (node) { if (node === this.renderingNode) { return; } if (this.renderingNode !== undefined) { this.renderingNode.setGLProgramState(this.oldProgramState); } this.renderingNode = node; if (this.renderingNode !== undefined) { this.oldProgramState = this.renderingNode.getGLProgramState(); this.renderingNode.setGLProgramState(this.programState); } }; HsvRenderer_1_9_Native.prototype.setEnabled = function (enabled) { if (enabled) { this.setRenderingNode(getRenderingNode(this.view)); } else { this.setRenderingNode(undefined); } }; HsvRenderer_1_9_Native.prototype.updateMatrix = function (matrix) { // Constantly update the current rendering node. this.setRenderingNode(getRenderingNode(this.view)); var array = Array.prototype.slice.call(matrix); this.programState.setUniformMat4('u_hsv', array); }; HsvRenderer_1_9_Native.prototype.updateMaterial = function () { }; return HsvRenderer_1_9_Native; }()); // tslint:disable-next-line:class-name var HsvRenderer_2_0_WebGL = /** @class */ (function () { function HsvRenderer_2_0_WebGL() { this.material = null; } HsvRenderer_2_0_WebGL.prototype.initialize = function (view) { this.view = view; var material = new HsvMaterial_1.HsvMaterial(); this.material = material; return true; }; HsvRenderer_2_0_WebGL.prototype.setEnabled = function (enabled) { // Do nothing. }; HsvRenderer_2_0_WebGL.prototype.updateMatrix = function (matrix) { var _a; var material = this.material; if (material === null) { return; } var array = Array.prototype.slice.call(matrix); // Convert gl.mat4 to cc.vmath.mat4. var convertedMatrix = cc.vmath.mat4.create(); (_a = cc.vmath.mat4.set).call.apply(_a, [null, convertedMatrix].concat(array)); material.setMatrix(convertedMatrix); }; HsvRenderer_2_0_WebGL.prototype.updateMaterial = function () { var material = this.material; if (material === null) { return; } var view = this.view; if (view === undefined) { return; } var sprite = view.getComponent(cc.Sprite); if (material === sprite.getMaterial()) { return; } sprite._updateMaterial(material); var texture = sprite.spriteFrame.getTexture(); material.setTexture(texture); if (sprite._renderData !== null) { sprite._renderData._material = material; } sprite.markForUpdateRenderData(true); sprite.markForRender(true); }; return HsvRenderer_2_0_WebGL; }()); var HsvComponent = /** @class */ (function (_super) { __extends(HsvComponent, _super); function HsvComponent() { var _this = _super.call(this) || this; _this._hue = 0; _this._brightness = 0.0; _this._saturation = 1.0; _this._contrast = 1.0; _this.hueMatrixDirty = true; _this.saturationMatrixDirty = true; _this.brightnessMatrixDirty = true; _this.contrastMatrixDirty = true; _this.matrix = gl.mat4.create(); _this.hueMatrix = gl.mat4.create(); _this.saturationMatrix = gl.mat4.create(); _this.brightnessMatrix = gl.mat4.create(); _this.contrastMatrix = gl.mat4.create(); _this.renderer = new NullHsvRenderer(); return _this; } Object.defineProperty(HsvComponent.prototype, "hue", { get: function () { return this._hue; }, set: function (value) { if (this._hue === value) { return; } this._hue = value; this.hueMatrixDirty = true; }, enumerable: true, configurable: true }); Object.defineProperty(HsvComponent.prototype, "brightness", { get: function () { return this._brightness; }, set: function (value) { if (this._brightness === value) { return; } this._brightness = value; this.brightnessMatrixDirty = true; }, enumerable: true, configurable: true }); Object.defineProperty(HsvComponent.prototype, "saturation", { get: function () { return this._saturation; }, set: function (value) { if (this._saturation === value) { return; } this._saturation = value; this.saturationMatrixDirty = true; }, enumerable: true, configurable: true }); Object.defineProperty(HsvComponent.prototype, "contrast", { get: function () { return this._contrast; }, set: function (value) { if (this._contrast === value) { return; } this._contrast = value; this.contrastMatrixDirty = true; }, enumerable: true, configurable: true }); HsvComponent.prototype.onLoad = function () { this.renderer = createRenderer(this.node); }; HsvComponent.prototype.onEnable = function () { this.renderer.setEnabled(true); }; HsvComponent.prototype.onDisable = function () { this.renderer.setEnabled(false); }; HsvComponent.prototype.update = function (delta) { if (this.updateMatrix()) { this.renderer.updateMatrix(this.matrix); } this.renderer.updateMaterial(); }; HsvComponent.prototype.updateMatrix = function () { var dirty = false; dirty = this.updateHueMatrix() || dirty; dirty = this.updateSaturationMatrix() || dirty; dirty = this.updateBrightnessMatrix() || dirty; dirty = this.updateContrastMatrix() || dirty; if (dirty) { gl.mat4.identity(this.matrix); gl.mat4.multiply(this.matrix, this.matrix, this.hueMatrix); gl.mat4.multiply(this.matrix, this.matrix, this.saturationMatrix); gl.mat4.multiply(this.matrix, this.matrix, this.brightnessMatrix); gl.mat4.multiply(this.matrix, this.matrix, this.contrastMatrix); return true; } return false; }; HsvComponent.prototype.updateHueMatrix = function () { if (this.hueMatrixDirty) { this.hueMatrixDirty = false; this.hueMatrix = HsvUtils_1.createHueMatrix(this.hue); return true; } return false; }; HsvComponent.prototype.updateSaturationMatrix = function () { if (this.saturationMatrixDirty) { this.saturationMatrixDirty = false; this.saturationMatrix = HsvUtils_1.createSaturationMatrix(this.saturation); return true; } return false; }; HsvComponent.prototype.updateBrightnessMatrix = function () { if (this.brightnessMatrixDirty) { this.brightnessMatrixDirty = false; this.brightnessMatrix = HsvUtils_1.createBrightnesMatrix(this.brightness, this.brightness, this.brightness); return true; } return false; }; HsvComponent.prototype.updateContrastMatrix = function () { if (this.contrastMatrixDirty) { this.contrastMatrixDirty = false; this.contrastMatrix = HsvUtils_1.createContrastMatrix(this.contrast, this.contrast, this.contrast); return true; } return false; }; __decorate([ property(cc.Integer) ], HsvComponent.prototype, "_hue", void 0); __decorate([ property(cc.Float) ], HsvComponent.prototype, "_brightness", void 0); __decorate([ property(cc.Float) ], HsvComponent.prototype, "_saturation", void 0); __decorate([ property(cc.Float) ], HsvComponent.prototype, "_contrast", void 0); __decorate([ property({ type: cc.Integer, min: 0, max: 359, slide: true, }) ], HsvComponent.prototype, "hue", null); __decorate([ property({ type: cc.Float, min: -1.0, max: +1.0, slide: true, }) ], HsvComponent.prototype, "brightness", null); __decorate([ property({ type: cc.Float }) ], HsvComponent.prototype, "saturation", null); __decorate([ property({ type: cc.Float }) ], HsvComponent.prototype, "contrast", null); HsvComponent = __decorate([ ccclass, disallowMultiple, executeInEditMode, menu('ee/HsvComponent') ], HsvComponent); return HsvComponent; }(cc.Component)); exports.HsvComponent = HsvComponent;