webgl_materials_compile.html 4.9 KB

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