gl-plane
Version:
Draw a plane with texture
103 lines (102 loc) • 3.81 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const gl_shader_1 = __importDefault(require("gl-shader"));
const trangle_vertex_1 = __importDefault(require("./shaders/trangle-vertex"));
const trangle_fragment_1 = __importDefault(require("./shaders/trangle-fragment"));
const gl_vao_1 = __importDefault(require("gl-vao"));
const gl_buffer_1 = __importDefault(require("gl-buffer"));
const gl_texture2d_1 = __importDefault(require("gl-texture2d"));
const IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
class GLPlane {
constructor(opt) {
this.bounds = [
[0.3, 0.3, 0.4],
[1, 1, 1.2]
];
this.option = Object.assign({ model: IDENTITY, view: IDENTITY, projection: IDENTITY, opacity: 1 }, opt);
this.texture = gl_texture2d_1.default(opt.gl, opt.texture || [1, 1]);
const vertex = this.getVertexCoords(...opt.points);
this.vertextBuffer = gl_buffer_1.default(opt.gl, vertex);
this.updateBounds(opt.points);
this.trangleShader = gl_shader_1.default(opt.gl, trangle_vertex_1.default, trangle_fragment_1.default);
this.vao = gl_vao_1.default(opt.gl, [
{
buffer: this.vertextBuffer,
type: opt.gl.FLOAT,
size: 3
},
{
buffer: gl_buffer_1.default(opt.gl, [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0]),
type: opt.gl.FLOAT,
size: 2
}
]);
}
getVertexCoords(p1, p2, p3, p4) {
return [...p1, ...p2, ...p3, ...p1, ...p3, ...p4];
}
updateBounds(points) {
const max = [-Infinity, -Infinity, -Infinity];
const min = [Infinity, Infinity, Infinity];
for (let i = 1; i < points.length; i++) {
const a = points[i - 1];
const b = points[i];
for (let j = 0; j < 3; j++) {
max[j] = Math.max(max[j], a[j], b[j]);
min[j] = Math.min(min[j], a[j], b[j]);
}
}
this.bounds = [
min, max
];
}
pick() {
// No pick implementation yet
// but plotly need a pick method
return null;
}
isOpaque() {
return this.option.opacity === 1;
}
isTransparent() {
return !this.isOpaque();
}
update(opt) {
var _a;
const gl = (_a = opt.gl, (_a !== null && _a !== void 0 ? _a : this.option.gl));
if (opt.texture && opt.texture !== this.option.texture) {
this.texture.dispose();
this.texture = gl_texture2d_1.default(gl, opt.texture);
}
this.option = Object.assign(Object.assign({}, this.option), opt);
if (opt.points) {
this.updateBounds(opt.points);
this.vertextBuffer.update(this.getVertexCoords(...opt.points));
}
}
draw(param) {
var _a, _b, _c;
const opt = this.option;
const { gl } = opt;
const uniforms = {
model: (_a = param.model, (_a !== null && _a !== void 0 ? _a : opt.model)),
view: (_b = param.view, (_b !== null && _b !== void 0 ? _b : opt.view)),
projection: (_c = param.projection, (_c !== null && _c !== void 0 ? _c : opt.projection)),
opacity: opt.opacity,
texture: 0
};
this.texture.bind(0);
this.trangleShader.bind();
this.trangleShader.uniforms = uniforms;
this.vao.bind();
gl.drawArrays(gl.TRIANGLES, 0, 6);
this.vao.unbind();
}
drawTransparent(param) {
this.draw(param);
}
}
exports.GLPlane = GLPlane;