Skip to content

RoyTexture

RoyTexture是纹理对象,同RoyMesh一样,也是一种资源对象。同样因为GPU对纹理的格式有类似的要求,所以我们依然可以抽象出公用的结构体。

typescript

/**
 * Interface for options when creating a new texture.
 */
interface RoyTextureOptions {
    /** The name of the texture. */
    name?: string;
    /** The width of the texture. */
    width?: number;
    /** The depth of the texture. */
    depth?: number;
    /** The height of the texture. */
    height?: number;
    /** The mip level of the texture. */
    level?: number;
    /** Whether the texture is in sRGB format. */
    srgb?: boolean;
    /** Whether the texture dimensions should be a power of 2. */
    powerof2Boo?: boolean;
    /** The internal format of the texture. */
    internalFmt?: Texture$InternalFormat;
    /** The usage of the texture. */
    usage?: Texture$Usage;
    /** The sampler type of the texture.*/
    samplerType?: string;
}
/**
 * Interface for the data of a skybox texture
 */
interface RoyTextureData extends RoyTransDataInfo {
    /** The options for the texture */
    options: RoyTextureOptions;
    /** The pixel data format of the texture */
    pixelDataFormat?: string;
    /** The pixel data type of the texture */
    pixelDataType?: string;
    /** The data type of the data */
    dataType: string;
    /** The data of the texture */
    data: any[];
}

其中RoyTextureData中的data是纹理数据,考虑有纹理有mipmap或者texturearray,所以是一个数组。

我们在plugins/plugins_texture下提供了各种形式的RoyTextureProvider,RoyTextureProvider不仅承担了内存优化,而且也承担了纹理解码的功能,解码出来的数据是符合GPU的统一形式,再上传到GPU中,如果想深入了解,那么可以阅读一个你熟悉的纹理格式的plugin。


在GPU上,往往有纹理数量的限制,如一个材质最大不得超过16张纹理。在做一些细致的材质表达的时候,往往很容易超出这个限制,那么就需要一个新的机制,来保证这一点。

  • 我们使用TextureArray的方式。在实现上,定义了一个RoyTextureArrayProvider,如果材质模板定了这种纹理是可以放入TextureArray的,那么TextureArrayProvider会将这些RoyTexture的RoyTextureProvider收集起来,并最终解码形成可以上传到GPU的TextureArray的数据格式。
  • 那么材质中的哪些纹理是可以被texturearray合并的呢,这个是依据业务侧定义的,那些从来不动态替换的纹理,如lightmap、normalmap这种类型的,永远不会动态替换,那么就可以去形成texturearray,这样texturearray也是比较稳定的,不会执行因纹理动态切换而产生重建的操作。