webgl_materials_variations_toon.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // Materials
  36. const cubeWidth = 400;
  37. const numberOfSphersPerSide = 5;
  38. const sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  39. const stepSize = 1.0 / numberOfSphersPerSide;
  40. const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
  41. for ( let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  42. const colors = new Uint8Array( alphaIndex + 2 );
  43. for ( let c = 0; c <= colors.length; c ++ ) {
  44. colors[ c ] = ( c / colors.length ) * 256;
  45. }
  46. const gradientMap = new THREE.DataTexture( colors, colors.length, 1, THREE.LuminanceFormat );
  47. gradientMap.minFilter = THREE.NearestFilter;
  48. gradientMap.magFilter = THREE.NearestFilter;
  49. gradientMap.generateMipmaps = false;
  50. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  51. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  52. // basic monochromatic energy preservation
  53. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  54. const material = new THREE.MeshToonMaterial( {
  55. color: diffuseColor,
  56. gradientMap: gradientMap
  57. } );
  58. const mesh = new THREE.Mesh( geometry, material );
  59. mesh.position.x = alpha * 400 - 200;
  60. mesh.position.y = beta * 400 - 200;
  61. mesh.position.z = gamma * 400 - 200;
  62. scene.add( mesh );
  63. }
  64. }
  65. }
  66. function addLabel( name, location ) {
  67. const textGeo = new TextGeometry( name, {
  68. font: font,
  69. size: 20,
  70. height: 1,
  71. curveSegments: 1
  72. } );
  73. const textMaterial = new THREE.MeshBasicMaterial();
  74. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  75. textMesh.position.copy( location );
  76. scene.add( textMesh );
  77. }
  78. addLabel( "-gradientMap", new THREE.Vector3( - 350, 0, 0 ) );
  79. addLabel( "+gradientMap", new THREE.Vector3( 350, 0, 0 ) );
  80. addLabel( "-diffuse", new THREE.Vector3( 0, 0, - 300 ) );
  81. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  82. particleLight = new THREE.Mesh(
  83. new THREE.SphereGeometry( 4, 8, 8 ),
  84. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  85. );
  86. scene.add( particleLight );
  87. // Lights
  88. scene.add( new THREE.AmbientLight( 0x888888 ) );
  89. const pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  90. particleLight.add( pointLight );
  91. //
  92. renderer = new THREE.WebGLRenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. container.appendChild( renderer.domElement );
  96. renderer.outputEncoding = THREE.sRGBEncoding;
  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>