hello_node.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. let plugin = new arm.Plugin();
  2. let categoryName = "My Nodes";
  3. let nodeName = "Hello World";
  4. let nodeType = "HELLO_WORLD";
  5. // Create new node category
  6. let categories = arm.NodesMaterial.categories;
  7. categories.push(categoryName);
  8. // Create new node
  9. let nodes = [
  10. {
  11. id: 0,
  12. name: nodeName,
  13. type: nodeType,
  14. x: 0,
  15. y: 0,
  16. color: 0xffb34f5a,
  17. inputs: [
  18. {
  19. id: 0,
  20. node_id: 0,
  21. name: "Scale",
  22. type: "VALUE",
  23. color: 0xffa1a1a1,
  24. default_value: 1,
  25. min: 0.0,
  26. max: 5.0
  27. }
  28. ],
  29. outputs: [
  30. {
  31. id: 0,
  32. node_id: 0,
  33. name: "Color",
  34. type: "RGBA",
  35. color: 0xffc7c729,
  36. default_value: new Float32Array([0.8, 0.8, 0.8, 1.0])
  37. },
  38. {
  39. id: 1,
  40. node_id: 0,
  41. name: "Fac",
  42. type: "VALUE",
  43. color: 0xffa1a1a1,
  44. default_value: 1.0
  45. }
  46. ],
  47. buttons: []
  48. }
  49. ];
  50. arm.NodesMaterial.list.push(nodes);
  51. // Node shader
  52. arm.MaterialParser.customNodes.set(nodeType, function(node, socket) {
  53. let frag = arm.MaterialParser.frag;
  54. let scale = arm.MaterialParser.parse_value_input(node.inputs[0]);
  55. let my_out = arm.MaterialParser.node_name(node) + "_out";
  56. frag.write(`
  57. float ${my_out} = cos(sin(texCoord.x * 200.0 * ${scale}) + cos(texCoord.y * 200.0 * ${scale}));
  58. `);
  59. if (socket.name == "Color") {
  60. return `vec3(${my_out}, ${my_out}, ${my_out})`;
  61. }
  62. else if (socket.name == "Fac") {
  63. return my_out;
  64. }
  65. });
  66. // Cleanup
  67. plugin.delete = function() {
  68. arm.MaterialParser.customNodes.delete(nodeType);
  69. arm.NodesMaterial.list.splice(arm.NodesMaterial.list.indexOf(nodes), 1);
  70. categories.splice(categories.indexOf(categoryName), 1);
  71. };