Study of Qt 3D (vi): Toon Shader
In the last experiment, we implemented the Gooch Shader and gave the Gooch Shader settings that were personalized through the palette. Toon Shader, also known as cell Shader, is a discrete color to represent the intensity of the light. Many cartoons are rendered in a way that uses toon shader. The following diagram can be implemented using toon shader.
Saiyang original article, starting address: http://blog.csdn.net/gamesdev/article/details/44084021. Welcome to come to discuss the peer.
Here is the code for Toon Shader:
//Toon.vert# Version 100//Qt 3D default supplied parameters attribute vec3 vertexposition;attribute vec3 vertexnormal;uniform mat4 modelview;uniform mat4 m Odelnormalmatrix;uniform mat4 mvp;//Self-supplied parameters uniform vec3 lightposition;varying vec3 reflectvec;varying vec3 Viewvec; Varying float ndotl;void main (void) {VEC3 Ecpos = (Modelview * VEC4 (Vertexposition, 1.0)). xyz; VEC3 normal = normalize (Modelnormalmatrix * VEC4 (Vertexnormal, 1.0)). xyz; VEC3 Lightvec = normalize (Lightposition-ecpos); Reflectvec = normalize (reflect (-lightvec, normal)); Viewvec = normalize (-ecpos); Ndotl = (dot (lightvec, normal) + 1.0) * 0.5; gl_position = MVP * VEC4 (Vertexposition, 1.0);}
Toon.frag#version 100//Self-supplied parameters varying vec3 reflectvec;varying vec3 viewvec;varying float ndotl;uniform sampler2d TexP Alette;float graytexture (vec4 color) { return dot (color.xyz, VEC3 (0.299, 0.587, 0.114));} void Main (void) { Gl_fragcolor = texture2d (Texpalette, VEC2 (Ndotl, 1.0));}
The discrete textures we use are as follows:
The results of the program run are as follows:
Study of Qt 3D (vi): Toon Shader