Пример #1
0
    public void setMaterial(Object3D object, String materialName) throws TextureException {
      MaterialDef matDef = null;

      for (int i = 0; i < mMaterials.size(); ++i) {
        if (mMaterials.get(i).name.equals(materialName)) {
          matDef = mMaterials.get(i);
          break;
        }
      }

      boolean hasTexture = matDef != null && matDef.diffuseTexture != null;
      boolean hasBump = matDef != null && matDef.bumpTexture != null;
      boolean hasSpecularTexture = matDef != null && matDef.specularColorTexture != null;
      boolean hasSpecular =
          matDef != null && matDef.specularColor > 0xff000000 && matDef.specularCoefficient > 0;

      AMaterial mat = null;

      if (hasSpecular && !hasBump) mat = new PhongMaterial();
      else if (hasBump && !hasSpecularTexture) mat = new NormalMapMaterial();
      else if (hasBump && hasSpecularTexture) mat = new NormalMapPhongMaterial();
      else mat = new DiffuseMaterial();

      mat.setUseSingleColor(!hasTexture);
      object.setMaterial(mat);
      object.setColor(
          matDef != null ? matDef.diffuseColor : (0xff000000 + ((int) (Math.random() * 0xffffff))));
      if (hasSpecular || hasSpecularTexture) {
        PhongMaterial phong = (PhongMaterial) mat;
        phong.setSpecularColor(matDef.specularColor);
        phong.setShininess(matDef.specularCoefficient);
      }

      if (hasTexture) {
        RajLog.i("hastex " + object.getName() + ", " + matDef.diffuseTexture);
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.diffuseTexture), "drawable", mResourcePackage);
          mat.addTexture(new Texture(identifier));
        } else {
          String filePath =
              mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.diffuseTexture);
          mat.addTexture(
              new Texture(
                  getOnlyFileName(matDef.diffuseTexture), BitmapFactory.decodeFile(filePath)));
        }
      }
      if (hasBump) {
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.bumpTexture), "drawable", mResourcePackage);
          mat.addTexture(new NormalMapTexture(identifier));
        } else {
          String filePath =
              mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.bumpTexture);
          mat.addTexture(
              new NormalMapTexture(
                  getOnlyFileName(matDef.bumpTexture), BitmapFactory.decodeFile(filePath)));
        }
      }
      if (hasSpecularTexture) {
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.specularColorTexture),
                  "drawable",
                  mResourcePackage);
          mat.addTexture(new SpecularMapTexture(identifier));
        } else {
          String filePath =
              mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.specularColorTexture);
          mat.addTexture(
              new SpecularMapTexture(
                  getOnlyFileName(matDef.specularColorTexture),
                  BitmapFactory.decodeFile(filePath)));
        }
      }
    }
Пример #2
0
    public void setMaterial(BaseObject3D object, String materialName) {
      MaterialDef matDef = null;

      for (int i = 0; i < mMaterials.size(); ++i) {
        if (mMaterials.get(i).name.equals(materialName)) {
          matDef = mMaterials.get(i);
          break;
        }
      }

      boolean hasTexture = matDef != null && matDef.diffuseTexture != null;
      boolean hasBump = matDef != null && matDef.bumpTexture != null;
      boolean hasSpecular =
          matDef != null && matDef.specularColor > 0xff000000 && matDef.specularCoefficient > 0;

      AMaterial mat = null;

      if (hasSpecular && !hasBump) mat = new PhongMaterial();
      else if (hasBump) mat = new BumpmapMaterial();
      else mat = new DiffuseMaterial();

      mat.setUseColor(!hasTexture);
      object.setColor(
          matDef != null ? matDef.diffuseColor : (0xff000000 + ((int) (Math.random() * 0xffffff))));
      object.setMaterial(mat);
      if (hasSpecular && !hasBump) {
        PhongMaterial phong = (PhongMaterial) mat;
        phong.setSpecularColor(matDef.specularColor);
        phong.setShininess(matDef.specularCoefficient);
      }

      if (hasTexture) {
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.diffuseTexture), "drawable", mResourcePackage);
          object.addTexture(
              mTextureManager.addTexture(BitmapFactory.decodeResource(mResources, identifier)));
        } else {
          try {
            String filePath =
                mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.diffuseTexture);
            object.addTexture(
                mTextureManager.addTexture(BitmapFactory.decodeFile(filePath), TextureType.BUMP));
          } catch (Exception e) {
            RajLog.e(
                "["
                    + getClass().getCanonicalName()
                    + "] Could not find file "
                    + matDef.diffuseTexture);
            e.printStackTrace();
            return;
          }
        }
      }
      if (hasBump) {
        if (mFile == null) {
          int identifier =
              mResources.getIdentifier(
                  getFileNameWithoutExtension(matDef.bumpTexture), "drawable", mResourcePackage);
          object.addTexture(
              mTextureManager.addTexture(
                  BitmapFactory.decodeResource(mResources, identifier), TextureType.BUMP));
        } else {
          try {
            String filePath =
                mFile.getParent() + File.separatorChar + getOnlyFileName(matDef.bumpTexture);
            object.addTexture(
                mTextureManager.addTexture(BitmapFactory.decodeFile(filePath), TextureType.BUMP));
          } catch (Exception e) {
            RajLog.e(
                "["
                    + getClass().getCanonicalName()
                    + "] Could not find file "
                    + matDef.bumpTexture);
            e.printStackTrace();
            return;
          }
        }
      }
    }