|
| 1 | +package io.github.oguzhancevik.obj2gltf; |
| 2 | + |
| 3 | +import de.javagl.jgltf.model.GltfConstants; |
| 4 | +import de.javagl.jgltf.model.GltfModel; |
| 5 | +import de.javagl.jgltf.model.GltfModels; |
| 6 | +import de.javagl.jgltf.model.io.GltfAsset; |
| 7 | +import de.javagl.jgltf.model.io.GltfModelWriter; |
| 8 | +import io.github.oguzhancevik.obj2gltf.creator.ObjGltfAssetCreatorV2; |
| 9 | +import io.github.oguzhancevik.obj2gltf.obj.BufferStrategy; |
| 10 | +import io.github.oguzhancevik.obj2gltf.obj.GltfWriteType; |
| 11 | +import io.github.oguzhancevik.obj2gltf.obj.IndicesComponentType; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.net.URI; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +public class ConvertObjToGltf { |
| 19 | + |
| 20 | + private final String inputObjFilePath, inputMtlFileName, outputFilePath, outputFileName; |
| 21 | + private final BufferStrategy bufferStrategy; |
| 22 | + private final IndicesComponentType indicesComponentType; |
| 23 | + private final GltfWriteType gltfWriteType; |
| 24 | + |
| 25 | + |
| 26 | + public ConvertObjToGltf(Builder builder) { |
| 27 | + this.inputObjFilePath = builder.inputObjFilePath; |
| 28 | + this.inputMtlFileName = builder.inputMtlFileName; |
| 29 | + this.outputFilePath = builder.outputFilePath; |
| 30 | + this.outputFileName = builder.outputFileName; |
| 31 | + this.bufferStrategy = builder.bufferStrategy; |
| 32 | + this.indicesComponentType = builder.indicesComponentType; |
| 33 | + this.gltfWriteType = builder.gltfWriteType; |
| 34 | + } |
| 35 | + |
| 36 | + public static class Builder { |
| 37 | + private String inputObjFilePath, inputMtlFileName, outputFilePath, outputFileName; |
| 38 | + private BufferStrategy bufferStrategy; |
| 39 | + private IndicesComponentType indicesComponentType; |
| 40 | + private GltfWriteType gltfWriteType; |
| 41 | + |
| 42 | + public Builder inputObjFilePath(String inputObjFilePath) { |
| 43 | + this.inputObjFilePath = inputObjFilePath; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public Builder inputMtlFileName(String inputMtlFileName) { |
| 48 | + this.inputMtlFileName = inputMtlFileName; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + public Builder outputFilePath(String outputFilePath) { |
| 53 | + this.outputFilePath = outputFilePath; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public Builder outputFileName(String outputFileName) { |
| 58 | + this.outputFileName = outputFileName; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public Builder bufferStrategy(BufferStrategy bufferStrategy) { |
| 63 | + this.bufferStrategy = bufferStrategy; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public Builder indicesComponentType(IndicesComponentType indicesComponentType) { |
| 68 | + this.indicesComponentType = indicesComponentType; |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + public Builder gltfWriteType(GltfWriteType gltfWriteType) { |
| 73 | + this.gltfWriteType = gltfWriteType; |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + public ConvertObjToGltf build() { |
| 78 | + return new ConvertObjToGltf(this); |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + public void convert() { |
| 84 | + try { |
| 85 | + |
| 86 | + long startTime = System.nanoTime(); |
| 87 | + |
| 88 | + if (inputObjFilePath == null || inputObjFilePath.isEmpty()) |
| 89 | + throw new NullPointerException("inputObjFilePath cannot be empty!"); |
| 90 | + URI objUri = Paths.get(inputObjFilePath).toUri(); |
| 91 | + |
| 92 | + if (bufferStrategy == null) throw new NullPointerException("bufferStrategy cannot be empty!"); |
| 93 | + ObjGltfAssetCreatorV2 gltfAssetCreator = new ObjGltfAssetCreatorV2(bufferStrategy); |
| 94 | + |
| 95 | + if (indicesComponentType == null) throw new NullPointerException("indicesComponentType cannot be empty!"); |
| 96 | + else if (indicesComponentType == IndicesComponentType.GL_UNSIGNED_BYTE) |
| 97 | + gltfAssetCreator.setIndicesComponentType(GltfConstants.GL_UNSIGNED_BYTE); |
| 98 | + else if (indicesComponentType == IndicesComponentType.GL_UNSIGNED_SHORT) |
| 99 | + gltfAssetCreator.setIndicesComponentType(GltfConstants.GL_UNSIGNED_SHORT); |
| 100 | + else if (indicesComponentType == IndicesComponentType.GL_UNSIGNED_INT) |
| 101 | + gltfAssetCreator.setIndicesComponentType(GltfConstants.GL_UNSIGNED_INT); |
| 102 | + |
| 103 | + GltfAsset gltfAsset = gltfAssetCreator.create(objUri, inputMtlFileName); |
| 104 | + |
| 105 | + GltfModel gltfModel = GltfModels.create(gltfAsset); |
| 106 | + |
| 107 | + GltfModelWriter gltfModelWriter = new GltfModelWriter(); |
| 108 | + |
| 109 | + if (outputFilePath == null || outputFilePath.isEmpty()) |
| 110 | + throw new NullPointerException("outputFilePath cannot be empty!"); |
| 111 | + if (outputFileName == null || outputFileName.isEmpty()) |
| 112 | + throw new NullPointerException("outputFileName cannot be empty!"); |
| 113 | + File outputFile = new File(outputFilePath + "/" + outputFileName + ".gltf"); |
| 114 | + File parentFile = outputFile.getParentFile(); |
| 115 | + |
| 116 | + if (parentFile != null) parentFile.mkdirs(); |
| 117 | + |
| 118 | + if (gltfWriteType == GltfWriteType.BINARY) gltfModelWriter.writeBinary(gltfModel, outputFile); |
| 119 | + else if (gltfWriteType == GltfWriteType.EMBEDDED) gltfModelWriter.writeEmbedded(gltfModel, outputFile); |
| 120 | + else gltfModelWriter.write(gltfModel, outputFile); |
| 121 | + |
| 122 | + long finishTime = System.nanoTime(); |
| 123 | + long duration = finishTime - startTime; |
| 124 | + long seconds = TimeUnit.SECONDS.convert(duration, TimeUnit.NANOSECONDS); |
| 125 | + System.out.println(seconds + " seconds"); |
| 126 | + } catch (Exception e) { |
| 127 | + e.printStackTrace(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments