cocos2dx Material System讲解一

前端之家收集整理的这篇文章主要介绍了cocos2dx Material System讲解一前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2dx 增加了材料系统,因为之前我深入研究过RenderMonkey这个软件,又深入学习了OpenGl es2.0,所以理解起来还是挺轻松的。但是有些写法还是不太一样,所以在这里记下,和大家分享一下。

首先,我们先看一个material:

material mf
{

technique m0
   {
   pass 0
   {
       renderState
       {
           cullFace=true
           depthTest=true
       }
       shader
       {
           vertexShader=shaders/molecular_formula.vert
           fragmentShader=shaders/molecular_formula.frag
           u_lightLocation=0,0
           u_Camera=0,0
           uColorR=0.1
           uColorG=0.6
           uColorB=0.9
           uColorA=0.0
		   transparent=0.0
       }
   }
   }

   technique m1
   {
   pass 1
   {
       renderState
       {
           cullFace=true
           depthTest=true
       }
       shader
       {
           vertexShader=shaders/molecular_formula.vert
           fragmentShader=shaders/molecular_formula.frag
           u_lightLocation=0,0
           uColorR=0.1
           uColorG=0.6
           uColorB=0.9
           uColorA=1.0
		   transparent=0.7
       }
   }
   }

 technique m2
   {
   pass 2
   {
       renderState
       {
           cullFace=true
           depthTest=true
       }
       shader
       {
           vertexShader=shaders/molecular_formula.vert
           fragmentShader=shaders/molecular_formula.frag
           u_lightLocation=0,-20
           u_Camera=0,10
           uColorR=0.95
           uColorG=0.95
           uColorB=0.10
           uColorA=1.0
		   transparent=0.7
       }
   }
   }

    technique m3
   {
   pass 3
   {
       renderState
       {
           cullFace=true
           depthTest=true
       }
       shader
       {
           vertexShader=shaders/molecular_formula.vert
           fragmentShader=shaders/molecular_formula.frag
           u_lightLocation=10,100
           uColorR=0.95
           uColorG=0.45
           uColorB=0.25
           uColorA=1.0
		   transparent=0.7
       }
   }
   }

    technique m4
   {
   pass 1
   {
       renderState
       {
           cullFace=true
           depthTest=true
       }
       shader
       {
           vertexShader=shaders/molecular_formula.vert
           fragmentShader=shaders/molecular_formula.frag
           u_lightLocation=-100,100
           u_Camera=100,50
           uColorR=0.1
           uColorG=0.6
           uColorB=0.9
           uColorA=1.0
		   transparent=0.7
       }
   }
   }

}
molecular_formula.vert

attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord;

uniform vec3 u_lightLocation;
uniform vec3 u_Camera;

varying vec2 v_texture_coord;
varying vec4 v_ambient;
varying vec4 v_diffuse;
varying vec4 v_specular;

void pointLight(
  in vec3 normal,inout vec4 ambient,inout vec4 diffuse,inout vec4 specular,in vec3 lightLocation,in vec4 lightAmbient,in vec4 lightDiffuse,in vec4 lightSpecular
 ){
  ambient=lightAmbient;
  vec3 normalTarget= a_position.xyz+normal;
  vec3 newNormal=(CC_MVMatrix*vec4(normalTarget,1)).xyz-(CC_MVMatrix*a_position).xyz;
  newNormal=normalize(newNormal);
  vec3 eye=normalize(u_Camera-(CC_MVMatrix*a_position).xyz);
  vec3 vp=normalize(lightLocation-(CC_MVMatrix*a_position).xyz);
  vp=normalize(vp);
  vec3 halfVector=normalize(vp+eye);
  float shininess=50.0;
  float nDotViewPosition=max(0.0,dot(newNormal,vp));
  diffuse=lightDiffuse*nDotViewPosition;
  float nDotViewHalfVector=dot(newNormal,halfVector);
  float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess));
  specular=lightSpecular*powerFactor;
  }

void main(void)
{
    gl_Position = CC_MVPMatrix * a_position;
    
    vec4 ambientTemp,diffuseTemp,specularTemp;
    
    pointLight(normalize(a_normal),ambientTemp,specularTemp,u_lightLocation,vec4(0.8,0.8,1.0),vec4(0.9,0.9,1.0));
    
    v_ambient=ambientTemp;
    v_diffuse=diffuseTemp;
    v_specular=specularTemp;
    
    v_texture_coord = a_texCoord;
    v_texture_coord.y = (1.0 - v_texture_coord.y);
}
molecular_formula.frag

#ifdef GL_ES
precision mediump float;
#endif

uniform vec4  u_color;
uniform float uColorR;
uniform float uColorG;
uniform float uColorB;
uniform float uColorA;
uniform float transparent;

varying vec4 v_ambient;
varying vec4 v_diffuse;
varying vec4 v_specular;

void main(void)
{
	 vec4 finalColor =vec4(uColorR,uColorG,uColorB,uColorA);
	 vec4 color=finalColor*v_ambient+finalColor*v_diffuse+finalColor*v_specular;
     gl_FragColor=color*u_color*transparent;
    
}

下面是效果图:
未完待续
原文链接:https://www.f2er.com/cocos2dx/341005.html

猜你在找的Cocos2d-x相关文章