webgl_materials_instance_uniform_nodes.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import Stats from './jsm/libs/stats.module.js';
  23. import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';
  24. import * as Nodes from './jsm/renderers/nodes/Nodes.js';
  25. class InstanceUniformNode extends Nodes.Node {
  26. constructor() {
  27. super( 'vec3' );
  28. this.updateType = Nodes.NodeUpdateType.Object;
  29. this.inputNode = new Nodes.ColorNode();
  30. }
  31. update( frame ) {
  32. const rendererState = frame.renderer.state;
  33. const mesh = frame.object;
  34. const meshColor = mesh.color;
  35. this.inputNode.value.copy( meshColor );
  36. // force refresh material uniforms
  37. rendererState.useProgram( null );
  38. }
  39. generate( builder, output ) {
  40. return this.inputNode.build( builder, output );
  41. }
  42. }
  43. let stats;
  44. let camera, scene, renderer;
  45. let pointLight;
  46. const objects = [];
  47. init();
  48. animate();
  49. function init() {
  50. const container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  53. camera.position.set( 0, 200, 800 );
  54. scene = new THREE.Scene();
  55. // Grid
  56. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  57. helper.position.y = - 75;
  58. scene.add( helper );
  59. // Material
  60. const instanceUniform = new InstanceUniformNode();
  61. const material = new THREE.MeshStandardMaterial();
  62. material.colorNode = instanceUniform;
  63. // Spheres geometry
  64. const geometry = new THREE.SphereGeometry( 70, 32, 16 );
  65. for ( let i = 0, l = 12; i < l; i ++ ) {
  66. addMesh( geometry, material );
  67. }
  68. // Lights
  69. scene.add( new THREE.AmbientLight( 0x111111 ) );
  70. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  71. directionalLight.position.x = Math.random() - 0.5;
  72. directionalLight.position.y = Math.random() - 0.5;
  73. directionalLight.position.z = Math.random() - 0.5;
  74. directionalLight.position.normalize();
  75. scene.add( directionalLight );
  76. pointLight = new THREE.PointLight( 0xffffff, 1 );
  77. scene.add( pointLight );
  78. pointLight.add( new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  79. //
  80. renderer = new THREE.WebGLRenderer( { antialias: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. container.appendChild( renderer.domElement );
  84. //
  85. stats = new Stats();
  86. container.appendChild( stats.dom );
  87. //
  88. window.addEventListener( 'resize', onWindowResize );
  89. }
  90. function addMesh( geometry, material ) {
  91. const mesh = new THREE.Mesh( geometry, material );
  92. mesh.color = new THREE.Color( Math.random() * 0xffffff );
  93. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  94. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  95. mesh.rotation.x = Math.random() * 200 - 100;
  96. mesh.rotation.y = Math.random() * 200 - 100;
  97. mesh.rotation.z = Math.random() * 200 - 100;
  98. objects.push( mesh );
  99. scene.add( mesh );
  100. }
  101. function onWindowResize() {
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. }
  106. //
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. nodeFrame.update();
  110. render();
  111. stats.update();
  112. }
  113. function render() {
  114. const timer = 0.0001 * Date.now();
  115. camera.position.x = Math.cos( timer ) * 1000;
  116. camera.position.z = Math.sin( timer ) * 1000;
  117. camera.lookAt( scene.position );
  118. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  119. const object = objects[ i ];
  120. object.rotation.x += 0.01;
  121. object.rotation.y += 0.005;
  122. }
  123. pointLight.position.x = Math.sin( timer * 7 ) * 300;
  124. pointLight.position.y = Math.cos( timer * 5 ) * 400;
  125. pointLight.position.z = Math.cos( timer * 3 ) * 300;
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>