mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
3d stable (#4041)
* 3d (#4036) * compiler error (#4033) * add gen decl module * fix global event and other bug (#4032) * components such as event-target to ts * divide CCEvent and CCTouch into game coordinates and ui coordinate mod * repair editor environment where mask does not take effect bug * fix render component blendState modification does not take effect bug * fix bug * fix ts tip * sampler from texture (#4037)
This commit is contained in:
@@ -29,6 +29,7 @@ import { GFXBindingType } from '../../gfx/define';
|
||||
import { GFXTextureView } from '../../gfx/texture-view';
|
||||
import { Effect } from '../../renderer/core/effect';
|
||||
import { IDefineMap, Pass, PassOverrides } from '../../renderer/core/pass';
|
||||
import { samplerLib } from '../../renderer/core/sampler-lib';
|
||||
import { builtinResMgr } from '../builtin';
|
||||
import { RenderableComponent } from '../framework/renderable-component';
|
||||
import { EffectAsset } from './effect-asset';
|
||||
@@ -189,7 +190,7 @@ export class Material extends Asset {
|
||||
} else {
|
||||
this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides);
|
||||
}
|
||||
this._onPassChange();
|
||||
this._onPassesChange();
|
||||
}
|
||||
|
||||
public onLoaded () {
|
||||
@@ -220,7 +221,7 @@ export class Material extends Asset {
|
||||
} else {
|
||||
this._props.fill({});
|
||||
}
|
||||
this._onPassChange();
|
||||
this._onPassesChange();
|
||||
}
|
||||
|
||||
protected _uploadProperty (pass: Pass, name: string, val: any) {
|
||||
@@ -234,15 +235,20 @@ export class Material extends Asset {
|
||||
pass.setUniform(handle, val);
|
||||
}
|
||||
} else if (bindingType === GFXBindingType.SAMPLER) {
|
||||
const textureView = val instanceof GFXTextureView ? val : val.getGFXTextureView();
|
||||
if (!textureView) { console.warn('texture asset incomplete!'); return false; }
|
||||
const binding = Pass.getBindingFromHandle(handle);
|
||||
pass.bindTextureView(binding, textureView);
|
||||
if (val instanceof GFXTextureView) {
|
||||
pass.bindTextureView(binding, val);
|
||||
} else {
|
||||
const textureView = val.getGFXTextureView();
|
||||
if (!textureView) { console.warn('texture asset incomplete!'); return false; }
|
||||
pass.bindTextureView(binding, textureView);
|
||||
pass.bindSampler(binding, samplerLib.getSampler(cc.game._gfxDevice, val.getGFXSamplerInfo()));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected _onPassChange () {
|
||||
protected _onPassesChange () {
|
||||
let str = '';
|
||||
for (const pass of this._passes) {
|
||||
str += pass.serializePipelineStates();
|
||||
|
||||
@@ -110,8 +110,7 @@ export class TextureCube extends TextureBase {
|
||||
super();
|
||||
}
|
||||
|
||||
onLoaded () {
|
||||
super.onLoaded();
|
||||
public onLoaded () {
|
||||
this.mipmaps = this._mipmaps;
|
||||
}
|
||||
|
||||
|
||||
Vendored
+20
-18
File diff suppressed because one or more lines are too long
@@ -6,7 +6,7 @@ import { AttributeBaseType, AttributeType,
|
||||
import { IGeometry } from '../primitive/define';
|
||||
|
||||
const copyAttribute = (attribute: IVertexAttribute) => Object.assign({}, attribute);
|
||||
export function createMesh (geometry: IGeometry) {
|
||||
export function createMesh (geometry: IGeometry, out?: Mesh) {
|
||||
// Collect attributes and calculate length of result vertex buffer.
|
||||
const attributes: IVertexAttribute[] = [];
|
||||
let stride = 0;
|
||||
@@ -103,10 +103,10 @@ export function createMesh (geometry: IGeometry) {
|
||||
};
|
||||
|
||||
// Create mesh.
|
||||
const mesh = new Mesh();
|
||||
mesh.assign(meshStruct, new Uint8Array(bufferBlob.getCombined()));
|
||||
if (!out) { out = new Mesh(); }
|
||||
out.assign(meshStruct, new Uint8Array(bufferBlob.getCombined()));
|
||||
|
||||
return mesh;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -78,7 +78,6 @@ export class Texture2D extends TextureBase {
|
||||
}
|
||||
|
||||
public onLoaded () {
|
||||
super.onLoaded();
|
||||
this.mipmaps = this._mipmaps;
|
||||
}
|
||||
|
||||
|
||||
+59
-125
@@ -27,15 +27,13 @@
|
||||
import {ccclass, property} from '../core/data/class-decorator';
|
||||
import { EventTargetFactory } from '../core/event/event-target-factory';
|
||||
import IDGenerator from '../core/utils/id-generator';
|
||||
import { ccenum } from '../core/value-types/enum';
|
||||
import { GFXAddress, GFXBufferTextureCopy, GFXFilter, GFXFormat,
|
||||
GFXTextureFlagBit, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define';
|
||||
import { GFXDevice } from '../gfx/device';
|
||||
import { GFXSampler } from '../gfx/sampler';
|
||||
import { GFXTexture, IGFXTextureInfo } from '../gfx/texture';
|
||||
import { GFXTextureView, IGFXTextureViewInfo } from '../gfx/texture-view';
|
||||
import { Asset } from './asset';
|
||||
import { ImageAsset, ImageSource } from './image-asset';
|
||||
import { ImageAsset } from './image-asset';
|
||||
|
||||
const CHAR_CODE_1 = 49; // '1'
|
||||
|
||||
@@ -101,11 +99,6 @@ export enum PixelFormat {
|
||||
*/
|
||||
RGBA_PVRTC_4BPPV1 = GFXFormat.PVRTC_RGBA4,
|
||||
}
|
||||
ccenum(PixelFormat);
|
||||
|
||||
function toGfxFormat (pixelFormat: PixelFormat) {
|
||||
return pixelFormat as unknown as GFXFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* The texture wrap mode.
|
||||
@@ -129,11 +122,6 @@ export enum WrapMode {
|
||||
*/
|
||||
CLAMP_TO_BORDER = GFXAddress.BORDER,
|
||||
}
|
||||
ccenum(WrapMode);
|
||||
|
||||
function toGfxAddressMode (wrapMode: WrapMode) {
|
||||
return wrapMode as unknown as GFXAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* The texture filter mode
|
||||
@@ -149,11 +137,6 @@ export enum Filter {
|
||||
*/
|
||||
NEAREST = GFXFilter.POINT,
|
||||
}
|
||||
ccenum(Filter);
|
||||
|
||||
function toGfxFilterMode (filter: Filter) {
|
||||
return filter as unknown as GFXFilter;
|
||||
}
|
||||
|
||||
@ccclass('cc.TextureBase')
|
||||
export class TextureBase extends EventTargetFactory(Asset) {
|
||||
@@ -196,6 +179,39 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
return texture._format >= PixelFormat.RGB_PVRTC_2BPPV1 && texture._format <= PixelFormat.RGBA_PVRTC_4BPPV1;
|
||||
}
|
||||
|
||||
@property
|
||||
protected _genMipmap = false;
|
||||
|
||||
@property
|
||||
protected _format: number = PixelFormat.RGBA8888;
|
||||
|
||||
@property
|
||||
protected _premultiplyAlpha = false;
|
||||
|
||||
@property
|
||||
protected _flipY = false;
|
||||
|
||||
@property
|
||||
protected _minFilter: number = Filter.LINEAR;
|
||||
|
||||
@property
|
||||
protected _magFilter: number = Filter.LINEAR;
|
||||
|
||||
@property
|
||||
protected _mipFilter: number = Filter.LINEAR;
|
||||
|
||||
@property
|
||||
protected _wrapU: number = WrapMode.REPEAT;
|
||||
|
||||
@property
|
||||
protected _wrapV: number = WrapMode.REPEAT;
|
||||
|
||||
@property
|
||||
protected _wrapW: number = WrapMode.REPEAT;
|
||||
|
||||
@property
|
||||
protected _anisotropy = 16;
|
||||
|
||||
protected _texture: GFXTexture | null = null;
|
||||
|
||||
protected _textureView: GFXTextureView | null = null;
|
||||
@@ -206,40 +222,10 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
|
||||
private _mipmapLevel: number = 1;
|
||||
|
||||
@property
|
||||
private _genMipmap = false;
|
||||
|
||||
@property
|
||||
private _format: PixelFormat = PixelFormat.RGBA8888;
|
||||
|
||||
@property
|
||||
private _premultiplyAlpha = false;
|
||||
|
||||
@property
|
||||
private _flipY = false;
|
||||
|
||||
@property
|
||||
private _minFilter = Filter.LINEAR;
|
||||
|
||||
@property
|
||||
private _magFilter = Filter.LINEAR;
|
||||
|
||||
@property
|
||||
private _mipFilter = Filter.LINEAR;
|
||||
|
||||
@property
|
||||
private _wrapS: WrapMode = WrapMode.CLAMP_TO_EDGE;
|
||||
|
||||
@property
|
||||
private _wrapT: WrapMode = WrapMode.CLAMP_TO_EDGE;
|
||||
|
||||
@property
|
||||
private _anisotropy = 1;
|
||||
|
||||
private _sampler: GFXSampler | null = null;
|
||||
|
||||
private _id: string;
|
||||
|
||||
private _samplerInfo: number[] = [];
|
||||
|
||||
protected constructor (flipY: boolean = false) {
|
||||
super();
|
||||
|
||||
@@ -262,7 +248,6 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
this._potientialHeight = height;
|
||||
this._format = format;
|
||||
this._mipmapLevel = mipmapLevel;
|
||||
this._updateSampler();
|
||||
this._recreateTexture();
|
||||
}
|
||||
|
||||
@@ -323,15 +308,14 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
* If the texture size is NPOT (non power of 2), then in can only use gl.CLAMP_TO_EDGE in gl.TEXTURE_WRAP_{S,T}.
|
||||
* !#zh 设置纹理包装模式。
|
||||
* 若纹理贴图尺寸是 NPOT(non power of 2),则只能使用 Texture2D.WrapMode.CLAMP_TO_EDGE。
|
||||
* @param wrapS
|
||||
* @param wrapT
|
||||
* @param wrapU
|
||||
* @param wrapV
|
||||
*/
|
||||
public setWrapMode (wrapS: WrapMode, wrapT: WrapMode) {
|
||||
if (this._wrapS !== wrapS || this._wrapT !== wrapT) {
|
||||
this._wrapS = wrapS;
|
||||
this._wrapT = wrapT;
|
||||
this._updateSampler();
|
||||
}
|
||||
public setWrapMode (wrapU: WrapMode, wrapV: WrapMode, wrapW?: WrapMode) {
|
||||
if (wrapU !== this._wrapU) { this._wrapU = wrapU; this._samplerInfo[3] = wrapU; }
|
||||
if (wrapV !== this._wrapV) { this._wrapV = wrapV; this._samplerInfo[4] = wrapV; }
|
||||
if (wrapW === undefined) { return; }
|
||||
if (wrapW !== this._wrapW) { this._wrapW = wrapW; this._samplerInfo[5] = wrapW; }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -341,11 +325,8 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
* @param magFilter
|
||||
*/
|
||||
public setFilters (minFilter: Filter, magFilter: Filter) {
|
||||
if (this._minFilter !== minFilter || this._magFilter !== magFilter) {
|
||||
this._minFilter = minFilter;
|
||||
this._magFilter = magFilter;
|
||||
this._updateSampler();
|
||||
}
|
||||
if (minFilter !== this._minFilter) { this._minFilter = minFilter; this._samplerInfo[0] = minFilter; }
|
||||
if (magFilter !== this._magFilter) { this._magFilter = magFilter; this._samplerInfo[1] = magFilter; }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,10 +335,7 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
* @param mipFilter
|
||||
*/
|
||||
public setMipFilter (mipFilter: Filter) {
|
||||
if (this._mipFilter !== mipFilter) {
|
||||
this._mipFilter = mipFilter;
|
||||
this._updateSampler();
|
||||
}
|
||||
if (mipFilter !== this._mipFilter) { this._mipFilter = mipFilter; this._samplerInfo[2] = mipFilter; }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,10 +345,7 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
* @param flipY
|
||||
*/
|
||||
public setFlipY (flipY: boolean) {
|
||||
if (this._flipY !== flipY) {
|
||||
this._flipY = flipY;
|
||||
this._updateSampler();
|
||||
}
|
||||
this._flipY = flipY;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -380,10 +355,7 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
* @param premultiply
|
||||
*/
|
||||
public setPremultiplyAlpha (premultiply: boolean) {
|
||||
if (this._premultiplyAlpha !== premultiply) {
|
||||
this._premultiplyAlpha = premultiply;
|
||||
this._updateSampler();
|
||||
}
|
||||
this._premultiplyAlpha = premultiply;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -392,10 +364,7 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
* @param anisotropy
|
||||
*/
|
||||
public setAnisotropy (anisotropy: number) {
|
||||
if (this._anisotropy !== anisotropy) {
|
||||
this._anisotropy = anisotropy;
|
||||
this._updateSampler();
|
||||
}
|
||||
if (anisotropy !== this._anisotropy) { this._anisotropy = anisotropy; this._samplerInfo[6] = anisotropy; }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -415,10 +384,6 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
public destroy () {
|
||||
this._destroyTexture();
|
||||
|
||||
if (this._sampler) {
|
||||
this._sampler.destroy();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -430,8 +395,8 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
return this._textureView;
|
||||
}
|
||||
|
||||
public getGFXSampler () {
|
||||
return this._sampler;
|
||||
public getGFXSamplerInfo () {
|
||||
return this._samplerInfo;
|
||||
}
|
||||
|
||||
// SERIALIZATION
|
||||
@@ -441,7 +406,7 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
*/
|
||||
public _serialize (): any {
|
||||
return this._minFilter + ',' + this._magFilter + ',' +
|
||||
this._wrapS + ',' + this._wrapT + ',' +
|
||||
this._wrapU + ',' + this._wrapV + ',' +
|
||||
(this._premultiplyAlpha ? 1 : 0) + ',' +
|
||||
this._mipFilter + ',' + this._anisotropy + ',' +
|
||||
(this._flipY ? 1 : 0) + ',' +
|
||||
@@ -458,17 +423,15 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
fields.unshift('');
|
||||
if (fields.length >= 6) {
|
||||
// decode filters
|
||||
this._minFilter = parseInt(fields[1], undefined);
|
||||
this._magFilter = parseInt(fields[2], undefined);
|
||||
this.setFilters(parseInt(fields[1]), parseInt(fields[2]));
|
||||
// decode wraps
|
||||
this._wrapS = parseInt(fields[3], undefined);
|
||||
this._wrapT = parseInt(fields[4], undefined);
|
||||
this.setWrapMode(parseInt(fields[3]), parseInt(fields[4]));
|
||||
// decode premultiply alpha
|
||||
this._premultiplyAlpha = fields[5].charCodeAt(0) === CHAR_CODE_1;
|
||||
}
|
||||
if (fields.length >= 8) {
|
||||
this._mipFilter = parseInt(fields[6], undefined);
|
||||
this._anisotropy = parseInt(fields[7], undefined);
|
||||
this.setMipFilter(parseInt(fields[6]));
|
||||
this.setAnisotropy(parseInt(fields[7]));
|
||||
}
|
||||
if (fields.length >= 10) {
|
||||
this._flipY = fields[8].charCodeAt(0) === CHAR_CODE_1;
|
||||
@@ -476,10 +439,6 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
}
|
||||
}
|
||||
|
||||
public onLoaded () {
|
||||
this._updateSampler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates mipmaps at level 0.
|
||||
*/
|
||||
@@ -508,10 +467,6 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
}
|
||||
}
|
||||
|
||||
protected _getGfxFormat () {
|
||||
return toGfxFormat(this._format);
|
||||
}
|
||||
|
||||
protected _assignImage (image: ImageAsset, level: number, arrayIndex?: number) {
|
||||
const upload = () => {
|
||||
if (!image.data) {
|
||||
@@ -582,7 +537,7 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
return {
|
||||
type: GFXTextureType.TEX2D,
|
||||
usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST,
|
||||
format: toGfxFormat(this._format),
|
||||
format: this._format,
|
||||
width: this._potientialWidth,
|
||||
height: this._potientialHeight,
|
||||
mipLevel: this._mipmapLevel,
|
||||
@@ -594,7 +549,7 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
return {
|
||||
texture: this._texture!,
|
||||
type: GFXTextureViewType.TV2D,
|
||||
format: this._getGfxFormat(),
|
||||
format: this._format,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -622,27 +577,6 @@ export class TextureBase extends EventTargetFactory(Asset) {
|
||||
this._texture = null;
|
||||
}
|
||||
}
|
||||
|
||||
private _updateSampler () {
|
||||
if (this._sampler) {
|
||||
this._sampler.destroy();
|
||||
}
|
||||
|
||||
const gfxDevice = this._getGlobalDevice();
|
||||
if (!gfxDevice) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._sampler = gfxDevice.createSampler({
|
||||
name: `Sampler of ${this.name}`,
|
||||
minFilter: toGfxFilterMode(this._minFilter),
|
||||
magFilter: toGfxFilterMode(this._magFilter),
|
||||
addressU: toGfxAddressMode(this._wrapS),
|
||||
addressV: toGfxAddressMode(this._wrapT),
|
||||
maxAnisotropy: this._anisotropy,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/* tslint:disable:no-string-literal */
|
||||
cc['TextureBase'] = TextureBase;
|
||||
cc.TextureBase = TextureBase;
|
||||
|
||||
@@ -22,9 +22,9 @@ export class GFXSamplerState {
|
||||
public minFilter: GFXFilter = GFXFilter.LINEAR;
|
||||
public magFilter: GFXFilter = GFXFilter.LINEAR;
|
||||
public mipFilter: GFXFilter = GFXFilter.NONE;
|
||||
public addressU: GFXAddress = GFXAddress.CLAMP;
|
||||
public addressV: GFXAddress = GFXAddress.CLAMP;
|
||||
public addressW: GFXAddress = GFXAddress.CLAMP;
|
||||
public addressU: GFXAddress = GFXAddress.WRAP;
|
||||
public addressV: GFXAddress = GFXAddress.WRAP;
|
||||
public addressW: GFXAddress = GFXAddress.WRAP;
|
||||
public maxAnisotropy: number = 16;
|
||||
public cmpFunc: GFXComparisonFunc = GFXComparisonFunc.NEVER;
|
||||
public borderColor: IGFXColor = { r: 0, g: 0, b: 0, a: 0 };
|
||||
|
||||
@@ -83,7 +83,7 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
|
||||
this.addRenderPass(RenderPassStage.DEFAULT, windowPass);
|
||||
|
||||
this._macros.USE_HDR = this._isHDR;
|
||||
this._macros.CC_USE_HDR = this._isHDR;
|
||||
|
||||
// create flows
|
||||
this.createFlow(ForwardFlow, {
|
||||
|
||||
@@ -233,7 +233,7 @@ export abstract class RenderPipeline {
|
||||
|
||||
// colorFmt = GFXFormat.RGBA16F;
|
||||
|
||||
console.info('USE_HDR: ' + this._isHDR);
|
||||
console.info('CC_USE_HDR: ' + this._isHDR);
|
||||
console.info('Shading Color Format: ' + GFXFormatInfos[colorFmt].name);
|
||||
console.info('Shading Depth Format: ' + GFXFormatInfos[depthStencilFmt].name);
|
||||
|
||||
|
||||
@@ -13,12 +13,13 @@ import { GFXPipelineLayout } from '../../gfx/pipeline-layout';
|
||||
import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState,
|
||||
GFXInputState, GFXPipelineState, GFXRasterizerState } from '../../gfx/pipeline-state';
|
||||
import { GFXRenderPass } from '../../gfx/render-pass';
|
||||
import { GFXSampler, IGFXSamplerInfo } from '../../gfx/sampler';
|
||||
import { GFXSampler } from '../../gfx/sampler';
|
||||
import { GFXShader } from '../../gfx/shader';
|
||||
import { GFXTextureView } from '../../gfx/texture-view';
|
||||
import { RenderPassStage, RenderPriority } from '../../pipeline/define';
|
||||
import { RenderPipeline } from '../../pipeline/render-pipeline';
|
||||
import { programLib } from './program-lib';
|
||||
import { samplerLib } from './sampler-lib';
|
||||
|
||||
export interface IDefineMap { [name: string]: number | boolean | string; }
|
||||
export interface IPassInfoFull extends IPassInfo {
|
||||
@@ -170,13 +171,12 @@ export class Pass {
|
||||
for (const u of shaderInfo.samplers) {
|
||||
this._handleMap[u.name] = genHandle(GFXBindingType.SAMPLER, u.type, u.binding);
|
||||
const inf = info.properties && info.properties[u.name];
|
||||
const samplerInfo = Object.assign({}, inf && inf.sampler);
|
||||
const sampler = getSampler(device, samplerInfo);
|
||||
if (sampler) { this._samplers[u.binding] = sampler; }
|
||||
else { console.error('create sampler failed.'); }
|
||||
const texName = inf && inf.value ? inf.value + '-texture' : _type2default[u.type];
|
||||
const texture = builtinResMgr.get<TextureBase>(texName);
|
||||
if (texture) { this._textureViews[u.binding] = texture.getGFXTextureView()!; }
|
||||
if (texture) {
|
||||
this._textureViews[u.binding] = texture.getGFXTextureView()!;
|
||||
this._samplers[u.binding] = samplerLib.getSampler(device, texture.getGFXSamplerInfo());
|
||||
}
|
||||
else { console.warn('illegal texture default value ' + texName); }
|
||||
}
|
||||
|
||||
@@ -408,21 +408,3 @@ const serializeDepthStencilState = (dss: GFXDepthStencilState) => {
|
||||
res += `,${dss.stencilFailOpBack},${dss.stencilZFailOpBack},${dss.stencilPassOpBack},${dss.stencilWriteMaskBack}`;
|
||||
return res;
|
||||
};
|
||||
|
||||
const samplerLib = new Map<string, GFXSampler>();
|
||||
const getSampler = (() => {
|
||||
const serializeObject = (info: Object) => {
|
||||
let res = '';
|
||||
for (const key of Object.keys(info).sort()) {
|
||||
const value = info[key];
|
||||
res += `${key}:${typeof value === 'object' ? serializeObject(value) : value},`;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
return (device: GFXDevice, info: IGFXSamplerInfo) => {
|
||||
const s = serializeObject(info);
|
||||
let res = samplerLib.get(s);
|
||||
if (!res) { res = device.createSampler(info); samplerLib.set(s, res); }
|
||||
return res;
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -24,18 +24,21 @@ export interface IDefineValue {
|
||||
result: string | number;
|
||||
}
|
||||
|
||||
const mapDefine = (def: number | string | boolean) => {
|
||||
switch (typeof def) {
|
||||
const mapDefine = (info: IDefineInfo, def: number | string | boolean) => {
|
||||
switch (info.type) {
|
||||
case 'boolean': return def ? 1 : 0;
|
||||
case 'string': case 'number': return def;
|
||||
case 'string': return def !== undefined ? def as string : info.options![0];
|
||||
case 'number': return def !== undefined ? def as number : info.range![0];
|
||||
}
|
||||
return 0;
|
||||
console.warn(`unknown define type '${info.type}'`);
|
||||
return -1; // should neven happen
|
||||
};
|
||||
|
||||
const prepareDefines = (defs: IDefineMap, tDefs: IDefineInfo[]) => {
|
||||
const defines: IDefineValue[] = [];
|
||||
for (const { name } of tDefs) {
|
||||
const result = mapDefine(defs[name]);
|
||||
for (const tmpl of tDefs) {
|
||||
const name = tmpl.name;
|
||||
const result = mapDefine(tmpl, defs[name]);
|
||||
defines.push({ name, result });
|
||||
}
|
||||
return defines;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import { GFXDevice } from '../../gfx/device';
|
||||
import { GFXSampler, IGFXSamplerInfo } from '../../gfx/sampler';
|
||||
|
||||
const gfxInfo: IGFXSamplerInfo = {};
|
||||
|
||||
class SamplerLib {
|
||||
protected _cache: Record<string, GFXSampler> = {};
|
||||
|
||||
public getSampler (device: GFXDevice, info: number[]) {
|
||||
const hash = info.toString();
|
||||
const cache = this._cache[hash];
|
||||
if (cache) { return cache; }
|
||||
gfxInfo.minFilter = info[0];
|
||||
gfxInfo.magFilter = info[1];
|
||||
gfxInfo.mipFilter = info[2];
|
||||
gfxInfo.addressU = info[3];
|
||||
gfxInfo.addressV = info[4];
|
||||
gfxInfo.addressW = info[5];
|
||||
gfxInfo.maxAnisotropy = info[6];
|
||||
if (info.length > 7) {
|
||||
gfxInfo.cmpFunc = info[7];
|
||||
gfxInfo.borderColor = { r: info[8], g: info[9], b: info[10], a: info[11] };
|
||||
gfxInfo.minLOD = info[12];
|
||||
gfxInfo.maxLOD = info[13];
|
||||
gfxInfo.mipLODBias = info[14];
|
||||
}
|
||||
const sampler = this._cache[hash] = device.createSampler(gfxInfo);
|
||||
return sampler;
|
||||
}
|
||||
}
|
||||
|
||||
export const samplerLib = new SamplerLib();
|
||||
cc.samplerLib = samplerLib;
|
||||
Reference in New Issue
Block a user