com.jme3.shader.VarType Java Examples

The following examples show how to use com.jme3.shader.VarType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SkeletonControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule in = im.getCapsule(this);
    skeleton = (Skeleton) in.readSavable("skeleton", null);
    
    numberOfBonesParam = (MatParamOverride) in.readSavable("numberOfBonesParam", null);
    boneMatricesParam = (MatParamOverride) in.readSavable("boneMatricesParam", null);
    
    if (numberOfBonesParam == null) {
        numberOfBonesParam = new MatParamOverride(VarType.Int, "NumberOfBones", null);
        boneMatricesParam = new MatParamOverride(VarType.Matrix4Array, "BoneMatrices", null);
        getSpatial().addMatParamOverride(numberOfBonesParam);
        getSpatial().addMatParamOverride(boneMatricesParam);
    }
}
 
Example #2
Source File: SkinningControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule in = im.getCapsule(this);
    armature = (Armature) in.readSavable("armature", null);

    numberOfJointsParam = (MatParamOverride) in.readSavable("numberOfBonesParam", null);
    jointMatricesParam = (MatParamOverride) in.readSavable("boneMatricesParam", null);

    if (numberOfJointsParam == null) {
        numberOfJointsParam = new MatParamOverride(VarType.Int, "NumberOfBones", null);
        jointMatricesParam = new MatParamOverride(VarType.Matrix4Array, "BoneMatrices", null);
        getSpatial().addMatParamOverride(numberOfJointsParam);
        getSpatial().addMatParamOverride(jointMatricesParam);
    }
}
 
Example #3
Source File: Joint.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Access the attachments node of this joint. If this joint doesn't already
 * have an attachments node, create one. Models and effects attached to the
 * attachments node will follow this bone's motions.
 *
 * @param jointIndex this bone's index in its armature (≥0)
 * @param targets    a list of geometries animated by this bone's skeleton (not
 *                   null, unaffected)
 */
Node getAttachmentsNode(int jointIndex, SafeArrayList<Geometry> targets) {
    targetGeometry = null;
    /*
     * Search for a geometry animated by this particular bone.
     */
    for (Geometry geometry : targets) {
        Mesh mesh = geometry.getMesh();
        if (mesh != null && mesh.isAnimatedByJoint(jointIndex)) {
            targetGeometry = geometry;
            break;
        }
    }

    if (attachedNode == null) {
        attachedNode = new Node(name + "_attachnode");
        attachedNode.setUserData("AttachedBone", this);
        //We don't want the node to have a numBone set by a parent node so we force it to null
        attachedNode.addMatParamOverride(new MatParamOverride(VarType.Int, "NumberOfBones", null));
    }

    return attachedNode;
}
 
Example #4
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testRemoveOverride() {
    material("Common/MatDefs/Light/Lighting.j3md");

    reset();
    inputMp(mpoInt("NumberOfBones", 1234));
    outDefines(def("NUM_BONES", VarType.Int, 1234));
    outUniforms(uniform("NumberOfBones", VarType.Int, 1234));

    reset();
    inputMpo(mpoInt("NumberOfBones", 4321));
    outDefines(def("NUM_BONES", VarType.Int, 4321));
    outUniforms(uniform("NumberOfBones", VarType.Int, 4321));

    reset();
    geometry.clearMatParamOverrides();
    outDefines(def("NUM_BONES", VarType.Int, 1234));
    outUniforms(uniform("NumberOfBones", VarType.Int, 1234));
}
 
Example #5
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testRemoveTexture() {
    material("Common/MatDefs/Light/Lighting.j3md");
    Texture2D tex = new Texture2D(128, 128, Format.RGBA8);

    reset();
    inputMpo(mpoTexture2D("DiffuseMap", tex));
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex);

    reset();
    geometry.clearMatParamOverrides();
    root.updateGeometricState();
    outDefines();
    outUniforms();
    outTextures();
}
 
Example #6
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testRemoveTextureOverride() {
    material("Common/MatDefs/Light/Lighting.j3md");
    Texture2D tex1 = new Texture2D(128, 128, Format.RGBA8);
    Texture2D tex2 = new Texture2D(128, 128, Format.RGBA8);

    reset();
    inputMp(mpoTexture2D("DiffuseMap", tex1));
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex1));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex1);

    reset();
    inputMpo(mpoTexture2D("DiffuseMap", tex2));
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex2));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex2);

    reset();
    geometry.clearMatParamOverrides();
    root.updateGeometricState();
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex1));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex1);
}
 
Example #7
Source File: MaterialPropertyBuilder.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Convert material parameter type to editable property type.
 *
 * @param varType the material parameter type.
 * @return the editable property type or null.
 */
@FxThread
protected @Nullable EditablePropertyType convert(@NotNull VarType varType) {

    switch (varType) {
        case Boolean:
            return EditablePropertyType.BOOLEAN;
        case Float:
            return EditablePropertyType.FLOAT;
        case Int:
            return EditablePropertyType.INTEGER;
        case Vector4:
            return EditablePropertyType.COLOR;
        case Vector3:
            return EditablePropertyType.VECTOR_3F;
        case Vector2:
            return EditablePropertyType.VECTOR_2F;
        case Texture2D:
            return EditablePropertyType.TEXTURE_2D;
    }

    return null;
}
 
Example #8
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void outDefines(Define... expectedDefinesArray) {
    StringBuilder expectedDefineSource = new StringBuilder();
    for (Define define : expectedDefinesArray) {
        expectedDefineSource.append(define.toString());
    }
    
    if (!evaluated) {
        evaluateTechniqueDef();
    }

    Material mat = geometry.getMaterial();
    Technique tech = mat.getActiveTechnique();
    TechniqueDef def = tech.getDef();
    DefineList actualDefines = tech.getDynamicDefines();
    String[] defineNames = def.getDefineNames();
    VarType[] defineTypes = def.getDefineTypes();
    String actualDefineSource = actualDefines.generateSource(Arrays.asList(defineNames), Arrays.asList(defineTypes));

    assertEquals(expectedDefineSource.toString(), actualDefineSource);
}
 
Example #9
Source File: Bone.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Access the attachments node of this bone. If this bone doesn't already
 * have an attachments node, create one. Models and effects attached to the
 * attachments node will follow this bone's motions.
 *
 * @param boneIndex this bone's index in its skeleton (&ge;0)
 * @param targets a list of geometries animated by this bone's skeleton (not
 * null, unaffected)
 */
Node getAttachmentsNode(int boneIndex, SafeArrayList<Geometry> targets) {
    targetGeometry = null;
    /*
     * Search for a geometry animated by this particular bone.
     */
    for (Geometry geometry : targets) {
        Mesh mesh = geometry.getMesh();
        if (mesh != null && mesh.isAnimatedByBone(boneIndex)) {
            targetGeometry = geometry;
            break;
        }
    }

    if (attachNode == null) {
        attachNode = new Node(name + "_attachnode");
        attachNode.setUserData("AttachedBone", this);
        //We don't want the node to have a numBone set by a parent node so we force it to null
        attachNode.addMatParamOverride(new MatParamOverride(VarType.Int, "NumberOfBones", null));
    }

    return attachNode;
}
 
Example #10
Source File: MaterialTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testForcedColorSpace(){
   
    Image img=new Image(Format.RGBA8,2,2,BufferUtils.createByteBuffer(16),null,ColorSpace.sRGB);
    Image img2=new Image(Format.RGBA8,2,2,BufferUtils.createByteBuffer(16),null,ColorSpace.sRGB);
    Texture2D tx=new Texture2D(img);
    Texture2D tx2=new Texture2D(img2);

    assertTrue(tx2.getImage().getColorSpace()==ColorSpace.sRGB);
    assertTrue(tx2.getImage().getColorSpace()==ColorSpace.sRGB);

    AssetManager assetManager = TestUtil.createAssetManager();
    MaterialDef def=new MaterialDef(assetManager,"test");
    def.addMaterialParamTexture(VarType.Texture2D, "ColorMap",ColorSpace.Linear, null);
    Material mat=new Material(def);
    
    mat.setTexture("ColorMap",tx);          
    assertTrue(tx.getImage().getColorSpace()==ColorSpace.Linear);
    
    mat.setTexture("ColorMap",tx2);  
    assertTrue(tx2.getImage().getColorSpace()==ColorSpace.Linear);       

}
 
Example #11
Source File: Material.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private String checkSetParam(VarType type, String name) {
    MatParam paramDef = def.getMaterialParam(name);
    String newName = name;

    if (paramDef == null && name.startsWith("m_")) {
        newName = name.substring(2);
        paramDef = def.getMaterialParam(newName);
        if (paramDef == null) {
            throw new IllegalArgumentException("Material parameter is not defined: " + name);
        } else {
            logger.log(Level.WARNING, "Material parameter {0} uses a deprecated naming convention use {1} instead ", new Object[]{name, newName});
        }
    } else if (paramDef == null) {
        throw new IllegalArgumentException("Material parameter is not defined: " + name);
    }

    if (type != null && paramDef.getVarType() != type) {
        logger.log(Level.WARNING, "Material parameter being set: {0} with "
                + "type {1} doesn''t match definition types {2}", new Object[]{name, type.name(), paramDef.getVarType()} );
    }

    return newName;
}
 
Example #12
Source File: Technique.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void updateUniformParam(int paramIndex, VarType type, Object value, boolean ifNotOwner) {
        Uniform u = uniformArray[paramIndex];
        if (u == null) {
            u = shader.getUniform(paramValues.getValue(paramIndex).getPrefixedName());
            uniformArray[paramIndex] = u;
        }

//        if (ifNotOwner && u.getLastChanger() == owner)
//            return;

        switch (type) {
            case Texture2D: // fall intentional
            case Texture3D:
            case TextureArray:
            case TextureCubeMap:
            case Int:
                u.setValue(VarType.Int, value);
                break;
            default:
                u.setValue(type, value);
                break;
        }
//        u.setLastChanger(owner);
    }
 
Example #13
Source File: MatParam.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);
    type = ic.readEnum("varType", VarType.class, null);
    name = ic.readString("name", null);
    ffBinding = ic.readEnum("ff_binding", FixedFuncBinding.class, null);
    switch (getVarType()) {
        case Boolean:
            value = ic.readBoolean("value_bool", false);
            break;
        case Float:
            value = ic.readFloat("value_float", 0f);
            break;
        case Int:
            value = ic.readInt("value_int", 0);
            break;
        default:
            value = ic.readSavable("value_savable", null);
            break;
    }
}
 
Example #14
Source File: Material.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void setParam(int paramIndex, VarType type, Object value) {
        MatParam val = paramValues.getValue(paramIndex);
//        if (technique != null) {
//            technique.notifySetParam(name, type, value);
//        }

        val.setValue(value);
        Object value2 = val.multiData != null ? val.multiData : value;
        if (techniqueArray == null) {
            setTechniqueArray();
        }
        paramValues.getValue(0);
        for(Technique tech : techniqueArray) {
            tech.notifySetParam(paramIndex, type, value2);
        }
    }
 
Example #15
Source File: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Refresh textures in a material.
 *
 * @param material   the material.
 * @param textureKey the texture key.
 */
@JmeThread
private static void refreshTextures(@NotNull Material material, @NotNull String textureKey) {

    var assetManager = EditorUtil.getAssetManager();

    material.getParams().forEach(matParam -> {

        var varType = matParam.getVarType();
        var value = matParam.getValue();

        if (varType != VarType.Texture2D || value == null) {
            return;
        }

        var texture = (Texture) value;
        var key = (TextureKey) texture.getKey();

        if (key != null && StringUtils.equals(key.getName(), textureKey)) {
            var newTexture = assetManager.loadAsset(key);
            matParam.setValue(newTexture);
        }
    });

}
 
Example #16
Source File: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Check a material on containing a texture.
 *
 * @param material  the material.
 * @param assetPath the path of the texture.
 * @return true if the material definition contains the texture.
 */
@FromAnyThread
private static boolean containsTexture(@NotNull Material material, @NotNull String assetPath) {

    var materialParams = material.getParams();

    for (var materialParam : materialParams) {

        if (materialParam.getVarType() != VarType.Texture2D) {
            continue;
        }

        var value = (Texture) materialParam.getValue();
        var textureKey = value == null ? null : (TextureKey) value.getKey();
        if (textureKey != null && StringUtils.equals(textureKey.getName(), assetPath)) {
            return true;
        }
    }

    return false;
}
 
Example #17
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Define def(String name, VarType type, Object value) {
    Define d = new Define();
    d.name = name;
    d.type = type;
    d.value = value;
    return d;
}
 
Example #18
Source File: Material.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Pass a texture to the material shader.
 *
 * @param name the name of the texture defined in the material definition
 * (j3md) (for example Texture for Lighting.j3md)
 * @param value the Texture object previously loaded by the asset manager
 */
public void setTexture(String name, Texture value) {
    if (value == null) {
        // clear it
        clearTextureParam(name);
        return;
    }

    VarType paramType = null;
    switch (value.getType()) {
        case TwoDimensional:
            paramType = VarType.Texture2D;
            break;
        case TwoDimensionalArray:
            paramType = VarType.TextureArray;
            break;
        case ThreeDimensional:
            paramType = VarType.Texture3D;
            break;
        case CubeMap:
            paramType = VarType.TextureCubeMap;
            break;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + value.getType());
    }

    setTextureParam(name, paramType, value);
}
 
Example #19
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testFloatOverride() {
    material("Common/MatDefs/Light/Lighting.j3md");
    inputMp(mpoFloat("AlphaDiscardThreshold", 3.12f));
    inputMpo(mpoFloat("AlphaDiscardThreshold", 2.79f));
    outDefines(def("DISCARD_ALPHA", VarType.Float, 2.79f));
    outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 2.79f));
}
 
Example #20
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testTextureOverride() {
    material("Common/MatDefs/Light/Lighting.j3md");
    Texture2D tex1 = new Texture2D(128, 128, Format.RGBA8);
    Texture2D tex2 = new Texture2D(128, 128, Format.RGBA8);

    inputMp(mpoTexture2D("DiffuseMap", tex1));
    inputMpo(mpoTexture2D("DiffuseMap", tex2));

    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex2));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex2);
}
 
Example #21
Source File: J3MLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void readDefine(String statement) throws IOException{
    String[] split = statement.split(":");
    if (split.length == 1){
        // add preset define
        technique.addShaderPresetDefine(split[0].trim(), VarType.Boolean, true);
    }else if (split.length == 2){
        technique.addShaderParamDefine(split[1].trim(), split[0].trim());
    }else{
        throw new IOException("Define syntax incorrect");
    }
}
 
Example #22
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testTextureMpoOnly() {
    material("Common/MatDefs/Light/Lighting.j3md");
    Texture2D tex = new Texture2D(128, 128, Format.RGBA8);

    inputMpo(mpoTexture2D("DiffuseMap", tex));
    outDefines(def("DIFFUSEMAP", VarType.Texture2D, tex));
    outUniforms(uniform("DiffuseMap", VarType.Int, 0));
    outTextures(tex);
}
 
Example #23
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testIntMpOnly() {
    material("Common/MatDefs/Light/Lighting.j3md");
    inputMp(mpoInt("NumberOfBones", 1234));
    outDefines(def("NUM_BONES", VarType.Int, 1234));
    outUniforms(uniform("NumberOfBones", VarType.Int, 1234));
}
 
Example #24
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testIntMpoOnly() {
    material("Common/MatDefs/Light/Lighting.j3md");
    inputMpo(mpoInt("NumberOfBones", 1234));
    outDefines(def("NUM_BONES", VarType.Int, 1234));
    outUniforms(uniform("NumberOfBones", VarType.Int, 1234));
}
 
Example #25
Source File: RadialBlurFilter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected Material getMaterial() {
    material.setFloat("SampleDist", sampleDist);
    material.setFloat("SampleStrength", sampleStrength);
    material.setParam("Samples", VarType.FloatArray, samples);
    return material;
}
 
Example #26
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testChildOverridesParent() {
    material("Common/MatDefs/Light/Lighting.j3md");

    inputParentMpo(mpoFloat("AlphaDiscardThreshold", 3.12f));
    inputMpo(mpoFloat("AlphaDiscardThreshold", 2.79f));

    outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 2.79f));
    outDefines(def("DISCARD_ALPHA", VarType.Float, 2.79f));
}
 
Example #27
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testForcedOverride() {
    material("Common/MatDefs/Light/Lighting.j3md");
    inputMp(mpoFloat("AlphaDiscardThreshold", 3.12f));
    inputMpo(mpoFloat("AlphaDiscardThreshold", 2.79f));
    inputFpo(mpoFloat("AlphaDiscardThreshold", 1.23f));
    outDefines(def("DISCARD_ALPHA", VarType.Float, 1.23f));
    outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 1.23f));

    reset();
    root.clearMatParamOverrides();
    root.updateGeometricState();
    outDefines(def("DISCARD_ALPHA", VarType.Float, 2.79f));
    outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 2.79f));
}
 
Example #28
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testFloatMpZero() {
    material("Common/MatDefs/Light/Lighting.j3md");
    inputMp(mpoFloat("AlphaDiscardThreshold", 0.0f));
    outDefines(def("DISCARD_ALPHA", VarType.Float, 0.0f));
    outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 0.0f));
}
 
Example #29
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testFloatMpOnly() {
    material("Common/MatDefs/Light/Lighting.j3md");
    inputMp(mpoFloat("AlphaDiscardThreshold", 3.12f));
    outDefines(def("DISCARD_ALPHA", VarType.Float, 3.12f));
    outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 3.12f));
}
 
Example #30
Source File: MaterialMatParamTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testFloatMpoOnly() {
    material("Common/MatDefs/Light/Lighting.j3md");
    inputMpo(mpoFloat("AlphaDiscardThreshold", 3.12f));
    outDefines(def("DISCARD_ALPHA", VarType.Float, 3.12f));
    outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 3.12f));
}