webgl_materials_instance_uniform_nodes.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - material instance uniform</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl material instance uniform
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three-nodes/": "./jsm/nodes/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import * as Nodes from 'three-nodes/Nodes.js';
  27. import { add, mul } from 'three-nodes/Nodes.js';
  28. import Stats from './jsm/libs/stats.module.js';
  29. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  30. import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';
  31. class InstanceUniformNode extends Nodes.Node {
  32. constructor() {
  33. super( 'vec3' );
  34. this.updateType = Nodes.NodeUpdateType.Object;
  35. this.uniformNode = new Nodes.UniformNode( new THREE.Color() );
  36. }
  37. update( frame ) {
  38. const rendererState = frame.renderer.state;
  39. const mesh = frame.object;
  40. const meshColor = mesh.color;
  41. this.uniformNode.value.copy( meshColor );
  42. }
  43. generate( builder, output ) {
  44. return this.uniformNode.build( builder, output );
  45. }
  46. }
  47. let stats;
  48. let camera, scene, renderer;
  49. let controls;
  50. let pointLight;
  51. const objects = [];
  52. init();
  53. animate();
  54. function init() {
  55. const container = document.createElement( 'div' );
  56. document.body.appendChild( container );
  57. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
  58. camera.position.set( 0, 200, 1200 );
  59. scene = new THREE.Scene();
  60. // Grid
  61. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  62. helper.position.y = - 75;
  63. scene.add( helper );
  64. // CubeMap
  65. const path = 'textures/cube/SwedishRoyalCastle/';
  66. const format = '.jpg';
  67. const urls = [
  68. path + 'px' + format, path + 'nx' + format,
  69. path + 'py' + format, path + 'ny' + format,
  70. path + 'pz' + format, path + 'nz' + format
  71. ];
  72. const cubeTexture = new THREE.CubeTextureLoader().load( urls );
  73. // Material
  74. const instanceUniform = new InstanceUniformNode();
  75. const cubeTextureNode = new Nodes.CubeTextureNode( cubeTexture );
  76. const material = new THREE.MeshStandardMaterial();
  77. material.colorNode = add( instanceUniform, cubeTextureNode );
  78. material.emissiveNode = mul( instanceUniform, cubeTextureNode );
  79. // Spheres geometry
  80. const geometry = new THREE.SphereGeometry( 70, 32, 16 );
  81. for ( let i = 0, l = 12; i < l; i ++ ) {
  82. addMesh( geometry, material );
  83. }
  84. // Lights
  85. scene.add( new THREE.AmbientLight( 0x111111 ) );
  86. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  87. directionalLight.position.x = Math.random() - 0.5;
  88. directionalLight.position.y = Math.random() - 0.5;
  89. directionalLight.position.z = Math.random() - 0.5;
  90. directionalLight.position.normalize();
  91. scene.add( directionalLight );
  92. pointLight = new THREE.PointLight( 0xffffff, 1 );
  93. scene.add( pointLight );
  94. pointLight.add( new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  95. //
  96. renderer = new THREE.WebGLRenderer( { antialias: true } );
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. container.appendChild( renderer.domElement );
  100. //
  101. controls = new OrbitControls( camera, renderer.domElement );
  102. controls.minDistance = 400;
  103. controls.maxDistance = 2000;
  104. //
  105. stats = new Stats();
  106. container.appendChild( stats.dom );
  107. //
  108. window.addEventListener( 'resize', onWindowResize );
  109. }
  110. function addMesh( geometry, material ) {
  111. const mesh = new THREE.Mesh( geometry, material );
  112. mesh.color = new THREE.Color( Math.random() * 0xffffff );
  113. mesh.position.x = ( objects.length % 4 ) * 200 - 300;
  114. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  115. mesh.rotation.x = Math.random() * 200 - 100;
  116. mesh.rotation.y = Math.random() * 200 - 100;
  117. mesh.rotation.z = Math.random() * 200 - 100;
  118. objects.push( mesh );
  119. scene.add( mesh );
  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. nodeFrame.update();
  130. render();
  131. stats.update();
  132. }
  133. function render() {
  134. const timer = 0.0001 * Date.now();
  135. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  136. const object = objects[ i ];
  137. object.rotation.x += 0.01;
  138. object.rotation.y += 0.005;
  139. }
  140. pointLight.position.x = Math.sin( timer * 7 ) * 300;
  141. pointLight.position.y = Math.cos( timer * 5 ) * 400;
  142. pointLight.position.z = Math.cos( timer * 3 ) * 300;
  143. renderer.render( scene, camera );
  144. }
  145. </script>
  146. </body>
  147. </html>