예제 #1
0
  // <TYPE> <NAME> [ "(" <FFBINDING> ")" ] [-LINEAR] [ ":" <DEFAULTVAL> ]
  private void readParam(String statement) throws IOException {
    String name;
    String defaultVal = null;
    ColorSpace colorSpace = null;

    String[] split = statement.split(":");

    // Parse default val
    if (split.length == 1) {
      // Doesn't contain default value
    } else {
      if (split.length != 2) {
        throw new IOException("Parameter statement syntax incorrect");
      }
      statement = split[0].trim();
      defaultVal = split[1].trim();
    }

    if (statement.endsWith("-LINEAR")) {
      colorSpace = ColorSpace.Linear;
      statement = statement.substring(0, statement.length() - "-LINEAR".length());
    }

    // Parse ffbinding
    int startParen = statement.indexOf("(");
    if (startParen != -1) {
      // get content inside parentheses
      int endParen = statement.indexOf(")", startParen);
      String bindingStr = statement.substring(startParen + 1, endParen).trim();
      // don't care about bindingStr
      statement = statement.substring(0, startParen);
    }

    // Parse type + name
    split = statement.split(whitespacePattern);
    if (split.length != 2) {
      throw new IOException("Parameter statement syntax incorrect");
    }

    VarType type;
    if (split[0].equals("Color")) {
      type = VarType.Vector4;
    } else {
      type = VarType.valueOf(split[0]);
    }

    name = split[1];

    Object defaultValObj = null;
    if (defaultVal != null) {
      defaultValObj = readValue(type, defaultVal);
    }
    if (type.isTextureType()) {
      materialDef.addMaterialParamTexture(type, name, colorSpace);
    } else {
      materialDef.addMaterialParam(type, name, defaultValObj);
    }
  }
예제 #2
0
  // <DEFINENAME> [ ":" <PARAMNAME> ]
  private void readDefine(String statement) throws IOException {
    String[] split = statement.split(":");
    if (split.length == 1) {
      String defineName = split[0].trim();
      presetDefines.add(defineName);
    } else if (split.length == 2) {
      String defineName = split[0].trim();
      String paramName = split[1].trim();
      MatParam param = materialDef.getMaterialParam(paramName);
      if (param == null) {
        logger.log(
            Level.WARNING,
            "In technique ''{0}'':\n"
                + "Define ''{1}'' mapped to non-existent"
                + " material parameter ''{2}'', ignoring.",
            new Object[] {technique.getName(), defineName, paramName});
        return;
      }

      VarType paramType = param.getVarType();
      technique.addShaderParamDefine(paramName, paramType, defineName);
    } else {
      throw new IOException("Define syntax incorrect");
    }
  }
예제 #3
0
  private void readTechnique(Statement techStat) throws IOException {
    isUseNodes = false;
    String[] split = techStat.getLine().split(whitespacePattern);
    if (split.length == 1) {
      technique = new TechniqueDef(null);
    } else if (split.length == 2) {
      String techName = split[1];
      technique = new TechniqueDef(techName);
    } else {
      throw new IOException("Technique statement syntax incorrect");
    }

    for (Statement statement : techStat.getContents()) {
      readTechniqueStatement(statement);
    }

    if (isUseNodes) {
      nodesLoaderDelegate.computeConditions();
      // used for caching later, the shader here is not a file.
      technique.setShaderFile(
          technique.hashCode() + "", technique.hashCode() + "", "GLSL100", "GLSL100");
    }

    if (shaderName.containsKey(Shader.ShaderType.Vertex)
        && shaderName.containsKey(Shader.ShaderType.Fragment)) {
      technique.setShaderFile(shaderName, shaderLanguage);
    }

    materialDef.addTechniqueDef(technique);
    technique = null;
    shaderLanguage.clear();
    shaderName.clear();
  }
 /**
  * finds a MatParam in the materialDef from the given name
  *
  * @param varName the matparam name
  * @return the MatParam
  */
 protected MatParam findMatParam(String varName) {
   for (MatParam matParam : materialDef.getMaterialParams()) {
     if (varName.equals(matParam.getName())) {
       return matParam;
     }
   }
   return null;
 }
예제 #5
0
  private void loadFromRoot(List<Statement> roots) throws IOException {
    if (roots.size() == 2) {
      Statement exception = roots.get(0);
      String line = exception.getLine();
      if (line.startsWith("Exception")) {
        throw new AssetLoadException(line.substring("Exception ".length()));
      } else {
        throw new IOException("In multiroot material, expected first statement to be 'Exception'");
      }
    } else if (roots.size() != 1) {
      throw new IOException("Too many roots in J3M/J3MD file");
    }

    boolean extending = false;
    Statement materialStat = roots.get(0);
    String materialName = materialStat.getLine();
    if (materialName.startsWith("MaterialDef")) {
      materialName = materialName.substring("MaterialDef ".length()).trim();
      extending = false;
    } else if (materialName.startsWith("Material")) {
      materialName = materialName.substring("Material ".length()).trim();
      extending = true;
    } else {
      throw new IOException("Specified file is not a Material file");
    }

    String[] split = materialName.split(":", 2);

    if (materialName.equals("")) {
      throw new MatParseException("Material name cannot be empty", materialStat);
    }

    if (split.length == 2) {
      if (!extending) {
        throw new MatParseException("Must use 'Material' when extending.", materialStat);
      }

      String extendedMat = split[1].trim();

      MaterialDef def = (MaterialDef) assetManager.loadAsset(new AssetKey(extendedMat));
      if (def == null) {
        throw new MatParseException(
            "Extended material " + extendedMat + " cannot be found.", materialStat);
      }

      material = new Material(def);
      material.setKey(key);
      material.setName(split[0].trim());
      //            material.setAssetName(fileName);
    } else if (split.length == 1) {
      if (extending) {
        throw new MatParseException("Expected ':', got '{'", materialStat);
      }
      materialDef = new MaterialDef(assetManager, materialName);
      // NOTE: pass file name for defs so they can be loaded later
      materialDef.setAssetName(key.getName());
    } else {
      throw new MatParseException("Cannot use colon in material name/path", materialStat);
    }

    for (Statement statement : materialStat.getContents()) {
      split = statement.getLine().split("[ \\{]");
      String statType = split[0];
      if (extending) {
        if (statType.equals("MaterialParameters")) {
          readExtendingMaterialParams(statement.getContents());
        } else if (statType.equals("AdditionalRenderState")) {
          readAdditionalRenderState(statement.getContents());
        } else if (statType.equals("Transparent")) {
          readTransparentStatement(statement.getLine());
        }
      } else {
        if (statType.equals("Technique")) {
          readTechnique(statement);
        } else if (statType.equals("MaterialParameters")) {
          readMaterialParams(statement.getContents());
        } else {
          throw new MatParseException(
              "Expected material statement, got '" + statType + "'", statement);
        }
      }
    }
  }
예제 #6
0
  private void readTechnique(Statement techStat) throws IOException {
    isUseNodes = false;
    String[] split = techStat.getLine().split(whitespacePattern);

    String name;
    if (split.length == 1) {
      name = TechniqueDef.DEFAULT_TECHNIQUE_NAME;
    } else if (split.length == 2) {
      name = split[1];
    } else {
      throw new IOException("Technique statement syntax incorrect");
    }

    String techniqueUniqueName = materialDef.getAssetName() + "@" + name;
    technique = new TechniqueDef(name, techniqueUniqueName.hashCode());

    for (Statement statement : techStat.getContents()) {
      readTechniqueStatement(statement);
    }

    technique.setShaderPrologue(createShaderPrologue(presetDefines));

    switch (technique.getLightMode()) {
      case Disable:
        technique.setLogic(new DefaultTechniqueDefLogic(technique));
        break;
      case MultiPass:
        technique.setLogic(new MultiPassLightingLogic(technique));
        break;
      case SinglePass:
        technique.setLogic(new SinglePassLightingLogic(technique));
        break;
      case StaticPass:
        technique.setLogic(new StaticPassLightingLogic(technique));
        break;
      case SinglePassAndImageBased:
        technique.setLogic(new SinglePassAndImageBasedLightingLogic(technique));
        break;
      default:
        throw new UnsupportedOperationException();
    }

    List<TechniqueDef> techniqueDefs = new ArrayList<>();

    if (isUseNodes) {
      nodesLoaderDelegate.computeConditions();

      // used for caching later, the shader here is not a file.

      // KIRILL 9/19/2015
      // Not sure if this is needed anymore, since shader caching
      // is now done by TechniqueDef.
      technique.setShaderFile(
          technique.hashCode() + "", technique.hashCode() + "", "GLSL100", "GLSL100");
      techniqueDefs.add(technique);
    } else if (shaderNames.containsKey(Shader.ShaderType.Vertex)
        && shaderNames.containsKey(Shader.ShaderType.Fragment)) {
      if (shaderLanguages.size() > 1) {
        for (int i = 1; i < shaderLanguages.size(); i++) {
          TechniqueDef td = null;
          try {
            td = (TechniqueDef) technique.clone();
          } catch (CloneNotSupportedException e) {
            e.printStackTrace();
          }
          td.setShaderFile(shaderNames, shaderLanguages.get(i));
          techniqueDefs.add(td);
        }
      }
      technique.setShaderFile(shaderNames, shaderLanguages.get(0));
      techniqueDefs.add(technique);

    } else {
      technique = null;
      shaderLanguages.clear();
      shaderNames.clear();
      presetDefines.clear();
      langSize = 0;
      logger.log(Level.WARNING, "Fixed function technique was ignored");
      logger.log(
          Level.WARNING,
          "Fixed function technique ''{0}'' was ignored for material {1}",
          new Object[] {name, key});
      return;
    }

    for (TechniqueDef techniqueDef : techniqueDefs) {
      materialDef.addTechniqueDef(techniqueDef);
    }

    technique = null;
    langSize = 0;
    shaderLanguages.clear();
    shaderNames.clear();
    presetDefines.clear();
  }