webgl_materials_variations_toon.html 5.4 KB

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