|
| 1 | +<meta charset=utf-8> |
| 2 | +<head> |
| 3 | +<script type="text/javascript" src="glutil_min.js"></script> |
| 4 | +<script type="text/javascript" src="frame.js"></script> |
| 5 | +</head> |
| 6 | +<body> |
| 7 | +<canvas width="600" height="450" id=canvas></canvas> |
| 8 | +<script id="demo"> |
| 9 | +//<!-- |
| 10 | +/* |
| 11 | +Written by Peter O. in 2015. |
| 12 | +
|
| 13 | +Any copyright is dedicated to the Public Domain. |
| 14 | +http://creativecommons.org/publicdomain/zero/1.0/ |
| 15 | +If you like this, you should donate to Peter O. |
| 16 | +at: http://upokecenter.dreamhosters.com/articles/donate-now-2/ |
| 17 | +*/ |
| 18 | + |
| 19 | +// Generates a linear gradient in the horizontal direction. |
| 20 | +// This function demonstrates generating a custom texture. |
| 21 | +function horizontalGradient(color1,color2){ |
| 22 | + color1=GLUtil.toGLColor(color1); |
| 23 | + color2=GLUtil.toGLColor(color2); |
| 24 | + var arr=[]; |
| 25 | + var size=32; |
| 26 | + for(var i=0;i<size;i++){ |
| 27 | + arr.push( |
| 28 | + GLMath.vec4scaleInPlace(GLMath.vec4lerp(color1,color2,i/(size-1)),255) |
| 29 | + ) |
| 30 | + } |
| 31 | + var gradient=[] |
| 32 | + var i=0; |
| 33 | + for(var y=0;y<size;y++){ |
| 34 | + for(var x=0;x<size;x++,i+=4){ |
| 35 | + gradient[i]=Math.floor(arr[x][0]) |
| 36 | + gradient[i+1]=Math.floor(arr[x][1]) |
| 37 | + gradient[i+2]=Math.floor(arr[x][2]) |
| 38 | + gradient[i+3]=Math.floor(arr[x][3]) |
| 39 | + } |
| 40 | + } |
| 41 | + return Texture.fromUint8Array( |
| 42 | + new Uint8Array(gradient),size,size).setClamp(true); |
| 43 | +} |
| 44 | + |
| 45 | + // Create the 3D scene; find the HTML canvas and pass it |
| 46 | + // to Scene3D. |
| 47 | + var scene=new Scene3D(document.getElementById("canvas")); |
| 48 | + var fc=new FrameCounterDiv(scene) |
| 49 | + scene.setPerspective(45,scene.getAspect(),0.1,100); |
| 50 | + scene.setLookAt([0,0,40]); |
| 51 | + // Generate a red-blue gradient texture |
| 52 | + var tex=horizontalGradient("red","blue"); |
| 53 | + var mesh=Meshes.createBox(10,20,20); |
| 54 | + var shape=scene.makeShape(mesh).setTexture(tex) |
| 55 | + scene.addShape(shape); |
| 56 | + var rotation=[0,0,0]; |
| 57 | + GLUtil.renderLoop(function(){ |
| 58 | + fc.update(); |
| 59 | + rotation[0]+=0.5; |
| 60 | + rotation[1]+=1; |
| 61 | + var q=GLMath.quatFromTaitBryan(rotation); |
| 62 | + shape.setQuaternion(q); |
| 63 | + scene.render(); |
| 64 | + }) |
| 65 | +//--> |
| 66 | +</script> |
| 67 | +<script> |
| 68 | +window.addEventListener("load",function(){ |
| 69 | + var e=document.createElement("pre") |
| 70 | + e.innerHTML=document.getElementById("demo").textContent.replace(/</g,"<") |
| 71 | + document.body.appendChild(e) |
| 72 | +}) |
| 73 | +</script> |
| 74 | +</body> |
0 commit comments