webgl_nodes_materials_instance_uniform.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/addons/": "./jsm/",
  21. "three/nodes": "./jsm/nodes/Nodes.js"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { MeshStandardNodeMaterial, Node, NodeUpdateType, nodeObject, uniform, cubeTexture } from 'three/nodes';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  31. class InstanceUniformNode extends Node {
  32. constructor() {
  33. super( 'vec3' );
  34. this.updateType = NodeUpdateType.OBJECT;
  35. this.uniformNode = uniform( new THREE.Color() );
  36. }
  37. update( frame ) {
  38. this.uniformNode.value.copy( frame.object.color );
  39. }
  40. generate( builder, output ) {
  41. return this.uniformNode.build( builder, output );
  42. }
  43. }
  44. let stats;
  45. let camera, scene, renderer;
  46. let controls;
  47. let pointLight;
  48. const objects = [];
  49. init();
  50. animate();
  51. function init() {
  52. const container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
  55. camera.position.set( 0, 200, 1200 );
  56. scene = new THREE.Scene();
  57. // Grid
  58. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  59. helper.position.y = - 75;
  60. scene.add( helper );
  61. // CubeMap
  62. const path = 'textures/cube/SwedishRoyalCastle/';
  63. const format = '.jpg';
  64. const urls = [
  65. path + 'px' + format, path + 'nx' + format,
  66. path + 'py' + format, path + 'ny' + format,
  67. path + 'pz' + format, path + 'nz' + format
  68. ];
  69. const cubeMap = new THREE.CubeTextureLoader().load( urls );
  70. cubeMap.colorSpace = THREE.SRGBColorSpace;
  71. // Material
  72. const instanceUniform = nodeObject( new InstanceUniformNode() );
  73. const cubeTextureNode = cubeTexture( cubeMap );
  74. const material = new MeshStandardNodeMaterial();
  75. material.colorNode = instanceUniform.add( cubeTextureNode );
  76. material.emissiveNode = instanceUniform.mul( cubeTextureNode );
  77. // Spheres geometry
  78. const geometry = new THREE.SphereGeometry( 70, 32, 16 );
  79. for ( let i = 0, l = 12; i < l; i ++ ) {
  80. addMesh( geometry, material );
  81. }
  82. // Lights
  83. scene.add( new THREE.AmbientLight( 0x111111 ) );
  84. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  85. directionalLight.position.x = Math.random() - 0.5;
  86. directionalLight.position.y = Math.random() - 0.5;
  87. directionalLight.position.z = Math.random() - 0.5;
  88. directionalLight.position.normalize();
  89. scene.add( directionalLight );
  90. pointLight = new THREE.PointLight( 0xffffff, 1 );
  91. scene.add( pointLight );
  92. pointLight.add( new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  93. //
  94. renderer = new THREE.WebGLRenderer( { antialias: true } );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. container.appendChild( renderer.domElement );
  98. //
  99. controls = new OrbitControls( camera, renderer.domElement );
  100. controls.minDistance = 400;
  101. controls.maxDistance = 2000;
  102. //
  103. stats = new Stats();
  104. container.appendChild( stats.dom );
  105. //
  106. window.addEventListener( 'resize', onWindowResize );
  107. }
  108. function addMesh( geometry, material ) {
  109. const mesh = new THREE.Mesh( geometry, material );
  110. mesh.color = new THREE.Color( Math.random() * 0xffffff );
  111. mesh.position.x = ( objects.length % 4 ) * 200 - 300;
  112. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  113. mesh.rotation.x = Math.random() * 200 - 100;
  114. mesh.rotation.y = Math.random() * 200 - 100;
  115. mesh.rotation.z = Math.random() * 200 - 100;
  116. objects.push( mesh );
  117. scene.add( mesh );
  118. }
  119. function onWindowResize() {
  120. camera.aspect = window.innerWidth / window.innerHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. }
  124. //
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. nodeFrame.update();
  128. render();
  129. stats.update();
  130. }
  131. function render() {
  132. const timer = 0.0001 * Date.now();
  133. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  134. const object = objects[ i ];
  135. object.rotation.x += 0.01;
  136. object.rotation.y += 0.005;
  137. }
  138. pointLight.position.x = Math.sin( timer * 7 ) * 300;
  139. pointLight.position.y = Math.cos( timer * 5 ) * 400;
  140. pointLight.position.z = Math.cos( timer * 3 ) * 300;
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>