Search in sources :

Example 1 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class SkeletonControl method controlRenderHardware.

private void controlRenderHardware() {
    offsetMatrices = skeleton.computeSkinningMatrices();
    for (Material m : materials) {
        MatParam currentParam = m.getParam("BoneMatrices");
        if (currentParam != null) {
            if (currentParam.getValue() != offsetMatrices) {
                // Check to see if other SkeletonControl
                // is operating on this material, in that case, user
                // is sharing materials between models which is NOT allowed
                // when hardware skinning used.
                Logger.getLogger(SkeletonControl.class.getName()).log(Level.SEVERE, "Material instances cannot be shared when hardware skinning is used. " + "Ensure all models use unique material instances.");
            }
        }
        m.setParam("BoneMatrices", VarType.Matrix4Array, offsetMatrices);
    }
}
Also used : MatParam(com.jme3.material.MatParam) Material(com.jme3.material.Material)

Example 2 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class SkeletonControl method cloneFields.

@Override
public void cloneFields(Cloner cloner, Object original) {
    super.cloneFields(cloner, original);
    this.skeleton = cloner.clone(skeleton);
    // If the targets were cloned then this will clone them.  If the targets
    // were shared then this will share them.
    this.targets = cloner.clone(targets);
    // Not automatic set cloning yet
    Set<Material> newMaterials = new HashSet<Material>();
    for (Material m : this.materials) {
        Material mClone = cloner.clone(m);
        newMaterials.add(mClone);
        if (mClone != m) {
            // Material was really cloned so clear the bone matrices in case
            // this is hardware skinned.  This allows a local version to be
            // used and will be reset on the material.  Really this just avoids
            // the 'safety' check in controlRenderHardware().  Right now material
            // doesn't clone itself with the cloner (and doesn't clone its parameters)
            // else this would be unnecessary.
            MatParam boneMatrices = mClone.getParam("BoneMatrices");
            // parameter.
            if (boneMatrices != null) {
                mClone.clearParam("BoneMatrices");
            }
        }
    }
    this.materials = newMaterials;
}
Also used : MatParam(com.jme3.material.MatParam) Material(com.jme3.material.Material) HashSet(java.util.HashSet)

Example 3 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class MaterialHelper method getParticlesMaterial.

/**
     * This method converts the given material into particles-usable material.
     * The texture and glow color are being copied.
     * The method assumes it receives the Lighting type of material.
     * @param material
     *            the source material
     * @param blenderContext
     *            the blender context
     * @return material converted into particles-usable material
     */
public Material getParticlesMaterial(Material material, Integer alphaMaskIndex, BlenderContext blenderContext) {
    Material result = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Misc/Particle.j3md");
    // copying texture
    MatParam diffuseMap = material.getParam("DiffuseMap");
    if (diffuseMap != null) {
        Texture texture = ((Texture) diffuseMap.getValue()).clone();
        // applying alpha mask to the texture
        Image image = texture.getImage();
        ByteBuffer sourceBB = image.getData(0);
        sourceBB.rewind();
        int w = image.getWidth();
        int h = image.getHeight();
        ByteBuffer bb = BufferUtils.createByteBuffer(w * h * 4);
        IAlphaMask iAlphaMask = alphaMasks.get(alphaMaskIndex);
        iAlphaMask.setImageSize(w, h);
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                bb.put(sourceBB.get());
                bb.put(sourceBB.get());
                bb.put(sourceBB.get());
                bb.put(iAlphaMask.getAlpha(x, y));
            }
        }
        image = new Image(Format.RGBA8, w, h, bb, ColorSpace.Linear);
        texture.setImage(image);
        result.setTextureParam("Texture", VarType.Texture2D, texture);
    }
    // copying glow color
    MatParam glowColor = material.getParam("GlowColor");
    if (glowColor != null) {
        ColorRGBA color = (ColorRGBA) glowColor.getValue();
        result.setParam("GlowColor", VarType.Vector3, color);
    }
    return result;
}
Also used : MatParam(com.jme3.material.MatParam) ColorRGBA(com.jme3.math.ColorRGBA) Material(com.jme3.material.Material) Image(com.jme3.texture.Image) Texture(com.jme3.texture.Texture) ByteBuffer(java.nio.ByteBuffer)

Example 4 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class InstancedNode method addToInstancedGeometry.

private void addToInstancedGeometry(Geometry geom) {
    Material material = geom.getMaterial();
    MatParam param = material.getParam("UseInstancing");
    if (param == null || !((Boolean) param.getValue()).booleanValue()) {
        throw new IllegalStateException("You must set the 'UseInstancing' " + "parameter to true on the material prior " + "to adding it to InstancedNode");
    }
    InstancedGeometry ig = lookUpByGeometry(geom);
    igByGeom.put(geom, ig);
    geom.associateWithGroupNode(this, 0);
    ig.addInstance(geom);
}
Also used : MatParam(com.jme3.material.MatParam) Material(com.jme3.material.Material)

Example 5 with MatParam

use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.

the class Material method read.

public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);
    name = ic.readString("name", null);
    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;
            // 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));
        }
        if (def.getMaterialParam(param.getName()) == null) {
            logger.log(Level.WARNING, "The material parameter is not defined: {0}. Ignoring..", param.getName());
        } else {
            checkSetParam(param.getVarType(), param.getName());
            paramValues.put(param.getName(), param);
        }
    }
    if (applyDefaultValues) {
        // 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.applyBlendMode = additionalState.blendMode != BlendMode.Off;
        additionalState.applyColorWrite = !additionalState.colorWrite;
        additionalState.applyCullMode = additionalState.cullMode != FaceCullMode.Back;
        additionalState.applyDepthTest = !additionalState.depthTest;
        additionalState.applyDepthWrite = !additionalState.depthWrite;
        additionalState.applyStencilTest = additionalState.stencilTest;
        additionalState.applyWireFrame = additionalState.wireframe;
    }
    if (enableVcolor) {
        setBoolean("VertexColor", true);
    }
    if (separateTexCoord) {
        setBoolean("SeparateTexCoord", true);
    }
}
Also used : AssetKey(com.jme3.asset.AssetKey) ListMap(com.jme3.util.ListMap)

Aggregations

MatParam (com.jme3.material.MatParam)7 Material (com.jme3.material.Material)5 VarType (com.jme3.shader.VarType)3 ShaderNodeVariable (com.jme3.shader.ShaderNodeVariable)2 Uniform (com.jme3.shader.Uniform)2 Image (com.jme3.texture.Image)2 Texture (com.jme3.texture.Texture)2 AssetKey (com.jme3.asset.AssetKey)1 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 J3mdExporter (com.jme3.material.plugin.export.materialdef.J3mdExporter)1 J3MLoader (com.jme3.material.plugins.J3MLoader)1 ColorRGBA (com.jme3.math.ColorRGBA)1 RendererException (com.jme3.renderer.RendererException)1 Geometry (com.jme3.scene.Geometry)1 Box (com.jme3.scene.shape.Box)1 ShaderNode (com.jme3.shader.ShaderNode)1 UniformBinding (com.jme3.shader.UniformBinding)1 VariableMapping (com.jme3.shader.VariableMapping)1 ListMap (com.jme3.util.ListMap)1 IOException (java.io.IOException)1