目前我正在學習的OpenGL和整個本教程中偶然發現: http://patriciogonzalezvivo.com/2015/thebookofshaders/03/動畫自定義着色器/ three.js所
我試圖使用的WebGL的剪斷,以便進一步瞭解機制,但不知何故,它不工作,我老實說不知道爲什麼。我相信肯定會有一些語法錯誤,但它會是什麼?如果沒有,那我該如何做這項工作?
說實話,我試圖瞭解如何實施u_time。我認爲GPU自動有一個內置的計時器,它會引起顏色過渡動畫。
// set the scene size
var WIDTH = 400,
HEIGHT = 300;
// set some camera attributes
var VIEW_ANGLE = 45,
ASPECT = WIDTH/HEIGHT,
NEAR = 0.1,
FAR = 10000;
// get the DOM element to attach to
// - assume we've got jQuery to hand
var $container = $('#container');
// create a WebGL renderer, camera
// and a scene
var renderer = new THREE.WebGLRenderer();
var camera = new THREE.Camera( VIEW_ANGLE,
ASPECT,
NEAR,
FAR );
var scene = new THREE.Scene();
// the camera starts at 0,0,0 so pull it back
camera.position.z = 300;
// start the renderer
renderer.setSize(WIDTH, HEIGHT);
// attach the render-supplied DOM element
$container.append(renderer.domElement);
// create the sphere's material
var shaderMaterial = new THREE.MeshShaderMaterial({
vertexShader: $('#vertexshader').text(),
fragmentShader: $('#fragmentshader').text()
});
// set up the sphere vars
var radius = 50, segments = 16, rings = 16;
// create a new mesh with sphere geometry -
// we will cover the sphereMaterial next!
var sphere = new THREE.Mesh(
new THREE.Sphere(radius, segments, rings),
shaderMaterial);
// add the sphere to the scene
scene.addChild(sphere);
// draw!
renderer.render(scene, camera);
<div id="container"></div>
<script type="x-shader/x-vertex" id="vertexshader">
// switch on high precision floats
#ifdef GL_ES
precision highp float;
#endif
void main()
{
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
uniform float u_time;
void main() {
gl_FragColor = vec4(sin(u_time),0.0,0.0,1.0);
}
</script>
<script src="https://aerotwist.com/static/tutorials/an-introduction-to-shaders-part-1/demo/js/Three.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
你在哪裏聲明瞭projectionMatrix,modelViewMatrix和position?投影矩陣和modelViewMatrix應聲明爲制服;位置作爲屬性,加上你應該將它們發送到GPU。 如果這不是你的問題,那麼你應該澄清什麼是不工作,我猜。 – Zouch
順便說一句,我不知道THREE.js,但我沒有看到你發送u_time制服的位置。 – Zouch
@Zouch我在學習本教程。我想我必須以某種方式綁定JavaScript代碼中的u_time。創建某種計時器。 – Asperger