You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
260 lines
5.3 KiB
260 lines
5.3 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<title>three.js webgl - node material</title>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
<style>
|
|
body {
|
|
color: #fff;
|
|
font-family:Monospace;
|
|
font-size:13px;
|
|
margin: 0px;
|
|
text-align:center;
|
|
overflow: hidden;
|
|
}
|
|
|
|
#info {
|
|
color: #fff;
|
|
position: absolute;
|
|
top: 10px;
|
|
width: 100%;
|
|
text-align: center;
|
|
display:block;
|
|
}
|
|
|
|
#waitScreen {
|
|
color: #000;
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 50%;
|
|
margin-top: -50px;
|
|
margin-left: -50px;
|
|
width: 100px;
|
|
height: 100px;
|
|
}
|
|
|
|
.hide {
|
|
display:none;
|
|
}
|
|
|
|
a { color: white }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div id="container"></div>
|
|
<div id="info">
|
|
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Material
|
|
<br /><span class="button" id="preload">change preload</span>
|
|
</div>
|
|
|
|
<div id="waitScreen">
|
|
Loading ...
|
|
</div>
|
|
|
|
<script src="../build/three.js"></script>
|
|
|
|
<script src='js/geometries/TeapotBufferGeometry.js'></script>
|
|
<script src="js/controls/OrbitControls.js"></script>
|
|
<script src="js/libs/dat.gui.min.js"></script>
|
|
|
|
<script type="module">
|
|
|
|
import './js/nodes/THREE.Nodes.js';
|
|
|
|
var container = document.getElementById( 'container' );
|
|
|
|
var renderer, scene, camera, clock = new THREE.Clock(), fov = 50;
|
|
var frame = new THREE.NodeFrame();
|
|
var teapot;
|
|
var controls;
|
|
var rtTexture, rtMaterial;
|
|
var meshes = [];
|
|
|
|
document.getElementById( "preload" ).addEventListener( 'click', function () {
|
|
|
|
var hash = document.location.hash.substr( 1 );
|
|
|
|
if ( hash.length === 0 ) {
|
|
|
|
window.location.hash = "#NoPreLoad";
|
|
|
|
} else {
|
|
|
|
window.location.hash = "";
|
|
|
|
}
|
|
|
|
location.reload( true );
|
|
|
|
}, false );
|
|
|
|
window.addEventListener( 'load', init );
|
|
|
|
function init() {
|
|
|
|
renderer = new THREE.WebGLRenderer( { antialias: true } );
|
|
renderer.setPixelRatio( window.devicePixelRatio );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
container.appendChild( renderer.domElement );
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
|
|
camera.position.x = 0;
|
|
camera.position.z = - 300;
|
|
camera.position.y = 200;
|
|
camera.target = new THREE.Vector3();
|
|
|
|
controls = new THREE.OrbitControls( camera, renderer.domElement );
|
|
controls.minDistance = 50;
|
|
controls.maxDistance = 400;
|
|
|
|
scene.add( new THREE.AmbientLight( 0x464646 ) );
|
|
|
|
var light = new THREE.DirectionalLight( 0xffddcc, 1 );
|
|
light.position.set( 1, 0.75, 0.5 );
|
|
scene.add( light );
|
|
|
|
var light = new THREE.DirectionalLight( 0xccccff, 1 );
|
|
light.position.set( - 1, 0.75, - 0.5 );
|
|
scene.add( light );
|
|
|
|
teapot = new THREE.TeapotBufferGeometry( 15, 18 );
|
|
|
|
var itemsonrow = 10;
|
|
|
|
for ( var i = 0; i < itemsonrow * itemsonrow; i ++ ) {
|
|
|
|
var mesh = new THREE.Mesh( teapot );
|
|
|
|
mesh.position.x = 50 * ( i % itemsonrow ) - 50 * itemsonrow / 2;
|
|
mesh.position.z = 50 * Math.floor( i / itemsonrow ) - 150;
|
|
updateMaterial( mesh );
|
|
scene.add( mesh );
|
|
meshes.push( mesh );
|
|
|
|
}
|
|
|
|
window.addEventListener( 'resize', onWindowResize, false );
|
|
|
|
var hash = document.location.hash.substr( 1 );
|
|
|
|
if ( hash.length === 0 ) {
|
|
|
|
renderer.compile( scene, camera );
|
|
|
|
}
|
|
|
|
document.getElementById( "waitScreen" ).className = "hide";
|
|
|
|
setTimeout( function () {
|
|
|
|
onWindowResize();
|
|
animate();
|
|
|
|
}, 1 );
|
|
|
|
}
|
|
|
|
function updateMaterial( mesh ) {
|
|
|
|
if ( mesh.material ) mesh.material.dispose();
|
|
|
|
if ( rtTexture ) {
|
|
|
|
rtTexture.dispose();
|
|
rtTexture = null;
|
|
|
|
}
|
|
|
|
if ( rtMaterial ) {
|
|
|
|
rtMaterial.dispose();
|
|
rtMaterial = null;
|
|
|
|
}
|
|
|
|
var mtl = new THREE.PhongNodeMaterial();
|
|
|
|
var time = new THREE.TimerNode();
|
|
var speed = new THREE.FloatNode( Math.random() );
|
|
|
|
var color = new THREE.ColorNode( Math.random() * 0xFFFFFF );
|
|
|
|
var timeSpeed = new THREE.OperatorNode(
|
|
time,
|
|
speed,
|
|
THREE.OperatorNode.MUL
|
|
);
|
|
|
|
var sinCycleInSecs = new THREE.OperatorNode(
|
|
timeSpeed,
|
|
new THREE.ConstNode( THREE.ConstNode.PI2 ),
|
|
THREE.OperatorNode.MUL
|
|
);
|
|
|
|
var cycle = new THREE.Math1Node( sinCycleInSecs, THREE.Math1Node.SIN );
|
|
|
|
var cycleColor = new THREE.OperatorNode(
|
|
cycle,
|
|
color,
|
|
THREE.OperatorNode.MUL
|
|
);
|
|
|
|
var cos = new THREE.Math1Node( cycleColor, THREE.Math1Node.SIN );
|
|
|
|
mtl.color = new THREE.ColorNode( 0 );
|
|
mtl.emissive = cos;
|
|
|
|
|
|
var transformer = new THREE.ExpressionNode( "position + 0.0 * " + Math.random(), "vec3", [ ] );
|
|
mtl.transform = transformer;
|
|
|
|
// build shader ( ignore auto build )
|
|
mtl.build();
|
|
|
|
// set material
|
|
mesh.material = mtl;
|
|
|
|
}
|
|
|
|
function onWindowResize() {
|
|
|
|
var width = window.innerWidth, height = window.innerHeight;
|
|
|
|
camera.aspect = width / height;
|
|
camera.updateProjectionMatrix();
|
|
|
|
renderer.setSize( width, height );
|
|
|
|
if ( rtTexture ) rtTexture.setSize( width, height );
|
|
|
|
}
|
|
|
|
function animate() {
|
|
|
|
var delta = clock.getDelta();
|
|
|
|
frame.update( delta );
|
|
|
|
for ( var i = 0; i < meshes.length; i ++ ) {
|
|
|
|
var mesh = meshes[ i ];
|
|
|
|
frame.updateNode( mesh.material );
|
|
|
|
}
|
|
|
|
renderer.render( scene, camera );
|
|
|
|
requestAnimationFrame( animate );
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|