|
| 1 | +package com.jme3.vulkan.material; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; |
| 6 | +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; |
| 7 | +import com.jme3.vulkan.descriptors.Descriptor; |
| 8 | +import com.jme3.vulkan.descriptors.DescriptorPool; |
| 9 | +import com.jme3.vulkan.material.uniforms.BufferUniform; |
| 10 | +import com.jme3.vulkan.material.uniforms.TextureUniform; |
| 11 | +import com.jme3.vulkan.material.uniforms.Uniform; |
| 12 | +import com.jme3.vulkan.pipelines.GraphicsPipeline; |
| 13 | +import com.jme3.vulkan.util.ReflectionArgs; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.lang.reflect.InvocationTargetException; |
| 18 | +import java.util.*; |
| 19 | +import java.util.function.BiFunction; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +public class LoadedMaterial extends NewMaterial { |
| 23 | + |
| 24 | + private static final Map<String, Function<ReflectionArgs, Object>> natives = new HashMap<>(); |
| 25 | + |
| 26 | + public LoadedMaterial(DescriptorPool pool, String fileName) { |
| 27 | + super(pool); |
| 28 | + ObjectMapper mapper = YAMLMapper.builder() |
| 29 | + .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER) |
| 30 | + .build(); |
| 31 | + InputStream stream = null; |
| 32 | + Map<Integer, List<Uniform<?>>> sets = new HashMap<>(); |
| 33 | + try { |
| 34 | + JsonNode root = mapper.readTree(stream); |
| 35 | + JsonNode imports = root.get("imports"); |
| 36 | + JsonNode parameters = root.get("parameters"); |
| 37 | + parameters.fields(); |
| 38 | + for (Iterator<Map.Entry<String, JsonNode>> it = parameters.fields(); it.hasNext();) { |
| 39 | + Map.Entry<String, JsonNode> param = it.next(); |
| 40 | + int set = Objects.requireNonNull(param.getValue().get("set"), |
| 41 | + "Set index not defined in \"" + param.getKey() + "\"").asInt(); |
| 42 | + sets.computeIfAbsent(set, n -> new ArrayList<>()).add( |
| 43 | + instantiate(param.getKey(), param.getValue(), imports)); |
| 44 | + } |
| 45 | + // add uniforms to sets |
| 46 | + for (Map.Entry<Integer, List<Uniform<?>>> set : sets.entrySet()) { |
| 47 | + addSet(set.getKey(), set.getValue().toArray(new Uniform[0])); |
| 48 | + } |
| 49 | + // load pipelines |
| 50 | + JsonNode pipelines = root.get("pipelines"); |
| 51 | + for (Iterator<Map.Entry<String, JsonNode>> it = pipelines.fields(); it.hasNext();) { |
| 52 | + Map.Entry<String, JsonNode> pipeline = it.next(); |
| 53 | + Object state = instantiate(pipeline.getKey(), pipeline.getValue(), imports); |
| 54 | + } |
| 55 | + } catch (IOException |
| 56 | + | ClassNotFoundException |
| 57 | + | NoSuchMethodException |
| 58 | + | InvocationTargetException |
| 59 | + | InstantiationException |
| 60 | + | IllegalAccessException e) { |
| 61 | + throw new RuntimeException(e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments