mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
134 lines
3.9 KiB
TypeScript
134 lines
3.9 KiB
TypeScript
import { GFXFilter, GFXStatus } from '../define';
|
|
import { GFXDevice } from '../device';
|
|
import { GFXSampler, GFXSamplerState, IGFXSamplerInfo } from '../sampler';
|
|
import { WebGLGPUSampler } from './webgl-gpu-objects';
|
|
|
|
const WebGLWraps: GLenum[] = [
|
|
WebGLRenderingContext.REPEAT,
|
|
WebGLRenderingContext.MIRRORED_REPEAT,
|
|
WebGLRenderingContext.CLAMP_TO_EDGE,
|
|
WebGLRenderingContext.CLAMP_TO_EDGE,
|
|
];
|
|
|
|
export class WebGLGFXSampler extends GFXSampler {
|
|
|
|
public get gpuSampler (): WebGLGPUSampler {
|
|
return this._gpuSampler!;
|
|
}
|
|
|
|
private _gpuSampler: WebGLGPUSampler | null = null;
|
|
|
|
constructor (device: GFXDevice) {
|
|
super(device);
|
|
|
|
this._state = new GFXSamplerState();
|
|
}
|
|
|
|
public initialize (info: IGFXSamplerInfo): boolean {
|
|
|
|
if (info.name !== undefined) {
|
|
this._state.name = info.name;
|
|
}
|
|
|
|
if (info.minFilter !== undefined) {
|
|
this._state.minFilter = info.minFilter;
|
|
}
|
|
|
|
if (info.magFilter !== undefined) {
|
|
this._state.magFilter = info.magFilter;
|
|
}
|
|
|
|
if (info.mipFilter !== undefined) {
|
|
this._state.mipFilter = info.mipFilter;
|
|
}
|
|
|
|
if (info.addressU !== undefined) {
|
|
this._state.addressU = info.addressU;
|
|
}
|
|
|
|
if (info.addressV !== undefined) {
|
|
this._state.addressV = info.addressV;
|
|
}
|
|
|
|
if (info.addressW !== undefined) {
|
|
this._state.addressW = info.addressW;
|
|
}
|
|
|
|
if (info.maxAnisotropy !== undefined) {
|
|
this._state.maxAnisotropy = info.maxAnisotropy;
|
|
}
|
|
|
|
if (info.cmpFunc !== undefined) {
|
|
this._state.cmpFunc = info.cmpFunc;
|
|
}
|
|
|
|
if (info.borderColor !== undefined) {
|
|
this._state.borderColor = info.borderColor;
|
|
}
|
|
|
|
if (info.minLOD !== undefined) {
|
|
this._state.minLOD = info.minLOD;
|
|
}
|
|
|
|
if (info.maxLOD !== undefined) {
|
|
this._state.maxLOD = info.maxLOD;
|
|
}
|
|
|
|
if (info.mipLODBias !== undefined) {
|
|
this._state.mipLODBias = info.mipLODBias;
|
|
}
|
|
|
|
let glMinFilter = WebGLRenderingContext.NONE;
|
|
let glMagFilter = WebGLRenderingContext.NONE;
|
|
|
|
const minFilter = this._state.minFilter;
|
|
const magFilter = this._state.magFilter;
|
|
const mipFilter = this._state.mipFilter;
|
|
|
|
if (minFilter === GFXFilter.LINEAR || minFilter === GFXFilter.ANISOTROPIC) {
|
|
if (mipFilter === GFXFilter.LINEAR || mipFilter === GFXFilter.ANISOTROPIC) {
|
|
glMinFilter = WebGLRenderingContext.LINEAR_MIPMAP_LINEAR;
|
|
} else if (mipFilter === GFXFilter.POINT) {
|
|
glMinFilter = WebGLRenderingContext.LINEAR_MIPMAP_NEAREST;
|
|
} else {
|
|
glMinFilter = WebGLRenderingContext.LINEAR;
|
|
}
|
|
} else {
|
|
if (mipFilter === GFXFilter.LINEAR || mipFilter === GFXFilter.ANISOTROPIC) {
|
|
glMinFilter = WebGLRenderingContext.NEAREST_MIPMAP_LINEAR;
|
|
} else if (mipFilter === GFXFilter.POINT) {
|
|
glMinFilter = WebGLRenderingContext.NEAREST_MIPMAP_NEAREST;
|
|
} else {
|
|
glMinFilter = WebGLRenderingContext.NEAREST;
|
|
}
|
|
}
|
|
|
|
if (magFilter === GFXFilter.LINEAR || magFilter === GFXFilter.ANISOTROPIC) {
|
|
glMagFilter = WebGLRenderingContext.LINEAR;
|
|
} else {
|
|
glMagFilter = WebGLRenderingContext.NEAREST;
|
|
}
|
|
|
|
const glWrapS = WebGLWraps[this._state.addressU];
|
|
const glWrapT = WebGLWraps[this._state.addressV];
|
|
const glWrapR = WebGLWraps[this._state.addressW];
|
|
|
|
this._gpuSampler = {
|
|
glMinFilter,
|
|
glMagFilter,
|
|
glWrapS,
|
|
glWrapT,
|
|
glWrapR,
|
|
};
|
|
|
|
this._status = GFXStatus.SUCCESS;
|
|
|
|
return true;
|
|
}
|
|
|
|
public destroy () {
|
|
this._gpuSampler = null;
|
|
this._status = GFXStatus.UNREADY;
|
|
}
|
|
}
|