ShaderNodeDefinition.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2009-2012 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.shader;
  33. import com.jme3.export.InputCapsule;
  34. import com.jme3.export.JmeExporter;
  35. import com.jme3.export.JmeImporter;
  36. import com.jme3.export.OutputCapsule;
  37. import com.jme3.export.Savable;
  38. import com.jme3.shader.Shader.ShaderType;
  39. import java.io.IOException;
  40. import java.util.ArrayList;
  41. import java.util.Arrays;
  42. import java.util.List;
  43. /**
  44. * Shader node definition structure meant for holding loaded datat from a
  45. * material definition j3md file
  46. *
  47. * @author Nehon
  48. */
  49. public class ShaderNodeDefinition implements Savable {
  50. private String name;
  51. private Shader.ShaderType type;
  52. private List<String> shadersLanguage = new ArrayList<String>();
  53. private List<String> shadersPath = new ArrayList<String>();
  54. private String documentation;
  55. private List<ShaderNodeVariable> inputs = new ArrayList<ShaderNodeVariable>();
  56. private List<ShaderNodeVariable> outputs = new ArrayList<ShaderNodeVariable>();
  57. private String path = null;
  58. private boolean noOutput = false;
  59. /**
  60. * creates a ShaderNodeDefinition
  61. *
  62. * @param name the name of the definition
  63. * @param type the type of the shader
  64. * @param shaderPath the path of the shader
  65. * @param shaderLanguage the shader language (minimum required for this
  66. * definition)
  67. */
  68. public ShaderNodeDefinition(String name, ShaderType type, String shaderPath, String shaderLanguage) {
  69. this.name = name;
  70. this.type = type;
  71. shadersLanguage.add(shaderLanguage);
  72. shadersPath.add(shaderPath);
  73. }
  74. /**
  75. * creates a ShaderNodeDefinition
  76. */
  77. public ShaderNodeDefinition() {
  78. }
  79. /**
  80. * returns the name of the definition
  81. *
  82. * @return the name
  83. */
  84. public String getName() {
  85. return name;
  86. }
  87. /**
  88. * sets the name of the definition
  89. *
  90. * @param name the name
  91. */
  92. public void setName(String name) {
  93. this.name = name;
  94. }
  95. /**
  96. *
  97. * @return the type of shader the definition applies to
  98. */
  99. public ShaderType getType() {
  100. return type;
  101. }
  102. /**
  103. * sets the type of shader this def applies to
  104. *
  105. * @param type the type
  106. */
  107. public void setType(ShaderType type) {
  108. this.type = type;
  109. }
  110. /**
  111. *
  112. * @return the docuentation for tthis definition
  113. */
  114. public String getDocumentation() {
  115. return documentation;
  116. }
  117. /**
  118. * sets the dcumentation
  119. *
  120. * @param documentation the documentation
  121. */
  122. public void setDocumentation(String documentation) {
  123. this.documentation = documentation;
  124. }
  125. /**
  126. *
  127. * @return the input variables of this definition
  128. */
  129. public List<ShaderNodeVariable> getInputs() {
  130. return inputs;
  131. }
  132. /**
  133. * sets the input variables of this definition
  134. *
  135. * @param inputs the inputs
  136. */
  137. public void setInputs(List<ShaderNodeVariable> inputs) {
  138. this.inputs = inputs;
  139. }
  140. /**
  141. *
  142. * @return the output variables of this definition
  143. */
  144. public List<ShaderNodeVariable> getOutputs() {
  145. return outputs;
  146. }
  147. /**
  148. * sets the output variables of this definition
  149. *
  150. * @param inputs the output
  151. */
  152. public void setOutputs(List<ShaderNodeVariable> outputs) {
  153. this.outputs = outputs;
  154. }
  155. /**
  156. * retrun the path of this definition
  157. * @return
  158. */
  159. public String getPath() {
  160. return path;
  161. }
  162. /**
  163. * sets the path of this definition
  164. * @param path
  165. */
  166. public void setPath(String path) {
  167. this.path = path;
  168. }
  169. /**
  170. * jme seralization (not used)
  171. *
  172. * @param ex the exporter
  173. * @throws IOException
  174. */
  175. public void write(JmeExporter ex) throws IOException {
  176. OutputCapsule oc = (OutputCapsule) ex.getCapsule(this);
  177. oc.write(name, "name", "");
  178. String[] str = new String[shadersLanguage.size()];
  179. oc.write(shadersLanguage.toArray(str), "shadersLanguage", null);
  180. oc.write(shadersPath.toArray(str), "shadersPath", null);
  181. oc.write(type, "type", null);
  182. oc.writeSavableArrayList((ArrayList) inputs, "inputs", new ArrayList<ShaderNodeVariable>());
  183. oc.writeSavableArrayList((ArrayList) outputs, "inputs", new ArrayList<ShaderNodeVariable>());
  184. }
  185. public List<String> getShadersLanguage() {
  186. return shadersLanguage;
  187. }
  188. public List<String> getShadersPath() {
  189. return shadersPath;
  190. }
  191. public boolean isNoOutput() {
  192. return noOutput;
  193. }
  194. public void setNoOutput(boolean noOutput) {
  195. this.noOutput = noOutput;
  196. }
  197. /**
  198. * jme seralization (not used)
  199. *
  200. * @param im the importer
  201. * @throws IOException
  202. */
  203. public void read(JmeImporter im) throws IOException {
  204. InputCapsule ic = (InputCapsule) im.getCapsule(this);
  205. name = ic.readString("name", "");
  206. String[] str = ic.readStringArray("shadersLanguage", null);
  207. if (str != null) {
  208. shadersLanguage = Arrays.asList(str);
  209. } else {
  210. shadersLanguage = new ArrayList<String>();
  211. }
  212. str = ic.readStringArray("shadersPath", null);
  213. if (str != null) {
  214. shadersPath = Arrays.asList(str);
  215. } else {
  216. shadersPath = new ArrayList<String>();
  217. }
  218. type = ic.readEnum("type", Shader.ShaderType.class, null);
  219. inputs = (List<ShaderNodeVariable>) ic.readSavableArrayList("inputs", new ArrayList<ShaderNodeVariable>());
  220. outputs = (List<ShaderNodeVariable>) ic.readSavableArrayList("outputs", new ArrayList<ShaderNodeVariable>());
  221. }
  222. /**
  223. * convenience tostring
  224. *
  225. * @return a string
  226. */
  227. @Override
  228. public String toString() {
  229. return "\nShaderNodeDefinition{\n" + "name=" + name + "\ntype=" + type + "\nshaderPath=" + shadersPath + "\nshaderLanguage=" + shadersLanguage + "\ndocumentation=" + documentation + "\ninputs=" + inputs + ",\noutputs=" + outputs + '}';
  230. }
  231. }