inputDisplayManager.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { IDisplayManager } from './displayManager';
  2. import { NodeMaterialBlock } from 'babylonjs/Materials/Node/nodeMaterialBlock';
  3. import { InputBlock } from 'babylonjs/Materials/Node/Blocks/Input/inputBlock';
  4. import { NodeMaterialSystemValues } from 'babylonjs/Materials/Node/Enums/nodeMaterialSystemValues';
  5. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/Enums/nodeMaterialBlockConnectionPointTypes';
  6. import { AnimatedInputBlockTypes } from 'babylonjs/Materials/Node/Blocks/Input/animatedInputBlockTypes';
  7. import { Vector2, Vector3, Vector4 } from 'babylonjs/Maths/math.vector';
  8. import { Color3 } from 'babylonjs/Maths/math.color';
  9. import { BlockTools } from '../../blockTools';
  10. import { StringTools } from '../../stringTools';
  11. export class InputDisplayManager implements IDisplayManager {
  12. public getHeaderClass(block: NodeMaterialBlock) {
  13. let inputBlock = block as InputBlock;
  14. if (inputBlock.isConstant) {
  15. return "constant"
  16. }
  17. if (inputBlock.visibleInInspector) {
  18. return "inspector"
  19. }
  20. return "";
  21. }
  22. public shouldDisplayPortLabels(block: NodeMaterialBlock): boolean {
  23. return false;
  24. }
  25. public getHeaderText(block: NodeMaterialBlock): string {
  26. let inputBlock = block as InputBlock;
  27. let name = `${inputBlock.name} (${StringTools.GetBaseType(inputBlock.output.type)})`;
  28. if (inputBlock.isAttribute) {
  29. name = StringTools.GetBaseType(inputBlock.output.type);
  30. }
  31. return name;
  32. }
  33. public getBackgroundColor(block: NodeMaterialBlock): string {
  34. let color = "";
  35. let inputBlock = block as InputBlock;
  36. switch (inputBlock.type) {
  37. case NodeMaterialBlockConnectionPointTypes.Color3:
  38. case NodeMaterialBlockConnectionPointTypes.Color4: {
  39. if (inputBlock.value) {
  40. color = (inputBlock.value as Color3).toHexString();
  41. break;
  42. }
  43. }
  44. default:
  45. color = BlockTools.GetColorFromConnectionNodeType(inputBlock.type);
  46. break;
  47. }
  48. return color;
  49. }
  50. public updatePreviewContent(block: NodeMaterialBlock, contentArea: HTMLDivElement): void {
  51. let value = "";
  52. let inputBlock = block as InputBlock;
  53. if (inputBlock.isAttribute) {
  54. const attrVal = inputBlock.name === 'position2d' ? 'position' : inputBlock.name;
  55. const attrName = inputBlock.name === 'position2d' ? 'postprocess' : 'mesh';
  56. value = attrName + "." + attrVal;
  57. } else if (inputBlock.isSystemValue) {
  58. switch (inputBlock.systemValue) {
  59. case NodeMaterialSystemValues.World:
  60. value = "World";
  61. break;
  62. case NodeMaterialSystemValues.WorldView:
  63. value = "World x View";
  64. break;
  65. case NodeMaterialSystemValues.WorldViewProjection:
  66. value = "World x View x Projection";
  67. break;
  68. case NodeMaterialSystemValues.View:
  69. value = "View";
  70. break;
  71. case NodeMaterialSystemValues.ViewProjection:
  72. value = "View x Projection";
  73. break;
  74. case NodeMaterialSystemValues.Projection:
  75. value = "Projection";
  76. break;
  77. case NodeMaterialSystemValues.CameraPosition:
  78. value = "Camera position";
  79. break;
  80. case NodeMaterialSystemValues.FogColor:
  81. value = "Fog color";
  82. break;
  83. case NodeMaterialSystemValues.DeltaTime:
  84. value = "Delta time";
  85. break;
  86. }
  87. } else {
  88. switch (inputBlock.type) {
  89. case NodeMaterialBlockConnectionPointTypes.Float:
  90. if (inputBlock.animationType !== AnimatedInputBlockTypes.None) {
  91. value = AnimatedInputBlockTypes[inputBlock.animationType];
  92. } else {
  93. value = inputBlock.value.toFixed(2);
  94. }
  95. break;
  96. case NodeMaterialBlockConnectionPointTypes.Vector2:
  97. let vec2Value = inputBlock.value as Vector2
  98. value = `(${vec2Value.x.toFixed(2)}, ${vec2Value.y.toFixed(2)})`;
  99. break;
  100. case NodeMaterialBlockConnectionPointTypes.Vector3:
  101. let vec3Value = inputBlock.value as Vector3
  102. value = `(${vec3Value.x.toFixed(2)}, ${vec3Value.y.toFixed(2)}, ${vec3Value.z.toFixed(2)})`;
  103. break;
  104. case NodeMaterialBlockConnectionPointTypes.Vector4:
  105. let vec4Value = inputBlock.value as Vector4
  106. value = `(${vec4Value.x.toFixed(2)}, ${vec4Value.y.toFixed(2)}, ${vec4Value.z.toFixed(2)}, ${vec4Value.w.toFixed(2)})`;
  107. break;
  108. }
  109. }
  110. contentArea.innerHTML = value;
  111. contentArea.classList.add("input-block");
  112. }
  113. }