webgl_materials_variations_toon.html 5.3 KB

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