@openhps/core
Version:
Open Hybrid Positioning System - Core component
112 lines (102 loc) • 3.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.bumpMap = void 0;
var _TempNode = _interopRequireDefault(require("../core/TempNode.js"));
var _UV = require("../accessors/UV.js");
var _Normal = require("../accessors/Normal.js");
var _Position = require("../accessors/Position.js");
var _FrontFacingNode = require("./FrontFacingNode.js");
var _TSLBase = require("../tsl/TSLBase.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Bump Mapping Unparametrized Surfaces on the GPU by Morten S. Mikkelsen
// https://mmikk.github.io/papers3d/mm_sfgrad_bump.pdf
const dHdxy_fwd = (0, _TSLBase.Fn)(({
textureNode,
bumpScale
}) => {
// It's used to preserve the same TextureNode instance
const sampleTexture = callback => textureNode.cache().context({
getUV: texNode => callback(texNode.uvNode || (0, _UV.uv)()),
forceUVContext: true
});
const Hll = (0, _TSLBase.float)(sampleTexture(uvNode => uvNode));
return (0, _TSLBase.vec2)((0, _TSLBase.float)(sampleTexture(uvNode => uvNode.add(uvNode.dFdx()))).sub(Hll), (0, _TSLBase.float)(sampleTexture(uvNode => uvNode.add(uvNode.dFdy()))).sub(Hll)).mul(bumpScale);
});
// Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
const perturbNormalArb = (0, _TSLBase.Fn)(inputs => {
const {
surf_pos,
surf_norm,
dHdxy
} = inputs;
// normalize is done to ensure that the bump map looks the same regardless of the texture's scale
const vSigmaX = surf_pos.dFdx().normalize();
const vSigmaY = surf_pos.dFdy().normalize();
const vN = surf_norm; // normalized
const R1 = vSigmaY.cross(vN);
const R2 = vN.cross(vSigmaX);
const fDet = vSigmaX.dot(R1).mul(_FrontFacingNode.faceDirection);
const vGrad = fDet.sign().mul(dHdxy.x.mul(R1).add(dHdxy.y.mul(R2)));
return fDet.abs().mul(surf_norm).sub(vGrad).normalize();
});
/**
* This class can be used for applying bump maps to materials.
*
* ```js
* material.normalNode = bumpMap( texture( bumpTex ) );
* ```
*
* @augments TempNode
*/
class BumpMapNode extends _TempNode.default {
static get type() {
return 'BumpMapNode';
}
/**
* Constructs a new bump map node.
*
* @param {Node<float>} textureNode - Represents the bump map data.
* @param {?Node<float>} [scaleNode=null] - Controls the intensity of the bump effect.
*/
constructor(textureNode, scaleNode = null) {
super('vec3');
/**
* Represents the bump map data.
*
* @type {Node<float>}
*/
this.textureNode = textureNode;
/**
* Controls the intensity of the bump effect.
*
* @type {?Node<float>}
* @default null
*/
this.scaleNode = scaleNode;
}
setup() {
const bumpScale = this.scaleNode !== null ? this.scaleNode : 1;
const dHdxy = dHdxy_fwd({
textureNode: this.textureNode,
bumpScale
});
return perturbNormalArb({
surf_pos: _Position.positionView,
surf_norm: _Normal.normalView,
dHdxy
});
}
}
var _default = exports.default = BumpMapNode;
/**
* TSL function for creating a bump map node.
*
* @tsl
* @function
* @param {Node<float>} textureNode - Represents the bump map data.
* @param {?Node<float>} [scaleNode=null] - Controls the intensity of the bump effect.
* @returns {BumpMapNode}
*/
const bumpMap = exports.bumpMap = /*@__PURE__*/(0, _TSLBase.nodeProxy)(BumpMapNode).setParameterLength(1, 2);