webgl_materials_variations_physical.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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> - Physical Material Variations 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/Detector.js"></script>
  30. <script src="js/libs/stats.min.js"></script>
  31. <script>
  32. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  33. var container, stats;
  34. var camera, scene, renderer, controls;
  35. var particleLight;
  36. var loader = new THREE.FontLoader();
  37. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  38. init( font );
  39. animate();
  40. } );
  41. function init( font ) {
  42. container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  45. camera.position.set( 0.0, 400, 400 * 3.5 );
  46. //
  47. var reflectionCube = new THREE.CubeTextureLoader()
  48. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  49. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  50. reflectionCube.format = THREE.RGBFormat;
  51. scene = new THREE.Scene();
  52. scene.background = reflectionCube;
  53. // Materials
  54. var cubeWidth = 400;
  55. var numberOfSphersPerSide = 5;
  56. var sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  57. var stepSize = 1.0 / numberOfSphersPerSide;
  58. var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );
  59. for ( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
  60. for ( var beta = 0; beta <= 1.0; beta += stepSize ) {
  61. for ( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  62. var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, 0.5 );
  63. var material = new THREE.MeshPhysicalMaterial( {
  64. color: diffuseColor,
  65. metalness: 0,
  66. roughness: 0.5,
  67. clearCoat: 1.0 - alpha,
  68. clearCoatRoughness: 1.0 - beta,
  69. reflectivity: 1.0 - gamma,
  70. envMap: reflectionCube
  71. } );
  72. var mesh = new THREE.Mesh( geometry, material );
  73. mesh.position.x = alpha * 400 - 200;
  74. mesh.position.y = beta * 400 - 200;
  75. mesh.position.z = gamma * 400 - 200;
  76. scene.add( mesh );
  77. }
  78. }
  79. }
  80. function addLabel( name, location ) {
  81. var textGeo = new THREE.TextGeometry( name, {
  82. font: font,
  83. size: 20,
  84. height: 1,
  85. curveSegments: 1
  86. });
  87. var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  88. var textMesh = new THREE.Mesh( textGeo, textMaterial );
  89. textMesh.position.copy( location );
  90. scene.add( textMesh );
  91. }
  92. addLabel( "+clearCoat", new THREE.Vector3( -350, 0, 0 ) );
  93. addLabel( "-clearCoat", new THREE.Vector3( 350, 0, 0 ) );
  94. addLabel( "+clearCoatRoughness", new THREE.Vector3( 0, -300, 0 ) );
  95. addLabel( "-clearCoatRoughness", new THREE.Vector3( 0, 300, 0 ) );
  96. addLabel( "+reflectivity", new THREE.Vector3( 0, 0, -300 ) );
  97. addLabel( "-reflectivity", new THREE.Vector3( 0, 0, 300 ) );
  98. particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  99. scene.add( particleLight );
  100. // Lights
  101. scene.add( new THREE.AmbientLight( 0x222222 ) );
  102. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  103. directionalLight.position.set( 1, 1, 1 ).normalize();
  104. scene.add( directionalLight );
  105. var pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  106. particleLight.add( pointLight );
  107. //
  108. renderer = new THREE.WebGLRenderer( { antialias: true } );
  109. renderer.setPixelRatio( window.devicePixelRatio );
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. container.appendChild( renderer.domElement );
  112. renderer.gammaInput = true;
  113. renderer.gammaOutput = true;
  114. //
  115. stats = new Stats();
  116. container.appendChild( stats.dom );
  117. controls = new THREE.OrbitControls( camera );
  118. controls.target.set( 0, 0, 0 );
  119. controls.update();
  120. window.addEventListener( 'resize', onWindowResize, false );
  121. }
  122. function onWindowResize() {
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. }
  127. //
  128. function animate() {
  129. requestAnimationFrame( animate );
  130. render();
  131. stats.update();
  132. }
  133. function render() {
  134. var timer = Date.now() * 0.00025;
  135. //camera.position.x = Math.cos( timer ) * 800;
  136. //camera.position.z = Math.sin( timer ) * 800;
  137. camera.lookAt( scene.position );
  138. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  139. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  140. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>