In the previous tutorial, we have been exposed to materials. This tutorial will explore how to use some basic materials: color materials, Phong shader, and lighting.
Adding colors and wireframes to 3D elements makes 3D elements more 3D. This tutorial explains how to use some basic color materials in the popular 3D engine away3d.
Prerequisites
This tutorial is based on other tutorials. If you are a flash 3D cainiao, you may want to take a look at these tutorials first. For example, for some source files, click the attached as file to view how they work. A cover. As file is used in all these examples. This file makes flash films used in the tutorial not work at the same time, or it will drag your computer down. If you are not sure how to use these examples, seeThis tutorial.
"Texture" is used in some examples in this tutorial ". For details about textures and materials, refer to the following tutorial. If you want the code to work in Flash CS3, read this tutorial.
Color Materials)
By default, all 3D elements in away3d have a standard material with a black box and random background color. Most examples in the tutorial use this type of material, so each time we re-open these examples, those 3D models are different colors. This material is called wirecolormaterial.
Writing code in away3d has two styles: compact and easy-to-read. In the Compact mode, you must pass the color value to the constructor when creating an instance:
1. var sphere: sphere = new sphere ({material: "Orange # Red "});
2. View. Scene. addchild (sphere );
3. var sphere: sphere = new sphere ({material: "Orange # Red "});
4. View. Scene. addchild (sphere );
This is an easy-to-read method:
1. var wirecolormaterial: wirecolormaterial = new wirecolormaterial ();
2. wirecolormaterial. Color = 0xffa500; wirecolormaterial. wirecolor = 0xff0000;
3. var sphere: sphere = new sphere (); sphere. Material = wirecolormaterial;
4. View. Scene. addchild (sphere );
(Basic09_wirecolormaterial.as)
Wirecolormaterial can set the line width:
1. wirecolormaterial. width = 5;
You can also specify the solid color material (colormaterial) of the wireless box for 3D elements ):
1. var sphere: sphere = new sphere ({material: "orange "});
Easy-to-Read mode:
1. var colormaterial: colormaterial = new colormaterial ();
2. colormaterial. Color = 0xffa500; var sphere: sphere = new sphere ();
3. sphere. Material = colormaterial;
(Basic09_colormaterial.as)
There is also a relatively wireframematerial material (wireframematerial ):
1. var colormaterial: wireframematerial = new wireframematerial ();
2. colormaterial. Color = 0xffa500; colormaterial. width = 2;
3. var sphere: sphere = new sphere (); sphere. Material = colormaterial;
4. View. Scene. addchild (sphere );
(Basic09_wireframematerial.as)
This material is not as "concise" syntax as before.
It is difficult to tell when the 3D model of the color material is rotated. However, adding a contour line is different. Contour lines can be added to all 3D elements, just like this:
1. var outlinematerial: wireframematerial = new wireframematerial (0x000000 );
2. outlinematerial. width = 3; sphere. Outline = outlinematerial;
In this way, the sphere has a three-pixel wide contour line. Let's look at the turtles below.
Code: basic09_colormaterial2.as
Light and reflective color Materials
When rendering a 3D model, the shadow material considers the light source. If you forget to add a light source when using a material that requires a "light source", the model will not be rendered. Three light sources are available in away3d, but the directionallight3d is used in several materials.
1. var light: directionallight3d = new directionallight3d ();
2. View. Scene. addchild (light );
After adding a light source to a scenario, you find that the model in the scenario is not as shown after we have a light source. This is because the light source is placed in the center of the 3D world by default (0, 0, 0 ). To produce results, we need to offset the position of the light source.
1. Light. Y = 500; light. x =-300; light. z =-200;
The preceding values are random and the positions of different light sources vary. In addition, the reflection of light varies according to the values of the three attributes, including ambient, diffuse, and specular. Their values range from 0 ~ Between 1:
1. Light. specular = 1; light. Diffuse = 0.5; light. Ambient = 0.5;
Note: The light source has another property.Brightness.This attribute cannot be reset after rendering, so the value of this attribute is set once the light source is created.
Shadingcolormaterial
This material is very useful for you to arrange the light and shade effects for the scenario. With it, we can see the effect of the light source on the 3D model. You can assign an initial value to the material constructor or specify the color by setting the. color attribute:
VaR mat: shadingcolormaterial = new shadingcolormaterial (0xff00ff );
Click the ball below to generate a random color.
Code: basic09_shadingcolormaterial.as
Phongcolormaterial
Phongcolormaterial produces shadows like shadingcolormaterial, but the difference is that it looks smoother.
(Basic09_phongcolormaterial.as)
Each light source has its own color. By default, the light source is pure white. The color of the light and the material will be mixed. If it is a yellow light and a blue ball, the final result is to see the green ball. In the following flex program, you can set each attribute to generate the desired effect:
Code: directionallight3dtest. mxml, viewport. mxml
In the next tutorial, we will further discuss textures and more advanced shaders. These can improve the 3D model effect for us.