Ejemplo n.º 1
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");
    }
  }
 /**
  * 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;
 }
 /**
  * extract and check a condition expression
  *
  * @param cond the condition expression
  * @param statement the statement being read
  * @throws IOException
  */
 public void extractCondition(String cond, Statement statement) throws IOException {
   List<String> defines = conditionParser.extractDefines(cond);
   for (String string : defines) {
     MatParam param = findMatParam(string);
     if (param != null) {
       addDefine(param.getName(), param.getVarType());
     } else {
       throw new MatParseException(
           "Invalid condition, condition must match a Material Parameter named " + cond,
           statement);
     }
   }
 }
Ejemplo n.º 4
0
 public static BufferedImage terrainGetTexture(
     Terrain terrain, int width, int height, String map) {
   MatParam matParam = terrain.getMaterial().getParam(map);
   if (matParam == null) {
     return null;
   }
   Texture tex = (Texture) matParam.getValue();
   if (tex == null) {
     return null;
   }
   Image i = tex.getImage();
   if (i == null) {
     return null;
   }
   return scaleImage(ImageToAwt.convert(i, true, true, 0), width, height);
 }
 /**
  * updates the right variable of the given mapping from a MatParam (a WorldParam) it checks if the
  * unifrom hasn't already been loaded, add it to the maps if not.
  *
  * @param param the MatParam
  * @param mapping the mapping
  * @param map the map of uniforms to search into
  * @return true if the param was added to the map
  */
 public boolean updateRightFromUniforms(
     MatParam param,
     VariableMapping mapping,
     Map<String, DeclaredVariable> map,
     Statement statement)
     throws MatParseException {
   ShaderNodeVariable right = mapping.getRightVariable();
   DeclaredVariable dv = map.get(param.getPrefixedName());
   if (dv == null) {
     right.setType(param.getVarType().getGlslType());
     right.setName(param.getPrefixedName());
     if (mapping.getLeftVariable().getMultiplicity() != null) {
       if (!param.getVarType().name().endsWith("Array")) {
         throw new MatParseException(param.getName() + " is not of Array type", statement);
       }
       String multiplicity = mapping.getLeftVariable().getMultiplicity();
       try {
         Integer.parseInt(multiplicity);
       } catch (NumberFormatException nfe) {
         // multiplicity is not an int attempting to find for a material parameter.
         MatParam mp = findMatParam(multiplicity);
         if (mp != null) {
           addDefine(multiplicity, VarType.Int);
           multiplicity = multiplicity.toUpperCase();
         } else {
           throw new MatParseException(
               "Wrong multiplicity for variable"
                   + mapping.getLeftVariable().getName()
                   + ". "
                   + multiplicity
                   + " should be an int or a declared material parameter.",
               statement);
         }
       }
       right.setMultiplicity(multiplicity);
     }
     dv = new DeclaredVariable(right);
     map.put(right.getName(), dv);
     dv.addNode(shaderNode);
     mapping.setRightVariable(right);
     return true;
   }
   dv.addNode(shaderNode);
   mapping.setRightVariable(dv.var);
   return false;
 }
Ejemplo n.º 6
0
  private void readValueParam(String statement) throws IOException {
    // Use limit=1 incase filename contains colons
    String[] split = statement.split(":", 2);
    if (split.length != 2) {
      throw new IOException("Value parameter statement syntax incorrect");
    }
    String name = split[0].trim();

    // parse value
    MatParam p = material.getMaterialDef().getMaterialParam(name);
    if (p == null) {
      throw new IOException("The material parameter: " + name + " is undefined.");
    }

    Object valueObj = readValue(p.getVarType(), split[1]);
    if (p.getVarType().isTextureType()) {
      material.setTextureParam(name, p.getVarType(), (Texture) valueObj);
    } else {
      material.setParam(name, p.getVarType(), valueObj);
    }
  }
  /**
   * reads an input mapping
   *
   * @param statement1 the statement being read
   * @return the mapping
   * @throws IOException
   */
  public VariableMapping readInputMapping(Statement statement1) throws IOException {
    VariableMapping mapping = null;
    try {
      mapping = parseMapping(statement1, new boolean[] {false, true});
    } catch (Exception e) {
      throw new MatParseException("Unexpected mapping format", statement1, e);
    }
    ShaderNodeVariable left = mapping.getLeftVariable();
    ShaderNodeVariable right = mapping.getRightVariable();
    if (!updateVariableFromList(left, shaderNode.getDefinition().getInputs())) {
      throw new MatParseException(
          left.getName() + " is not an input variable of " + shaderNode.getDefinition().getName(),
          statement1);
    }

    if (left.getType().startsWith("sampler") && !right.getNameSpace().equals("MatParam")) {
      throw new MatParseException("Samplers can only be assigned to MatParams", statement1);
    }

    if (right.getNameSpace().equals("Global")) {
      right.setType("vec4"); // Globals are all vec4 for now (maybe forever...)
      //        updateCondition(right, mapping);
      storeGlobal(right, statement1);

    } else if (right.getNameSpace().equals("Attr")) {
      if (shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) {
        throw new MatParseException(
            "Cannot have an attribute as input in a fragment shader" + right.getName(), statement1);
      }
      updateVarFromAttributes(mapping.getRightVariable(), mapping);
      //          updateCondition(mapping.getRightVariable(), mapping);
      storeAttribute(mapping.getRightVariable());
    } else if (right.getNameSpace().equals("MatParam")) {
      MatParam param = findMatParam(right.getName());
      if (param == null) {
        throw new MatParseException(
            "Could not find a Material Parameter named " + right.getName(), statement1);
      }
      if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
        if (updateRightFromUniforms(param, mapping, vertexDeclaredUniforms, statement1)) {
          storeVertexUniform(mapping.getRightVariable());
        }
      } else {
        if (updateRightFromUniforms(param, mapping, fragmentDeclaredUniforms, statement1)) {
          if (mapping.getRightVariable().getType().contains("|")) {
            String type = fixSamplerType(left.getType(), mapping.getRightVariable().getType());
            if (type != null) {
              mapping.getRightVariable().setType(type);
            } else {
              throw new MatParseException(
                  param.getVarType().toString()
                      + " can only be matched to one of "
                      + param.getVarType().getGlslType().replaceAll("\\|", ",")
                      + " found "
                      + left.getType(),
                  statement1);
            }
          }
          storeFragmentUniform(mapping.getRightVariable());
        }
      }

    } else if (right.getNameSpace().equals("WorldParam")) {
      UniformBinding worldParam = findWorldParam(right.getName());
      if (worldParam == null) {
        throw new MatParseException(
            "Could not find a World Parameter named " + right.getName(), statement1);
      }
      if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
        if (updateRightFromUniforms(worldParam, mapping, vertexDeclaredUniforms)) {
          storeVertexUniform(mapping.getRightVariable());
        }
      } else {
        if (updateRightFromUniforms(worldParam, mapping, fragmentDeclaredUniforms)) {
          storeFragmentUniform(mapping.getRightVariable());
        }
      }

    } else {
      ShaderNode node = nodes.get(right.getNameSpace());
      if (node == null) {
        throw new MatParseException(
            "Undeclared node"
                + right.getNameSpace()
                + ". Make sure this node is declared before the current node",
            statement1);
      }
      ShaderNodeVariable var = findNodeOutput(node.getDefinition().getOutputs(), right.getName());
      if (var == null) {
        throw new MatParseException(
            "Cannot find output variable" + right.getName() + " form ShaderNode " + node.getName(),
            statement1);
      }
      right.setNameSpace(node.getName());
      right.setType(var.getType());
      right.setMultiplicity(var.getMultiplicity());
      mapping.setRightVariable(right);
      storeVaryings(node, mapping.getRightVariable());
    }

    checkTypes(mapping, statement1);

    return mapping;
  }