Search in sources :

Example 66 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readInputMapping.

/**
     * reads an input mapping
     *
     * @param statement1 the statement being read
     * @return the mapping
     * @throws IOException
     */
public VariableMapping readInputMapping(Statement statement1) throws IOException {
    VariableMapping mapping = null;
    try {
        mapping = parseMapping(statement1, new boolean[] { false, true });
    } catch (Exception e) {
        throw new MatParseException("Unexpected mapping format", statement1, e);
    }
    ShaderNodeVariable left = mapping.getLeftVariable();
    ShaderNodeVariable right = mapping.getRightVariable();
    if (!updateVariableFromList(left, shaderNode.getDefinition().getInputs())) {
        throw new MatParseException(left.getName() + " is not an input variable of " + shaderNode.getDefinition().getName(), statement1);
    }
    if (left.getType().startsWith("sampler") && !right.getNameSpace().equals("MatParam")) {
        throw new MatParseException("Samplers can only be assigned to MatParams", statement1);
    }
    if (right.getNameSpace().equals("Global")) {
        //Globals are all vec4 for now (maybe forever...)
        right.setType("vec4");
        //        updateCondition(right, mapping);
        storeGlobal(right, statement1);
    } else if (right.getNameSpace().equals("Attr")) {
        if (shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) {
            throw new MatParseException("Cannot have an attribute as input in a fragment shader" + right.getName(), statement1);
        }
        updateVarFromAttributes(mapping.getRightVariable(), mapping);
        //          updateCondition(mapping.getRightVariable(), mapping);
        storeAttribute(mapping.getRightVariable());
    } else if (right.getNameSpace().equals("MatParam")) {
        MatParam param = findMatParam(right.getName());
        if (param == null) {
            throw new MatParseException("Could not find a Material Parameter named " + right.getName(), statement1);
        }
        if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
            if (updateRightFromUniforms(param, mapping, vertexDeclaredUniforms, statement1)) {
                storeVertexUniform(mapping.getRightVariable());
            }
        } else {
            if (updateRightFromUniforms(param, mapping, fragmentDeclaredUniforms, statement1)) {
                if (mapping.getRightVariable().getType().contains("|")) {
                    String type = fixSamplerType(left.getType(), mapping.getRightVariable().getType());
                    if (type != null) {
                        mapping.getRightVariable().setType(type);
                    } else {
                        throw new MatParseException(param.getVarType().toString() + " can only be matched to one of " + param.getVarType().getGlslType().replaceAll("\\|", ",") + " found " + left.getType(), statement1);
                    }
                }
                storeFragmentUniform(mapping.getRightVariable());
            }
        }
    } else if (right.getNameSpace().equals("WorldParam")) {
        UniformBinding worldParam = findWorldParam(right.getName());
        if (worldParam == null) {
            throw new MatParseException("Could not find a World Parameter named " + right.getName(), statement1);
        }
        if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
            if (updateRightFromUniforms(worldParam, mapping, vertexDeclaredUniforms)) {
                storeVertexUniform(mapping.getRightVariable());
            }
        } else {
            if (updateRightFromUniforms(worldParam, mapping, fragmentDeclaredUniforms)) {
                storeFragmentUniform(mapping.getRightVariable());
            }
        }
    } else {
        ShaderNode node = nodes.get(right.getNameSpace());
        if (node == null) {
            throw new MatParseException("Undeclared node" + right.getNameSpace() + ". Make sure this node is declared before the current node", statement1);
        }
        ShaderNodeVariable var = findNodeOutput(node.getDefinition().getOutputs(), right.getName());
        if (var == null) {
            throw new MatParseException("Cannot find output variable" + right.getName() + " form ShaderNode " + node.getName(), statement1);
        }
        right.setNameSpace(node.getName());
        right.setType(var.getType());
        right.setMultiplicity(var.getMultiplicity());
        mapping.setRightVariable(right);
        storeVaryings(node, mapping.getRightVariable());
    }
    checkTypes(mapping, statement1);
    return mapping;
}
Also used : UniformBinding(com.jme3.shader.UniformBinding) MatParam(com.jme3.material.MatParam) ShaderNode(com.jme3.shader.ShaderNode) VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable) IOException(java.io.IOException) AssetNotFoundException(com.jme3.asset.AssetNotFoundException)

Example 67 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class SaveGame method loadGame.

/**
     * Loads a savable that has been saved on this system with saveGame() before.
     * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
     * @param dataName A unique name for this savegame, e.g. "save_001"
     * @param manager Link to an AssetManager if required for loading the data (e.g. models with textures)
     * @param storageType The specific type of folder to use to save the data
     * @return The savable that was saved or null if none was found
     */
public static Savable loadGame(String gamePath, String dataName, AssetManager manager, JmeSystem.StorageFolderType storageType) {
    if (storageType == null) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Base Storage Folder Type is null, using External!");
        storageType = JmeSystem.StorageFolderType.External;
    }
    InputStream is = null;
    Savable sav = null;
    try {
        File baseFolder = JmeSystem.getStorageFolder(storageType);
        if (baseFolder == null) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error reading base storage folder!");
            return null;
        }
        File file = new File(baseFolder.getAbsolutePath() + File.separator + gamePath.replace('/', File.separatorChar) + File.separator + dataName);
        if (!file.exists()) {
            return null;
        }
        is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
        BinaryImporter imp = BinaryImporter.getInstance();
        if (manager != null) {
            imp.setAssetManager(manager);
        }
        sav = imp.load(is);
        Logger.getLogger(SaveGame.class.getName()).log(Level.FINE, "Loading data from: {0}", file.getAbsolutePath());
    } catch (IOException ex) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
        ex.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
                ex.printStackTrace();
            }
        }
    }
    return sav;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BinaryImporter(com.jme3.export.binary.BinaryImporter) Savable(com.jme3.export.Savable) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 68 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class SaveGame method saveGame.

/**
     * Saves a savable in a system-dependent way.
     * @param gamePath A unique path for this game, e.g. com/mycompany/mygame
     * @param dataName A unique name for this savegame, e.g. "save_001"
     * @param data The Savable to save
     * @param storageType The specific type of folder to use to save the data
     */
public static void saveGame(String gamePath, String dataName, Savable data, JmeSystem.StorageFolderType storageType) {
    if (storageType == null) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Base Storage Folder Type is null, using External!");
        storageType = JmeSystem.StorageFolderType.External;
    }
    BinaryExporter ex = BinaryExporter.getInstance();
    OutputStream os = null;
    try {
        File baseFolder = JmeSystem.getStorageFolder(storageType);
        if (baseFolder == null) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
            throw new IllegalStateException("SaveGame dataset cannot be created");
        }
        File daveFolder = new File(baseFolder.getAbsolutePath() + File.separator + gamePath.replace('/', File.separatorChar));
        if (!daveFolder.exists() && !daveFolder.mkdirs()) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
            throw new IllegalStateException("SaveGame dataset cannot be created");
        }
        File saveFile = new File(daveFolder.getAbsolutePath() + File.separator + dataName);
        if (!saveFile.exists()) {
            if (!saveFile.createNewFile()) {
                Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error creating save file!");
                throw new IllegalStateException("SaveGame dataset cannot be created");
            }
        }
        os = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
        ex.save(data, os);
        Logger.getLogger(SaveGame.class.getName()).log(Level.FINE, "Saving data to: {0}", saveFile.getAbsolutePath());
    } catch (IOException ex1) {
        Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
        ex1.printStackTrace();
        throw new IllegalStateException("SaveGame dataset cannot be saved");
    } finally {
        try {
            if (os != null) {
                os.close();
            }
        } catch (IOException ex1) {
            Logger.getLogger(SaveGame.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
            ex1.printStackTrace();
            throw new IllegalStateException("SaveGame dataset cannot be saved");
        }
    }
}
Also used : BinaryExporter(com.jme3.export.binary.BinaryExporter) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 69 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class MaterialMatParamTest method uniform.

private Uniform uniform(String name, VarType type, Object value) {
    Uniform u = new Uniform();
    u.setName("m_" + name);
    u.setValue(type, value);
    return u;
}
Also used : Uniform(com.jme3.shader.Uniform)

Example 70 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class ConstraintHelper method getTransform.

/**
     * The method retreives the transform from a feature in a given space.
     * 
     * @param oma
     *            the OMA of the feature (spatial or armature node)
     * @param subtargetName
     *            the feature's subtarget (bone in a case of armature's node)
     * @param space
     *            the space the transform is evaluated to
     * @return thensform of a feature in a given space
     */
public Transform getTransform(Long oma, String subtargetName, Space space) {
    Spatial feature = (Spatial) blenderContext.getLoadedFeature(oma, LoadedDataType.FEATURE);
    boolean isArmature = blenderContext.getMarkerValue(ObjectHelper.ARMATURE_NODE_MARKER, feature) != null;
    if (isArmature) {
        blenderContext.getSkeleton(oma).updateWorldVectors();
        BoneContext targetBoneContext = blenderContext.getBoneByName(oma, subtargetName);
        Bone bone = targetBoneContext.getBone();
        if (bone.getParent() == null && (space == Space.CONSTRAINT_SPACE_LOCAL || space == Space.CONSTRAINT_SPACE_PARLOCAL)) {
            space = Space.CONSTRAINT_SPACE_POSE;
        }
        // use readable names of the matrices so that the code is more clear
        TempVars tempVars = TempVars.get();
        Transform result;
        switch(space) {
            case CONSTRAINT_SPACE_WORLD:
                Spatial model = (Spatial) blenderContext.getLoadedFeature(targetBoneContext.getSkeletonOwnerOma(), LoadedDataType.FEATURE);
                Matrix4f boneModelMatrix = this.toMatrix(bone.getModelSpacePosition(), bone.getModelSpaceRotation(), bone.getModelSpaceScale(), tempVars.tempMat4);
                Matrix4f modelWorldMatrix = this.toMatrix(model.getWorldTransform(), tempVars.tempMat42);
                Matrix4f boneMatrixInWorldSpace = modelWorldMatrix.multLocal(boneModelMatrix);
                result = new Transform(boneMatrixInWorldSpace.toTranslationVector(), boneMatrixInWorldSpace.toRotationQuat(), boneMatrixInWorldSpace.toScaleVector());
                break;
            case CONSTRAINT_SPACE_LOCAL:
                assert bone.getParent() != null : "CONSTRAINT_SPACE_LOCAL should be evaluated as CONSTRAINT_SPACE_POSE if the bone has no parent!";
                result = new Transform(bone.getLocalPosition(), bone.getLocalRotation(), bone.getLocalScale());
                break;
            case CONSTRAINT_SPACE_POSE:
                {
                    Matrix4f boneWorldMatrix = this.toMatrix(this.getTransform(oma, subtargetName, Space.CONSTRAINT_SPACE_WORLD), tempVars.tempMat4);
                    Matrix4f armatureInvertedWorldMatrix = this.toMatrix(feature.getWorldTransform(), tempVars.tempMat42).invertLocal();
                    Matrix4f bonePoseMatrix = armatureInvertedWorldMatrix.multLocal(boneWorldMatrix);
                    result = new Transform(bonePoseMatrix.toTranslationVector(), bonePoseMatrix.toRotationQuat(), bonePoseMatrix.toScaleVector());
                    break;
                }
            case CONSTRAINT_SPACE_PARLOCAL:
                {
                    Matrix4f boneWorldMatrix = this.toMatrix(this.getTransform(oma, subtargetName, Space.CONSTRAINT_SPACE_WORLD), tempVars.tempMat4);
                    Matrix4f armatureInvertedWorldMatrix = this.toMatrix(feature.getWorldTransform(), tempVars.tempMat42).invertLocal();
                    Matrix4f bonePoseMatrix = armatureInvertedWorldMatrix.multLocal(boneWorldMatrix);
                    result = new Transform(bonePoseMatrix.toTranslationVector(), bonePoseMatrix.toRotationQuat(), bonePoseMatrix.toScaleVector());
                    Bone parent = bone.getParent();
                    if (parent != null) {
                        BoneContext parentContext = blenderContext.getBoneContext(parent);
                        Vector3f head = parent.getModelSpacePosition();
                        Vector3f tail = head.add(bone.getModelSpaceRotation().mult(Vector3f.UNIT_Y.mult(parentContext.getLength())));
                        result.getTranslation().subtractLocal(tail);
                    }
                    break;
                }
            default:
                throw new IllegalStateException("Unknown space type: " + space);
        }
        tempVars.release();
        return result;
    } else {
        switch(space) {
            case CONSTRAINT_SPACE_LOCAL:
                return feature.getLocalTransform();
            case CONSTRAINT_SPACE_WORLD:
                return feature.getWorldTransform();
            case CONSTRAINT_SPACE_PARLOCAL:
            case CONSTRAINT_SPACE_POSE:
                throw new IllegalStateException("Nodes can have only Local and World spaces applied!");
            default:
                throw new IllegalStateException("Unknown space type: " + space);
        }
    }
}
Also used : Matrix4f(com.jme3.math.Matrix4f) Spatial(com.jme3.scene.Spatial) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) Vector3f(com.jme3.math.Vector3f) Bone(com.jme3.animation.Bone) TempVars(com.jme3.util.TempVars) Transform(com.jme3.math.Transform)

Aggregations

ArrayList (java.util.ArrayList)18 Structure (com.jme3.scene.plugins.blender.file.Structure)12 Vector3f (com.jme3.math.Vector3f)11 Pointer (com.jme3.scene.plugins.blender.file.Pointer)10 IOException (java.io.IOException)10 List (java.util.List)9 ColorRGBA (com.jme3.math.ColorRGBA)8 Texture (com.jme3.texture.Texture)8 Geometry (com.jme3.scene.Geometry)6 Image (com.jme3.texture.Image)6 BoundingBox (com.jme3.bounding.BoundingBox)5 BoundingSphere (com.jme3.bounding.BoundingSphere)5 Light (com.jme3.light.Light)5 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 Uniform (com.jme3.shader.Uniform)5 Material (com.jme3.material.Material)4 Quaternion (com.jme3.math.Quaternion)4 Transform (com.jme3.math.Transform)4 Mesh (com.jme3.scene.Mesh)4 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)4