blockTools.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { AlphaTestBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/alphaTestBlock';
  2. import { BonesBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/bonesBlock';
  3. import { InstancesBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/instancesBlock';
  4. import { MorphTargetsBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/morphTargetsBlock';
  5. import { ImageProcessingBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/imageProcessingBlock';
  6. import { ColorMergerBlock } from 'babylonjs/Materials/Node/Blocks/colorMergerBlock';
  7. import { VectorMergerBlock } from 'babylonjs/Materials/Node/Blocks/vectorMergerBlock';
  8. import { ColorSplitterBlock } from 'babylonjs/Materials/Node/Blocks/colorSplitterBlock';
  9. import { VectorSplitterBlock } from 'babylonjs/Materials/Node/Blocks/vectorSplitterBlock';
  10. import { RemapBlock } from 'babylonjs/Materials/Node/Blocks/remapBlock';
  11. import { TextureBlock } from 'babylonjs/Materials/Node/Blocks/Dual/textureBlock';
  12. import { ReflectionTextureBlock } from 'babylonjs/Materials/Node/Blocks/Dual/reflectionTextureBlock';
  13. import { LightBlock } from 'babylonjs/Materials/Node/Blocks/Dual/lightBlock';
  14. import { FogBlock } from 'babylonjs/Materials/Node/Blocks/Dual/fogBlock';
  15. import { VertexOutputBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/vertexOutputBlock';
  16. import { FragmentOutputBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/fragmentOutputBlock';
  17. import { NormalizeBlock } from 'babylonjs/Materials/Node/Blocks/normalizeBlock';
  18. import { AddBlock } from 'babylonjs/Materials/Node/Blocks/addBlock';
  19. import { ScaleBlock } from 'babylonjs/Materials/Node/Blocks/scaleBlock';
  20. import { TrigonometryBlock, TrigonometryBlockOperations } from 'babylonjs/Materials/Node/Blocks/trigonometryBlock';
  21. import { ClampBlock } from 'babylonjs/Materials/Node/Blocks/clampBlock';
  22. import { CrossBlock } from 'babylonjs/Materials/Node/Blocks/crossBlock';
  23. import { DotBlock } from 'babylonjs/Materials/Node/Blocks/dotBlock';
  24. import { MultiplyBlock } from 'babylonjs/Materials/Node/Blocks/multiplyBlock';
  25. import { TransformBlock } from 'babylonjs/Materials/Node/Blocks/transformBlock';
  26. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
  27. import { FresnelBlock } from 'babylonjs/Materials/Node/Blocks/fresnelBlock';
  28. import { LerpBlock } from 'babylonjs/Materials/Node/Blocks/lerpBlock';
  29. import { DivideBlock } from 'babylonjs/Materials/Node/Blocks/divideBlock';
  30. import { SubtractBlock } from 'babylonjs/Materials/Node/Blocks/subtractBlock';
  31. import { StepBlock } from 'babylonjs/Materials/Node/Blocks/stepBlock';
  32. import { InputBlock } from 'babylonjs/Materials/Node/Blocks/Input/inputBlock';
  33. import { NodeMaterialWellKnownValues } from 'babylonjs/Materials/Node/nodeMaterialWellKnownValues';
  34. import { AnimatedInputBlockTypes } from 'babylonjs/Materials/Node/Blocks/Input/animatedInputBlockTypes';
  35. import { OppositeBlock } from 'babylonjs/Materials/Node/Blocks/oppositeBlock';
  36. import { ViewDirectionBlock } from 'babylonjs/Materials/Node/Blocks/viewDirectionBlock';
  37. import { LightInformationBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/lightInformationBlock';
  38. import { MaxBlock } from 'babylonjs/Materials/Node/Blocks/maxBlock';
  39. import { MinBlock } from 'babylonjs/Materials/Node/Blocks/minBlock';
  40. export class BlockTools {
  41. public static GetBlockFromString(data: string) {
  42. switch (data) {
  43. case "BonesBlock":
  44. return new BonesBlock("Bones");
  45. case "InstancesBlock":
  46. return new InstancesBlock("Instances");
  47. case "MorphTargetsBlock":
  48. return new MorphTargetsBlock("MorphTargets");
  49. case "AlphaTestBlock":
  50. return new AlphaTestBlock("AlphaTest");
  51. case "ImageProcessingBlock":
  52. return new ImageProcessingBlock("ImageProcessing");
  53. case "ColorMergerBlock":
  54. return new ColorMergerBlock("ColorMerger");
  55. case "VectorMergerBlock":
  56. return new VectorMergerBlock("VectorMerger");
  57. case "ColorSplitterBlock":
  58. return new ColorSplitterBlock("ColorSplitter");
  59. case "VectorSplitterBlock":
  60. return new VectorSplitterBlock("VectorSplitter");
  61. case "TextureBlock":
  62. return new TextureBlock("Texture");
  63. case "ReflectionTextureBlock":
  64. return new ReflectionTextureBlock("Texture");
  65. case "LightBlock":
  66. return new LightBlock("Lights");
  67. case "FogBlock":
  68. return new FogBlock("Fog");
  69. case "VertexOutputBlock":
  70. return new VertexOutputBlock("VertexOutput");
  71. case "FragmentOutputBlock":
  72. return new FragmentOutputBlock("FragmentOutput");
  73. case "AddBlock":
  74. return new AddBlock("Add");
  75. case "ClampBlock":
  76. return new ClampBlock("Clamp");
  77. case "ScaleBlock":
  78. return new ScaleBlock("Scale");
  79. case "CrossBlock":
  80. return new CrossBlock("Dot");
  81. case "DotBlock":
  82. return new DotBlock("Dot");
  83. case "MultiplyBlock":
  84. return new MultiplyBlock("Multiply");
  85. case "TransformBlock":
  86. return new TransformBlock("Transform");
  87. case "TrigonometryBlock":
  88. return new TrigonometryBlock("Trigonometry");
  89. case "RemapBlock":
  90. return new RemapBlock("Remap");
  91. case "NormalizeBlock":
  92. return new NormalizeBlock("Normalize");
  93. case "FresnelBlock":
  94. return new FresnelBlock("Fresnel");
  95. case "LerpBlock":
  96. return new LerpBlock("Lerp");
  97. case "DivideBlock":
  98. return new DivideBlock("Divide");
  99. case "SubtractBlock":
  100. return new SubtractBlock("Subtract");
  101. case "StepBlock":
  102. return new StepBlock("Step");
  103. case "OppositeBlock":
  104. return new OppositeBlock("Opposite");
  105. case "ViewDirectionBlock":
  106. return new ViewDirectionBlock("View direction");
  107. case "LightInformationBlock":
  108. return new LightInformationBlock("Light information");
  109. case "MaxBlock":
  110. return new MaxBlock("Max");
  111. case "MinBlock":
  112. return new MinBlock("Min");
  113. case "CosBlock": {
  114. let cosBlock = new TrigonometryBlock("Cos");
  115. cosBlock.operation = TrigonometryBlockOperations.Cos;
  116. return cosBlock;
  117. }
  118. case "SinBlock": {
  119. let sinBlock = new TrigonometryBlock("Sin");
  120. sinBlock.operation = TrigonometryBlockOperations.Sin;
  121. return sinBlock;
  122. }
  123. case "AbsBlock": {
  124. let absBlock = new TrigonometryBlock("Abs");
  125. absBlock.operation = TrigonometryBlockOperations.Abs;
  126. return absBlock;
  127. }
  128. case "ExpBlock": {
  129. let expBlock = new TrigonometryBlock("Exp");
  130. expBlock.operation = TrigonometryBlockOperations.Exp;
  131. return expBlock;
  132. }
  133. case "Exp2Block": {
  134. let exp2Block = new TrigonometryBlock("Exp2");
  135. exp2Block.operation = TrigonometryBlockOperations.Exp2;
  136. return exp2Block;
  137. }
  138. case "RoundBlock": {
  139. let roundBlock = new TrigonometryBlock("Round");
  140. roundBlock.operation = TrigonometryBlockOperations.Round;
  141. return roundBlock;
  142. }
  143. case "CeilingBlock": {
  144. let ceilingBlock = new TrigonometryBlock("Ceiling");
  145. ceilingBlock.operation = TrigonometryBlockOperations.Ceiling;
  146. return ceilingBlock;
  147. }
  148. case "FloorBlock": {
  149. let floorBlock = new TrigonometryBlock("Floor");
  150. floorBlock.operation = TrigonometryBlockOperations.Floor;
  151. return floorBlock;
  152. }
  153. case "WorldMatrixBlock": {
  154. let worldMatrixBlock = new InputBlock("World");
  155. worldMatrixBlock.setAsWellKnownValue(NodeMaterialWellKnownValues.World);
  156. return worldMatrixBlock;
  157. }
  158. case "WorldViewMatrixBlock": {
  159. let worldViewMatrixBlock = new InputBlock("World x View");
  160. worldViewMatrixBlock.setAsWellKnownValue(NodeMaterialWellKnownValues.WorldView);
  161. return worldViewMatrixBlock;
  162. }
  163. case "WorldViewProjectionMatrixBlock": {
  164. let worldViewProjectionMatrixBlock = new InputBlock("World x View x Projection");
  165. worldViewProjectionMatrixBlock.setAsWellKnownValue(NodeMaterialWellKnownValues.WorldViewProjection);
  166. return worldViewProjectionMatrixBlock;
  167. }
  168. case "ViewMatrixBlock": {
  169. let viewMatrixBlock = new InputBlock("View");
  170. viewMatrixBlock.setAsWellKnownValue(NodeMaterialWellKnownValues.View);
  171. return viewMatrixBlock;
  172. }
  173. case "ViewProjectionMatrixBlock": {
  174. let viewProjectionMatrixBlock = new InputBlock("View x Projection");
  175. viewProjectionMatrixBlock.setAsWellKnownValue(NodeMaterialWellKnownValues.ViewProjection);
  176. return viewProjectionMatrixBlock;
  177. }
  178. case "ProjectionMatrixBlock": {
  179. let projectionMatrixBlock = new InputBlock("Projection");
  180. projectionMatrixBlock.setAsWellKnownValue(NodeMaterialWellKnownValues.Projection);
  181. return projectionMatrixBlock;
  182. }
  183. case "CameraPositionBlock": {
  184. let cameraPosition = new InputBlock("Camera position");
  185. cameraPosition.setAsWellKnownValue(NodeMaterialWellKnownValues.CameraPosition);
  186. return cameraPosition;
  187. }
  188. case "FogColorBlock": {
  189. let FogColor = new InputBlock("Fog color");
  190. FogColor.setAsWellKnownValue(NodeMaterialWellKnownValues.FogColor);
  191. return FogColor;
  192. }
  193. case "PositionBlock": {
  194. let meshPosition = new InputBlock("position");
  195. meshPosition.setAsAttribute("position");
  196. return meshPosition;
  197. }
  198. case "UVBlock": {
  199. let meshUV = new InputBlock("uv");
  200. meshUV.setAsAttribute("uv");
  201. return meshUV;
  202. }
  203. case "ColorBlock": {
  204. let meshColor = new InputBlock("color");
  205. meshColor.setAsAttribute("color");
  206. return meshColor;
  207. }
  208. case "NormalBlock": {
  209. let meshNormal = new InputBlock("normal");
  210. meshNormal.setAsAttribute("normal");
  211. return meshNormal;
  212. }
  213. case "TangentBlock": {
  214. let meshTangent = new InputBlock("tangent");
  215. meshTangent.setAsAttribute("tangent");
  216. return meshTangent;
  217. }
  218. case "MatrixIndicesBlock": {
  219. let meshMatrixIndices = new InputBlock("matricesIndices");
  220. meshMatrixIndices.setAsAttribute("matricesIndices");
  221. return meshMatrixIndices;
  222. }
  223. case "MatrixWeightsBlock": {
  224. let meshMatrixWeights = new InputBlock("matricesWeights");
  225. meshMatrixWeights.setAsAttribute("matricesWeights");
  226. return meshMatrixWeights;
  227. }
  228. case "TimeBlock": {
  229. let timeBlock = new InputBlock("Time", undefined, NodeMaterialBlockConnectionPointTypes.Float);
  230. timeBlock.animationType = AnimatedInputBlockTypes.Time;
  231. return timeBlock;
  232. }
  233. }
  234. return null;
  235. }
  236. public static GetColorFromConnectionNodeType(type: NodeMaterialBlockConnectionPointTypes) {
  237. let color = "Red";
  238. switch (type) {
  239. case NodeMaterialBlockConnectionPointTypes.Float:
  240. color = "#ca9e27";
  241. break;
  242. case NodeMaterialBlockConnectionPointTypes.Vector2:
  243. color = "#16bcb1";
  244. break;
  245. case NodeMaterialBlockConnectionPointTypes.Vector3:
  246. case NodeMaterialBlockConnectionPointTypes.Color3:
  247. color = "#b786cb";
  248. break;
  249. case NodeMaterialBlockConnectionPointTypes.Vector4:
  250. case NodeMaterialBlockConnectionPointTypes.Color4:
  251. color = "#be5126";
  252. break;
  253. case NodeMaterialBlockConnectionPointTypes.Matrix:
  254. color = "#591990";
  255. break;
  256. }
  257. return color;
  258. }
  259. public static GetConnectionNodeTypeFromString(type: string) {
  260. switch (type) {
  261. case "Float":
  262. return NodeMaterialBlockConnectionPointTypes.Float;
  263. case "Vector2":
  264. return NodeMaterialBlockConnectionPointTypes.Vector2;
  265. case "Vector3":
  266. return NodeMaterialBlockConnectionPointTypes.Vector3;
  267. case "Vector4":
  268. return NodeMaterialBlockConnectionPointTypes.Vector4;
  269. case "Matrix":
  270. return NodeMaterialBlockConnectionPointTypes.Matrix;
  271. case "Color3":
  272. return NodeMaterialBlockConnectionPointTypes.Color3;
  273. case "Color4":
  274. return NodeMaterialBlockConnectionPointTypes.Color4;
  275. }
  276. return NodeMaterialBlockConnectionPointTypes.AutoDetect;
  277. }
  278. public static GetStringFromConnectionNodeType(type: NodeMaterialBlockConnectionPointTypes) {
  279. switch (type){
  280. case NodeMaterialBlockConnectionPointTypes.Float:
  281. return "Float";
  282. case NodeMaterialBlockConnectionPointTypes.Vector2:
  283. return "Vector2";
  284. case NodeMaterialBlockConnectionPointTypes.Vector3:
  285. return "Vector3";
  286. case NodeMaterialBlockConnectionPointTypes.Vector4:
  287. return "Vector4";
  288. case NodeMaterialBlockConnectionPointTypes.Color3:
  289. return "Color3";
  290. case NodeMaterialBlockConnectionPointTypes.Color4:
  291. return "Color4";
  292. case NodeMaterialBlockConnectionPointTypes.Matrix:
  293. return "Matrix";
  294. }
  295. return "";
  296. }
  297. }