webgl_materials_variations_basic.html 5.4 KB

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