babylon-material-factory

    0.0.1 • Public • Published

    Babylon material factory

    Creates babylon materials from an abstract data format. All textures are referenced at your will as an user-defined URL resolver. will be called.

    This library is coded in ES6.

    Getting Started

    Add the npm package babylon-material-factory to your project:

    npm install babylon-material-factory babylonjs --save
    

    or clone:

    git clone git@github.com:wanadev/babylon-material-factory.git
    

    Please not that the BabylonJS dependency is configured to be a peer one. Thus, you'll need to provide the dependency by yourself.

    Usage

    const materialFactory = require("babylon-material-factory");
     
    // Set the URL resolver..
    materialFactory.setUrlResolver((assetId) => {
        // Returning a valid URL (or blob-URL) to an image.
        return /* Promise<String> */;
    });
     
    // Creates a BABYLON.StandardMaterial.
    materialFactory.generateMaterial({
        "specularPower": 0.1,
        "diffuseTexture": {
            "__type__": "texture",
            "assetId": /* Whatever your URL resolver needs */,
            "uScale": 4,
        },
    })
        .then(material => /* BABYLON.Material */)
        .done();

    URL resolver

    When not set, the default URL resolver supposes that the assetId is an URL. Which means it's implementation looks like:

    materialFactory.setUrlResolver(function(assetId) {
        return new Promise(resolve => resolve(assetId)));
    };

    Material's params

    The generateMaterial accepts a list of data that are stored in a plain object way:

    {
        "specularPower": 0.1,
        ...
    },

    Validity of each parameter is not checked: it is just applied to a object through the applyData(material, data) function. This means, even if BabylonJS's API change in the future, this library won't need any update.

    However, for non-basic attributes (those which are not numbers nor strings), you will need to create an object with __type__ attribute.

    Material's param: texture

    Within the material object, one can specify a texture:

        "diffuseTexture"{
            "__type__": "texture",
            "assetId": /* Whatever your URL resolver needs */,
            "uScale": 4,
            ...
        }

    NOTE: One can specify an "url" property directly with no "assetId". In that case, no URL resolver is needed.

    You can specify if you want a BABYLON.Texture (default) or a BABYLON.CubeTexture with the __kind__ attribute:

        "diffuseTexture"{
            "__type__": "texture",
            "assetId": /* Whatever your URL resolver needs */,
            "__kind__": "cube",
            ...
        }

    Material's param: color

    A color is defined as such:

        "diffuseColor"{
            "__type__": "color",
            "value": "#AD2D4E", // Will be imported with BABYLON.Color4.FromHexString(value)
        }

    The value is any parsable String by photonui.Color.

    Material's param: fresnelParameters

    A FresnelParameter is defined as such:

        "reflectionFresnelParameters"{
            "__type__": "fresnelParameter",
            "bias": 0.3,
            "power": 1,
            "leftColor": {
                "__type__": "color",
                "value": "#AD2D4E",
            },
            ...
        }

    PBR materials

    By default, a BABYLON.StandardMaterial is created, however a BABYLON.PBRMaterial can be instanciated with:

    "materialParams"{
        "__kind__": "pbr",
        "directIntensity ": 0.5,
        ...
    }

    Keywords

    none

    Install

    npm i babylon-material-factory

    DownloadsWeekly Downloads

    7

    Version

    0.0.1

    License

    BSD-3-Clause

    Last publish

    Collaborators

    • breush