-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
221 lines (196 loc) · 9.68 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**************************************************************
* UTILITY FUNCTIONS
* - scroll to BEGIN CONFIG to provide the config values
*************************************************************/
const fs = require("fs");
const dir = __dirname;
// adds a rarity to the configuration. This is expected to correspond with a directory containing the rarity for each defined layer
// @param _id - id of the rarity
// @param _from - number in the edition to start this rarity from
// @param _to - number in the edition to generate this rarity to
// @return a rarity object used to dynamically generate the NFTs
const addRarity = (_id, _from, _to) => {
const _rarityWeight = {
value: _id,
from: _from,
to: _to,
layerPercent: {}
};
return _rarityWeight;
};
// get the name without last 4 characters -> slice .png from the name
const cleanName = (_str) => {
let name = _str.slice(0, -4);
return name;
};
// reads the filenames of a given folder and returns it with its name and path
const getElements = (_path, _elementCount) => {
return fs
.readdirSync(_path)
.filter((item) => !/(^|\/)\.[^\/\.]/g.test(item))
.map((i) => {
return {
id: _elementCount,
name: cleanName(i),
path: `${_path}/${i}`
};
});
};
// adds a layer to the configuration. The layer will hold information on all the defined parts and
// where they should be rendered in the image
// @param _id - id of the layer
// @param _position - on which x/y value to render this part
// @param _size - of the image
// @return a layer object used to dynamically generate the NFTs
const addLayer = (_id, _position, _size) => {
if (!_id) {
console.log('error adding layer, parameters id required');
return null;
}
if (!_position) {
_position = { x: 0, y: 0 };
}
if (!_size) {
_size = { width: width, height: height }
}
// add two different dimension for elements:
// - all elements with their path information
// - only the ids mapped to their rarity
let elements = [];
let elementCount = 0;
let elementIdsForRarity = {};
rarityWeights.forEach((rarityWeight) => {
let elementsForRarity = getElements(`${dir}/${_id}/${rarityWeight.value}`);
elementIdsForRarity[rarityWeight.value] = [];
elementsForRarity.forEach((_elementForRarity) => {
_elementForRarity.id = `${editionDnaPrefix}${elementCount}`;
elements.push(_elementForRarity);
elementIdsForRarity[rarityWeight.value].push(_elementForRarity.id);
elementCount++;
})
elements[rarityWeight.value] = elementsForRarity;
});
let elementsForLayer = {
id: _id,
position: _position,
size: _size,
elements,
elementIdsForRarity
};
return elementsForLayer;
};
// adds layer-specific percentages to use one vs another rarity
// @param _rarityId - the id of the rarity to specifiy
// @param _layerId - the id of the layer to specifiy
// @param _percentages - an object defining the rarities and the percentage with which a given rarity for this layer should be used
const addRarityPercentForLayer = (_rarityId, _layerId, _percentages) => {
let _rarityFound = false;
rarityWeights.forEach((_rarityWeight) => {
if (_rarityWeight.value === _rarityId) {
let _percentArray = [];
for (let percentType in _percentages) {
_percentArray.push({
id: percentType,
percent: _percentages[percentType]
})
}
_rarityWeight.layerPercent[_layerId] = _percentArray;
_rarityFound = true;
}
});
if (!_rarityFound) {
console.log(`rarity ${_rarityId} not found, failed to add percentage information`);
}
}
/**************************************************************
* BEGIN CONFIG
*************************************************************/
// image width in pixels
const width = 1000;
// image height in pixels
const height = 1000;
// description for NFT in metadata file
const description = "This is an NFT made by the coolest generative code.";
// base url to use in metadata file
// the id of the nft will be added to this url, in the example e.g. https://hashlips/nft/1 for NFT with id 1
const baseImageUri = "https://hashlips/nft";
// id for edition to start from
const startEditionFrom = 1;
// amount of NFTs to generate in edition
const editionSize = 100;
// prefix to add to edition dna ids (to distinguish dna counts from different generation processes for the same collection)
const editionDnaPrefix = 0
// create required weights
// for each weight, call 'addRarity' with the id and from which to which element this rarity should be applied
let rarityWeights = [
addRarity('super_rare', 1, editionSize * 0.01),
addRarity('extraordinary', editionSize * 0.01 + 1, editionSize * 0.08),
addRarity('rare', editionSize*0.08 + 1, editionSize * 0.45),
addRarity('original', editionSize * 0.45+1, editionSize)
];
// create required layers
// for each layer, call 'addLayer' with the id and optionally the positioning and size
// the id would be the name of the folder in your input directory, e.g. 'ball' for ./input/ball
const layers = [
addLayer('background', {x: 0, y:0}, {width:width, height:height}),
addLayer('00background'),
addLayer('01body'),
addLayer('02face'),
addLayer('03hair'),
addLayer('03body hoodie'),
addLayer('04mouth'),
addLayer('05eye'),
addLayer('06accessory'),
addLayer('07facemask')
// addLayer('ball', { x: 0, y: 0 }, { width: width, height: height }),
// addLayer('eye color'),
// addLayer('iris'),
// addLayer('shine'),
// addLayer('bottom lid'),
// addLayer('top lid')
];
// provide any specific percentages that are required for a given layer and rarity level
// all provided options are used based on their percentage values to decide which layer to select from
addRarityPercentForLayer('extraordinary', '00background', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('super_rare', '00background', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('rare', '00background', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('original', '00background', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('extraordinary', '01body', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('super_rare', '01body', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('rare', '01body', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('original', '01body', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('extraordinary', '03hair', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('super_rare', '03hair', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('rare', '03hair', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('original', '03hair', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('extraordinary', '03body hoodie', {'extraordinary': 2, 'super_rare': 0, 'rare':12 , 'original': 86 });
addRarityPercentForLayer('super_rare', '03body hoodie', {'extraordinary': 2, 'super_rare': 0, 'rare':12 , 'original': 86 });
addRarityPercentForLayer('rare', '03body hoodie', {'extraordinary': 2, 'super_rare': 0, 'rare':12 , 'original': 86 });
addRarityPercentForLayer('original', '03body hoodie', {'extraordinary': 2, 'super_rare': 0, 'rare':12 , 'original': 86 });
addRarityPercentForLayer('extraordinary', '04mouth', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('super_rare', '04mouth', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('rare', '04mouth', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('original', '04mouth', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('extraordinary', '06accessory', {'extraordinary': 15, 'super_rare': 0, 'rare':39 , 'original': 46 });
addRarityPercentForLayer('super_rare', '06accessory', {'extraordinary': 15, 'super_rare': 0, 'rare':39 , 'original': 46 });
addRarityPercentForLayer('rare', '06accessory', {'extraordinary': 15, 'super_rare': 0, 'rare':39 , 'original': 46 });
addRarityPercentForLayer('original', '06accessory', {'extraordinary': 15, 'super_rare': 0, 'rare':39 , 'original': 46 });
addRarityPercentForLayer('extraordinary', '05eye', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('super_rare', '05eye', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('rare', '05eye', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('original', '05eye', {'extraordinary': 7, 'super_rare': 1, 'rare':37 , 'original': 55 });
addRarityPercentForLayer('extraordinary', '07facemask', {'extraordinary': 0, 'super_rare': 5, 'rare':0 , 'original': 95 });
addRarityPercentForLayer('super_rare', '07facemask', {'extraordinary': 0, 'super_rare': 5, 'rare':0 , 'original': 95 });
addRarityPercentForLayer('rare', '07facemask', {'extraordinary': 0, 'super_rare': 5, 'rare':0 , 'original': 95 });
addRarityPercentForLayer('original', '07facemask', {'extraordinary': 0, 'super_rare': 5, 'rare':0 , 'original': 95 });
//addRarityPercentForLayer('super_rare', 'ball', { 'super_rare': 33, 'rare': 33, 'original': 33 });
module.exports = {
layers,
width,
height,
// description,
// baseImageUri,
editionSize,
startEditionFrom,
rarityWeights,
};