webgl_materials_variations_phong.html 5.8 KB

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