Skip to content

Commit

Permalink
fix: Normal complementation problem
Browse files Browse the repository at this point in the history
  • Loading branch information
kyasbal committed Nov 3, 2017
1 parent 175fa81 commit 25759e7
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/Parser/Modules/NormalComplementorModule.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import ParserModule from "../ParserModule";
import { AddVertexAttributesArgument } from "../Arguments";
import Vector3 from "grimoirejs-math/ref/Vector3";
import ConstantConverter from "../ConstantConverter";
export default class NormalComplementorModule extends ParserModule {
public complementVertexAttributes(args: AddVertexAttributesArgument): boolean {
if (args.primitive.attributes["NORMAL"] === void 0 && args.primitive.attributes["POSITION"] !== void 0) {
const accessor = args.tf.accessors[args.primitive.attributes["POSITION"]];
const baseBufferView = args.bufferViews[accessor.bufferView];
const positions = new Float32Array(baseBufferView.buffer, baseBufferView.byteOffset + accessor.byteOffset);
// if (accessor.byteStride !== void 0 && accessor.byteStride !== 0) {
// throw new Error("Complementing normal with a position buffer which buffer has stride as a parameter");
// }
if (!accessor.count) {
const positionAccessor = args.tf.accessors[args.primitive.attributes["POSITION"]];
const positionBufferViewInfo = args.tf.bufferViews[positionAccessor.bufferView];
const positionBufferView = args.bufferViews[positionAccessor.bufferView];
const positionTypedArray = this.__convertBufferView(ConstantConverter.asTypedArrayConstructor(positionAccessor.componentType), positionBufferView, positionBufferViewInfo, positionAccessor);
if (!positionAccessor.count) {
throw new Error("Accessor count of POSITION buffer should be defined for complementing NORMAL buffer");
}
const defaultAccessor = args.tf.accessors[args.primitive.indices];
const indexAccessor = args.tf.accessors[args.primitive.indices];
// generate normal buffer
const normal = new Float32Array(accessor.count * 3);
if (defaultAccessor) {
const defaultBufferView = args.tf.bufferViews[defaultAccessor.bufferView];
const bufferSource = args.bufferViews[defaultAccessor.bufferView];
const byteAccessor = this.__getBufferReader(bufferSource, defaultAccessor.componentType, defaultAccessor.byteOffset, defaultBufferView.byteStride);
for (let i = 0; i < accessor.count / 3; i++) {
this._calcFlatNormal(positions, normal, byteAccessor(3 * i), byteAccessor(3 * i + 1, ), byteAccessor(3 * i + 2));
const complementedNormal = new Float32Array(positionAccessor.count * 3);
if (indexAccessor) {
const indexBufferViewInfo = args.tf.bufferViews[indexAccessor.bufferView];
const indexBufferView = args.bufferViews[indexAccessor.bufferView];
const indexTypedArray = this.__convertBufferView(ConstantConverter.asTypedArrayConstructor(indexAccessor.componentType), indexBufferView, indexBufferViewInfo, indexAccessor);
for (let i = 0; i < indexAccessor.count / 3; i++) {
this._calcFlatNormal(positionTypedArray, complementedNormal, indexTypedArray[3 * i], indexTypedArray[3 * i + 1], indexTypedArray[3 * i + 2]);
}
} else {
for (let i = 0; i < accessor.count / 3; i++) {
this._calcFlatNormal(positions, normal, 3 * i, 3 * i + 1, 3 * i + 2);
for (let i = 0; i < positionAccessor.count / 3; i++) {
this._calcFlatNormal(positionTypedArray, complementedNormal, 3 * i, 3 * i + 1, 3 * i + 2);
}
}
// add normal to geometry
args.geometry.addAttributes(normal, {
args.geometry.addAttributes(complementedNormal, {
NORMAL: {
size: 3
}
Expand All @@ -38,11 +37,11 @@ export default class NormalComplementorModule extends ParserModule {
return false;
}

private _getElement(positions: Float32Array, posbase: number, elemIndex: number): number {
private _getElement(positions: ArrayBufferView, posbase: number, elemIndex: number): number {
return positions[posbase + elemIndex];
}

private _calcFlatNormal(positions: Float32Array, normals: Float32Array, i0: number, i1: number, i2: number): void {
private _calcFlatNormal(positions: ArrayBufferView, normals: Float32Array, i0: number, i1: number, i2: number): void {
const v0Tov1 = new Vector3(this._getElement(positions, 3 * i1, 0) - this._getElement(positions, 3 * i0, 0), this._getElement(positions, 3 * i1, 1) - this._getElement(positions, 3 * i0, 1), this._getElement(positions, 3 * i1, 2) - this._getElement(positions, 3 * i0, 2));
const v0Tov2 = new Vector3(this._getElement(positions, 3 * i2, 0) - this._getElement(positions, 3 * i0, 0), this._getElement(positions, 3 * i2, 1) - this._getElement(positions, 3 * i0, 1), this._getElement(positions, 3 * i2, 2) - this._getElement(positions, 3 * i0, 2));
const nor = Vector3.cross(v0Tov1, v0Tov2).normalizeThis();
Expand Down

0 comments on commit 25759e7

Please sign in to comment.