webgl_materials_variations_toon.html 5.8 KB

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