TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ CustomShaderNode()

CustomShaderNode::CustomShaderNode ( GLSLHandler handler,
std::string  shader 
)

Definition at line 255 of file CustomShaderNode.cpp.

256 :SNENode(handler)
257{
258 name = "Custom Shader";
259 headerColor = ImColor(SHADER_VALUE_NODE_COLOR);
260 shader = s;
261 // Parse the shader
262 std::stringstream ss(shader);
263 std::string line;
264 std::string codeStr = "";
265 std::string metaStr = "";
266 bool isCodeActive = false;
267 bool isMetaActive = false;
268
269 while(std::getline(ss, line))
270 {
271 if(line[0] == '#')
272 {
273 continue;
274 }
275
276 else if(line == "[META]")
277 {
278 isMetaActive = true;
279 }
280
281 else if(line == "[CODE]")
282 {
283 isCodeActive = true;
284 }
285
286 else if(line == "[/META]")
287 {
288 isMetaActive = false;
289 }
290
291 else if(line == "[/CODE]")
292 {
293 isCodeActive = false;
294 }
295
296 else if(isMetaActive)
297 {
298 metaStr += line + "\n";
299 }
300
301 else if(isCodeActive)
302 {
303 codeStr += line + "\n";
304 }
305 }
306
307 // Load the meta data
308 if(metaStr.size() > 0)
309 {
310 meta = nlohmann::json::parse(metaStr);
311 }
312
313 else
314 {
315 throw std::runtime_error("No meta data found in shader");
316 }
317
318 // Load the code
319 if(codeStr.size() > 0)
320 {
321 code = codeStr;
322 }
323
324 else
325 {
326 throw std::runtime_error("No code found in shader");
327 }
328
329 // Load the data
330 name = meta["name"];
331 std::cout << "Loading Node : " << name << "\n";
332 std::string paramsStr = "";
333 int j = 0;
334
335 if(meta.find("param_count") != meta.end())
336 {
337 paramCount = meta["param_count"];
338 useArrayParams = true;
339 }
340 else
341 {
342 paramCount = 1;
343 useArrayParams = false;
344 }
345 int pss = meta["params"].size();
346
347 for(std::string param : meta["params"])
348 {
349 std::string pType = param.substr(0, param.find(":"));
350 std::string pName = param.substr(param.find(":") + 1);
351 params.push_back(std::make_pair(pType, pName));
352 if(useArrayParams)
353 paramsStr += pType + " " + pName + "[" + STR(paramCount) + "]" + (j < pss - 1 ? ", " : "");
354 else
355 paramsStr += pType + " " + pName + (j < pss - 1 ? ", " : "");
356 j++;
357 }
358
359 paramsStr += (params.size() == 0 ? "" : ", ") + std::string("int callerPin");
360
361 func = new GLSLFunction(meta["fname"], paramsStr, meta["returns"]);
362 func->name += STR(id);
363 sharedDataTemplate.clear();
364 int i = 0;
365
366 for(auto it = meta["sharedData"].begin() ; it != meta["sharedData"].end() ; it++)
367 {
368 std::string alias = ReplaceString(it.key(), " ", "");
369 if(it.value().find("alias") != it.value().end())
370 alias = it.value()["alias"];
371 sharedDataTemplate.push_back(SharedDataRep(it.key(), it.value()["type"], alias));
372
373 if(it.value()["type"] == "float")
374 {
375 fData[i] = it.value()["default"].get<float>();
376 }
377
378 else if(it.value()["type"] == "bool")
379 {
380 bData[i] = it.value()["default"].get<bool>();
381 }
382
383 i++;
384 }
385
386 if(meta.find("output_pins") != meta.end())
387 {
388 std::string returns = meta["returns"];
389 useMultipleOpins = true;
390 for(std::string oPin : meta["output_pins"])
391 {
392 oPinStrs.push_back(oPin);
393 if(meta["returns"] == "float")
394 {
395 outputPins.push_back(new SNEPin(NodeEditorPinType::Output, SNEPinType::SNEPinType_Float));
396 }
397 else if(meta["returns"] == "vec3")
398 {
399 outputPins.push_back(new SNEPin(NodeEditorPinType::Output, SNEPinType::SNEPinType_Float3));
400 }
401 }
402 }
403 else
404 {
405 if(meta["returns"] == "float")
406 {
407 outputPins.push_back(new SNEPin(NodeEditorPinType::Output, SNEPinType::SNEPinType_Float));
408 }
409 else if(meta["returns"] == "vec3")
410 {
411 outputPins.push_back(new SNEPin(NodeEditorPinType::Output, SNEPinType::SNEPinType_Float3));
412 }
413 else
414 {
415 std::string t = meta["returns"];
416 throw std::runtime_error("Custom shader node does not support return type " + t);
417 }
418 }
419
420
421 for(int k = 0 ; k < paramCount ; k++)
422 {
423 for(auto &it : params)
424 {
425 if(it.first == "float")
426 {
427 inputPins.push_back(new SNEPin(NodeEditorPinType::Input, SNEPinType::SNEPinType_Float));
428 }
429
430 else if(it.first == "vec3")
431 {
432 inputPins.push_back(new SNEPin(NodeEditorPinType::Input, SNEPinType::SNEPinType_Float3));
433 }
434
435 else
436 {
437 throw std::runtime_error("Custom shader node does not support parameter type " + it.first);
438 }
439 }
440 }
441 headerColor = ImColor(meta["color"]["r"].get<int>(), meta["color"]["r"].get<int>(), meta["color"]["b"].get<int>());
442}
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition: json.hpp:24605
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:22912
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:22448
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:22223