Example #1
0
  static {
    depthOnly.setDepthTest(true);
    depthOnly.setDepthWrite(true);
    depthOnly.setFaceCullMode(RenderState.FaceCullMode.Back);
    depthOnly.setColorWrite(false);

    additiveLight.setBlendMode(RenderState.BlendMode.AlphaAdditive);
    additiveLight.setDepthWrite(false);
  }
Example #2
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);
    }
  }
Example #3
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;
  }
Example #4
0
  public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);

    additionalState = (RenderState) ic.readSavable("render_state", null);
    transparent = ic.readBoolean("is_transparent", false);

    // Load the material def
    String defName = ic.readString("material_def", null);
    HashMap<String, MatParam> params =
        (HashMap<String, MatParam>) ic.readStringSavableMap("parameters", null);

    boolean enableVcolor = false;
    boolean separateTexCoord = false;
    boolean applyDefaultValues = false;
    boolean guessRenderStateApply = false;

    int ver = ic.getSavableVersion(Material.class);
    if (ver < 1) {
      applyDefaultValues = true;
    }
    if (ver < 2) {
      guessRenderStateApply = true;
    }
    if (im.getFormatVersion() == 0) {
      // Enable compatibility with old models
      if (defName.equalsIgnoreCase("Common/MatDefs/Misc/VertexColor.j3md")) {
        // Using VertexColor, switch to Unshaded and set VertexColor=true
        enableVcolor = true;
        defName = "Common/MatDefs/Misc/Unshaded.j3md";
      } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/SimpleTextured.j3md")
          || defName.equalsIgnoreCase("Common/MatDefs/Misc/SolidColor.j3md")) {
        // Using SimpleTextured/SolidColor, just switch to Unshaded
        defName = "Common/MatDefs/Misc/Unshaded.j3md";
      } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/WireColor.j3md")) {
        // Using WireColor, set wireframe renderstate = true and use Unshaded
        getAdditionalRenderState().setWireframe(true);
        defName = "Common/MatDefs/Misc/Unshaded.j3md";
      } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/Unshaded.j3md")) {
        // Uses unshaded, ensure that the proper param is set
        MatParam value = params.get("SeperateTexCoord");
        if (value != null && ((Boolean) value.getValue()) == true) {
          params.remove("SeperateTexCoord");
          separateTexCoord = true;
        }
      }
      assert applyDefaultValues && guessRenderStateApply;
    }

    def = (MaterialDef) im.getAssetManager().loadAsset(new AssetKey(defName));
    paramValues = new ListMap<String, MatParam>();

    // load the textures and update nextTexUnit
    for (Map.Entry<String, MatParam> entry : params.entrySet()) {
      MatParam param = entry.getValue();
      if (param instanceof MatParamTexture) {
        MatParamTexture texVal = (MatParamTexture) param;

        if (nextTexUnit < texVal.getUnit() + 1) {
          nextTexUnit = texVal.getUnit() + 1;
        }

        // the texture failed to load for this param
        // do not add to param values
        if (texVal.getTextureValue() == null || texVal.getTextureValue().getImage() == null) {
          continue;
        }
      }

      if (im.getFormatVersion() == 0 && param.getName().startsWith("m_")) {
        // Ancient version of jME3 ...
        param.setName(param.getName().substring(2));
      }

      checkSetParam(param.getVarType(), param.getName());
      paramValues.put(param.getName(), param);
    }

    if (applyDefaultValues) {
      // compatability with old versions where default vars were
      // not available
      for (MatParam param : def.getMaterialParams()) {
        if (param.getValue() != null && paramValues.get(param.getName()) == null) {
          setParam(param.getName(), param.getVarType(), param.getValue());
        }
      }
    }
    if (guessRenderStateApply && additionalState != null) {
      // Try to guess values of "apply" render state based on defaults
      // if value != default then set apply to true
      additionalState.applyPolyOffset = additionalState.offsetEnabled;
      additionalState.applyAlphaFallOff = additionalState.alphaTest;
      additionalState.applyAlphaTest = additionalState.alphaTest;
      additionalState.applyBlendMode = additionalState.blendMode != BlendMode.Off;
      additionalState.applyColorWrite = !additionalState.colorWrite;
      additionalState.applyCullMode = additionalState.cullMode != FaceCullMode.Back;
      additionalState.applyDepthTest = !additionalState.depthTest;
      additionalState.applyDepthWrite = !additionalState.depthWrite;
      additionalState.applyPointSprite = additionalState.pointSprite;
      additionalState.applyStencilTest = additionalState.stencilTest;
      additionalState.applyWireFrame = additionalState.wireframe;
    }
    if (enableVcolor) {
      setBoolean("VertexColor", true);
    }
    if (separateTexCoord) {
      setBoolean("SeparateTexCoord", true);
    }
  }