Source: package/wrapped_texture.js

import { RepeatWrapping, ClampToEdgeWrapping, MirroredRepeatWrapping, Vector2, Texture, sRGBEncoding, LinearEncoding } from '../../node_modules/three/build/three.module.js';
import { copyProps, checkPropTypes, UUIDRegex } from '../lib.js';
import { Reporter } from '../reporter/reporter.js';
import { WrappedImage } from './image/wrapped_image.js';
import { BuildableComponent } from './component/buildable_component.js';
import { Component } from '../component/component.js';
import { Project } from '../project.js';

const wrapTypes = [ RepeatWrapping, ClampToEdgeWrapping, MirroredRepeatWrapping];

const checkWrapType = val => {
    if ( ! wrapTypes.includes( val ) ) {
        return `Unknown wrapping ${val}`;
    }
    return true;
}

/** ProductBuilder Texture */

class WrappedTexture extends BuildableComponent {

    /**
     * @param {Reporter} reporter
     * @param {Object} settings
     * @param {UUID} [settings.id]
     * @param {string} [settings.name]
     * @param {number} [settings.anisotropy]
     * @param {(RepeatWrapping|ClampToEdgeWrapping|MirroredRepeatWrapping)} [settings.wrapS]
     * @param {(RepeatWrapping|ClampToEdgeWrapping|MirroredRepeatWrapping)} [settings.wrapT]
     * @param {Vector2} [settings.repeat]
     * @param {WrappedImage} settings.image
     */

    constructor(reporter, settings) {

        checkPropTypes(
            settings,
            {
                image: WrappedImage
            },
            {
                id: UUIDRegex,
                name: "string",
                wrapS: checkWrapType,
                wrapT: checkWrapType,
                repeat: Vector2,
                anisotropy: 'number'
            }
        );

        super(reporter, settings);
    }

    static _exportName = {
        singular: 'texture',
        plural: 'textures'
    };


    /**
     * Build texture
     * @protected
     * @method
     * @param {LoadingQuality} quality
     * @returns {Promise<Component>}
     */

    async _build(part, quality, dependencies) {

        this.report({ msg: `Building ${quality} ${part}` });

        switch (part) {
            
            case 'UI':
                if (this._settings.thumbnail) {
                    this._setContent('UI', quality, this._settings.thumbnail.content.main[quality]);
                }
                else {
                    this._setContent('UI', quality, Project.defaultImages.missing.cloneNode(true));
                }
                break;

            case 'main':
                const texture = new Texture();

                const propsToCopy = Object.keys(texture).filter(p => p !== 'image');

                //console.log( propsToCopy )

                copyProps({
                    from: this._settings,
                    into: texture,
                    required: [],
                    optional: propsToCopy
                });

                // console.log( dependencies );

                const maxAnisotropy = Project.maxAnisotropy 

                texture.anisotropy = maxAnisotropy
                texture.image = dependencies.image.content.main[quality];
                texture.flipY = false;

                //console.log( this.settings.name )

                texture.encoding = sRGBEncoding; //deze niet uitvoeren op de normal map!!

                texture.needsUpdate = true;

                this._setContent('main', quality, texture);

                break;
                
            default:

                this._setContent(part, quality, null);
                
                break;
        }

        return this
    }
}

export { WrappedTexture };