UNPKG

@awayjs/renderer

Version:
160 lines (159 loc) 6.15 kB
import { __extends } from "tslib"; import { ErrorBase } from '@awayjs/core'; import { ImageUtils } from '@awayjs/stage'; import { Texture2D } from './Texture2D'; var ImageTexture2D = /** @class */ (function (_super) { __extends(ImageTexture2D, _super); function ImageTexture2D(image, mappingMode) { if (image === void 0) { image = null; } if (mappingMode === void 0) { mappingMode = null; } var _this = _super.call(this, mappingMode) || this; _this.setNumImages(1); _this.image = image; return _this; } Object.defineProperty(ImageTexture2D.prototype, "assetType", { /** * * @returns {string} */ get: function () { return ImageTexture2D.assetType; }, enumerable: false, configurable: true }); Object.defineProperty(ImageTexture2D.prototype, "sampler", { /** * * @returns {ImageBase} */ get: function () { return this._samplers[0]; }, set: function (value) { if (this._samplers[0] == value) return; this.setSamplerAt(value, 0); }, enumerable: false, configurable: true }); Object.defineProperty(ImageTexture2D.prototype, "image", { /** * * @returns {ImageBase} */ get: function () { return this._images[0]; }, set: function (value) { if (this._images[0] == value) return; if (!ImageUtils.isImage2DValid(value)) throw new ErrorBase('Invalid imageData: Width and height must be power of 2 and cannot exceed 2048'); this.setImageAt(value, 0); }, enumerable: false, configurable: true }); ImageTexture2D.assetType = '[texture ImageTexture2D]'; return ImageTexture2D; }(Texture2D)); export { ImageTexture2D }; import { _Shader_ImageTexture } from './ImageTextureCube'; import { MappingMode } from '../base/MappingMode'; import { ShaderBase } from '../base/ShaderBase'; /** * * @class away.pool.GL_SingleImageTexture */ var _Shader_ImageTexture2D = /** @class */ (function (_super) { __extends(_Shader_ImageTexture2D, _super); function _Shader_ImageTexture2D() { return _super !== null && _super.apply(this, arguments) || this; } /** * * @param shader * @param regCache * @param targetReg The register in which to store the sampled colour. * @param uvReg The uv coordinate vector with which to sample the texture map. * @returns {string} * @private */ _Shader_ImageTexture2D.prototype._getFragmentCode = function (targetReg, regCache, sharedReg, inputReg) { var code = ''; var temp; //modify depending on mapping mode if (this.texture.mappingMode == MappingMode.RADIAL) { temp = regCache.getFreeFragmentVectorTemp(); code += 'mul ' + temp + '.xy, ' + inputReg + ', ' + inputReg + '\n'; code += 'add ' + temp + '.x, ' + temp + '.x, ' + temp + '.y\n'; code += 'sub ' + temp + '.y, ' + temp + '.y, ' + temp + '.y\n'; code += 'sqt ' + temp + '.x, ' + temp + '.x, ' + temp + '.x\n'; inputReg = temp; } //handles texture atlasing if (this._shader.useImageRect) { var samplerReg = regCache.getFreeFragmentConstant(); this._samplerIndex = samplerReg.index * 4; temp = regCache.getFreeFragmentVectorTemp(); code += 'mul ' + temp + ', ' + inputReg + ', ' + samplerReg + '.xy\n'; code += 'add ' + temp + ', ' + temp + ', ' + samplerReg + '.zw\n'; inputReg = temp; } code += _super.prototype._getFragmentCode.call(this, targetReg, regCache, sharedReg, inputReg); //un-premultiply alpha if is is required if (this._shader.usesPremultipliedAlpha) { var tmp = regCache.getFreeFragmentSingleTemp(); code += 'sge ' + tmp + ' #native vec4(0.0) native#, ' + targetReg + '.w \n'; code += 'add ' + tmp + ', ' + tmp + ',' + targetReg + '.w \n'; code += 'div ' + targetReg + '.xyz, ' + targetReg + ', ' + tmp + '\n'; } return code; }; _Shader_ImageTexture2D.prototype.activate = function () { _super.prototype.activate.call(this); var sampler = this._shader.renderMaterial.samplers[this._imageIndex]; if (this._shader.useImageRect) { var index = this._samplerIndex; var data = this._shader.fragmentConstantData; if (!sampler.imageRect) { data[index] = 1; data[index + 1] = 1; data[index + 2] = 0; data[index + 3] = 0; } else { data[index] = sampler.imageRect.width; data[index + 1] = sampler.imageRect.height; data[index + 2] = sampler.imageRect.x; data[index + 3] = sampler.imageRect.y; } } }; _Shader_ImageTexture2D.prototype._setRenderState = function (renderState) { _super.prototype._setRenderState.call(this, renderState); var sampler = renderState.samplers[this._imageIndex]; if (this._shader.useImageRect && sampler) { var index = this._samplerIndex; var data = this._shader.fragmentConstantData; if (!sampler.imageRect) { data[index] = 1; data[index + 1] = 1; data[index + 2] = 0; data[index + 3] = 0; } else { data[index] = sampler.imageRect.width; data[index + 1] = sampler.imageRect.height; data[index + 2] = sampler.imageRect.x; data[index + 3] = sampler.imageRect.y; } } }; return _Shader_ImageTexture2D; }(_Shader_ImageTexture)); export { _Shader_ImageTexture2D }; ShaderBase.registerAbstraction(_Shader_ImageTexture2D, ImageTexture2D);