@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
71 lines (65 loc) • 1.9 kB
text/typescript
/**
* Create points from a an array of json dictionaries
*
* @remarks
* The number of points created will be equal to the number of elements in the array.
* Each element of the array must be a dictionary. It can look like:
* `[{position: [1,2,3]}]`
* which will create a single point at the position x=1,y=2,z=3.
*
* In order to create 2 points with attributes `position` and `amp`, you would have:
* `[{position: [1,2,3], amp: 1},{position: [7,2,1], amp: 3}]`
*
*/
import {TypedSopNode} from './_Base';
import {JSONDataParser} from '../../../core/loader/geometry/JSONDataParser';
const DEFAULT_DATA = [
{value: -40},
{value: -30},
{value: -20},
{value: -10},
{value: 0},
{value: 10},
{value: 20},
{value: 30},
{value: 40},
{value: 50},
{value: 60},
{value: 70},
{value: 80},
];
const DEFAULT_DATA_STR = JSON.stringify(DEFAULT_DATA);
import {NodeParamsConfig, ParamConfig} from '../utils/params/ParamsConfig';
import {ObjectType} from '../../../core/geometry/Constant';
import {SopType} from '../../poly/registers/nodes/types/Sop';
class DataSopParamsConfig extends NodeParamsConfig {
/** @param json object used to create the geometry */
data = ParamConfig.STRING(DEFAULT_DATA_STR);
}
const ParamsConfig = new DataSopParamsConfig();
export class DataSopNode extends TypedSopNode<DataSopParamsConfig> {
override paramsConfig = ParamsConfig;
static override type() {
return SopType.DATA;
}
override cook() {
let json = null;
try {
json = JSON.parse(this.pv.data);
} catch (e) {
this.states.error.set('could not parse json');
}
if (json) {
try {
const loader = new JSONDataParser();
loader.setJSON(json);
const geometry = loader.createObject();
this.setGeometry(geometry, ObjectType.POINTS);
} catch (e) {
this.states.error.set('could not build geometry from json');
}
} else {
this.cookController.endCook();
}
}
}