webgl_materials_variations_phong.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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="https://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 * as THREE from '../build/three.module.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { FontLoader } from './jsm/loaders/FontLoader.js';
  17. import { TextGeometry } from './jsm/geometries/TextGeometry.js';
  18. let container, stats;
  19. let camera, scene, renderer;
  20. let particleLight;
  21. const loader = new FontLoader();
  22. loader.load( 'fonts/gentilis_regular.typeface.json', function ( font ) {
  23. init( font );
  24. animate();
  25. } );
  26. function init( font ) {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2500 );
  30. camera.position.set( 0.0, 400, 400 * 3.5 );
  31. //
  32. const reflectionCube = new THREE.CubeTextureLoader()
  33. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  34. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  35. reflectionCube.encoding = THREE.sRGBEncoding;
  36. scene = new THREE.Scene();
  37. scene.background = reflectionCube;
  38. // Materials
  39. let imgTexture = new THREE.TextureLoader().load( "textures/planets/moon_1024.jpg" );
  40. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  41. imgTexture.encoding = THREE.sRGBEncoding;
  42. imgTexture.anisotropy = 16;
  43. imgTexture = null;
  44. const bumpScale = 1;
  45. const cubeWidth = 400;
  46. const numberOfSphersPerSide = 5;
  47. const sphereRadius = ( cubeWidth / numberOfSphersPerSide ) * 0.8 * 0.5;
  48. const stepSize = 1.0 / numberOfSphersPerSide;
  49. const geometry = new THREE.SphereGeometry( sphereRadius, 32, 16 );
  50. for ( let alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {
  51. const specularShininess = Math.pow( 2, alpha * 10 );
  52. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  53. const specularColor = new THREE.Color( beta * 0.2, beta * 0.2, beta * 0.2 );
  54. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  55. // basic monochromatic energy preservation
  56. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 ).multiplyScalar( 1 - beta * 0.2 );
  57. const material = new THREE.MeshPhongMaterial( {
  58. map: imgTexture,
  59. bumpMap: imgTexture,
  60. bumpScale: bumpScale,
  61. color: diffuseColor,
  62. specular: specularColor,
  63. reflectivity: beta,
  64. shininess: specularShininess,
  65. envMap: alphaIndex % 2 === 0 ? null : reflectionCube
  66. } );
  67. const mesh = new THREE.Mesh( geometry, material );
  68. mesh.position.x = alpha * 400 - 200;
  69. mesh.position.y = beta * 400 - 200;
  70. mesh.position.z = gamma * 400 - 200;
  71. scene.add( mesh );
  72. }
  73. }
  74. }
  75. function addLabel( name, location ) {
  76. const textGeo = new TextGeometry( name, {
  77. font: font,
  78. size: 20,
  79. height: 1,
  80. curveSegments: 1
  81. } );
  82. const textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  83. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  84. textMesh.position.copy( location );
  85. scene.add( textMesh );
  86. }
  87. addLabel( "-shininess", new THREE.Vector3( - 350, 0, 0 ) );
  88. addLabel( "+shininess", new THREE.Vector3( 350, 0, 0 ) );
  89. addLabel( "-specular, -reflectivity", new THREE.Vector3( 0, - 300, 0 ) );
  90. addLabel( "+specular, +reflectivity", new THREE.Vector3( 0, 300, 0 ) );
  91. addLabel( "-diffuse", new THREE.Vector3( 0, 0, - 300 ) );
  92. addLabel( "+diffuse", new THREE.Vector3( 0, 0, 300 ) );
  93. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  94. scene.add( particleLight );
  95. // Lights
  96. scene.add( new THREE.AmbientLight( 0x222222 ) );
  97. const directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  98. directionalLight.position.set( 1, 1, 1 ).normalize();
  99. scene.add( directionalLight );
  100. const pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  101. particleLight.add( pointLight );
  102. //
  103. renderer = new THREE.WebGLRenderer( { antialias: true } );
  104. renderer.setPixelRatio( window.devicePixelRatio );
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. container.appendChild( renderer.domElement );
  107. renderer.outputEncoding = THREE.sRGBEncoding;
  108. //
  109. stats = new Stats();
  110. container.appendChild( stats.dom );
  111. const controls = new OrbitControls( camera, renderer.domElement );
  112. controls.minDistance = 200;
  113. controls.maxDistance = 2000;
  114. window.addEventListener( 'resize', onWindowResize );
  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. const 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>