xeolabs / scenejs

An extensible WebGL-based 3D engine. This is an archived project.
https://xeolabs.github.io/scenejs/
Other
649 stars 165 forks source link

Support unlimited number of UV layers #449

Closed xeolabs closed 8 years ago

xeolabs commented 8 years ago

Add a uvs array property to SceneJS.Geometry to accept an array of UV arrays, so that we can have an unlimited number of UV layers.

API Changes

See constructor config changes in example below. Note that SceneJS.Texture nodes can still use the same applyTo values of uv, uv2, uv3 etc, where those will reference uvs sub-arrays.

When uv, uv2 or uv3 are supplied, the Geometry will internally convert those into a uvs property, which will be accessible via getUVs.

The existing getUV, getUV2, getUV3, setUV, setUV2 and setUV3 accessors will internally access subarrays of the uvs property.

UV attributes in shaders will be named SCENEJS_aUVCoord0, SCENEJS_aUVCoord1, SCENEJS_aUVCoord<n> for UV layers 0...n.

Example

{
  type: "texture",
  src: "textures/general-zod.jpg",

  nodes: [

    // Second texture layer is a color map that maps to the geometry's second UV array
    {
      type: "texture",
      src: "textures/general-zod.jpg",
      applyFrom: "uv2", // Apply to second set of UVs
      blendFactor: 0.5,
      wrapS: "repeat",
      wrapT: "repeat",
      blendMode: "add",

      nodes: [

        // Third texture layer is a color map that maps to the geometry's third UV array
        {
          type: "texture",
          src: "textures/general-zod.jpg",
          applyFrom: "uv3", // Apply to third set of UVs
          blendFactor: 0.3,
          wrapS: "repeat",
          wrapT: "repeat",
          blendMode: "add",

          nodes: [

            // Custom box geometry with two arrays of UV coordinates
            {
              type: "geometry",
              id: "myGeometry",
              primitive: "triangles",
              positions: [
                5, 5, 5, -5, 5, 5, -5, -5, 5, 5, -5, 5,
                5, 5, 5, 5, -5, 5, 5, -5, -5, 5, 5, -5,
                5, 5, 5, 5, 5, -5, -5, 5, -5, -5, 5, 5,
                -5, 5, 5, -5, 5, -5, -5, -5, -5, -5, -5, 5,
                -5, -5, -5, 5, -5, -5, 5, -5, 5, -5, -5, 5,
                5, -5, -5, -5, -5, -5, -5, 5, -5, 5, 5, -5
              ],

              normals: [
                0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
                1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
                0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
                -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0,
                0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
                0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1
              ],

              uvs: [
                  // First UV array for first UV layer
                  [
                    1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1,
                    1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,
                    0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1
                  ],

                  // Second UV array for second UV layer
                  [
                    3, 3, 0, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 0, 3, 3,
                    3, 0, 3, 3, 0, 3, 0, 0, 3, 3, 0, 3, 0, 0, 3, 0,
                    0, 0, 3, 0, 3, 3, 0, 3, 0, 0, 3, 0, 3, 3, 0, 3
                  ],

                  // Third UV array for third UV layer
                  [
                    2, 2, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 2, 2,
                    2, 0, 2, 2, 0, 2, 0, 0, 2, 2, 0, 2, 0, 0, 2, 0,
                    0, 0, 2, 0, 2, 2, 0, 2, 0, 0, 2, 0, 2, 2, 0, 2
                  ]
              ],

Accessor changes:

var uvs = geometry.getUVs(); // Returns 3D array
//...

geometry.setUVs([  // Sets a 3D array
       [.....], // Layer 1
       [.....], // Layer 2
       [.....], // Layer 3
       //....etc
]);

Internal changes

xeolabs commented 8 years ago

Added in https://github.com/xeolabs/scenejs/commit/5674e4316ca1939c84b90db5724c1cd015c3d79a