webgl_materials_variations_basic.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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> - Basic 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 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; alpha <= 1.0; alpha += stepSize ) {
  61. for ( let beta = 0; beta <= 1.0; beta += stepSize ) {
  62. for ( let gamma = 0; gamma <= 1.0; gamma += stepSize ) {
  63. const diffuseColor = new THREE.Color().setHSL( alpha, 0.5, gamma * 0.5 + 0.1 );
  64. const material = new THREE.MeshBasicMaterial( {
  65. map: imgTexture,
  66. color: diffuseColor,
  67. reflectivity: beta,
  68. envMap: alpha < 0.5 ? reflectionCube : null
  69. } );
  70. const mesh = new THREE.Mesh( geometry, material );
  71. mesh.position.x = alpha * 400 - 200;
  72. mesh.position.y = beta * 400 - 200;
  73. mesh.position.z = gamma * 400 - 200;
  74. scene.add( mesh );
  75. }
  76. }
  77. }
  78. function addLabel( name, location ) {
  79. const textGeo = new TextGeometry( name, {
  80. font: font,
  81. size: 20,
  82. height: 1,
  83. curveSegments: 1
  84. } );
  85. const textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  86. const textMesh = new THREE.Mesh( textGeo, textMaterial );
  87. textMesh.position.copy( location );
  88. scene.add( textMesh );
  89. }
  90. addLabel( '+hue', new THREE.Vector3( - 350, 0, 0 ) );
  91. addLabel( '-hue', new THREE.Vector3( 350, 0, 0 ) );
  92. addLabel( '-reflectivity', new THREE.Vector3( 0, - 300, 0 ) );
  93. addLabel( '+reflectivity', new THREE.Vector3( 0, 300, 0 ) );
  94. addLabel( '-diffuse', new THREE.Vector3( 0, 0, - 300 ) );
  95. addLabel( '+diffuse', new THREE.Vector3( 0, 0, 300 ) );
  96. addLabel( 'envMap', new THREE.Vector3( - 350, 300, 0 ) );
  97. addLabel( 'no envMap', new THREE.Vector3( 350, 300, 0 ) );
  98. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  99. scene.add( particleLight );
  100. // Lights
  101. scene.add( new THREE.AmbientLight( 0x222222 ) );
  102. const directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  103. directionalLight.position.set( 1, 1, 1 ).normalize();
  104. scene.add( directionalLight );
  105. const pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  106. particleLight.add( pointLight );
  107. //
  108. renderer = new THREE.WebGLRenderer( { antialias: true } );
  109. renderer.setPixelRatio( window.devicePixelRatio );
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. container.appendChild( renderer.domElement );
  112. renderer.outputEncoding = THREE.sRGBEncoding;
  113. //
  114. stats = new Stats();
  115. container.appendChild( stats.dom );
  116. const controls = new OrbitControls( camera, renderer.domElement );
  117. controls.minDistance = 200;
  118. controls.maxDistance = 2000;
  119. window.addEventListener( 'resize', onWindowResize );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. //
  127. function animate() {
  128. requestAnimationFrame( animate );
  129. render();
  130. stats.update();
  131. }
  132. function render() {
  133. const timer = Date.now() * 0.00025;
  134. //camera.position.x = Math.cos( timer ) * 800;
  135. //camera.position.z = Math.sin( timer ) * 800;
  136. camera.lookAt( scene.position );
  137. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  138. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  139. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>