<commit_before>/*
 * Copyright © 2016 Viro Media. All rights reserved.
 */

package com.viromedia.bridge.module;


import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.viro.core.internal.Image;
import com.viro.core.Material;
import com.viro.core.Texture;
import com.viromedia.bridge.utility.Helper;
import com.viromedia.bridge.utility.ImageDownloader;

import java.util.HashMap;
import java.util.Map;


public class MaterialManager extends ReactContextBaseJavaModule {

    // TODO: figure out a good place to load the libraries.
    static {
        System.loadLibrary("native-lib");
    }

    private final ReactApplicationContext mContext;
    private Map<String, MaterialWrapper> mMaterialsMap;
    private Map<String, Image> mImageMap;
    private boolean mShouldReload = false;

    public MaterialManager(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
        mMaterialsMap = new HashMap<String, MaterialWrapper>();
        mImageMap = new HashMap<String, Image>();
    }

    @Override
    public String getName() {
        return "VRTMaterialManager";
    }

    public Material getMaterial(String name) {
        reloadMaterials();

        if (mMaterialsMap.containsKey(name)) {
            return mMaterialsMap.get(name).getNativeMaterial();
        }
        return null;
    }

    /**
     * Call this method if we should reload/recreate all the materials the
     * next time we create them.
     */
    public void shouldReload() {
        mShouldReload = true;
    }

    /**
     * This function goes through every material and reloads the material
     */
    public void reloadMaterials() {
        if (mShouldReload) {
            for (String key : mMaterialsMap.keySet()) {
                MaterialWrapper material = mMaterialsMap.get(key);
                material.recreate();
            }
            mShouldReload = false;
        }
    }

    @ReactMethod
    public void setJSMaterials(ReadableMap newMaterials) {
        loadMaterials(newMaterials);
    }

    private void loadMaterials(ReadableMap newMaterials) {
        ReadableMapKeySetIterator iter = newMaterials.keySetIterator();
        while (iter.hasNextKey()) {
            String key = iter.nextKey();
            ReadableMap material = newMaterials.getMap(key);
            MaterialWrapper materialWrapper = createMaterial(material);
            mMaterialsMap.put(key, materialWrapper);
        }
    }

    private MaterialWrapper createMaterial(ReadableMap materialMap) {
        MaterialWrapper materialWrapper = new MaterialWrapper(materialMap);

        // These defaults match those in the JNI's Material.java
        Material.LightingModel lightingModel = Material.LightingModel.CONSTANT;
        int diffuseColor = Color.WHITE;
        Texture diffuseTexture = null;
        float diffuseIntensity = 1.0f;
        Texture specularTexture = null;
        float shininess = 2.0f;
        float fresnelExponent = 1.0f;
        Texture normalMap = null;
        Material.CullMode cullMode = Material.CullMode.BACK;
        Material.TransparencyMode transparencyMode= Material.TransparencyMode.A_ONE;
        Material.BlendMode blendMode = Material.BlendMode.ALPHA;
        float bloomThreshold = 1.0f;
        boolean writesToDepthBuffer = true;
        boolean readsFromDepthBuffer = true;

        ReadableMapKeySetIterator iter = materialMap.keySetIterator();<commit_msg>VIRO-2253: default bloom was 1.0 instead of -1.0<commit_after>/*
 * Copyright © 2016 <NAME>. All rights reserved.
 */

package com.viromedia.bridge.module;


import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.viro.core.internal.Image;
import com.viro.core.Material;
import com.viro.core.Texture;
import com.viromedia.bridge.utility.Helper;
import com.viromedia.bridge.utility.ImageDownloader;

import java.util.HashMap;
import java.util.Map;


public class MaterialManager extends ReactContextBaseJavaModule {

    // TODO: figure out a good place to load the libraries.
    static {
        System.loadLibrary("native-lib");
    }

    private final ReactApplicationContext mContext;
    private Map<String, MaterialWrapper> mMaterialsMap;
    private Map<String, Image> mImageMap;
    private boolean mShouldReload = false;

    public MaterialManager(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
        mMaterialsMap = new HashMap<String, MaterialWrapper>();
        mImageMap = new HashMap<String, Image>();
    }

    @Override
    public String getName() {
        return "VRTMaterialManager";
    }

    public Material getMaterial(String name) {
        reloadMaterials();

        if (mMaterialsMap.containsKey(name)) {
            return mMaterialsMap.get(name).getNativeMaterial();
        }
        return null;
    }

    /**
     * Call this method if we should reload/recreate all the materials the
     * next time we create them.
     */
    public void shouldReload() {
        mShouldReload = true;
    }

    /**
     * This function goes through every material and reloads the material
     */
    public void reloadMaterials() {
        if (mShouldReload) {
            for (String key : mMaterialsMap.keySet()) {
                MaterialWrapper material = mMaterialsMap.get(key);
                material.recreate();
            }
            mShouldReload = false;
        }
    }

    @ReactMethod
    public void setJSMaterials(ReadableMap newMaterials) {
        loadMaterials(newMaterials);
    }

    private void loadMaterials(ReadableMap newMaterials) {
        ReadableMapKeySetIterator iter = newMaterials.keySetIterator();
        while (iter.hasNextKey()) {
            String key = iter.nextKey();
            ReadableMap material = newMaterials.getMap(key);
            MaterialWrapper materialWrapper = createMaterial(material);
            mMaterialsMap.put(key, materialWrapper);
        }
    }

    private MaterialWrapper createMaterial(ReadableMap materialMap) {
        MaterialWrapper materialWrapper = new MaterialWrapper(materialMap);

        // These defaults match those in the JNI's Material.java
        Material.LightingModel lightingModel = Material.LightingModel.CONSTANT;
        int diffuseColor = Color.WHITE;
        Texture diffuseTexture = null;
        float diffuseIntensity = 1.0f;
        Texture specularTexture = null;
        float shininess = 2.0f;
        float fresnelExponent = 1.0f;
        Texture normalMap = null;
        Material.CullMode cullMode = Material.CullMode.BACK;
        Material.TransparencyMode transparencyMode= Material.TransparencyMode.A_ONE;
        Material.BlendMode blendMode = Material.BlendMode.ALPHA;
        float bloomThreshold = -1.0f;
        boolean writesToDepthBuffer = true;
        boolean readsFromDepthBuffer = true;

        ReadableMapKeySetIterator iter = materialMap.keySetIterator();
{'fraction_non_alphanumeric': 0.08324579831932773, 'fraction_numerical': 0.004201680672268907, 'mean_word_length': 2.958939708939709, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501017', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1998, 'n_words': 485}