webgl_materials_instance_uniform_nodes.html 4.9 KB

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