1

I want to have two custom properties in my schema, that have a fallback value if they are not defined, or if their value is below 0.0

this is the schema:

schema: {
  url: { type: 'string'},
  key: { type: 'string'},
  intensity: {
    // default is -100 to trigger fallback
    default: -100, 
    parse: function (value) { 
      if (value >= 0.0) { 
        return value 
      } 
      return -100
    } 
  }
}

The property that is giving me issues is the intensity property. If it is defined in the a-entity

<a-entity io3d-data3d="key: mykey; lightMapIntensity: 1.0" shadow="receive: true"></a-entity>

the value is properly used by the component, but when I open the a-frame 3d editor and click on the entity - I get this error:

A-frame error

this does not happen, if the attributes are not set in a-entity.

Am I doing something wrong when using custom properties? Is there a way to define optional properties, that have undefined or null as a default value?

Custom Property Type:

https://aframe.io/docs/0.6.0/core/component.html#custom-property-type

2 Answers2

3

The a-frame inspector tries to round up the values using the toFixed(decimalPlaces) function.

The passed values in the component are strings, and as far as i know, toFixed() does not work with strings, at least not in my experiments, nor in the documentation.

A simple solution is to return a parsed value:

if (value >= 0.0) {
  return parseFloat(value);
}

Although when comparing strings with floats, it is doing some additional parsing, You may want to parse the value before the check:

value = parseFloat(value);
if (value >= 0.0) {
  return value);    
}
Piotr Adam Milewski
  • 14,150
  • 3
  • 21
  • 42
0

Okay so I noticed that my schema tried to parse my property as string. I guess this is the fallback, if you don't define a type?

lightMapIntensity: {
  // default is -100 to trigger fallback to value specified in data3d file
  type:'float',
  default:-100.0,
  parse: function (value) {
    if (parseFloat(value) >= 0.0) {
      return parseFloat(value)
    }
    return -100.0 // = fallback to value from data3d file
  }
}

At least I don't have the error anymore.

Maybe someone has a better answer.