webgl_materials_compile.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - node material</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. <style>
  9. #waitScreen {
  10. color: #fff;
  11. position: absolute;
  12. top: 50%;
  13. left: 50%;
  14. margin-top: -50px;
  15. margin-left: -50px;
  16. width: 100px;
  17. height: 100px;
  18. }
  19. .hide {
  20. display:none;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div id="info">
  27. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Material
  28. <br /><span class="button" id="preload">change preload</span>
  29. </div>
  30. <div id="waitScreen">
  31. Loading ...
  32. </div>
  33. <!-- Import maps polyfill -->
  34. <!-- Remove this when import maps will be widely supported -->
  35. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  36. <script type="importmap">
  37. {
  38. "imports": {
  39. "three": "../build/three.module.js"
  40. }
  41. }
  42. </script>
  43. <script type="module">
  44. import * as THREE from 'three';
  45. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  46. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  47. import {
  48. NodeFrame,
  49. FloatNode,
  50. ColorNode,
  51. ConstNode,
  52. ExpressionNode,
  53. MathNode,
  54. OperatorNode,
  55. TimerNode,
  56. PhongNodeMaterial
  57. } from './jsm/nodes/Nodes.js';
  58. const container = document.getElementById( 'container' );
  59. let renderer, scene, camera;
  60. const clock = new THREE.Clock(), fov = 50;
  61. const frame = new NodeFrame();
  62. let teapot;
  63. let controls;
  64. const meshes = [];
  65. document.getElementById( 'preload' ).addEventListener( 'click', function () {
  66. const hash = document.location.hash.substr( 1 );
  67. if ( hash.length === 0 ) {
  68. window.location.hash = '#NoPreLoad';
  69. } else {
  70. window.location.hash = '';
  71. }
  72. location.reload( true );
  73. } );
  74. window.addEventListener( 'load', init );
  75. function init() {
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. scene = new THREE.Scene();
  81. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  82. camera.position.x = 0;
  83. camera.position.z = - 300;
  84. camera.position.y = 200;
  85. controls = new OrbitControls( camera, renderer.domElement );
  86. controls.minDistance = 50;
  87. controls.maxDistance = 400;
  88. scene.add( new THREE.AmbientLight( 0x464646 ) );
  89. const light1 = new THREE.DirectionalLight( 0xffddcc, 1 );
  90. light1.position.set( 1, 0.75, 0.5 );
  91. scene.add( light1 );
  92. const light2 = new THREE.DirectionalLight( 0xccccff, 1 );
  93. light2.position.set( - 1, 0.75, - 0.5 );
  94. scene.add( light2 );
  95. teapot = new TeapotGeometry( 15, 18 );
  96. const itemsonrow = 10;
  97. for ( let i = 0; i < itemsonrow * itemsonrow; i ++ ) {
  98. const mesh = new THREE.Mesh( teapot );
  99. mesh.position.x = 50 * ( i % itemsonrow ) - 50 * itemsonrow / 2;
  100. mesh.position.z = 50 * Math.floor( i / itemsonrow ) - 150;
  101. updateMaterial( mesh );
  102. scene.add( mesh );
  103. meshes.push( mesh );
  104. }
  105. window.addEventListener( 'resize', onWindowResize );
  106. const hash = document.location.hash.substr( 1 );
  107. if ( hash.length === 0 ) {
  108. renderer.compile( scene, camera );
  109. }
  110. document.getElementById( 'waitScreen' ).className = 'hide';
  111. setTimeout( function () {
  112. onWindowResize();
  113. animate();
  114. }, 1 );
  115. }
  116. function updateMaterial( mesh ) {
  117. if ( mesh.material ) mesh.material.dispose();
  118. const mtl = new PhongNodeMaterial();
  119. const time = new TimerNode();
  120. const speed = new FloatNode( Math.random() );
  121. const color = new ColorNode( Math.random() * 0xFFFFFF );
  122. const timeSpeed = new OperatorNode(
  123. time,
  124. speed,
  125. OperatorNode.MUL
  126. );
  127. const sinCycleInSecs = new OperatorNode(
  128. timeSpeed,
  129. new ConstNode( ConstNode.PI2 ),
  130. OperatorNode.MUL
  131. );
  132. const cycle = new MathNode( sinCycleInSecs, MathNode.SIN );
  133. const cycleColor = new OperatorNode(
  134. cycle,
  135. color,
  136. OperatorNode.MUL
  137. );
  138. const cos = new MathNode( cycleColor, MathNode.SIN );
  139. mtl.color = new ColorNode( 0 );
  140. mtl.emissive = cos;
  141. const transformer = new ExpressionNode( 'position + 0.0 * ' + Math.random(), 'vec3', [ ] );
  142. mtl.transform = transformer;
  143. // build shader ( ignore auto build )
  144. mtl.build();
  145. // set material
  146. mesh.material = mtl;
  147. }
  148. function onWindowResize() {
  149. const width = window.innerWidth, height = window.innerHeight;
  150. camera.aspect = width / height;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( width, height );
  153. }
  154. function animate() {
  155. const delta = clock.getDelta();
  156. frame.update( delta );
  157. for ( let i = 0; i < meshes.length; i ++ ) {
  158. const mesh = meshes[ i ];
  159. frame.updateNode( mesh.material );
  160. }
  161. renderer.render( scene, camera );
  162. requestAnimationFrame( animate );
  163. }
  164. </script>
  165. </body>
  166. </html>