1、创建材质
2、设定效果
3、创建光源的位置
4、设置材质环境光反射系数
5、设定材质光线漫反射系数
6、设置材质光线反射系数
7、设定材质光洁度
/*加载效果文件,创建材质*/
var effect = g_pack.createObject('Effect');
effect.loadFromFXString(document.getElementById('shader').value);
var myMaterial = g_pack.createObject('Material');
myMaterial.effect = effect;
var light_pos_param = myMaterial.getParam('light_pos');//光源的位置
light_pos_param.value = [10, 10, 20];
var light_ambient_param = myMaterial.getParam('light_ambient');//环境光反射系数
light_ambient_param.value = [0.04, 0.04, 0.04, 1];
var light_diffuse_param = myMaterial.getParam('light_diffuse');//漫反射系数
light_diffuse_param.value = [0.8, 0, 0, 1];
var light_specular_param = myMaterial.getParam('light_specular');//反射系数
light_specular_param.value = [0.5, 0.5, 0.5, 1];
var shininess_param = myMaterial.getParam('shininess');//光洁度
shininess_param.value = 30.0;
//------------------效果文件'shader'--------------------/
<textarea id="shader" name="shader" cols="80" rows="20"
style="display: none;">
// The 4x4 world view projection matrix.
float4x4 worldViewProjection : WorldViewProjection;
// positions of the light and camera
float3 light_pos;
float3 camera_pos;
// phong lighting components of the light source
float4 light_ambient;
float4 light_diffuse;
float4 light_specular;
// shininess of the material. (for specular lighting)
float shininess;
// input parameters for our vertex shader
struct VertexShaderInput {
float4 postion : POSITION;
float3 normal : NORMAL;
float4 color : COLOR;
};
// input parameters for our pixel shader
// also the output parameters for our vertex shader
struct PixelShaderInput {
float4 postion : POSITION;
float3 lightVector : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 viewPosition : TEXCOORD2;
float4 color : COLOR;
};
/**
* Vertex Shader - vertex shader for phong illumination
*/
PixelShaderInput vertexShaderFunction(VertexShaderInput input) {
/**
* We use the standard phong illumination equation here.
* We restrict (clamp) the dot products so that we
* don't get any negative values.
* All vectors are normalized for proper calculations.
*
* The output color is the summation of the
* ambient, diffuse, and specular contributions.
*
* Note that we have to transform each vertex and normal
* by the world view projection matrix first.
*/
PixelShaderInput output;
output.postion = mul(input.postion, worldViewProjection);
/**
* lightVector - light vector
* normal - normal vector
* viewPosition - view vector (from camera)
*/
// NOTE: In this case we do not need to multiply by any matrices since the
// WORLD transformation matrix is the identity. If you were moving the
// object such that the WORLD transform matrix was not the identity, you
// would need to multiply the normal by the WORLDINVERSETTRANSFORM matrix
// since the normal is in object space. Other values (light_pos, camera_pos)
// are already in world space.
float3 lightVector = light_pos - input.postion.xyz;//计算光方向向量和长度
float3 normal = input.normal;
float3 viewPosition = camera_pos - input.postion.xyz;//计算照相机相对顶点的向量
output.lightVector = lightVector;
output.normal = normal;
output.viewPosition = viewPosition;
output.color = input.color;
return output;
}
/**
* Pixel Shader
*/
float4 pixelShaderFunction(PixelShaderInput input): COLOR {
float3 lightVector = normalize(input.lightVector);
float3 normal = normalize(input.normal);
float3 viewPosition = normalize(input.viewPosition);
float3 halfVector = normalize(lightVector + viewPosition);
// use lit function to calculate phong shading
// x component contains the ambient coefficient
// y component contains the diffuse coefficient:
// max(dot(normal, lightVector),0)
// z component contains the specular coefficient:
// dot(normal, lightVector) < 0 || dot(normal, halfVector) < 0 ?
// 0 : pow(dot(normal, halfVector), shininess)
// NOTE: This is actually Blinn-Phong shading, not Phong shading
// which would use the reflection vector instead of the half vector
float4 phong_coeff = lit(dot(normal, lightVector), dot(normal, halfVector), shininess);//lit光的计算函数
float4 ambient = light_ambient * phong_coeff.x * input.color;//环境光分量×颜色值
float4 diffuse = light_diffuse * phong_coeff.y * input.color;//漫反射分量X颜色值
float4 specular = light_specular * phong_coeff.z * input.color;//反色分量X颜色值
return ambient + diffuse + specular;
}
// Here we tell our effect file *which* functions are
// our vertex and pixel shaders.
// #o3d VertexShaderEntryPoint vertexShaderFunction
// #o3d PixelShaderEntryPoint pixelShaderFunction
// #o3d MatrixLoadOrder RowMajor
</textarea>
2、设定效果
3、创建光源的位置
4、设置材质环境光反射系数
5、设定材质光线漫反射系数
6、设置材质光线反射系数
7、设定材质光洁度
/*加载效果文件,创建材质*/
var effect = g_pack.createObject('Effect');
effect.loadFromFXString(document.getElementById('shader').value);
var myMaterial = g_pack.createObject('Material');
myMaterial.effect = effect;
var light_pos_param = myMaterial.getParam('light_pos');//光源的位置
light_pos_param.value = [10, 10, 20];
var light_ambient_param = myMaterial.getParam('light_ambient');//环境光反射系数
light_ambient_param.value = [0.04, 0.04, 0.04, 1];
var light_diffuse_param = myMaterial.getParam('light_diffuse');//漫反射系数
light_diffuse_param.value = [0.8, 0, 0, 1];
var light_specular_param = myMaterial.getParam('light_specular');//反射系数
light_specular_param.value = [0.5, 0.5, 0.5, 1];
var shininess_param = myMaterial.getParam('shininess');//光洁度
shininess_param.value = 30.0;
//------------------效果文件'shader'--------------------/
<textarea id="shader" name="shader" cols="80" rows="20"
style="display: none;">
// The 4x4 world view projection matrix.
float4x4 worldViewProjection : WorldViewProjection;
// positions of the light and camera
float3 light_pos;
float3 camera_pos;
// phong lighting components of the light source
float4 light_ambient;
float4 light_diffuse;
float4 light_specular;
// shininess of the material. (for specular lighting)
float shininess;
// input parameters for our vertex shader
struct VertexShaderInput {
float4 postion : POSITION;
float3 normal : NORMAL;
float4 color : COLOR;
};
// input parameters for our pixel shader
// also the output parameters for our vertex shader
struct PixelShaderInput {
float4 postion : POSITION;
float3 lightVector : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 viewPosition : TEXCOORD2;
float4 color : COLOR;
};
/**
* Vertex Shader - vertex shader for phong illumination
*/
PixelShaderInput vertexShaderFunction(VertexShaderInput input) {
/**
* We use the standard phong illumination equation here.
* We restrict (clamp) the dot products so that we
* don't get any negative values.
* All vectors are normalized for proper calculations.
*
* The output color is the summation of the
* ambient, diffuse, and specular contributions.
*
* Note that we have to transform each vertex and normal
* by the world view projection matrix first.
*/
PixelShaderInput output;
output.postion = mul(input.postion, worldViewProjection);
/**
* lightVector - light vector
* normal - normal vector
* viewPosition - view vector (from camera)
*/
// NOTE: In this case we do not need to multiply by any matrices since the
// WORLD transformation matrix is the identity. If you were moving the
// object such that the WORLD transform matrix was not the identity, you
// would need to multiply the normal by the WORLDINVERSETTRANSFORM matrix
// since the normal is in object space. Other values (light_pos, camera_pos)
// are already in world space.
float3 lightVector = light_pos - input.postion.xyz;//计算光方向向量和长度
float3 normal = input.normal;
float3 viewPosition = camera_pos - input.postion.xyz;//计算照相机相对顶点的向量
output.lightVector = lightVector;
output.normal = normal;
output.viewPosition = viewPosition;
output.color = input.color;
return output;
}
/**
* Pixel Shader
*/
float4 pixelShaderFunction(PixelShaderInput input): COLOR {
float3 lightVector = normalize(input.lightVector);
float3 normal = normalize(input.normal);
float3 viewPosition = normalize(input.viewPosition);
float3 halfVector = normalize(lightVector + viewPosition);
// use lit function to calculate phong shading
// x component contains the ambient coefficient
// y component contains the diffuse coefficient:
// max(dot(normal, lightVector),0)
// z component contains the specular coefficient:
// dot(normal, lightVector) < 0 || dot(normal, halfVector) < 0 ?
// 0 : pow(dot(normal, halfVector), shininess)
// NOTE: This is actually Blinn-Phong shading, not Phong shading
// which would use the reflection vector instead of the half vector
float4 phong_coeff = lit(dot(normal, lightVector), dot(normal, halfVector), shininess);//lit光的计算函数
float4 ambient = light_ambient * phong_coeff.x * input.color;//环境光分量×颜色值
float4 diffuse = light_diffuse * phong_coeff.y * input.color;//漫反射分量X颜色值
float4 specular = light_specular * phong_coeff.z * input.color;//反色分量X颜色值
return ambient + diffuse + specular;
}
// Here we tell our effect file *which* functions are
// our vertex and pixel shaders.
// #o3d VertexShaderEntryPoint vertexShaderFunction
// #o3d PixelShaderEntryPoint pixelShaderFunction
// #o3d MatrixLoadOrder RowMajor
</textarea>