com.jme3.material.MaterialDef Java Examples

The following examples show how to use com.jme3.material.MaterialDef. 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: MaterialFileEditor.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Reload the material.
 */
@FxThread
private void reload(@NotNull final Material material) {
    setCurrentMaterial(material);
    setIgnoreListeners(true);
    try {

        final MaterialEditor3DPart editor3DState = getEditor3DPart();
        editor3DState.updateMaterial(material);

        getSettingsTree().fill(new RootMaterialSettings(material));

        final ComboBox<String> materialDefinitionBox = getMaterialDefinitionBox();
        final ObservableList<String> items = materialDefinitionBox.getItems();
        items.clear();
        items.addAll(RESOURCE_MANAGER.getAvailableResources(FileExtensions.JME_MATERIAL_DEFINITION));

        final MaterialDef materialDef = material.getMaterialDef();
        materialDefinitionBox.getSelectionModel().select(materialDef.getAssetName());

    } finally {
        setIgnoreListeners(false);
    }
}
 
Example #2
Source File: ShaderCheck.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void checkMatDef(String matdefName) {
    MaterialDef def = (MaterialDef) assetManager.loadAsset(matdefName);
    EnumSet<Caps> rendererCaps = EnumSet.noneOf(Caps.class);
    rendererCaps.add(Caps.GLSL100);
    for (TechniqueDef techDef : def.getTechniqueDefs(TechniqueDef.DEFAULT_TECHNIQUE_NAME)) {
        DefineList defines = techDef.createDefineList();
        Shader shader = techDef.getShader(assetManager, rendererCaps, defines);
        for (Validator validator : validators) {
            StringBuilder sb = new StringBuilder();
            validator.validate(shader, sb);
            System.out.println("==== Validator: " + validator.getName() + " "
                    + validator.getInstalledVersion() + " ====");
            System.out.println(sb.toString());
        }
    }
    throw new UnsupportedOperationException();
}
 
Example #3
Source File: MaterialUtils.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Check a material definition on containing a shader.
 *
 * @param materialDef the material definition.
 * @param assetPath   the path of the shader.
 * @return true if the material definition contains the shader.
 */
@FromAnyThread
private static boolean containsShader(@NotNull MaterialDef materialDef, @NotNull String assetPath) {

    var defaultTechniques = materialDef.getTechniqueDefs("Default");

    for (var technique : defaultTechniques) {
        var shaderProgramNames = technique.getShaderProgramNames();
        if (shaderProgramNames.containsValue(assetPath)) {
            return true;
        }
    }

    return false;
}
 
Example #4
Source File: J3MExporter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public OutputCapsule getCapsule(Savable object) {
    if ((object instanceof Material) || (object instanceof MaterialDef)) {
        return rootCapsule;
    }

    return rootCapsule.getCapsule(object);
}
 
Example #5
Source File: ShaderCheck.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args){
    Logger.getLogger(MaterialDef.class.getName()).setLevel(Level.OFF);
    initAssetManager();
    checkMatDef("Common/MatDefs/Blur/HGaussianBlur.j3md");
    checkMatDef("Common/MatDefs/Blur/RadialBlur.j3md");
    checkMatDef("Common/MatDefs/Blur/VGaussianBlur.j3md");
    checkMatDef("Common/MatDefs/Gui/Gui.j3md");
    checkMatDef("Common/MatDefs/Hdr/LogLum.j3md");
    checkMatDef("Common/MatDefs/Hdr/ToneMap.j3md");
    checkMatDef("Common/MatDefs/Light/Lighting.j3md");
    checkMatDef("Common/MatDefs/Misc/ColoredTextured.j3md");
    checkMatDef("Common/MatDefs/Misc/Particle.j3md");
    checkMatDef("Common/MatDefs/Misc/ShowNormals.j3md");
    checkMatDef("Common/MatDefs/Misc/Sky.j3md");
    checkMatDef("Common/MatDefs/Misc/Unshaded.j3md");
    
    checkMatDef("Common/MatDefs/Post/BloomExtract.j3md");
    checkMatDef("Common/MatDefs/Post/BloomFinal.j3md");
    checkMatDef("Common/MatDefs/Post/CartoonEdge.j3md");
    checkMatDef("Common/MatDefs/Post/CrossHatch.j3md");
    checkMatDef("Common/MatDefs/Post/DepthOfField.j3md");
    checkMatDef("Common/MatDefs/Post/FXAA.j3md");
    checkMatDef("Common/MatDefs/Post/Fade.j3md");
    checkMatDef("Common/MatDefs/Post/Fog.j3md");
    checkMatDef("Common/MatDefs/Post/GammaCorrection.j3md");
    checkMatDef("Common/MatDefs/Post/LightScattering.j3md");
    checkMatDef("Common/MatDefs/Post/Overlay.j3md");
    checkMatDef("Common/MatDefs/Post/Posterization.j3md");
    
    checkMatDef("Common/MatDefs/SSAO/ssao.j3md");
    checkMatDef("Common/MatDefs/SSAO/ssaoBlur.j3md");
    checkMatDef("Common/MatDefs/Shadow/PostShadow.j3md");
    checkMatDef("Common/MatDefs/Shadow/PostShadowPSSM.j3md");
    checkMatDef("Common/MatDefs/Shadow/PreShadow.j3md");
    
    checkMatDef("Common/MatDefs/Water/SimpleWater.j3md");
    checkMatDef("Common/MatDefs/Water/Water.j3md");
}
 
Example #6
Source File: J3MLoaderTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void fixedPipelineTechnique_shouldBeIgnored() throws IOException {
    when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/no-shader-specified.j3md"));
    MaterialDef def = (MaterialDef) j3MLoader.load(assetInfo);
    assertEquals(null, def.getTechniqueDefs("A"));
    assertEquals(1, def.getTechniqueDefs("B").size());
}
 
Example #7
Source File: J3MLoaderTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void multipleSameNamedTechniques_shouldBeSupported() throws IOException {
    when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/same-name-technique.j3md"));
    MaterialDef def = (MaterialDef) j3MLoader.load(assetInfo);
    assertEquals(2, def.getTechniqueDefs("Test").size());
    assertEquals(EnumSet.of(Caps.GLSL150), def.getTechniqueDefs("Test").get(0).getRequiredCaps());
    assertEquals(EnumSet.of(Caps.GLSL100), def.getTechniqueDefs("Test").get(1).getRequiredCaps());
}
 
Example #8
Source File: MaterialSettingsPropertyBuilder.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
@Override
@FxThread
protected @Nullable List<EditableProperty<?, ?>> getProperties(@NotNull final Object object) {

    if (!(object instanceof MaterialSettings) || object instanceof RootMaterialSettings) {
        return null;
    }

    final MaterialSettings settings = (MaterialSettings) object;
    final Material material = settings.getMaterial();

    if(object instanceof RenderSettings) {

        final RenderState renderState = material.getAdditionalRenderState();

        final List<EditableProperty<?, ?>> result = new ArrayList<>();
        result.add(new SimpleProperty<>(BOOLEAN, Messages.MATERIAL_RENDER_STATE_COLOR_WRITE, settings,
                sett -> renderState.isColorWrite(),
                (sett, property) -> renderState.setColorWrite(property)));
        result.add(new SimpleProperty<>(BOOLEAN, Messages.MATERIAL_RENDER_STATE_DEPTH_WRITE, settings,
                sett -> renderState.isDepthWrite(),
                (sett, property) -> renderState.setDepthWrite(property)));
        result.add(new SimpleProperty<>(BOOLEAN, Messages.MATERIAL_RENDER_STATE_DEPTH_TEST, settings,
                sett -> renderState.isDepthTest(),
                (sett, property) -> renderState.setDepthTest(property)));
        result.add(new SimpleProperty<>(BOOLEAN, Messages.MATERIAL_RENDER_STATE_WIREFRAME, settings,
                sett -> renderState.isWireframe(),
                (sett, property) -> renderState.setWireframe(property)));
        result.add(new SimpleProperty<>(ENUM, Messages.MATERIAL_RENDER_STATE_FACE_CULL_MODE, settings,
                sett -> renderState.getFaceCullMode(),
                (sett, property) -> renderState.setFaceCullMode(property)));
        result.add(new SimpleProperty<>(ENUM, Messages.MATERIAL_RENDER_STATE_BLEND_MODE, settings,
                sett -> renderState.getBlendMode(),
                (sett, property) -> renderState.setBlendMode(property)));
        result.add(new SimpleProperty<>(ENUM, Messages.MATERIAL_RENDER_STATE_BLEND_EQUATION, settings,
                sett -> renderState.getBlendEquation(),
                (sett, property) -> renderState.setBlendEquation(property)));
        result.add(new SimpleProperty<>(ENUM, Messages.MATERIAL_RENDER_STATE_BLEND_EQUATION_ALPHA, settings,
                sett -> renderState.getBlendEquationAlpha(),
                (sett, property) -> renderState.setBlendEquationAlpha(property)));
        result.add(new SimpleProperty<>(FLOAT,  Messages.MATERIAL_RENDER_STATE_POLY_OFFSET_FACTOR, 0.1F, settings,
                sett -> renderState.getPolyOffsetFactor(),
                (sett, property) -> renderState.setPolyOffset(property, renderState.getPolyOffsetUnits())));
        result.add(new SimpleProperty<>(FLOAT,  Messages.MATERIAL_RENDER_STATE_POLY_OFFSET_UNITS, 0.1F, settings,
                sett -> renderState.getPolyOffsetUnits(),
                (sett, property) -> renderState.setPolyOffset(renderState.getPolyOffsetFactor(), property)));

        return result;
    }

    final MaterialDef materialDef = material.getMaterialDef();

    return materialDef.getMaterialParams()
            .stream()
            .filter(param -> filter(param, object))
            .sorted(MAT_PARAM_NAME_COMPARATOR)
            .map(param -> convert(param, material, settings))
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
}
 
Example #9
Source File: MaterialFileEditor.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Try to apply dropped texture.
 *
 * @param editor    the editor.
 * @param dragEvent the drag event.
 * @param path      the path to the texture.
 */
private void applyTexture(@NotNull final MaterialFileEditor editor, @NotNull final DragEvent dragEvent,
                          @NotNull final Path path) {

    final String textureName = path.getFileName().toString();
    final int textureType = MaterialUtils.getPossibleTextureType(textureName);

    if (textureType == 0) {
        return;
    }

    final String[] paramNames = MaterialUtils.getPossibleParamNames(textureType);
    final Material currentMaterial = getCurrentMaterial();
    final MaterialDef materialDef = currentMaterial.getMaterialDef();

    final Optional<MatParam> param = Arrays.stream(paramNames)
            .map(materialDef::getMaterialParam)
            .filter(Objects::nonNull)
            .filter(p -> p.getVarType() == VarType.Texture2D)
            .findAny();

    if (!param.isPresent()) {
        return;
    }

    final MatParam matParam = param.get();

    EXECUTOR_MANAGER.addJmeTask(() -> {

        final EditorConfig config = EditorConfig.getInstance();
        final Path assetFile = notNull(getAssetFile(path));
        final TextureKey textureKey = new TextureKey(toAssetPath(assetFile));
        textureKey.setFlipY(config.getBoolean(PREF_FLIPPED_TEXTURES, PREF_DEFAULT_FLIPPED_TEXTURES));

        final AssetManager assetManager = EditorUtil.getAssetManager();
        final Texture texture = assetManager.loadTexture(textureKey);
        texture.setWrap(Texture.WrapMode.Repeat);

        final String paramName = matParam.getName();
        final MatParamTexture textureParam = currentMaterial.getTextureParam(paramName);
        final Texture currentTexture = textureParam == null? null : textureParam.getTextureValue();

        PropertyOperation<ChangeConsumer, Material, Texture> operation =
                new PropertyOperation<>(currentMaterial, paramName, texture, currentTexture);
        operation.setApplyHandler((material, newTexture) -> material.setTexture(paramName, newTexture));

        execute(operation);
    });
}
 
Example #10
Source File: RenderManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Renders the given geometry.
 * <p>
 * First the proper world matrix is set, if 
 * the geometry's {@link Geometry#setIgnoreTransform(boolean) ignore transform}
 * feature is enabled, the identity world matrix is used, otherwise, the 
 * geometry's {@link Geometry#getWorldMatrix() world transform matrix} is used. 
 * <p>
 * Once the world matrix is applied, the proper material is chosen for rendering.
 * If a {@link #setForcedMaterial(com.jme3.material.Material) forced material} is
 * set on this RenderManager, then it is used for rendering the geometry,
 * otherwise, the {@link Geometry#getMaterial() geometry's material} is used.
 * <p>
 * If a {@link #setForcedTechnique(java.lang.String) forced technique} is
 * set on this RenderManager, then it is selected automatically
 * on the geometry's material and is used for rendering. Otherwise, one
 * of the {@link com.jme3.material.MaterialDef#getTechniqueDefsNames() default techniques} is
 * used.
 * <p>
 * If a {@link #setForcedRenderState(com.jme3.material.RenderState) forced
 * render state} is set on this RenderManager, then it is used
 * for rendering the material, and the material's own render state is ignored.
 * Otherwise, the material's render state is used as intended.
 * 
 * @param geom The geometry to render
   * 
 * @see Technique
 * @see RenderState
 * @see com.jme3.material.Material#selectTechnique(java.lang.String, com.jme3.renderer.RenderManager) 
 * @see com.jme3.material.Material#render(com.jme3.scene.Geometry, com.jme3.renderer.RenderManager) 
 */
public void renderGeometry(Geometry geom) {
    if (geom.isIgnoreTransform()) {
        setWorldMatrix(Matrix4f.IDENTITY);
    } else {
        setWorldMatrix(geom.getWorldMatrix());
    }
    
    // Perform light filtering if we have a light filter.
    LightList lightList = geom.getWorldLightList();
    
    if (lightFilter != null) {
        filteredLightList.clear();
        lightFilter.filterLights(geom, filteredLightList);
        lightList = filteredLightList;
    }

    Material material = geom.getMaterial();

    //if forcedTechnique we try to force it for render,
    //if it does not exists in the mat def, we check for forcedMaterial and render the geom if not null
    //else the geom is not rendered
    if (forcedTechnique != null) {
        MaterialDef matDef = material.getMaterialDef();
        if (matDef.getTechniqueDefs(forcedTechnique) != null) {

            Technique activeTechnique = material.getActiveTechnique();

            String previousTechniqueName = activeTechnique != null
                    ? activeTechnique.getDef().getName()
                    : TechniqueDef.DEFAULT_TECHNIQUE_NAME;

            geom.getMaterial().selectTechnique(forcedTechnique, this);
            //saving forcedRenderState for future calls
            RenderState tmpRs = forcedRenderState;
            if (geom.getMaterial().getActiveTechnique().getDef().getForcedRenderState() != null) {
                //forcing forced technique renderState
                forcedRenderState = geom.getMaterial().getActiveTechnique().getDef().getForcedRenderState();
            }
            // use geometry's material
            material.render(geom, lightList, this);
            material.selectTechnique(previousTechniqueName, this);

            //restoring forcedRenderState
            forcedRenderState = tmpRs;

            //Reverted this part from revision 6197
            //If forcedTechnique does not exists, and forcedMaterial is not set, the geom MUST NOT be rendered
        } else if (forcedMaterial != null) {
            // use forced material
            forcedMaterial.render(geom, lightList, this);
        }
    } else if (forcedMaterial != null) {
        // use forced material
        forcedMaterial.render(geom, lightList, this);
    } else {
        material.render(geom, lightList, this);
    }
}
 
Example #11
Source File: J3MLoaderTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void noDefaultTechnique_shouldBeSupported() throws IOException {
    when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/no-default-technique.j3md"));
    MaterialDef def = (MaterialDef) j3MLoader.load(assetInfo);
    assertEquals(1, def.getTechniqueDefs("Test").size());
}
 
Example #12
Source File: ShaderNodeLoaderDelegate.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * sets the material def currently being loaded
 *
 * @param materialDef
 */
public void setMaterialDef(MaterialDef materialDef) {
    this.materialDef = materialDef;
}