webgl_materials_variations_phong.html 6.2 KB

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