inputNodeWidget.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import * as React from "react";
  2. import { InputNodeModel } from './inputNodeModel';
  3. import { Nullable } from 'babylonjs/types';
  4. import { GlobalState } from '../../../globalState';
  5. import { NodeMaterialWellKnownValues } from 'babylonjs/Materials/Node/nodeMaterialWellKnownValues';
  6. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
  7. import { Color3 } from 'babylonjs/Maths/math';
  8. import { StringTools } from '../../../stringTools';
  9. import { PortHelper } from '../portHelper';
  10. /**
  11. * GenericNodeWidgetProps
  12. */
  13. export interface InputNodeWidgetProps {
  14. node: Nullable<InputNodeModel>;
  15. globalState: GlobalState;
  16. }
  17. /**
  18. * Used to display a node block for the node editor
  19. */
  20. export class InputNodeWidget extends React.Component<InputNodeWidgetProps> {
  21. /**
  22. * Creates a GenericNodeWidget
  23. * @param props
  24. */
  25. constructor(props: InputNodeWidgetProps) {
  26. super(props);
  27. this.state = {};
  28. if (this.props.node) {
  29. this.props.node.addListener({
  30. selectionChanged: () => {
  31. let selected = (this.props.node as any).selected;
  32. this.props.globalState.onSelectionChangedObservable.notifyObservers(selected ? this.props.node : null);
  33. }
  34. });
  35. }
  36. }
  37. renderValue(value: string) {
  38. if (value) {
  39. return (
  40. <div className="value-text">
  41. {value}
  42. </div>
  43. )
  44. }
  45. return null;
  46. }
  47. render() {
  48. var outputPorts = PortHelper.GenerateOutputPorts(this.props.node, true);
  49. let inputBlock = this.props.node!.inputBlock;
  50. let value = "";
  51. let name = StringTools.GetBaseType(inputBlock.output.type);
  52. let color = "";
  53. if (inputBlock) {
  54. if (inputBlock.isAttribute) {
  55. value = "mesh." + inputBlock.name;
  56. } else if (inputBlock.isWellKnownValue) {
  57. switch (inputBlock.wellKnownValue) {
  58. case NodeMaterialWellKnownValues.World:
  59. value = "World";
  60. break;
  61. case NodeMaterialWellKnownValues.WorldView:
  62. value = "World x View";
  63. break;
  64. case NodeMaterialWellKnownValues.WorldViewProjection:
  65. value = "World x View x Projection";
  66. break;
  67. case NodeMaterialWellKnownValues.View:
  68. value = "View";
  69. break;
  70. case NodeMaterialWellKnownValues.ViewProjection:
  71. value = "View x Projection";
  72. break;
  73. case NodeMaterialWellKnownValues.Projection:
  74. value = "Projection";
  75. break;
  76. case NodeMaterialWellKnownValues.CameraPosition:
  77. value = "Camera position";
  78. break;
  79. case NodeMaterialWellKnownValues.FogColor:
  80. value = "Fog color";
  81. break;
  82. }
  83. } else {
  84. if (!inputBlock || !inputBlock.isUniform) {
  85. return null;
  86. }
  87. switch (inputBlock.type) {
  88. case NodeMaterialBlockConnectionPointTypes.Color3:
  89. case NodeMaterialBlockConnectionPointTypes.Color3OrColor4:
  90. case NodeMaterialBlockConnectionPointTypes.Color4: {
  91. color = (inputBlock.value as Color3).toHexString();
  92. }
  93. }
  94. }
  95. } else {
  96. name = "Not connected input";
  97. }
  98. return (
  99. <div className={"diagramBlock input" + (inputBlock && inputBlock.isAttribute ? " attribute" : "")} style={{
  100. background: color
  101. }}>
  102. <div className="header">
  103. {name}
  104. </div>
  105. <div className="outputs">
  106. {outputPorts}
  107. </div>
  108. <div className="value">
  109. {
  110. this.renderValue(value)
  111. }
  112. </div>
  113. </div>
  114. );
  115. }
  116. }