webgl_materials_variations_toon.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Toon Material Variantions with OutlineEffect</div>
  12. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { OutlineEffect } from './jsm/effects/OutlineEffect.js';
  17. import { FontLoader } from './jsm/loaders/FontLoader.js';
  18. import { TextGeometry } from './jsm/geometries/TextGeometry.js';
  19. let container, stats;
  20. let camera, scene, renderer, effect;
  21. let particleLight;
  22. const loader = new FontLoader();
  23. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  24. init( font );
  25. animate();
  26. } );
  27. function init( font ) {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2500 );
  31. camera.position.set( 0.0, 400, 400 * 3.5 );
  32. //
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x444488 );
  35. //
  36. renderer = new THREE.WebGLRenderer( { antialias: true } );
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. container.appendChild( renderer.domElement );
  40. renderer.outputEncoding = THREE.sRGBEncoding;
  41. // Materials
  42. const cubeWidth = 400;
  43. const numberOfSphersPerSide = 5;
  44. const sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  45. const stepSize = 1.0 / numberOfSphersPerSide;
  46. const format = ( renderer.capabilities.isWebGL2 ) ? THREE.RedFormat : THREE.LuminanceFormat;
  47. const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
  48. for ( let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  49. const colors = new Uint8Array( alphaIndex + 2 );
  50. for ( let c = 0; c <= colors.length; c ++ ) {
  51. colors[ c ] = ( c / colors.length ) * 256;
  52. }
  53. const gradientMap = new THREE.DataTexture( colors, colors.length, 1, format );
  54. gradientMap.needsUpdate = true;
  55. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  56. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  57. // basic monochromatic energy preservation
  58. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  59. const material = new THREE.MeshToonMaterial( {
  60. color: diffuseColor,
  61. gradientMap: gradientMap
  62. } );
  63. const mesh = new THREE.Mesh( geometry, material );
  64. mesh.position.x = alpha * 400 - 200;
  65. mesh.position.y = beta * 400 - 200;
  66. mesh.position.z = gamma * 400 - 200;
  67. scene.add( mesh );
  68. }
  69. }
  70. }
  71. function addLabel( name, location ) {
  72. const textGeo = new TextGeometry( name, {
  73. font: font,
  74. size: 20,
  75. height: 1,
  76. curveSegments: 1
  77. } );
  78. const textMaterial = new THREE.MeshBasicMaterial();
  79. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  80. textMesh.position.copy( location );
  81. scene.add( textMesh );
  82. }
  83. addLabel( "-gradientMap", new THREE.Vector3( - 350, 0, 0 ) );
  84. addLabel( "+gradientMap", new THREE.Vector3( 350, 0, 0 ) );
  85. addLabel( "-diffuse", new THREE.Vector3( 0, 0, - 300 ) );
  86. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  87. particleLight = new THREE.Mesh(
  88. new THREE.SphereGeometry( 4, 8, 8 ),
  89. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  90. );
  91. scene.add( particleLight );
  92. // Lights
  93. scene.add( new THREE.AmbientLight( 0x888888 ) );
  94. const pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  95. particleLight.add( pointLight );
  96. //
  97. effect = new OutlineEffect( renderer );
  98. //
  99. stats = new Stats();
  100. container.appendChild( stats.dom );
  101. const controls = new OrbitControls( camera, renderer.domElement );
  102. controls.minDistance = 200;
  103. controls.maxDistance = 2000;
  104. window.addEventListener( 'resize', onWindowResize );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. //
  112. function animate() {
  113. requestAnimationFrame( animate );
  114. stats.begin();
  115. render();
  116. stats.end();
  117. }
  118. function render() {
  119. const timer = Date.now() * 0.00025;
  120. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  121. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  122. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  123. effect.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>