webgl_materials_variations_phong.html 6.2 KB

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