ToonShader.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ( function () {
  2. /**
  3. * Currently contains:
  4. *
  5. * toon1
  6. * toon2
  7. * hatching
  8. * dotted
  9. */
  10. const ToonShader1 = {
  11. uniforms: {
  12. 'uDirLightPos': {
  13. value: new THREE.Vector3()
  14. },
  15. 'uDirLightColor': {
  16. value: new THREE.Color( 0xeeeeee )
  17. },
  18. 'uAmbientLightColor': {
  19. value: new THREE.Color( 0x050505 )
  20. },
  21. 'uBaseColor': {
  22. value: new THREE.Color( 0xffffff )
  23. }
  24. },
  25. vertexShader:
  26. /* glsl */
  27. `
  28. varying vec3 vNormal;
  29. varying vec3 vRefract;
  30. void main() {
  31. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  32. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  33. vec3 worldNormal = normalize ( mat3( modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz ) * normal );
  34. vNormal = normalize( normalMatrix * normal );
  35. vec3 I = worldPosition.xyz - cameraPosition;
  36. vRefract = refract( normalize( I ), worldNormal, 1.02 );
  37. gl_Position = projectionMatrix * mvPosition;
  38. }`,
  39. fragmentShader:
  40. /* glsl */
  41. `
  42. uniform vec3 uBaseColor;
  43. uniform vec3 uDirLightPos;
  44. uniform vec3 uDirLightColor;
  45. uniform vec3 uAmbientLightColor;
  46. varying vec3 vNormal;
  47. varying vec3 vRefract;
  48. void main() {
  49. float directionalLightWeighting = max( dot( normalize( vNormal ), uDirLightPos ), 0.0);
  50. vec3 lightWeighting = uAmbientLightColor + uDirLightColor * directionalLightWeighting;
  51. float intensity = smoothstep( - 0.5, 1.0, pow( length(lightWeighting), 20.0 ) );
  52. intensity += length(lightWeighting) * 0.2;
  53. float cameraWeighting = dot( normalize( vNormal ), vRefract );
  54. intensity += pow( 1.0 - length( cameraWeighting ), 6.0 );
  55. intensity = intensity * 0.2 + 0.3;
  56. if ( intensity < 0.50 ) {
  57. gl_FragColor = vec4( 2.0 * intensity * uBaseColor, 1.0 );
  58. } else {
  59. gl_FragColor = vec4( 1.0 - 2.0 * ( 1.0 - intensity ) * ( 1.0 - uBaseColor ), 1.0 );
  60. }
  61. }`
  62. };
  63. const ToonShader2 = {
  64. uniforms: {
  65. 'uDirLightPos': {
  66. value: new THREE.Vector3()
  67. },
  68. 'uDirLightColor': {
  69. value: new THREE.Color( 0xeeeeee )
  70. },
  71. 'uAmbientLightColor': {
  72. value: new THREE.Color( 0x050505 )
  73. },
  74. 'uBaseColor': {
  75. value: new THREE.Color( 0xeeeeee )
  76. },
  77. 'uLineColor1': {
  78. value: new THREE.Color( 0x808080 )
  79. },
  80. 'uLineColor2': {
  81. value: new THREE.Color( 0x000000 )
  82. },
  83. 'uLineColor3': {
  84. value: new THREE.Color( 0x000000 )
  85. },
  86. 'uLineColor4': {
  87. value: new THREE.Color( 0x000000 )
  88. }
  89. },
  90. vertexShader:
  91. /* glsl */
  92. `
  93. varying vec3 vNormal;
  94. void main() {
  95. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  96. vNormal = normalize( normalMatrix * normal );
  97. }`,
  98. fragmentShader:
  99. /* glsl */
  100. `
  101. uniform vec3 uBaseColor;
  102. uniform vec3 uLineColor1;
  103. uniform vec3 uLineColor2;
  104. uniform vec3 uLineColor3;
  105. uniform vec3 uLineColor4;
  106. uniform vec3 uDirLightPos;
  107. uniform vec3 uDirLightColor;
  108. uniform vec3 uAmbientLightColor;
  109. varying vec3 vNormal;
  110. void main() {
  111. float camera = max( dot( normalize( vNormal ), vec3( 0.0, 0.0, 1.0 ) ), 0.4);
  112. float light = max( dot( normalize( vNormal ), uDirLightPos ), 0.0);
  113. gl_FragColor = vec4( uBaseColor, 1.0 );
  114. if ( length(uAmbientLightColor + uDirLightColor * light) < 1.00 ) {
  115. gl_FragColor *= vec4( uLineColor1, 1.0 );
  116. }
  117. if ( length(uAmbientLightColor + uDirLightColor * camera) < 0.50 ) {
  118. gl_FragColor *= vec4( uLineColor2, 1.0 );
  119. }
  120. }`
  121. };
  122. const ToonShaderHatching = {
  123. uniforms: {
  124. 'uDirLightPos': {
  125. value: new THREE.Vector3()
  126. },
  127. 'uDirLightColor': {
  128. value: new THREE.Color( 0xeeeeee )
  129. },
  130. 'uAmbientLightColor': {
  131. value: new THREE.Color( 0x050505 )
  132. },
  133. 'uBaseColor': {
  134. value: new THREE.Color( 0xffffff )
  135. },
  136. 'uLineColor1': {
  137. value: new THREE.Color( 0x000000 )
  138. },
  139. 'uLineColor2': {
  140. value: new THREE.Color( 0x000000 )
  141. },
  142. 'uLineColor3': {
  143. value: new THREE.Color( 0x000000 )
  144. },
  145. 'uLineColor4': {
  146. value: new THREE.Color( 0x000000 )
  147. }
  148. },
  149. vertexShader:
  150. /* glsl */
  151. `
  152. varying vec3 vNormal;
  153. void main() {
  154. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  155. vNormal = normalize( normalMatrix * normal );
  156. }`,
  157. fragmentShader:
  158. /* glsl */
  159. `
  160. uniform vec3 uBaseColor;
  161. uniform vec3 uLineColor1;
  162. uniform vec3 uLineColor2;
  163. uniform vec3 uLineColor3;
  164. uniform vec3 uLineColor4;
  165. uniform vec3 uDirLightPos;
  166. uniform vec3 uDirLightColor;
  167. uniform vec3 uAmbientLightColor;
  168. varying vec3 vNormal;
  169. void main() {
  170. float directionalLightWeighting = max( dot( normalize(vNormal), uDirLightPos ), 0.0);
  171. vec3 lightWeighting = uAmbientLightColor + uDirLightColor * directionalLightWeighting;
  172. gl_FragColor = vec4( uBaseColor, 1.0 );
  173. if ( length(lightWeighting) < 1.00 ) {
  174. if ( mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) {
  175. gl_FragColor = vec4( uLineColor1, 1.0 );
  176. }
  177. }
  178. if ( length(lightWeighting) < 0.75 ) {
  179. if (mod(gl_FragCoord.x - gl_FragCoord.y, 10.0) == 0.0) {
  180. gl_FragColor = vec4( uLineColor2, 1.0 );
  181. }
  182. }
  183. if ( length(lightWeighting) < 0.50 ) {
  184. if (mod(gl_FragCoord.x + gl_FragCoord.y - 5.0, 10.0) == 0.0) {
  185. gl_FragColor = vec4( uLineColor3, 1.0 );
  186. }
  187. }
  188. if ( length(lightWeighting) < 0.3465 ) {
  189. if (mod(gl_FragCoord.x - gl_FragCoord.y - 5.0, 10.0) == 0.0) {
  190. gl_FragColor = vec4( uLineColor4, 1.0 );
  191. }
  192. }
  193. }`
  194. };
  195. const ToonShaderDotted = {
  196. uniforms: {
  197. 'uDirLightPos': {
  198. value: new THREE.Vector3()
  199. },
  200. 'uDirLightColor': {
  201. value: new THREE.Color( 0xeeeeee )
  202. },
  203. 'uAmbientLightColor': {
  204. value: new THREE.Color( 0x050505 )
  205. },
  206. 'uBaseColor': {
  207. value: new THREE.Color( 0xffffff )
  208. },
  209. 'uLineColor1': {
  210. value: new THREE.Color( 0x000000 )
  211. }
  212. },
  213. vertexShader:
  214. /* glsl */
  215. `
  216. varying vec3 vNormal;
  217. void main() {
  218. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  219. vNormal = normalize( normalMatrix * normal );
  220. }`,
  221. fragmentShader:
  222. /* glsl */
  223. `
  224. uniform vec3 uBaseColor;
  225. uniform vec3 uLineColor1;
  226. uniform vec3 uLineColor2;
  227. uniform vec3 uLineColor3;
  228. uniform vec3 uLineColor4;
  229. uniform vec3 uDirLightPos;
  230. uniform vec3 uDirLightColor;
  231. uniform vec3 uAmbientLightColor;
  232. varying vec3 vNormal;
  233. void main() {
  234. float directionalLightWeighting = max( dot( normalize(vNormal), uDirLightPos ), 0.0);
  235. vec3 lightWeighting = uAmbientLightColor + uDirLightColor * directionalLightWeighting;
  236. gl_FragColor = vec4( uBaseColor, 1.0 );
  237. if ( length(lightWeighting) < 1.00 ) {
  238. if ( ( mod(gl_FragCoord.x, 4.001) + mod(gl_FragCoord.y, 4.0) ) > 6.00 ) {
  239. gl_FragColor = vec4( uLineColor1, 1.0 );
  240. }
  241. }
  242. if ( length(lightWeighting) < 0.50 ) {
  243. if ( ( mod(gl_FragCoord.x + 2.0, 4.001) + mod(gl_FragCoord.y + 2.0, 4.0) ) > 6.00 ) {
  244. gl_FragColor = vec4( uLineColor1, 1.0 );
  245. }
  246. }
  247. }`
  248. };
  249. THREE.ToonShader1 = ToonShader1;
  250. THREE.ToonShader2 = ToonShader2;
  251. THREE.ToonShaderDotted = ToonShaderDotted;
  252. THREE.ToonShaderHatching = ToonShaderHatching;
  253. } )();