7. Color and Transparency
In this section, you will learn to change both the color and transparency of model nodes.
- Setting Part Color
- Setting Part Transparency
Functions to Set Node Colors
Let's start by adding a new file called material.js to the JS folder of your application and add the following code:
In the first function above, we determine the currently selected entity and return its NodeID if something is selected.
The hexToRgb function takes a 6-digit hex code in the form of #000000 and returns a Communicator color object. Every two digits after # represent one RGB (red, green or blue) value in the range of 00 – FF. The communicator color object expects each RGB value in the range of 0 – 255. To convert from hex to integer, we first use a regular expression to parse each hex value. Then use the parseInt function to convert from hex to integer values. Once we have integer values, we use the corresponding RGB to create a color object.
The setNodeColor() function we've defined takes a NodeId and hex color, and uses the setNodesColors() function to set the provided color on a part. The setNodesColors() function requires one argument: Map<NodeId, Color>, where Color is an RGB value in the range of 0-255. To create colorMap, we first use hexToRgb to convert the hex color to a Communicator color object. Then we create a new Map and set the color to the provided NodeId. If there were multiple colors and NodeIds that we wanted to set at the same time, we could continue adding them to the color map before calling setNodesColors().
Add functionality from material.js to your application by adding this line to your HTML page:
Creating HTML for Color Picker
We still need some HTML to interact with these functions. Add the following elements to the HTML page inside the container div after the viewer div.
This adds a standard color input box for the color value (defined by the input type "color") and a button to apply the selected color. When the apply button is clicked, it calls setNodeColor() with the NodeId value from getSelectedNodeId() and the color value from the color input box.
Running the Application
Load the application again, and you should now see an HTML color picker, and an apply button. Select a part on the model and try setting its color.
If you set a color on a non-leaf node, it will automatically be pushed down to all the leaf nodes that contain geometry and applied there. In that case, the color will not be stored on the node itself.
Setting Node Opacity
Now, we will add functionality to set a node's opacity. Add the following code to material.js:
setNodeOpacity takes a NodeId and opacity value between 0 – 1. If a NodeId of null is provided, no opacity changes will take effect. An opacity value of 0 will hide a part, and an opacity value of 1 will make a part completely opaque.
HTML for Opacity Button
The next step is to add some HTML code at the end of the buttons <div> we created above.
This adds an input box for the opacity value and a button to apply the opacity value. When the apply button is clicked, it calls setNodeOpacity with the NodeId value from getSelectedNodeId and the opacity value from the input box.
Load the application again, and you should now see an input box for opacity level. Select a part on the model and try setting its opacity!