webgl_materials_variations_phong.html 5.8 KB

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