2
0

webgl_materials_variations_toon.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Toon Material Variantions with OutlineEffect</div>
  12. <script type="module">
  13. import {
  14. AmbientLight,
  15. Color,
  16. CubeTextureLoader,
  17. DirectionalLight,
  18. FontLoader,
  19. Mesh,
  20. MeshBasicMaterial,
  21. MeshToonMaterial,
  22. PerspectiveCamera,
  23. PointLight,
  24. RepeatWrapping,
  25. RGBFormat,
  26. Scene,
  27. SphereBufferGeometry,
  28. TextureLoader,
  29. TextBufferGeometry,
  30. Vector3,
  31. WebGLRenderer,
  32. } from "../build/three.module.js";
  33. import Stats from './jsm/libs/stats.module.js';
  34. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  35. import { OutlineEffect } from './jsm/effects/OutlineEffect.js';
  36. var container, stats;
  37. var camera, scene, renderer, effect;
  38. var particleLight;
  39. var loader = new FontLoader();
  40. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  41. init( font );
  42. animate();
  43. } );
  44. function init( font ) {
  45. container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. camera = new PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  48. camera.position.set( 0.0, 400, 400 * 3.5 );
  49. //
  50. var reflectionCube = new CubeTextureLoader()
  51. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  52. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  53. reflectionCube.format = RGBFormat;
  54. scene = new Scene();
  55. scene.background = reflectionCube;
  56. // Materials
  57. var imgTexture = new TextureLoader().load( "textures/planets/moon_1024.jpg" );
  58. imgTexture.wrapS = imgTexture.wrapT = RepeatWrapping;
  59. imgTexture.anisotropy = 16;
  60. imgTexture = null;
  61. var bumpScale = 1;
  62. var cubeWidth = 400;
  63. var numberOfSphersPerSide = 5;
  64. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  65. var stepSize = 1.0 / numberOfSphersPerSide;
  66. var geometry = new SphereBufferGeometry( sphereRadius, 32, 16 );
  67. for ( var alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  68. var specularShininess = Math.pow( 2, alpha * 10 );
  69. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  70. var specularColor = new Color( beta * 0.2, beta * 0.2, beta * 0.2 );
  71. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  72. // basic monochromatic energy preservation
  73. var diffuseColor = new Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  74. var material = new MeshToonMaterial( {
  75. map: imgTexture,
  76. bumpMap: imgTexture,
  77. bumpScale: bumpScale,
  78. color: diffuseColor,
  79. specular: specularColor,
  80. reflectivity: beta,
  81. shininess: specularShininess,
  82. envMap: alphaIndex % 2 === 0 ? null : reflectionCube
  83. } );
  84. var mesh = new Mesh( geometry, material );
  85. mesh.position.x = alpha * 400 - 200;
  86. mesh.position.y = beta * 400 - 200;
  87. mesh.position.z = gamma * 400 - 200;
  88. scene.add( mesh );
  89. }
  90. }
  91. }
  92. function addLabel( name, location ) {
  93. var textGeo = new TextBufferGeometry( name, {
  94. font: font,
  95. size: 20,
  96. height: 1,
  97. curveSegments: 1
  98. } );
  99. var textMaterial = new MeshBasicMaterial( { color: 0xffffff } );
  100. var textMesh = new Mesh( textGeo, textMaterial );
  101. textMesh.position.copy( location );
  102. scene.add( textMesh );
  103. }
  104. addLabel( "-shininess", new Vector3( - 350, 0, 0 ) );
  105. addLabel( "+shininess", new Vector3( 350, 0, 0 ) );
  106. addLabel( "-specular, -reflectivity", new Vector3( 0, - 300, 0 ) );
  107. addLabel( "+specular, +reflectivity", new Vector3( 0, 300, 0 ) );
  108. addLabel( "-diffuse", new Vector3( 0, 0, - 300 ) );
  109. addLabel( "+diffuse", new Vector3( 0, 0, 300 ) );
  110. particleLight = new Mesh( new SphereBufferGeometry( 4, 8, 8 ), new MeshBasicMaterial( { color: 0xffffff } ) );
  111. scene.add( particleLight );
  112. // Lights
  113. scene.add( new AmbientLight( 0x222222 ) );
  114. var directionalLight = new DirectionalLight( 0xffffff, 1 );
  115. directionalLight.position.set( 1, 1, 1 ).normalize();
  116. scene.add( directionalLight );
  117. var pointLight = new PointLight( 0xffffff, 2, 800 );
  118. particleLight.add( pointLight );
  119. //
  120. renderer = new WebGLRenderer( { antialias: true } );
  121. renderer.setPixelRatio( window.devicePixelRatio );
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. container.appendChild( renderer.domElement );
  124. renderer.gammaInput = true;
  125. renderer.gammaOutput = true;
  126. effect = new OutlineEffect( renderer );
  127. //
  128. stats = new Stats();
  129. container.appendChild( stats.dom );
  130. var controls = new OrbitControls( camera, renderer.domElement );
  131. window.addEventListener( 'resize', onWindowResize, false );
  132. }
  133. function onWindowResize() {
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. //
  139. function animate() {
  140. requestAnimationFrame( animate );
  141. stats.begin();
  142. render();
  143. stats.end();
  144. }
  145. function render() {
  146. var timer = Date.now() * 0.00025;
  147. //camera.position.x = Math.cos( timer ) * 800;
  148. //camera.position.z = Math.sin( timer ) * 800;
  149. camera.lookAt( scene.position );
  150. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  151. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  152. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  153. effect.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>