2
0

webgl_materials_compile.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. <script type="module">
  34. import * as THREE from '../build/three.module.js';
  35. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  36. import { TeapotBufferGeometry } from './jsm/geometries/TeapotBufferGeometry.js';
  37. import {
  38. NodeFrame,
  39. FloatNode,
  40. ColorNode,
  41. ConstNode,
  42. ExpressionNode,
  43. MathNode,
  44. OperatorNode,
  45. TimerNode,
  46. PhongNodeMaterial
  47. } from './jsm/nodes/Nodes.js';
  48. const container = document.getElementById( 'container' );
  49. let renderer, scene, camera;
  50. const clock = new THREE.Clock(), fov = 50;
  51. const frame = new NodeFrame();
  52. let teapot;
  53. let controls;
  54. const meshes = [];
  55. document.getElementById( "preload" ).addEventListener( 'click', function () {
  56. const hash = document.location.hash.substr( 1 );
  57. if ( hash.length === 0 ) {
  58. window.location.hash = "#NoPreLoad";
  59. } else {
  60. window.location.hash = "";
  61. }
  62. location.reload( true );
  63. }, false );
  64. window.addEventListener( 'load', init );
  65. function init() {
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. container.appendChild( renderer.domElement );
  70. scene = new THREE.Scene();
  71. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  72. camera.position.x = 0;
  73. camera.position.z = - 300;
  74. camera.position.y = 200;
  75. camera.target = new THREE.Vector3();
  76. controls = new OrbitControls( camera, renderer.domElement );
  77. controls.minDistance = 50;
  78. controls.maxDistance = 400;
  79. scene.add( new THREE.AmbientLight( 0x464646 ) );
  80. const light1 = new THREE.DirectionalLight( 0xffddcc, 1 );
  81. light1.position.set( 1, 0.75, 0.5 );
  82. scene.add( light1 );
  83. const light2 = new THREE.DirectionalLight( 0xccccff, 1 );
  84. light2.position.set( - 1, 0.75, - 0.5 );
  85. scene.add( light2 );
  86. teapot = new TeapotBufferGeometry( 15, 18 );
  87. const itemsonrow = 10;
  88. for ( let i = 0; i < itemsonrow * itemsonrow; i ++ ) {
  89. const mesh = new THREE.Mesh( teapot );
  90. mesh.position.x = 50 * ( i % itemsonrow ) - 50 * itemsonrow / 2;
  91. mesh.position.z = 50 * Math.floor( i / itemsonrow ) - 150;
  92. updateMaterial( mesh );
  93. scene.add( mesh );
  94. meshes.push( mesh );
  95. }
  96. window.addEventListener( 'resize', onWindowResize, false );
  97. const hash = document.location.hash.substr( 1 );
  98. if ( hash.length === 0 ) {
  99. renderer.compile( scene, camera );
  100. }
  101. document.getElementById( "waitScreen" ).className = "hide";
  102. setTimeout( function () {
  103. onWindowResize();
  104. animate();
  105. }, 1 );
  106. }
  107. function updateMaterial( mesh ) {
  108. if ( mesh.material ) mesh.material.dispose();
  109. const mtl = new PhongNodeMaterial();
  110. const time = new TimerNode();
  111. const speed = new FloatNode( Math.random() );
  112. const color = new ColorNode( Math.random() * 0xFFFFFF );
  113. const timeSpeed = new OperatorNode(
  114. time,
  115. speed,
  116. OperatorNode.MUL
  117. );
  118. const sinCycleInSecs = new OperatorNode(
  119. timeSpeed,
  120. new ConstNode( ConstNode.PI2 ),
  121. OperatorNode.MUL
  122. );
  123. const cycle = new MathNode( sinCycleInSecs, MathNode.SIN );
  124. const cycleColor = new OperatorNode(
  125. cycle,
  126. color,
  127. OperatorNode.MUL
  128. );
  129. const cos = new MathNode( cycleColor, MathNode.SIN );
  130. mtl.color = new ColorNode( 0 );
  131. mtl.emissive = cos;
  132. const transformer = new ExpressionNode( "position + 0.0 * " + Math.random(), "vec3", [ ] );
  133. mtl.transform = transformer;
  134. // build shader ( ignore auto build )
  135. mtl.build();
  136. // set material
  137. mesh.material = mtl;
  138. }
  139. function onWindowResize() {
  140. const width = window.innerWidth, height = window.innerHeight;
  141. camera.aspect = width / height;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( width, height );
  144. }
  145. function animate() {
  146. const delta = clock.getDelta();
  147. frame.update( delta );
  148. for ( let i = 0; i < meshes.length; i ++ ) {
  149. const mesh = meshes[ i ];
  150. frame.updateNode( mesh.material );
  151. }
  152. renderer.render( scene, camera );
  153. requestAnimationFrame( animate );
  154. }
  155. </script>
  156. </body>
  157. </html>