示例#1
0
  /** Clones this material. The result is returned. */
  @Override
  public Material clone() {
    try {
      Material mat = (Material) super.clone();

      if (additionalState != null) {
        mat.additionalState = additionalState.clone();
      }
      mat.technique = null;
      mat.techniques = new HashMap<String, Technique>();

      mat.paramValues = new ListMap<String, MatParam>();
      for (int i = 0; i < paramValues.size(); i++) {
        Map.Entry<String, MatParam> entry = paramValues.getEntry(i);
        mat.paramValues.put(entry.getKey(), entry.getValue().clone());
      }

      return mat;
    } catch (CloneNotSupportedException ex) {
      throw new AssertionError(ex);
    }
  }
示例#2
0
  /**
   * Compares two materials and returns true if they are equal. This methods compare definition,
   * parameters, additional render states. Since materials are mutable objects, implementing
   * equals() properly is not possible, hence the name contentEquals().
   *
   * @param otherObj the material to compare to this material
   * @return true if the materials are equal.
   */
  public boolean contentEquals(Object otherObj) {
    if (!(otherObj instanceof Material)) {
      return false;
    }

    Material other = (Material) otherObj;

    // Early exit if the material are the same object
    if (this == other) {
      return true;
    }

    // Check material definition
    if (this.getMaterialDef() != other.getMaterialDef()) {
      return false;
    }

    // Early exit if the size of the params is different
    if (this.paramValues.size() != other.paramValues.size()) {
      return false;
    }

    // Checking technique
    if (this.technique != null || other.technique != null) {
      // Techniques are considered equal if their names are the same
      // E.g. if user chose custom technique for one material but
      // uses default technique for other material, the materials
      // are not equal.
      String thisDefName = this.technique != null ? this.technique.getDef().getName() : "Default";
      String otherDefName =
          other.technique != null ? other.technique.getDef().getName() : "Default";
      if (!thisDefName.equals(otherDefName)) {
        return false;
      }
    }

    // Comparing parameters
    for (String paramKey : paramValues.keySet()) {
      MatParam thisParam = this.getParam(paramKey);
      MatParam otherParam = other.getParam(paramKey);

      // This param does not exist in compared mat
      if (otherParam == null) {
        return false;
      }

      if (!otherParam.equals(thisParam)) {
        return false;
      }
    }

    // Comparing additional render states
    if (additionalState == null) {
      if (other.additionalState != null) {
        return false;
      }
    } else {
      if (!additionalState.equals(other.additionalState)) {
        return false;
      }
    }

    return true;
  }