Search in sources :

Example 26 with Type

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

the class Material method applyOverrides.

private int applyOverrides(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> overrides, int unit) {
    for (MatParamOverride override : overrides.getArray()) {
        VarType type = override.getVarType();
        MatParam paramDef = def.getMaterialParam(override.getName());
        if (paramDef == null || paramDef.getVarType() != type || !override.isEnabled()) {
            continue;
        }
        Uniform uniform = shader.getUniform(override.getPrefixedName());
        if (override.getValue() != null) {
            if (type.isTextureType()) {
                renderer.setTexture(unit, (Texture) override.getValue());
                uniform.setValue(VarType.Int, unit);
                unit++;
            } else {
                uniform.setValue(type, override.getValue());
            }
        } else {
            uniform.clearValue();
        }
    }
    return unit;
}
Also used : Uniform(com.jme3.shader.Uniform) VarType(com.jme3.shader.VarType)

Example 27 with Type

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

the class Material method updateShaderMaterialParameters.

private int updateShaderMaterialParameters(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> worldOverrides, SafeArrayList<MatParamOverride> forcedOverrides) {
    int unit = 0;
    if (worldOverrides != null) {
        unit = applyOverrides(renderer, shader, worldOverrides, unit);
    }
    if (forcedOverrides != null) {
        unit = applyOverrides(renderer, shader, forcedOverrides, unit);
    }
    for (int i = 0; i < paramValues.size(); i++) {
        MatParam param = paramValues.getValue(i);
        VarType type = param.getVarType();
        Uniform uniform = shader.getUniform(param.getPrefixedName());
        if (uniform.isSetByCurrentMaterial()) {
            continue;
        }
        if (type.isTextureType()) {
            renderer.setTexture(unit, (Texture) param.getValue());
            uniform.setValue(VarType.Int, unit);
            unit++;
        } else {
            uniform.setValue(type, param.getValue());
        }
    }
    //TODO HACKY HACK remove this when texture unit is handled by the uniform.
    return unit;
}
Also used : Uniform(com.jme3.shader.Uniform) VarType(com.jme3.shader.VarType)

Example 28 with Type

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

the class FbxMesh method createGeometries.

private List<Geometry> createGeometries() throws IOException {
    Mesh mesh = new Mesh();
    mesh.setMode(Mode.Triangles);
    // Moreover quads should be triangulated (this increases number of vertices)
    if (indices != null) {
        iCount = indices.length;
        srcVertexCount = vertices.length / 3;
        // Indices contains negative numbers to define polygon last index
        // Check indices strides to be sure we have triangles or quads
        vCount = 0;
        // Count number of vertices to be produced
        int polyVertCount = 0;
        for (int i = 0; i < iCount; ++i) {
            int index = indices[i];
            polyVertCount++;
            if (index < 0) {
                if (polyVertCount == 3) {
                    // A triangle
                    vCount += 3;
                } else if (polyVertCount == 4) {
                    // A quad produce two triangles
                    vCount += 6;
                } else {
                    throw new AssetLoadException("Unsupported PolygonVertexIndex stride");
                }
                polyVertCount = 0;
            }
        }
        // Unroll index array into vertex mapping
        vertexMap = new ArrayList<>(vCount);
        indexMap = new ArrayList<>(vCount);
        polyVertCount = 0;
        for (int i = 0; i < iCount; ++i) {
            int index = indices[i];
            polyVertCount++;
            if (index < 0) {
                int lastIndex = -(index + 1);
                if (polyVertCount == 3) {
                    vertexMap.add(indices[i - 2]);
                    vertexMap.add(indices[i - 1]);
                    vertexMap.add(lastIndex);
                    indexMap.add(i - 2);
                    indexMap.add(i - 1);
                    indexMap.add(i - 0);
                } else if (polyVertCount == 4) {
                    vertexMap.add(indices[i - 3]);
                    vertexMap.add(indices[i - 2]);
                    vertexMap.add(indices[i - 1]);
                    vertexMap.add(indices[i - 3]);
                    vertexMap.add(indices[i - 1]);
                    vertexMap.add(lastIndex);
                    indexMap.add(i - 3);
                    indexMap.add(i - 2);
                    indexMap.add(i - 1);
                    indexMap.add(i - 3);
                    indexMap.add(i - 1);
                    indexMap.add(i - 0);
                }
                polyVertCount = 0;
            }
        }
        // Build reverse vertex mapping
        reverseVertexMap = new ArrayList<>(srcVertexCount);
        for (int i = 0; i < srcVertexCount; ++i) reverseVertexMap.add(new ArrayList<Integer>());
        for (int i = 0; i < vCount; ++i) {
            int index = vertexMap.get(i);
            reverseVertexMap.get(index).add(i);
        }
    } else {
        // Stub for no vertex indexing (direct mapping)
        iCount = vCount = srcVertexCount;
        vertexMap = new ArrayList<>(vCount);
        indexMap = new ArrayList<>(vCount);
        reverseVertexMap = new ArrayList<>(vCount);
        for (int i = 0; i < vCount; ++i) {
            vertexMap.set(i, i);
            indexMap.set(i, i);
            List<Integer> l = new ArrayList<Integer>(1);
            l.add(i);
            reverseVertexMap.add(l);
        }
    }
    if (vertices != null) {
        // Unroll vertices data array
        FloatBuffer posBuf = BufferUtils.createFloatBuffer(vCount * 3);
        mesh.setBuffer(VertexBuffer.Type.Position, 3, posBuf);
        int srcCount = vertices.length / 3;
        for (int i = 0; i < vCount; ++i) {
            int index = vertexMap.get(i);
            if (index > srcCount)
                throw new AssetLoadException("Invalid vertex mapping. Unexpected lookup vertex " + index + " from " + srcCount);
            // XXX Why we should scale by unit size?
            float x = (float) vertices[3 * index + 0] / scene.unitSize * scene.xAxis;
            float y = (float) vertices[3 * index + 1] / scene.unitSize * scene.yAxis;
            float z = (float) vertices[3 * index + 2] / scene.unitSize * scene.zAxis;
            posBuf.put(x).put(y).put(z);
        }
    }
    if (normals != null) {
        // Unroll normals data array
        FloatBuffer normBuf = BufferUtils.createFloatBuffer(vCount * 3);
        mesh.setBuffer(VertexBuffer.Type.Normal, 3, normBuf);
        List<Integer> mapping = null;
        if (normalsMapping.equals("ByVertice"))
            mapping = vertexMap;
        else if (normalsMapping.equals("ByPolygonVertex"))
            mapping = indexMap;
        else
            throw new IOException("Unknown normals mapping type: " + normalsMapping);
        int srcCount = normals.length / 3;
        for (int i = 0; i < vCount; ++i) {
            int index = mapping.get(i);
            if (index > srcCount)
                throw new AssetLoadException("Invalid normal mapping. Unexpected lookup normal " + index + " from " + srcCount);
            float x = (float) normals[3 * index + 0] * scene.xAxis;
            float y = (float) normals[3 * index + 1] * scene.yAxis;
            float z = (float) normals[3 * index + 2] * scene.zAxis;
            normBuf.put(x).put(y).put(z);
        }
    }
    if (tangents != null) {
        // Unroll normals data array
        FloatBuffer tanBuf = BufferUtils.createFloatBuffer(vCount * 4);
        mesh.setBuffer(VertexBuffer.Type.Tangent, 4, tanBuf);
        List<Integer> mapping = null;
        if (tangentsMapping.equals("ByVertice"))
            mapping = vertexMap;
        else if (tangentsMapping.equals("ByPolygonVertex"))
            mapping = indexMap;
        else
            throw new IOException("Unknown tangents mapping type: " + tangentsMapping);
        int srcCount = tangents.length / 3;
        for (int i = 0; i < vCount; ++i) {
            int index = mapping.get(i);
            if (index > srcCount)
                throw new AssetLoadException("Invalid tangent mapping. Unexpected lookup tangent " + index + " from " + srcCount);
            float x = (float) tangents[3 * index + 0] * scene.xAxis;
            float y = (float) tangents[3 * index + 1] * scene.yAxis;
            float z = (float) tangents[3 * index + 2] * scene.zAxis;
            tanBuf.put(x).put(y).put(z).put(-1.0f);
        }
    }
    if (binormals != null) {
        // Unroll normals data array
        FloatBuffer binormBuf = BufferUtils.createFloatBuffer(vCount * 3);
        mesh.setBuffer(VertexBuffer.Type.Binormal, 3, binormBuf);
        List<Integer> mapping = null;
        if (binormalsMapping.equals("ByVertice"))
            mapping = vertexMap;
        else if (binormalsMapping.equals("ByPolygonVertex"))
            mapping = indexMap;
        else
            throw new IOException("Unknown binormals mapping type: " + binormalsMapping);
        int srcCount = binormals.length / 3;
        for (int i = 0; i < vCount; ++i) {
            int index = mapping.get(i);
            if (index > srcCount)
                throw new AssetLoadException("Invalid binormal mapping. Unexpected lookup binormal " + index + " from " + srcCount);
            float x = (float) binormals[3 * index + 0] * scene.xAxis;
            float y = (float) binormals[3 * index + 1] * scene.yAxis;
            float z = (float) binormals[3 * index + 2] * scene.zAxis;
            binormBuf.put(x).put(y).put(z);
        }
    }
    for (int uvLayer = 0; uvLayer < uvs.size(); ++uvLayer) {
        double[] uv = uvs.get(uvLayer);
        int[] uvIndex = uvIndexes.size() > uvLayer ? uvIndexes.get(uvLayer) : null;
        List<Integer> unIndexMap = vertexMap;
        if (uvIndex != null) {
            int uvIndexSrcCount = uvIndex.length;
            if (uvIndexSrcCount != iCount)
                throw new AssetLoadException("Invalid number of texcoord index data " + uvIndexSrcCount + " expected " + iCount);
            // Unroll UV index array
            unIndexMap = new ArrayList<>(vCount);
            int polyVertCount = 0;
            for (int i = 0; i < iCount; ++i) {
                int index = indices[i];
                polyVertCount++;
                if (index < 0) {
                    if (polyVertCount == 3) {
                        unIndexMap.add(uvIndex[i - 2]);
                        unIndexMap.add(uvIndex[i - 1]);
                        unIndexMap.add(uvIndex[i - 0]);
                    } else if (polyVertCount == 4) {
                        unIndexMap.add(uvIndex[i - 3]);
                        unIndexMap.add(uvIndex[i - 2]);
                        unIndexMap.add(uvIndex[i - 1]);
                        unIndexMap.add(uvIndex[i - 3]);
                        unIndexMap.add(uvIndex[i - 1]);
                        unIndexMap.add(uvIndex[i - 0]);
                    }
                    polyVertCount = 0;
                }
            }
        }
        // Unroll UV data array
        FloatBuffer tcBuf = BufferUtils.createFloatBuffer(vCount * 2);
        VertexBuffer.Type type = VertexBuffer.Type.TexCoord;
        switch(uvLayer) {
            case 1:
                type = VertexBuffer.Type.TexCoord2;
                break;
            case 2:
                type = VertexBuffer.Type.TexCoord3;
                break;
            case 3:
                type = VertexBuffer.Type.TexCoord4;
                break;
            case 4:
                type = VertexBuffer.Type.TexCoord5;
                break;
            case 5:
                type = VertexBuffer.Type.TexCoord6;
                break;
            case 6:
                type = VertexBuffer.Type.TexCoord7;
                break;
            case 7:
                type = VertexBuffer.Type.TexCoord8;
                break;
        }
        mesh.setBuffer(type, 2, tcBuf);
        int srcCount = uv.length / 2;
        for (int i = 0; i < vCount; ++i) {
            int index = unIndexMap.get(i);
            if (index > srcCount)
                throw new AssetLoadException("Invalid texcoord mapping. Unexpected lookup texcoord " + index + " from " + srcCount);
            float u = (index >= 0) ? (float) uv[2 * index + 0] : 0;
            float v = (index >= 0) ? (float) uv[2 * index + 1] : 0;
            tcBuf.put(u).put(v);
        }
    }
    List<Geometry> geometries = new ArrayList<Geometry>();
    if (materialsReference.equals("IndexToDirect") && materialsMapping.equals("ByPolygon")) {
        IntMap<List<Integer>> indexBuffers = new IntMap<>();
        for (int polygon = 0; polygon < materials.length; ++polygon) {
            int material = materials[polygon];
            List<Integer> list = indexBuffers.get(material);
            if (list == null) {
                list = new ArrayList<>();
                indexBuffers.put(material, list);
            }
            list.add(polygon * 3 + 0);
            list.add(polygon * 3 + 1);
            list.add(polygon * 3 + 2);
        }
        Iterator<Entry<List<Integer>>> iterator = indexBuffers.iterator();
        while (iterator.hasNext()) {
            Entry<List<Integer>> e = iterator.next();
            int materialId = e.getKey();
            List<Integer> indexes = e.getValue();
            Mesh newMesh = mesh.clone();
            newMesh.setBuffer(VertexBuffer.Type.Index, 3, toArray(indexes.toArray(new Integer[indexes.size()])));
            newMesh.setStatic();
            newMesh.updateBound();
            newMesh.updateCounts();
            Geometry geom = new Geometry();
            geom.setMesh(newMesh);
            geometries.add(geom);
            geom.setUserData("FBXMaterial", materialId);
        }
    } else {
        mesh.setStatic();
        mesh.updateBound();
        mesh.updateCounts();
        Geometry geom = new Geometry();
        geom.setMesh(mesh);
        geometries.add(geom);
    }
    return geometries;
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) ArrayList(java.util.ArrayList) Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer) IOException(java.io.IOException) AssetLoadException(com.jme3.asset.AssetLoadException) Geometry(com.jme3.scene.Geometry) Entry(com.jme3.util.IntMap.Entry) IntMap(com.jme3.util.IntMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with Type

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

the class FbxNode method fromElement.

@Override
public void fromElement(FbxElement element) {
    super.fromElement(element);
    Vector3f localTranslation = new Vector3f();
    Quaternion localRotation = new Quaternion();
    Vector3f localScale = new Vector3f(Vector3f.UNIT_XYZ);
    Quaternion preRotation = new Quaternion();
    for (FbxElement e2 : element.getFbxProperties()) {
        String propName = (String) e2.properties.get(0);
        String type = (String) e2.properties.get(3);
        if (propName.equals("Lcl Translation")) {
            double x = (Double) e2.properties.get(4);
            double y = (Double) e2.properties.get(5);
            double z = (Double) e2.properties.get(6);
            //.divideLocal(unitSize);
            localTranslation.set((float) x, (float) y, (float) z);
        } else if (propName.equals("Lcl Rotation")) {
            double x = (Double) e2.properties.get(4);
            double y = (Double) e2.properties.get(5);
            double z = (Double) e2.properties.get(6);
            localRotation.fromAngles((float) x * FastMath.DEG_TO_RAD, (float) y * FastMath.DEG_TO_RAD, (float) z * FastMath.DEG_TO_RAD);
        } else if (propName.equals("Lcl Scaling")) {
            double x = (Double) e2.properties.get(4);
            double y = (Double) e2.properties.get(5);
            double z = (Double) e2.properties.get(6);
            //.multLocal(unitSize);
            localScale.set((float) x, (float) y, (float) z);
        } else if (propName.equals("PreRotation")) {
            double x = (Double) e2.properties.get(4);
            double y = (Double) e2.properties.get(5);
            double z = (Double) e2.properties.get(6);
            preRotation.set(FbxNodeUtil.quatFromBoneAngles((float) x * FastMath.DEG_TO_RAD, (float) y * FastMath.DEG_TO_RAD, (float) z * FastMath.DEG_TO_RAD));
        } else if (propName.equals("InheritType")) {
            int inheritType = (Integer) e2.properties.get(4);
            inheritMode = InheritMode.values()[inheritType];
        } else if (propName.equals("Visibility")) {
            visibility = (Double) e2.properties.get(4);
        } else if (type.contains("U")) {
            String userDataKey = (String) e2.properties.get(0);
            String userDataType = (String) e2.properties.get(1);
            Object userDataValue;
            if (userDataType.equals("KString")) {
                userDataValue = (String) e2.properties.get(4);
            } else if (userDataType.equals("int")) {
                userDataValue = (Integer) e2.properties.get(4);
            } else if (userDataType.equals("double")) {
                // NOTE: jME3 does not support doubles in UserData.
                //       Need to convert to float.
                userDataValue = ((Double) e2.properties.get(4)).floatValue();
            } else if (userDataType.equals("Vector")) {
                float x = ((Double) e2.properties.get(4)).floatValue();
                float y = ((Double) e2.properties.get(5)).floatValue();
                float z = ((Double) e2.properties.get(6)).floatValue();
                userDataValue = new Vector3f(x, y, z);
            } else {
                logger.log(Level.WARNING, "Unsupported user data type: {0}. Ignoring.", userDataType);
                continue;
            }
            userData.put(userDataKey, userDataValue);
        }
    }
    // Create local transform
    // TODO: take into account Maya-style transforms (pre / post rotation ..)
    jmeLocalNodeTransform.setTranslation(localTranslation);
    jmeLocalNodeTransform.setRotation(localRotation);
    jmeLocalNodeTransform.setScale(localScale);
    if (element.getChildById("Vertices") != null) {
        // This is an old-style FBX 6.1
        // Meshes could be embedded inside the node..
        // Inject the mesh into ourselves..
        FbxMesh mesh = new FbxMesh(assetManager, sceneFolderName);
        mesh.fromElement(element);
        connectObject(mesh);
    }
}
Also used : FbxElement(com.jme3.scene.plugins.fbx.file.FbxElement) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) FbxMesh(com.jme3.scene.plugins.fbx.mesh.FbxMesh) FbxObject(com.jme3.scene.plugins.fbx.obj.FbxObject) CullHint(com.jme3.scene.Spatial.CullHint)

Example 30 with Type

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

the class SceneLoader method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes attribs) throws SAXException {
    if (qName.equals("scene")) {
        if (elementStack.size() != 0) {
            throw new SAXException("dotScene parse error: 'scene' element must be the root XML element");
        }
        String version = attribs.getValue("formatVersion");
        if (version == null || (!version.equals("1.0.0") && !version.equals("1.0.1"))) {
            logger.log(Level.WARNING, "Unrecognized version number" + " in dotScene file: {0}", version);
        }
    } else if (qName.equals("nodes")) {
        if (root != null) {
            throw new SAXException("dotScene parse error: nodes element was specified twice");
        }
        if (sceneName == null) {
            root = new com.jme3.scene.Node("OgreDotScene" + (++sceneIdx));
        } else {
            root = new com.jme3.scene.Node(sceneName + "-scene_node");
        }
        node = root;
    } else if (qName.equals("externals")) {
        checkTopNode("scene");
    } else if (qName.equals("item")) {
        checkTopNode("externals");
    } else if (qName.equals("file")) {
        checkTopNode("item");
    // NOTE: This part of the file is ignored, it is parsed
    // by SceneMaterialLoader in the first pass.
    } else if (qName.equals("node")) {
        String curElement = elementStack.peek();
        if (!curElement.equals("node") && !curElement.equals("nodes")) {
            throw new SAXException("dotScene parse error: " + "node element can only appear under 'node' or 'nodes'");
        }
        parseNode(attribs);
    } else if (qName.equals("property")) {
        if (node != null) {
            String type = attribs.getValue("type");
            String name = attribs.getValue("name");
            String data = attribs.getValue("data");
            if (type.equals("BOOL")) {
                node.setUserData(name, Boolean.parseBoolean(data) || data.equals("1"));
            } else if (type.equals("FLOAT")) {
                node.setUserData(name, Float.parseFloat(data));
            } else if (type.equals("STRING")) {
                node.setUserData(name, data);
            } else if (type.equals("INT")) {
                node.setUserData(name, Integer.parseInt(data));
            }
        }
    } else if (qName.equals("entity")) {
        checkTopNode("node");
        parseEntity(attribs);
    } else if (qName.equals("camera")) {
        checkTopNode("node");
        parseCamera(attribs);
    } else if (qName.equals("clipping")) {
        checkTopNode("camera");
        parseCameraClipping(attribs);
    } else if (qName.equals("position")) {
        if (elementStack.peek().equals("node")) {
            node.setLocalTranslation(SAXUtil.parseVector3(attribs));
        } else if (elementStack.peek().equals("camera")) {
            cameraNode.setLocalTranslation(SAXUtil.parseVector3(attribs));
        }
    } else if (qName.equals("quaternion") || qName.equals("rotation")) {
        node.setLocalRotation(parseQuat(attribs));
    } else if (qName.equals("scale")) {
        node.setLocalScale(SAXUtil.parseVector3(attribs));
    } else if (qName.equals("light")) {
        parseLight(attribs);
    } else if (qName.equals("colourDiffuse") || qName.equals("colorDiffuse")) {
        if (elementStack.peek().equals("light")) {
            if (light != null) {
                light.setColor(parseColor(attribs));
            }
        } else {
            checkTopNode("environment");
        }
    } else if (qName.equals("colourAmbient") || qName.equals("colorAmbient")) {
        if (elementStack.peek().equals("environment")) {
            ColorRGBA color = parseColor(attribs);
            if (!color.equals(ColorRGBA.Black) && !color.equals(ColorRGBA.BlackNoAlpha)) {
                // Lets add an ambient light to the scene.
                AmbientLight al = new AmbientLight();
                al.setColor(color);
                root.addLight(al);
            }
        }
    } else if (qName.equals("normal") || qName.equals("direction")) {
        checkTopNode("light");
        parseLightNormal(attribs);
    } else if (qName.equals("lightAttenuation")) {
        parseLightAttenuation(attribs);
    } else if (qName.equals("spotLightRange") || qName.equals("lightRange")) {
        parseLightSpotLightRange(attribs);
    }
    elementStack.push(qName);
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) LightNode(com.jme3.scene.LightNode) CameraNode(com.jme3.scene.CameraNode) AmbientLight(com.jme3.light.AmbientLight) SAXException(org.xml.sax.SAXException)

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