⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asetojme.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        object.tempTexVerts[object.faces[j].coordIndex[2]];
                }

                int[] indices = new int[object.faces.length * 3];
                int count = 0;
                for (int j = 0; j < object.faces.length; j++) {
                    indices[count] = object.faces[j].vertIndex[0];
                    count++;
                    indices[count] = object.faces[j].vertIndex[1];
                    count++;
                    indices[count] = object.faces[j].vertIndex[2];
                    count++;
                }

                object.tm.setIndexBuffer(BufferUtils.createIntBuffer(indices));
                object.tm.setTextureCoords(TexCoords.makeNew(texCoords2));
                object.tm.setModelBound(new BoundingBox());
                object.tm.updateModelBound();
                mynode.attachChild(object.tm);
            }

            for (int j = 0; j < numOfMaterials; j++) {
                ASEMaterialInfo mat =
                    materials.get(j);
                if (mat.file.length() > 0) {
                    MaterialState ms =
                        DisplaySystem
                            .getDisplaySystem()
                            .getRenderer()
                            .createMaterialState();

                    ms.setEnabled(true);
                    ms.setAmbient(
                        new ColorRGBA(
                            mat.ambient[0],
                            mat.ambient[1],
                            mat.ambient[2],
                            1));
                    ms.setDiffuse(
                        new ColorRGBA(
                            mat.diffuse[0],
                            mat.diffuse[1],
                            mat.diffuse[2],
                            1));
                    ms.setSpecular(
                        new ColorRGBA(
                            mat.specular[0],
                            mat.specular[1],
                            mat.specular[2],
                            1));
                    ms.setEmissive(new ColorRGBA(0, 0, 0, 1));
                    ms.setShininess(mat.shine);
                    mynode.setRenderState(ms);
                }
            }

            for (int j = 0; j < numOfMaterials; j++) {
                // Check if the current material has a file name
                if (materials.get(j).file.length()
                    > 0) {

                    String filename =
                        materials.get(j).file;
                    URL fileURL = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, filename);
                    if (fileURL == null) {
                        logger.warning("Could not locate texture: " + filename);
                        continue;
                    }
                    TextureState ts =
                        DisplaySystem
                            .getDisplaySystem()
                            .getRenderer()
                            .createTextureState();
                    ts.setEnabled(true);
                        
                    Texture t=new Texture2D();
                    t.setImageLocation("file:/"+filename);
                    t.setTextureKey(new TextureKey(fileURL, true, TextureManager.COMPRESS_BY_DEFAULT ? Image.Format.Guess : Image.Format.GuessNoCompression));
                    t.setAnisotropicFilterPercent(0.0f);
                    t.setMinificationFilter(Texture.MinificationFilter.Trilinear);
                    t.setMagnificationFilter(Texture.MagnificationFilter.Bilinear);
                    ts.setTexture(t);
                    mynode.setRenderState(ts);
                }

            }

        }

        /**
         *
         * <code>getObjectCount</code> counts the number of Geomobject entries
         * in the ASE file. This count is then returned.
         * @return the number of Geomobject entries.
         */
        private int getObjectCount() {
            int objectCount = 0;
            tokenizer = new StringTokenizer(fileContents);

            while (tokenizer.hasMoreTokens()) {
                // Check if we hit the start of an object
                if (OBJECT.equals(tokenizer.nextToken())) {
                    objectCount++;
                }
            }

            return objectCount;
        }

        /**
         *
         * <code>getMaterialCount</code> retrieves the number of materials in the
         * ASE file. The file is read until the *MATERIAL flag is encountered. Once
         * this flag is found, the value is read.
         *
         * @return the number of materials as defined in the ASE file.
         */
        private int getMaterialCount() {
            int materialCount = 0;

            // Go to the beginning of the file
            tokenizer = new StringTokenizer(fileContents);

            // GO through the whole file until we hit the end
            while (tokenizer.hasMoreTokens()) {
                if (MATERIAL_COUNT.equals(tokenizer.nextToken())) {
                    materialCount = Integer.parseInt(tokenizer.nextToken());
                    return materialCount;
                }
            }

            //Material tag never found
            return 0;
        }

        /**
         *
         * <code>getMaterialInfo</code> reads the data for a given material
         * entry in the file. The material state information is read and
         * set as well as the texture state information.
         * @param material the material structure to store into.
         * @param desiredMaterial the material to load from the file.
         */
        private void getMaterialInfo(
            ASEMaterialInfo material,
            int desiredMaterial) {
            String strWord;
            int materialCount = 0;

            // Go to the beginning of the file
            tokenizer = new StringTokenizer(fileContents);

            //read through the file until the correct material entry is found.
            while (tokenizer.hasMoreTokens()) {
                if (MATERIAL.equals(tokenizer.nextToken())) {
                    materialCount++;

                    // Check if it's the one we want to stop at, if so break
                    if (materialCount == desiredMaterial)
                        break;
                }
            }

            while (tokenizer.hasMoreTokens()) {
                strWord = tokenizer.nextToken();

                if (strWord.equals(MATERIAL)) {
                    return;
                }

                //read material properites.
                if (strWord.equals(MATERIAL_AMBIENT)) {
                    material.ambient[0] = Float.parseFloat(tokenizer.nextToken());
                    material.ambient[1] = Float.parseFloat(tokenizer.nextToken());
                    material.ambient[2] = Float.parseFloat(tokenizer.nextToken());
                } else if (strWord.equals(MATERIAL_DIFFUSE)) {
                    material.diffuse[0] = Float.parseFloat(tokenizer.nextToken());
                    material.diffuse[1] = Float.parseFloat(tokenizer.nextToken());
                    material.diffuse[2] = Float.parseFloat(tokenizer.nextToken());
                } else if (strWord.equals(MATERIAL_SPECULAR)) {
                    material.specular[0] = Float.parseFloat(tokenizer.nextToken());
                    material.specular[1] = Float.parseFloat(tokenizer.nextToken());
                    material.specular[2] = Float.parseFloat(tokenizer.nextToken());
                } else if (strWord.equals(MATERIAL_SHINE)) {
                    material.shine = Float.parseFloat(tokenizer.nextToken());
                }

                //read texture information.
                if (strWord.equals(TEXTURE)) {
                    material.file = tokenizer.nextToken().replace('"', ' ').trim();
                } else if (strWord.equals(MATERIAL_NAME)) {
                    material.name = tokenizer.nextToken();
                } else if (strWord.equals(UTILE)) {
                    material.uTile = Float.parseFloat(tokenizer.nextToken());
                } else if (strWord.equals(VTILE)) {
                    material.vTile = Float.parseFloat(tokenizer.nextToken());
                }
            }
        }

        /**
         *
         * <code>moveToObject</code> moves the file pointer to a specific
         * GEOMOBJECT entry in the ase file.
         * @param desiredObject the object number to move to.
         */
        private void moveToObject(int desiredObject) {
            int objectCount = 0;

            tokenizer = new StringTokenizer(fileContents);

            while (tokenizer.hasMoreTokens()) {
                if (OBJECT.equals(tokenizer.nextToken())) {
                    objectCount++;

                    if (objectCount == desiredObject)
                        return;
                }
            }
        }

        /**
         *
         * <code>readObjectInfo</code> reads the mesh information defined by
         * the GEOMOBJECT entry in the file. This information is kept in the
         * ASEObject class until it is ready to be converted to a TriMesh.
         * @param currentObject the object to store the data in.
         * @param desiredObject the object to read.
         */
        private void readObjectInfo(ASEObject currentObject, int desiredObject) {
            String word;

            moveToObject(desiredObject);

            while (tokenizer.hasMoreTokens()) {
                word = tokenizer.nextToken();

                if (word.equals("*NODE_NAME")) {
                    currentObject.tm.setName(tokenizer.nextToken());
                }

                if (word.equals(NUM_VERTEX)) {
                    int numOfVerts = Integer.parseInt(tokenizer.nextToken());
                    currentObject.tm.setVertexBuffer(BufferUtils.createVector3Buffer(numOfVerts));
                } else if (word.equals(NUM_FACES)) {
                    int numOfFaces = Integer.parseInt(tokenizer.nextToken());
                    currentObject.faces = new Face[numOfFaces];
                } else if (word.equals(NUM_TVERTEX)) {
                    int numTexVertex = Integer.parseInt(tokenizer.nextToken());

                    currentObject.tempTexVerts = new Vector2f[numTexVertex];
                } else if (word.equals(OBJECT)) {
                    return;
                }
            }
        }

        /**
         *
         * <code>readObjectData</code> reads each bit of data defined by a
         * GEOMOBJECT. Namely, material id, vertices, texture vertices, faces,
         * texture faces, texture file, u and v tiling.
         * @param currentObject the object to store the information in.
         * @param desiredObject the object to read.
         */
        private void readObjectData(ASEObject currentObject, int desiredObject) {
            // Load the material ID for this object
            getData(currentObject, MATERIAL_ID, desiredObject);

            // Load the vertices for this object
            getData(currentObject, VERTEX, desiredObject);

            // Load the texture coordinates for this object
            getData(currentObject, TVERTEX, desiredObject);

            // Load the vertex faces list for this object
            getData(currentObject, FACE, desiredObject);

            // Load the texture face list for this object
            getData(currentObject, TFACE, desiredObject);

            // Load the texture for this object

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -