StateModifier's are consumed the same as the Modifier object they extend. They are required to have a one to one relationship with their child node. StateModifiers change the state of their child renderable visual changes as stated in the comments.
The reason you need a separate modifier for each node is by design. There is a one to one relationship from a modifier to another node in the tree. This is due to the placement of each modifier (node) to have a parent child relationship.
The functional reason, for not applying a modifier to more than one Renderable at a time, may be for performance reasons and to keep code maintainable. There is a design pattern and best practice that is shown in the examples below to accomplish the answer to the question above. For our purposes, a StateModifier is not really anything more than a Modifier that maintains properties of it's children, so we will not explain the difference here.
A couple facts:
- There are two types of nodes in the tree, Modifiers and Renderables
- Each node represents a level of a branch in the tree
In your example you wanted to do the same effect on surface one and surface two:
Take this example first: Two nodes
mainContext.add(translateModifierOne).add(rotateModifierOne).add(redSurface);
mainContext.add(translateModifierTwo).add(rotateModifierTwo).add(greySurface);
It is similar to your example and has two modifier nodes doing the same exact thing by placing the surfaces in the same exact location as each other but rotating different directions with the rotate modifiers.
Now let's look at this example: One node
var myView = new RenderNode();
myView.add(rotateModifierOne).add(redSurface);
myView.add(rotateModifierTwo).add(greySurface);
mainContext.add(translateModifierTwo).add(myView);
It uses one modifier applied to a render node that places the two surfaces in the same exact place. The parent render node (myView) has two nodes attached at the same level and effects both of their placement. It does show how the one to one relationship of modifiers to nodes can be applied to more than one surface by using a render node.
Yet Another example: One node chaining another One
var myView = new RenderNode();
var initialTime = Date.now();
var myState = new Modifier({
transform: function(){
return Transform.rotateZ(0.002 * (Date.now() - initialTime));
}
});
myView.add(redSurface);
myView.add(greySurface);
mainContext.add(translateModifierTwo).add(myState).add(myView);
In this example, we see two modifiers chained together to transition a render node that has two surfaces added. In this case we are applying the same effect to both surfaces using their parent render node. It is hard to see, but they are stacked on top of each other in the same exact location. It kind of translates to how you would set up applying a modifier to two different surfaces. Not really useful, but did get rid of the duplicate modifiers.
Simple fix example: Give each surface it's own modifier
myView.add(rotateModifierOne).add(redSurface);
myView.add(rotateModifierTwo).add(greySurface);
We apply a parent modifier to each of the surfaces to give them their uniqueness in the tree to make their rotation offset by our original modifiers.