webgl_materials_variations_toon.html 6.2 KB

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