import { Mesh, SphereBufferGeometry } from "three";
import { Sampler2D } from "../../../../../src/engine/graphics/texture/sampler/Sampler2D.js";
import sampler2D2Canvas from "../../../../../src/engine/graphics/texture/sampler/sampler2d_to_html_canvas.js";
import { renderObjectToSampler2D } from "../../../../../src/engine/graphics/util/renderObjectToSampler2D.js";
import { CanvasView } from "../../../../../src/view/elements/CanvasView.js";
import { TypeEditor } from "../../TypeEditor.js";

export class MaterialEditor extends TypeEditor {
    build(parent, field, registry) {

        const mat = field.adapter.read(parent, field.name);

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

        const preview = renderObjectToSampler2D(sampler, new Mesh(new SphereBufferGeometry(1), mat));

        preview.render();

        const view = new CanvasView();

        sampler2D2Canvas(sampler, 1, 0, view.el);

        preview.dispose();

        return view;
    }
}
