ShaderToon.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. *
  5. * ShaderToon currently contains:
  6. *
  7. * toon1
  8. * toon2
  9. * hatching
  10. * dotted
  11. */
  12. THREE.ShaderToon = {
  13. 'toon1' : {
  14. uniforms: {
  15. "uDirLightPos": { value: new THREE.Vector3() },
  16. "uDirLightColor": { value: new THREE.Color( 0xeeeeee ) },
  17. "uAmbientLightColor": { value: new THREE.Color( 0x050505 ) },
  18. "uBaseColor": { value: new THREE.Color( 0xffffff ) }
  19. },
  20. vertexShader: [
  21. "varying vec3 vNormal;",
  22. "varying vec3 vRefract;",
  23. "void main() {",
  24. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  25. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  26. "vec3 worldNormal = normalize ( mat3( modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz ) * normal );",
  27. "vNormal = normalize( normalMatrix * normal );",
  28. "vec3 I = worldPosition.xyz - cameraPosition;",
  29. "vRefract = refract( normalize( I ), worldNormal, 1.02 );",
  30. "gl_Position = projectionMatrix * mvPosition;",
  31. "}"
  32. ].join( "\n" ),
  33. fragmentShader: [
  34. "uniform vec3 uBaseColor;",
  35. "uniform vec3 uDirLightPos;",
  36. "uniform vec3 uDirLightColor;",
  37. "uniform vec3 uAmbientLightColor;",
  38. "varying vec3 vNormal;",
  39. "varying vec3 vRefract;",
  40. "void main() {",
  41. "float directionalLightWeighting = max( dot( normalize( vNormal ), uDirLightPos ), 0.0);",
  42. "vec3 lightWeighting = uAmbientLightColor + uDirLightColor * directionalLightWeighting;",
  43. "float intensity = smoothstep( - 0.5, 1.0, pow( length(lightWeighting), 20.0 ) );",
  44. "intensity += length(lightWeighting) * 0.2;",
  45. "float cameraWeighting = dot( normalize( vNormal ), vRefract );",
  46. "intensity += pow( 1.0 - length( cameraWeighting ), 6.0 );",
  47. "intensity = intensity * 0.2 + 0.3;",
  48. "if ( intensity < 0.50 ) {",
  49. "gl_FragColor = vec4( 2.0 * intensity * uBaseColor, 1.0 );",
  50. "} else {",
  51. "gl_FragColor = vec4( 1.0 - 2.0 * ( 1.0 - intensity ) * ( 1.0 - uBaseColor ), 1.0 );",
  52. "}",
  53. "}"
  54. ].join( "\n" )
  55. },
  56. 'toon2' : {
  57. uniforms: {
  58. "uDirLightPos": { value: new THREE.Vector3() },
  59. "uDirLightColor": { value: new THREE.Color( 0xeeeeee ) },
  60. "uAmbientLightColor": { value: new THREE.Color( 0x050505 ) },
  61. "uBaseColor": { value: new THREE.Color( 0xeeeeee ) },
  62. "uLineColor1": { value: new THREE.Color( 0x808080 ) },
  63. "uLineColor2": { value: new THREE.Color( 0x000000 ) },
  64. "uLineColor3": { value: new THREE.Color( 0x000000 ) },
  65. "uLineColor4": { value: new THREE.Color( 0x000000 ) }
  66. },
  67. vertexShader: [
  68. "varying vec3 vNormal;",
  69. "void main() {",
  70. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  71. "vNormal = normalize( normalMatrix * normal );",
  72. "}"
  73. ].join( "\n" ),
  74. fragmentShader: [
  75. "uniform vec3 uBaseColor;",
  76. "uniform vec3 uLineColor1;",
  77. "uniform vec3 uLineColor2;",
  78. "uniform vec3 uLineColor3;",
  79. "uniform vec3 uLineColor4;",
  80. "uniform vec3 uDirLightPos;",
  81. "uniform vec3 uDirLightColor;",
  82. "uniform vec3 uAmbientLightColor;",
  83. "varying vec3 vNormal;",
  84. "void main() {",
  85. "float camera = max( dot( normalize( vNormal ), vec3( 0.0, 0.0, 1.0 ) ), 0.4);",
  86. "float light = max( dot( normalize( vNormal ), uDirLightPos ), 0.0);",
  87. "gl_FragColor = vec4( uBaseColor, 1.0 );",
  88. "if ( length(uAmbientLightColor + uDirLightColor * light) < 1.00 ) {",
  89. "gl_FragColor *= vec4( uLineColor1, 1.0 );",
  90. "}",
  91. "if ( length(uAmbientLightColor + uDirLightColor * camera) < 0.50 ) {",
  92. "gl_FragColor *= vec4( uLineColor2, 1.0 );",
  93. "}",
  94. "}"
  95. ].join( "\n" )
  96. },
  97. 'hatching' : {
  98. uniforms: {
  99. "uDirLightPos": { value: new THREE.Vector3() },
  100. "uDirLightColor": { value: new THREE.Color( 0xeeeeee ) },
  101. "uAmbientLightColor": { value: new THREE.Color( 0x050505 ) },
  102. "uBaseColor": { value: new THREE.Color( 0xffffff ) },
  103. "uLineColor1": { value: new THREE.Color( 0x000000 ) },
  104. "uLineColor2": { value: new THREE.Color( 0x000000 ) },
  105. "uLineColor3": { value: new THREE.Color( 0x000000 ) },
  106. "uLineColor4": { value: new THREE.Color( 0x000000 ) }
  107. },
  108. vertexShader: [
  109. "varying vec3 vNormal;",
  110. "void main() {",
  111. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  112. "vNormal = normalize( normalMatrix * normal );",
  113. "}"
  114. ].join( "\n" ),
  115. fragmentShader: [
  116. "uniform vec3 uBaseColor;",
  117. "uniform vec3 uLineColor1;",
  118. "uniform vec3 uLineColor2;",
  119. "uniform vec3 uLineColor3;",
  120. "uniform vec3 uLineColor4;",
  121. "uniform vec3 uDirLightPos;",
  122. "uniform vec3 uDirLightColor;",
  123. "uniform vec3 uAmbientLightColor;",
  124. "varying vec3 vNormal;",
  125. "void main() {",
  126. "float directionalLightWeighting = max( dot( normalize(vNormal), uDirLightPos ), 0.0);",
  127. "vec3 lightWeighting = uAmbientLightColor + uDirLightColor * directionalLightWeighting;",
  128. "gl_FragColor = vec4( uBaseColor, 1.0 );",
  129. "if ( length(lightWeighting) < 1.00 ) {",
  130. "if ( mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) {",
  131. "gl_FragColor = vec4( uLineColor1, 1.0 );",
  132. "}",
  133. "}",
  134. "if ( length(lightWeighting) < 0.75 ) {",
  135. "if (mod(gl_FragCoord.x - gl_FragCoord.y, 10.0) == 0.0) {",
  136. "gl_FragColor = vec4( uLineColor2, 1.0 );",
  137. "}",
  138. "}",
  139. "if ( length(lightWeighting) < 0.50 ) {",
  140. "if (mod(gl_FragCoord.x + gl_FragCoord.y - 5.0, 10.0) == 0.0) {",
  141. "gl_FragColor = vec4( uLineColor3, 1.0 );",
  142. "}",
  143. "}",
  144. "if ( length(lightWeighting) < 0.3465 ) {",
  145. "if (mod(gl_FragCoord.x - gl_FragCoord.y - 5.0, 10.0) == 0.0) {",
  146. "gl_FragColor = vec4( uLineColor4, 1.0 );",
  147. "}",
  148. "}",
  149. "}"
  150. ].join( "\n" )
  151. },
  152. 'dotted' : {
  153. uniforms: {
  154. "uDirLightPos": { value: new THREE.Vector3() },
  155. "uDirLightColor": { value: new THREE.Color( 0xeeeeee ) },
  156. "uAmbientLightColor": { value: new THREE.Color( 0x050505 ) },
  157. "uBaseColor": { value: new THREE.Color( 0xffffff ) },
  158. "uLineColor1": { value: new THREE.Color( 0x000000 ) }
  159. },
  160. vertexShader: [
  161. "varying vec3 vNormal;",
  162. "void main() {",
  163. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  164. "vNormal = normalize( normalMatrix * normal );",
  165. "}"
  166. ].join( "\n" ),
  167. fragmentShader: [
  168. "uniform vec3 uBaseColor;",
  169. "uniform vec3 uLineColor1;",
  170. "uniform vec3 uLineColor2;",
  171. "uniform vec3 uLineColor3;",
  172. "uniform vec3 uLineColor4;",
  173. "uniform vec3 uDirLightPos;",
  174. "uniform vec3 uDirLightColor;",
  175. "uniform vec3 uAmbientLightColor;",
  176. "varying vec3 vNormal;",
  177. "void main() {",
  178. "float directionalLightWeighting = max( dot( normalize(vNormal), uDirLightPos ), 0.0);",
  179. "vec3 lightWeighting = uAmbientLightColor + uDirLightColor * directionalLightWeighting;",
  180. "gl_FragColor = vec4( uBaseColor, 1.0 );",
  181. "if ( length(lightWeighting) < 1.00 ) {",
  182. "if ( ( mod(gl_FragCoord.x, 4.001) + mod(gl_FragCoord.y, 4.0) ) > 6.00 ) {",
  183. "gl_FragColor = vec4( uLineColor1, 1.0 );",
  184. "}",
  185. "}",
  186. "if ( length(lightWeighting) < 0.50 ) {",
  187. "if ( ( mod(gl_FragCoord.x + 2.0, 4.001) + mod(gl_FragCoord.y + 2.0, 4.0) ) > 6.00 ) {",
  188. "gl_FragColor = vec4( uLineColor1, 1.0 );",
  189. "}",
  190. "}",
  191. "}"
  192. ].join( "\n" )
  193. }
  194. };