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_ESprecision mediump float;#endifuniform 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; }
下面是效果图: 未完待续 总结
以上是内存溢出为你收集整理的cocos2dx Material System讲解一全部内容,希望文章能够帮你解决cocos2dx Material System讲解一所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)