Ticket #892: pathplacer2.js

File pathplacer2.js, 5.0 KB (added by Jonathan Waller, 12 years ago)

Updated script with fixed fill code

Line 
1
2/////////////////////////////////////////////////////////////////////////////////////////
3// PathPlacer
4//
5// Class for creating a winding path between two points
6//
7// x1,z1: Starting point of path
8// x2,z2: Ending point of path
9// width: Width of the path
10// a: Waviness - how wavy the path will be (higher is wavier, 0.0 is straight line)
11// b: Smoothness - how smooth the path will be (higher is smoother)
12// c: Offset - max amplitude of waves along the path (0.0 is straight line)
13// taper: Tapering - how much the width of the path changes from start to end
14// if positive, the width will decrease by that factor, if negative the width
15// will increase by that factor
16//
17/////////////////////////////////////////////////////////////////////////////////////////
18
19function PathPlacer(x1, z1, x2, z2, width, a, b, c, taper)
20{
21 this.x1 = x1;
22 this.z1 = z1;
23 this.x2 = x2;
24 this.z2 = z2;
25 this.width = width;
26 this.a = a;
27 this.b = b;
28 this.c = c;
29 this.taper = taper;
30}
31
32PathPlacer.prototype.place = function(constraint)
33{
34 // Preliminary bounds check
35 if (!g_Map.validT(this.x1, this.z1) || !constraint.allows(this.x1, this.z1) ||
36 !g_Map.validT(this.x2, this.z2) || !constraint.allows(this.x2, this.z2))
37 {
38 return undefined;
39 }
40
41 var dx = (this.x2 - this.x1);
42 var dz = (this.z2 - this.z1);
43 var dist = Math.sqrt(dx*dx + dz*dz);
44 dx /= dist;
45 dz /= dist;
46
47 var numSteps = 1 + Math.floor(dist/4 * this.a);
48 var numISteps = 1 + Math.floor(dist/4 * this.b);
49 var totalSteps = numSteps*numISteps;
50 var offset = 1 + Math.floor(dist/4 * this.c);
51
52 // Generate random offsets
53 var ctrlVals = new Float32Array(numSteps); //float32
54 for (var j = 1; j < (numSteps-1); ++j)
55 {
56 ctrlVals[j] = randFloat(-offset, offset);
57 }
58
59 // Interpolate for smoothed 1D noise
60 var noise = new Float32Array(totalSteps+1); //float32
61 for (var j = 0; j < numSteps; ++j)
62 {
63 // Cubic interpolation
64 var v0 = ctrlVals[(j+numSteps-1)%numSteps];
65 var v1 = ctrlVals[j];
66 var v2 = ctrlVals[(j+1)%numSteps];
67 var v3 = ctrlVals[(j+2)%numSteps];
68 var P = (v3 - v2) - (v0 - v1);
69 var Q = (v0 - v1) - P;
70 var R = v2 - v0;
71 var S = v1;
72
73 for (var k = 0; k < numISteps; ++k)
74 {
75 var t = k/numISteps;
76 noise[j*numISteps + k] = P*t*t*t + Q*t*t + R*t + S;
77 }
78 }
79
80 var halfWidth = 0.5 * this.width;
81
82 // Add smoothed noise to straight path
83 var segments1 = [];
84 var segments2 = [];
85
86 for (var j = 0; j < totalSteps; ++j)
87 {
88 // Interpolated points along straight path
89 var t = j/totalSteps;
90 var tx = this.x1 * (1.0 - t) + this.x2 * t;
91 var tz = this.z1 * (1.0 - t) + this.z2 * t;
92 var t2 = (j+1)/totalSteps;
93 var tx2 = this.x1 * (1.0 - t2) + this.x2 * t2;
94 var tz2 = this.z1 * (1.0 - t2) + this.z2 * t2;
95
96 // Find noise offset points
97 var nx = (tx - dz * noise[j]);
98 var nz = (tz + dx * noise[j]);
99 var nx2 = (tx2 - dz * noise[j+1]);
100 var nz2 = (tz2 + dx * noise[j+1]);
101
102 // Find slope of offset points
103 var ndx = (nx2 - nx);
104 var ndz = (nz2 - nz);
105 var dist = Math.sqrt(ndx*ndx + ndz*ndz);
106 ndx /= dist;
107 ndz /= dist;
108
109 var taperedWidth = (1.0 - t*this.taper) * halfWidth;
110
111 // Find slope of offset path
112 var px = Math.round(nx - ndz * -taperedWidth);
113 var pz = Math.round(nz + ndx * -taperedWidth);
114 segments1.push(new PointXZ(px, pz));
115 var px2 = Math.round(nx2 - ndz * taperedWidth);
116 var pz2 = Math.round(nz2 + ndx * taperedWidth);
117 segments2.push(new PointXZ(px2, pz2));
118
119
120 }
121
122 var retVec = [];
123
124 // Draw path segments
125 var num = segments1.length - 1;
126 for (var j = 0; j < num; ++j)
127 {
128 // Fill quad formed by these 4 points
129 // Note the code assumes these points have been rounded to integer values
130 var pt11 = segments1[j];
131 var pt12 = segments1[j+1];
132 var pt21 = segments2[j];
133 var pt22 = segments2[j+1];
134
135 var tris = [[pt12, pt11, pt21], [pt12, pt21, pt22]];
136
137 for (var t = 0; t < 2; ++t)
138 {
139 // Sort vertices by min z
140 var tri = tris[t].sort(
141 function(a, b)
142 {
143 return a.z - b.z;
144 }
145 );
146
147 // Fills in a line from (z, x1) to (z,x2)
148 var fillLine = function(z, x1, x2)
149 {
150 var left = Math.round(Math.min(x1, x2));
151 var right = Math.round(Math.max(x1, x2));
152 for (var x = left; x <= right; x++)
153 {
154 if (g_Map.validT(x, z) && constraint.allows(x, z))
155 {
156 retVec.push(new PointXZ(x, z));
157 }
158 }
159 }
160
161 var A = tri[0];
162 var B = tri[1];
163 var C = tri[2];
164
165 var dx1 = (B.z != A.z) ? ((B.x - A.x) / (B.z - A.z)) : 0;
166 var dx2 = (C.z != A.z) ? ((C.x - A.x) / (C.z - A.z)) : 0;
167 var dx3 = (C.z != B.z) ? ((C.x - B.x) / (C.z - B.z)) : 0;
168
169 if (A.z == B.z)
170 {
171 fillLine(A.z, A.x, B.x);
172 }
173 else
174 {
175 for (var z = A.z; z <= B.z; z++)
176 {
177 fillLine(z, A.x + dx1*(z - A.z), A.x + dx2*(z - A.z));
178 }
179 }
180 if (B.z == C.z)
181 {
182 fillLine(B.z, B.x, C.x);
183 }
184 else
185 {
186 for (var z = B.z + 1; z < C.z; z++)
187 {
188 fillLine(z, B.x + dx3*(z - B.z), A.x + dx2*(z - A.z));
189 }
190 }
191 }
192 }
193
194 return retVec;
195}