webgl_materials_variations_toon.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. var container, stats;
  18. var camera, scene, renderer, effect;
  19. var particleLight;
  20. var 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, 2000 );
  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. var imgTexture = new THREE.TextureLoader().load( "textures/planets/moon_1024.jpg" );
  35. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  36. imgTexture.encoding = THREE.sRGBEncoding;
  37. imgTexture.anisotropy = 16;
  38. imgTexture = null;
  39. var bumpScale = 1;
  40. var cubeWidth = 400;
  41. var numberOfSphersPerSide = 5;
  42. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  43. var stepSize = 1.0 / numberOfSphersPerSide;
  44. var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
  45. for ( var alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  46. var specularShininess = Math.pow( 2, alpha * 10 );
  47. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  48. var specularColor = new THREE.Color( beta * 0.2, beta * 0.2, beta * 0.2 );
  49. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  50. // basic monochromatic energy preservation
  51. var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  52. var material = new THREE.MeshToonMaterial( {
  53. map: imgTexture,
  54. bumpMap: imgTexture,
  55. bumpScale: bumpScale,
  56. color: diffuseColor,
  57. specular: specularColor,
  58. shininess: specularShininess,
  59. } );
  60. var mesh = new THREE.Mesh( geometry, material );
  61. mesh.position.x = alpha * 400 - 200;
  62. mesh.position.y = beta * 400 - 200;
  63. mesh.position.z = gamma * 400 - 200;
  64. scene.add( mesh );
  65. }
  66. }
  67. }
  68. function addLabel( name, location ) {
  69. var textGeo = new THREE.TextBufferGeometry( name, {
  70. font: font,
  71. size: 20,
  72. height: 1,
  73. curveSegments: 1
  74. } );
  75. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  76. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  77. textMesh.position.copy( location );
  78. scene.add( textMesh );
  79. }
  80. addLabel( "-shininess", new THREE.Vector3( - 350, 0, 0 ) );
  81. addLabel( "+shininess", new THREE.Vector3( 350, 0, 0 ) );
  82. addLabel( "-specular", new THREE.Vector3( 0, - 300, 0 ) );
  83. addLabel( "+specular", new THREE.Vector3( 0, 300, 0 ) );
  84. addLabel( "-diffuse", new THREE.Vector3( 0, 0, - 300 ) );
  85. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  86. particleLight = new THREE.Mesh(
  87. new THREE.SphereBufferGeometry( 4, 8, 8 ),
  88. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  89. );
  90. scene.add( particleLight );
  91. // Lights
  92. scene.add( new THREE.AmbientLight( 0x111111 ) );
  93. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  94. directionalLight.position.set( 1, 1, 1 ).normalize();
  95. scene.add( directionalLight );
  96. var pointLight = new THREE.PointLight( 0xffffff, 1, 800 );
  97. particleLight.add( pointLight );
  98. //
  99. renderer = new THREE.WebGLRenderer( { antialias: true } );
  100. renderer.setPixelRatio( window.devicePixelRatio );
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. container.appendChild( renderer.domElement );
  103. renderer.outputEncoding = THREE.sRGBEncoding;
  104. effect = new OutlineEffect( renderer );
  105. //
  106. stats = new Stats();
  107. container.appendChild( stats.dom );
  108. var controls = new OrbitControls( camera, renderer.domElement );
  109. window.addEventListener( 'resize', onWindowResize, false );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. //
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. stats.begin();
  120. render();
  121. stats.end();
  122. }
  123. function render() {
  124. var timer = Date.now() * 0.00025;
  125. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  126. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  127. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  128. effect.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>