Пример #1
0
  /**
   * Preloads this material for the given render manager.
   *
   * <p>Preloading the material can ensure that when the material is first used for rendering, there
   * won't be any delay since the material has been already been setup for rendering.
   *
   * @param rm The render manager to preload for
   */
  public void preload(RenderManager rm) {
    autoSelectTechnique(rm);

    Renderer r = rm.getRenderer();
    TechniqueDef techDef = technique.getDef();

    Collection<MatParam> params = paramValues.values();
    for (MatParam param : params) {
      if (param instanceof MatParamTexture) {
        MatParamTexture texParam = (MatParamTexture) param;
        r.setTexture(0, texParam.getTextureValue());
      } else {
        if (!techDef.isUsingShaders()) {
          continue;
        }

        technique.updateUniformParam(param.getName(), param.getVarType(), param.getValue());
      }
    }

    Shader shader = technique.getShader();
    if (techDef.isUsingShaders()) {
      r.setShader(shader);
    }
  }
Пример #2
0
  /**
   * Clear a parameter from this material. The parameter must exist
   *
   * @param name the name of the parameter to clear
   */
  public void clearParam(String name) {
    checkSetParam(null, name);
    MatParam matParam = getParam(name);
    if (matParam == null) {
      return;
    }

    paramValues.remove(name);
    if (matParam instanceof MatParamTexture) {
      int texUnit = ((MatParamTexture) matParam).getUnit();
      nextTexUnit--;
      for (MatParam param : paramValues.values()) {
        if (param instanceof MatParamTexture) {
          MatParamTexture texParam = (MatParamTexture) param;
          if (texParam.getUnit() > texUnit) {
            texParam.setUnit(texParam.getUnit() - 1);
          }
        }
      }
      sortingId = -1;
    }
    if (technique != null) {
      technique.notifyParamChanged(name, null, null);
    }
  }
Пример #3
0
  /**
   * Set a texture parameter.
   *
   * @param name The name of the parameter
   * @param type The variable type {@link VarType}
   * @param value The texture value of the parameter.
   * @throws IllegalArgumentException is value is null
   */
  public void setTextureParam(String name, VarType type, Texture value) {
    if (value == null) {
      throw new IllegalArgumentException();
    }

    checkSetParam(type, name);
    MatParamTexture val = getTextureParam(name);
    if (val == null) {
      paramValues.put(name, new MatParamTexture(type, name, value, nextTexUnit++));
    } else {
      val.setTextureValue(value);
    }

    if (technique != null) {
      technique.notifyParamChanged(name, type, nextTexUnit - 1);
    }

    // need to recompute sort ID
    sortingId = -1;
  }
Пример #4
0
 /**
  * Returns the sorting ID or sorting index for this material.
  *
  * <p>The sorting ID is used internally by the system to sort rendering of geometries. It sorted
  * to reduce shader switches, if the shaders are equal, then it is sorted by textures.
  *
  * @return The sorting ID used for sorting geometries for rendering.
  */
 public int getSortId() {
   Technique t = getActiveTechnique();
   if (sortingId == -1 && t != null && t.getShader() != null) {
     int texId = -1;
     for (int i = 0; i < paramValues.size(); i++) {
       MatParam param = paramValues.getValue(i);
       if (param instanceof MatParamTexture) {
         MatParamTexture tex = (MatParamTexture) param;
         if (tex.getTextureValue() != null && tex.getTextureValue().getImage() != null) {
           if (texId == -1) {
             texId = 0;
           }
           texId += tex.getTextureValue().getImage().getId() % 0xff;
         }
       }
     }
     sortingId = texId + t.getShader().getId() * 1000;
   }
   return sortingId;
 }
Пример #5
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);
    }
  }