@openhps/core
Version:
Open Hybrid Positioning System - Core component
126 lines (121 loc) • 4.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.batch = void 0;
var _Node = _interopRequireDefault(require("../core/Node.js"));
var _Normal = require("./Normal.js");
var _Position = require("./Position.js");
var _TSLBase = require("../tsl/TSLBase.js");
var _TextureNode = require("./TextureNode.js");
var _TextureSizeNode = require("./TextureSizeNode.js");
var _Tangent = require("./Tangent.js");
var _IndexNode = require("../core/IndexNode.js");
var _PropertyNode = require("../core/PropertyNode.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* This node implements the vertex shader logic which is required
* when rendering 3D objects via batching. `BatchNode` must be used
* with instances of {@link BatchedMesh}.
*
* @augments Node
*/
class BatchNode extends _Node.default {
static get type() {
return 'BatchNode';
}
/**
* Constructs a new batch node.
*
* @param {BatchedMesh} batchMesh - A reference to batched mesh.
*/
constructor(batchMesh) {
super('void');
/**
* A reference to batched mesh.
*
* @type {BatchedMesh}
*/
this.batchMesh = batchMesh;
/**
* The batching index node.
*
* @type {?IndexNode}
* @default null
*/
this.batchingIdNode = null;
}
/**
* Setups the internal buffers and nodes and assigns the transformed vertex data
* to predefined node variables for accumulation. That follows the same patterns
* like with morph and skinning nodes.
*
* @param {NodeBuilder} builder - The current node builder.
*/
setup(builder) {
if (this.batchingIdNode === null) {
if (builder.getDrawIndex() === null) {
this.batchingIdNode = _IndexNode.instanceIndex;
} else {
this.batchingIdNode = _IndexNode.drawIndex;
}
}
const getIndirectIndex = (0, _TSLBase.Fn)(([id]) => {
const size = (0, _TSLBase.int)((0, _TextureSizeNode.textureSize)((0, _TextureNode.textureLoad)(this.batchMesh._indirectTexture), 0).x);
const x = (0, _TSLBase.int)(id).mod(size);
const y = (0, _TSLBase.int)(id).div(size);
return (0, _TextureNode.textureLoad)(this.batchMesh._indirectTexture, (0, _TSLBase.ivec2)(x, y)).x;
}).setLayout({
name: 'getIndirectIndex',
type: 'uint',
inputs: [{
name: 'id',
type: 'int'
}]
});
const indirectId = getIndirectIndex((0, _TSLBase.int)(this.batchingIdNode));
const matricesTexture = this.batchMesh._matricesTexture;
const size = (0, _TSLBase.int)((0, _TextureSizeNode.textureSize)((0, _TextureNode.textureLoad)(matricesTexture), 0).x);
const j = (0, _TSLBase.float)(indirectId).mul(4).toInt().toVar();
const x = j.mod(size);
const y = j.div(size);
const batchingMatrix = (0, _TSLBase.mat4)((0, _TextureNode.textureLoad)(matricesTexture, (0, _TSLBase.ivec2)(x, y)), (0, _TextureNode.textureLoad)(matricesTexture, (0, _TSLBase.ivec2)(x.add(1), y)), (0, _TextureNode.textureLoad)(matricesTexture, (0, _TSLBase.ivec2)(x.add(2), y)), (0, _TextureNode.textureLoad)(matricesTexture, (0, _TSLBase.ivec2)(x.add(3), y)));
const colorsTexture = this.batchMesh._colorsTexture;
if (colorsTexture !== null) {
const getBatchingColor = (0, _TSLBase.Fn)(([id]) => {
const size = (0, _TSLBase.int)((0, _TextureSizeNode.textureSize)((0, _TextureNode.textureLoad)(colorsTexture), 0).x);
const j = id;
const x = j.mod(size);
const y = j.div(size);
return (0, _TextureNode.textureLoad)(colorsTexture, (0, _TSLBase.ivec2)(x, y)).rgb;
}).setLayout({
name: 'getBatchingColor',
type: 'vec3',
inputs: [{
name: 'id',
type: 'int'
}]
});
const color = getBatchingColor(indirectId);
(0, _PropertyNode.varyingProperty)('vec3', 'vBatchColor').assign(color);
}
const bm = (0, _TSLBase.mat3)(batchingMatrix);
_Position.positionLocal.assign(batchingMatrix.mul(_Position.positionLocal));
const transformedNormal = _Normal.normalLocal.div((0, _TSLBase.vec3)(bm[0].dot(bm[0]), bm[1].dot(bm[1]), bm[2].dot(bm[2])));
const batchingNormal = bm.mul(transformedNormal).xyz;
_Normal.normalLocal.assign(batchingNormal);
if (builder.hasGeometryAttribute('tangent')) {
_Tangent.tangentLocal.mulAssign(bm);
}
}
}
var _default = exports.default = BatchNode;
/**
* TSL function for creating a batch node.
*
* @tsl
* @function
* @param {BatchedMesh} batchMesh - A reference to batched mesh.
* @returns {BatchNode}
*/
const batch = exports.batch = /*@__PURE__*/(0, _TSLBase.nodeProxy)(BatchNode).setParameterLength(1);