@polarfront-lab/ionian 1.7.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ionian.iife.js +1 -1
- package/dist/ionian.iife.js.map +1 -1
- package/dist/ionian.js +70 -9
- package/dist/ionian.js.map +1 -1
- package/package.json +2 -2
package/dist/ionian.js
CHANGED
|
@@ -206,6 +206,11 @@ const _face = new Triangle();
|
|
|
206
206
|
const _color = new Vector3();
|
|
207
207
|
const _uva = new Vector2(), _uvb = new Vector2(), _uvc = new Vector2();
|
|
208
208
|
class MeshSurfaceSampler {
|
|
209
|
+
/**
|
|
210
|
+
* Constructs a mesh surface sampler.
|
|
211
|
+
*
|
|
212
|
+
* @param {Mesh} mesh - Surface mesh from which to sample.
|
|
213
|
+
*/
|
|
209
214
|
constructor(mesh) {
|
|
210
215
|
this.geometry = mesh.geometry;
|
|
211
216
|
this.randomFunction = Math.random;
|
|
@@ -217,10 +222,27 @@ class MeshSurfaceSampler {
|
|
|
217
222
|
this.weightAttribute = null;
|
|
218
223
|
this.distribution = null;
|
|
219
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Specifies a vertex attribute to be used as a weight when sampling from the surface.
|
|
227
|
+
* Faces with higher weights are more likely to be sampled, and those with weights of
|
|
228
|
+
* zero will not be sampled at all. For vector attributes, only .x is used in sampling.
|
|
229
|
+
*
|
|
230
|
+
* If no weight attribute is selected, sampling is randomly distributed by area.
|
|
231
|
+
*
|
|
232
|
+
* @param {string} name - The attribute name.
|
|
233
|
+
* @return {MeshSurfaceSampler} A reference to this sampler.
|
|
234
|
+
*/
|
|
220
235
|
setWeightAttribute(name) {
|
|
221
236
|
this.weightAttribute = name ? this.geometry.getAttribute(name) : null;
|
|
222
237
|
return this;
|
|
223
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Processes the input geometry and prepares to return samples. Any configuration of the
|
|
241
|
+
* geometry or sampler must occur before this method is called. Time complexity is O(n)
|
|
242
|
+
* for a surface with n faces.
|
|
243
|
+
*
|
|
244
|
+
* @return {MeshSurfaceSampler} A reference to this sampler.
|
|
245
|
+
*/
|
|
224
246
|
build() {
|
|
225
247
|
const indexAttribute = this.indexAttribute;
|
|
226
248
|
const positionAttribute = this.positionAttribute;
|
|
@@ -255,19 +277,37 @@ class MeshSurfaceSampler {
|
|
|
255
277
|
this.distribution = distribution;
|
|
256
278
|
return this;
|
|
257
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Allows to set a custom random number generator. Default is `Math.random()`.
|
|
282
|
+
*
|
|
283
|
+
* @param {Function} randomFunction - A random number generator.
|
|
284
|
+
* @return {MeshSurfaceSampler} A reference to this sampler.
|
|
285
|
+
*/
|
|
258
286
|
setRandomGenerator(randomFunction) {
|
|
259
287
|
this.randomFunction = randomFunction;
|
|
260
288
|
return this;
|
|
261
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Selects a random point on the surface of the input geometry, returning the
|
|
292
|
+
* position and optionally the normal vector, color and UV Coordinate at that point.
|
|
293
|
+
* Time complexity is O(log n) for a surface with n faces.
|
|
294
|
+
*
|
|
295
|
+
* @param {Vector3} targetPosition - The target object holding the sampled position.
|
|
296
|
+
* @param {Vector3} targetNormal - The target object holding the sampled normal.
|
|
297
|
+
* @param {Color} targetColor - The target object holding the sampled color.
|
|
298
|
+
* @param {Vector2} targetUV - The target object holding the sampled uv coordinates.
|
|
299
|
+
* @return {MeshSurfaceSampler} A reference to this sampler.
|
|
300
|
+
*/
|
|
262
301
|
sample(targetPosition, targetNormal, targetColor, targetUV) {
|
|
263
|
-
const faceIndex = this.
|
|
264
|
-
return this.
|
|
302
|
+
const faceIndex = this._sampleFaceIndex();
|
|
303
|
+
return this._sampleFace(faceIndex, targetPosition, targetNormal, targetColor, targetUV);
|
|
265
304
|
}
|
|
266
|
-
|
|
305
|
+
// private
|
|
306
|
+
_sampleFaceIndex() {
|
|
267
307
|
const cumulativeTotal = this.distribution[this.distribution.length - 1];
|
|
268
|
-
return this.
|
|
308
|
+
return this._binarySearch(this.randomFunction() * cumulativeTotal);
|
|
269
309
|
}
|
|
270
|
-
|
|
310
|
+
_binarySearch(x) {
|
|
271
311
|
const dist = this.distribution;
|
|
272
312
|
let start = 0;
|
|
273
313
|
let end = dist.length - 1;
|
|
@@ -285,7 +325,7 @@ class MeshSurfaceSampler {
|
|
|
285
325
|
}
|
|
286
326
|
return index;
|
|
287
327
|
}
|
|
288
|
-
|
|
328
|
+
_sampleFace(faceIndex, targetPosition, targetNormal, targetColor, targetUV) {
|
|
289
329
|
let u = this.randomFunction();
|
|
290
330
|
let v = this.randomFunction();
|
|
291
331
|
if (u + v > 1) {
|
|
@@ -1012,15 +1052,34 @@ class FullscreenTriangleGeometry extends BufferGeometry {
|
|
|
1012
1052
|
}
|
|
1013
1053
|
const _geometry = new FullscreenTriangleGeometry();
|
|
1014
1054
|
class FullScreenQuad {
|
|
1055
|
+
/**
|
|
1056
|
+
* Constructs a new full screen quad.
|
|
1057
|
+
*
|
|
1058
|
+
* @param {?Material} material - The material to render te full screen quad with.
|
|
1059
|
+
*/
|
|
1015
1060
|
constructor(material) {
|
|
1016
1061
|
this._mesh = new Mesh(_geometry, material);
|
|
1017
1062
|
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Frees the GPU-related resources allocated by this instance. Call this
|
|
1065
|
+
* method whenever the instance is no longer used in your app.
|
|
1066
|
+
*/
|
|
1018
1067
|
dispose() {
|
|
1019
1068
|
this._mesh.geometry.dispose();
|
|
1020
1069
|
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Renders the full screen quad.
|
|
1072
|
+
*
|
|
1073
|
+
* @param {WebGLRenderer} renderer - The renderer.
|
|
1074
|
+
*/
|
|
1021
1075
|
render(renderer) {
|
|
1022
1076
|
renderer.render(this._mesh, _camera);
|
|
1023
1077
|
}
|
|
1078
|
+
/**
|
|
1079
|
+
* The quad's material.
|
|
1080
|
+
*
|
|
1081
|
+
* @type {?Material}
|
|
1082
|
+
*/
|
|
1024
1083
|
get material() {
|
|
1025
1084
|
return this._mesh.material;
|
|
1026
1085
|
}
|
|
@@ -1030,9 +1089,11 @@ class FullScreenQuad {
|
|
|
1030
1089
|
}
|
|
1031
1090
|
class GPUComputationRenderer {
|
|
1032
1091
|
/**
|
|
1033
|
-
*
|
|
1034
|
-
|
|
1035
|
-
|
|
1092
|
+
* Constructs a new GPU computation renderer.
|
|
1093
|
+
*
|
|
1094
|
+
* @param {number} sizeX - Computation problem size is always 2d: sizeX * sizeY elements.
|
|
1095
|
+
* @param {number} sizeY - Computation problem size is always 2d: sizeX * sizeY elements.
|
|
1096
|
+
* @param {WebGLRenderer} renderer - The renderer.
|
|
1036
1097
|
*/
|
|
1037
1098
|
constructor(sizeX, sizeY, renderer) {
|
|
1038
1099
|
this.variables = [];
|