webgl_materials_variations_toon.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. let container, stats;
  18. let camera, scene, renderer, effect;
  19. let particleLight;
  20. const loader = new THREE.FontLoader();
  21. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  22. init( font );
  23. animate();
  24. } );
  25. function init( font ) {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2500 );
  29. camera.position.set( 0.0, 400, 400 * 3.5 );
  30. //
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0x444488 );
  33. // Materials
  34. const cubeWidth = 400;
  35. const numberOfSphersPerSide = 5;
  36. const sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  37. const stepSize = 1.0 / numberOfSphersPerSide;
  38. const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
  39. for ( let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  40. const colors = new Uint8Array( alphaIndex + 2 );
  41. for ( let c = 0; c <= colors.length; c ++ ) {
  42. colors[ c ] = ( c / colors.length ) * 256;
  43. }
  44. const gradientMap = new THREE.DataTexture( colors, colors.length, 1, THREE.LuminanceFormat );
  45. gradientMap.minFilter = THREE.NearestFilter;
  46. gradientMap.magFilter = THREE.NearestFilter;
  47. gradientMap.generateMipmaps = false;
  48. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  49. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  50. // basic monochromatic energy preservation
  51. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  52. const material = new THREE.MeshToonMaterial( {
  53. color: diffuseColor,
  54. gradientMap: gradientMap
  55. } );
  56. const mesh = new THREE.Mesh( geometry, material );
  57. mesh.position.x = alpha * 400 - 200;
  58. mesh.position.y = beta * 400 - 200;
  59. mesh.position.z = gamma * 400 - 200;
  60. scene.add( mesh );
  61. }
  62. }
  63. }
  64. function addLabel( name, location ) {
  65. const textGeo = new THREE.TextGeometry( name, {
  66. font: font,
  67. size: 20,
  68. height: 1,
  69. curveSegments: 1
  70. } );
  71. const textMaterial = new THREE.MeshBasicMaterial();
  72. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  73. textMesh.position.copy( location );
  74. scene.add( textMesh );
  75. }
  76. addLabel( "-gradientMap", new THREE.Vector3( - 350, 0, 0 ) );
  77. addLabel( "+gradientMap", new THREE.Vector3( 350, 0, 0 ) );
  78. addLabel( "-diffuse", new THREE.Vector3( 0, 0, - 300 ) );
  79. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  80. particleLight = new THREE.Mesh(
  81. new THREE.SphereGeometry( 4, 8, 8 ),
  82. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  83. );
  84. scene.add( particleLight );
  85. // Lights
  86. scene.add( new THREE.AmbientLight( 0x888888 ) );
  87. const pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  88. particleLight.add( pointLight );
  89. //
  90. renderer = new THREE.WebGLRenderer( { antialias: true } );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. container.appendChild( renderer.domElement );
  94. renderer.outputEncoding = THREE.sRGBEncoding;
  95. effect = new OutlineEffect( renderer );
  96. //
  97. stats = new Stats();
  98. container.appendChild( stats.dom );
  99. const controls = new OrbitControls( camera, renderer.domElement );
  100. controls.minDistance = 200;
  101. controls.maxDistance = 2000;
  102. window.addEventListener( 'resize', onWindowResize );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. //
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. stats.begin();
  113. render();
  114. stats.end();
  115. }
  116. function render() {
  117. const timer = Date.now() * 0.00025;
  118. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  119. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  120. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  121. effect.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>