webgl_materials_variations_phong.html 6.3 KB

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