import { Mesh, TetrahedronBufferGeometry } from "three";
import { assert } from "../core/assert.js";
import AABB2 from "../core/geom/AABB2.js";
import { Sampler2D } from "../engine/graphics/texture/sampler/Sampler2D.js";
import { renderObjectToSampler2D } from "../engine/graphics/util/renderObjectToSampler2D.js";

/**
 *
 * @param {Material} material
 * @param {WebGLRenderer} [renderer]
 * @param {number} [size]
 * @returns {Sampler2D}
 */
export function makeMaterialIcon(material, renderer, size = 32) {
    assert.defined(material, "material");
    assert.defined(renderer, "renderer");
    assert.isNumber(size, "size");
    assert.isNonNegativeInteger(size, "size");

    const geometry = new TetrahedronBufferGeometry(1, 10);
    const mesh = new Mesh(geometry, material);

    const sampler = Sampler2D.uint8(4, size, size);

    const model_view = renderObjectToSampler2D(sampler, mesh, renderer, new AABB2(0, 0, 1, 1));

    model_view.render();

    // cleanup resources
    model_view.dispose();
    geometry.dispose();

    return sampler;
}
