webgl_materials_toon.html 5.1 KB

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