Skip to content

Commit 4f5b6ef

Browse files
committed
Fixed cylinder UV calculation on cylinders that are placed rectilinearly.
1 parent c7de3d8 commit 4f5b6ef

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

js/LDRGeometries.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,18 @@ LDR.LDRGeometry.prototype.buildPhysicalGeometriesAndColors = function() {
381381
b.getSize(size);
382382

383383
// Mark lines shared by lines and conditional lines:
384-
let edges = []; // 0/1 => soft, 2 => hard
384+
let softEdges = [];
385385
for(let c in this.conditionalLines) {
386386
if(this.conditionalLines.hasOwnProperty(c)) {
387387
let lines = this.conditionalLines[c];
388-
lines.forEach(line => edges[key(line.p1, line.p2)] = true);
388+
lines.forEach(line => softEdges[key(line.p1, line.p2)] = true);
389389
}
390390
}
391391
for(let c in this.lines) {
392392
if(this.lines.hasOwnProperty(c)) {
393393
let lines = this.lines[c];
394394
lines.forEach(line => vertices[line.p1].hard = vertices[line.p2].hard = true);
395-
lines.forEach(line => edges[key(line.p1, line.p2)] = false); // This fixes duplicate soft and hard edges.
395+
lines.forEach(line => softEdges[key(line.p1, line.p2)] = false); // Some elements have overlappind lines and conditional lines. This reduces the impact of such issues.
396396
}
397397
}
398398

@@ -414,42 +414,42 @@ LDR.LDRGeometry.prototype.buildPhysicalGeometriesAndColors = function() {
414414

415415
function renew(i) {
416416
let v = vertices[i];
417-
vertices.push({x:v.x, y:v.y, z:v.z}); // No mark as it is new and will not be visited again.
417+
vertices.push({x:v.x, y:v.y, z:v.z}); // No hard/soft marks, as it is new and will not be visited again.
418418
vLen++;
419419
return vLen-1;
420420
}
421421

422422
function updateTriangleIndices(t) {
423423
let p1 = t.p1, p2 = t.p2, p3 = t.p3;
424424
let h1 = vertices[p1].hard, h2 = vertices[p2].hard, h3 = vertices[p3].hard;
425-
let k12 = edges[key(p1, p2)], k23 = edges[key(p2, p3)], k31 = edges[key(p3, p1)];
425+
let soft12 = softEdges[key(p1, p2)], soft23 = softEdges[key(p2, p3)], soft31 = softEdges[key(p3, p1)];
426426

427-
if(h1 && !k12 && !k31) {
427+
if(h1 && !soft12 && !soft31) {
428428
t.p1 = renew(t.p1);
429429
}
430-
if(h2 && !k12 && !k23) {
430+
if(h2 && !soft12 && !soft23) {
431431
t.p2 = renew(t.p2);
432432
}
433-
if(h3 && !k23 && !k31) {
433+
if(h3 && !soft23 && !soft31) {
434434
t.p3 = renew(t.p3);
435435
}
436436
}
437437

438438
function updateQuadIndices(t) {
439439
let p1 = t.p1, p2 = t.p2, p3 = t.p3, p4 = t.p4;
440440
let h1 = vertices[p1].hard, h2 = vertices[p2].hard, h3 = vertices[p3].hard, h4 = vertices[p4].hard;
441-
let k12 = edges[key(p1, p2)], k23 = edges[key(p2, p3)], k34 = edges[key(p3, p4)], k41 = edges[key(p4, p1)];
441+
let soft12 = softEdges[key(p1, p2)], soft23 = softEdges[key(p2, p3)], soft34 = softEdges[key(p3, p4)], soft41 = softEdges[key(p4, p1)];
442442

443-
if(h1 && !k12 && !k41) {
443+
if(h1 && !soft12 && !soft41) {
444444
t.p1 = renew(t.p1);
445445
}
446-
if(h2 && !k12 && !k23) {
446+
if(h2 && !soft12 && !soft23) {
447447
t.p2 = renew(t.p2);
448448
}
449-
if(h3 && !k23 && !k34) {
449+
if(h3 && !soft23 && !soft34) {
450450
t.p3 = renew(t.p3);
451451
}
452-
if(h4 && !k41 && !k34) {
452+
if(h4 && !soft41 && !soft34) {
453453
t.p4 = renew(t.p4);
454454
}
455455
}
@@ -586,7 +586,7 @@ LDR.LDRGeometry.prototype.buildPhysicalGeometriesAndColors = function() {
586586
if(atLeast3EqualNormals()) { // Just project onto the plane where the normals point the most:
587587
// First check if this is a simple rectilinear face:
588588
let DX, DY, DZ;
589-
if(vs.some(v => v.o === true)) {
589+
if(vs.some(v => v.o === true)) { // Logo position: Overwrite setUV():
590590
let origo = vs.find(v => v.o === true);
591591
let anyOther = vs.find(v => v.o !== true);
592592
DX = origo.x - anyOther.x;
@@ -634,22 +634,34 @@ LDR.LDRGeometry.prototype.buildPhysicalGeometriesAndColors = function() {
634634
}
635635

636636
// Math.atan2 -> [-PI;PI], and Math.acos => [0;PI]
637-
const PI1 = 0.8 / Math.PI;
638-
const PI2 = 0.3 / Math.PI;
639-
let toCircle = (y, x) => (Math.atan2(y, x)+Math.PI)*PI2;
640-
let toHeight = x => Math.acos(x)*PI1;
641-
const C3 = 0.2 / (size.x + size.y + size.z);
642-
let dxyz = v => 0.1 + (v.x + v.y + v.z)*C3;
643-
644-
if(NY >= Math.max(NX + NZ)) {
645-
setUV((v,i) => dxyz(v) + toCircle(ns[i].x, ns[i].z),
646-
(v,i) => dxyz(v) + toHeight(ns[i].y), false) ||
637+
const CONST1 = 0.7 / Math.PI;
638+
let toCircle = (y, x) => {
639+
let ret = Math.abs(Math.atan2(y, x)) * CONST1; // Circle direction => 70%
640+
return ret;
641+
}
642+
const CONST3 = 0.3 / (size.x + size.y + size.z);
643+
let dxyz = v => 0.1 + (v.x + v.y + v.z)*CONST3; // Scramble 30% offset by vertex position.
644+
645+
if(NX < 1e-7 && setUV((v,i) => dxyz(v) + toCircle(ns[i].y, ns[i].z), dx, false) ||
646+
NY < 1e-7 && setUV((v,i) => dxyz(v) + toCircle(ns[i].x, ns[i].z), dy, false) ||
647+
NZ < 1e-7 && setUV((v,i) => dxyz(v) + toCircle(ns[i].x, ns[i].y), dz, false)) {
648+
return;
649+
}
650+
651+
const CONST2 = 0.7 / Math.PI;
652+
let toHeight = x => Math.acos(x)*CONST2; // Height caused by normal turn => 70%
653+
654+
if(NY <= Math.min(NX, NZ)) {
655+
if(!setUV((v,i) => dxyz(v) + toCircle(ns[i].x, ns[i].z),
656+
(v,i) => dxyz(v) + toHeight(ns[i].y), false)) {
647657
setUV(dx, dz, true);
658+
}
648659
}
649660
else {
650-
setUV((v,i) => dxyz(v) + toCircle(ns[i].x, ns[i].y),
651-
(v,i) => dxyz(v) + toHeight(ns[i].z), false) ||
661+
if(!setUV((v,i) => dxyz(v) + toCircle(ns[i].x, ns[i].y),
662+
(v,i) => dxyz(v) + toHeight(ns[i].z), false)) {
652663
setUV(dx, dy, true);
664+
}
653665
}
654666
}
655667

models/speckle.ldr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
0 Name: speckle.ldr
22
0 Author: Lasse Deleuran [Lasse Deleuran]
33
0 !LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt
4-
1 75 0 0 0 1 0 0 0 1 0 0 0 1 3001.dat
5-
1 76 -20 -24 -20 0 0 -1 0 1 0 1 0 0 3001.dat
4+
1 75 0 0 0 1 0 0 0 1 0 0 0 1 3001.dat
5+
1 76 -20 -24 -20 1 0 0 0 1 0 0 0 1 3001.dat
66
1 132 0 -48 -40 1 0 0 0 1 0 0 0 1 3001.dat
7-
1 133 20 -72 -20 0 0 1 0 1 0 -1 0 0 3001.dat
7+
1 133 20 -72 -20 1 0 0 0 1 0 0 0 1 3001.dat

0 commit comments

Comments
 (0)