简体   繁体   English

我如何使用纯 webGL 而不是three.js来做这个操作

[英]How can I do this operations using pure webGL and not three.js

if( keyboard.pressed("up"))
            pobjects[movementControls.translate].translateX(1);
        if( keyboard.pressed("down"))
            pobjects[movementControls.translate].translateX(-1);
        if( keyboard.pressed("left"))
            pobjects[movementControls.translate].translateZ(-1);
        if( keyboard.pressed("right"))
            pobjects[movementControls.translate].translateZ(1);
        if( keyboard.pressed("w"))
            pobjects[movementControls.translate].translateY(1);
        if( keyboard.pressed("s"))
            pobjects[movementControls.translate].translateY(-1);
        if( keyboard.pressed("x"))
            cobjects[movementControls.rotate].rotation.x+=0.1;
        if( keyboard.pressed("y"))
            cobjects[movementControls.rotate].rotation.y+=0.1;
        if( keyboard.pressed("z"))
            cobjects[movementControls.rotate].rotation.z+=0.1;

Basically I want to rotate,scale and translate objects using pure webGl and not three.js functions.基本上我想使用纯 webGl 而不是 Three.js 函数来旋转、缩放和平移对象。

You might want to take a look upon this tutorial on learningwebgl.com .您可能想在learningwebgl.com上查看本教程。

You basically have to add event listener for the 'keydown' and the 'keyup' events and write corresponding functions to catch the events when they occur, for example by adding the pressed keys as attributes to a function object like:您基本上必须为 'keydown' 和 'keyup' 事件添加事件侦听器,并编写相应的函数以在事件发生时捕获事件,例如通过将按下的键作为属性添加到函数对象中,例如:

var currentlyPressedKeys = { };

function onKeyDown (e) {

  currentlyPressedKeys[e.keyCode] = true;

}

function onKeyUp (e) {

  currentlyPressedKeys[e.keyCode] = false;

}

Then you'll need an other function where you define what should be done, when a certain key is pressed, like然后你需要一个其他的函数来定义当按下某个键时应该做什么,比如

if ( currentlyPressedKeys[38] ) { //...

for example.例如。 (The numbers representing the keys you can find here .) (代表您可以在此处找到的键的数字。)

So, this is where you change the variables rotationY , translationZ etc. that you pass as parameters to the functions where you rotate , scale and translate the matrices representing your objects and which are later multiplied with the original vertex position data onto the vertexshader.因此,这是您更改变量rotationYtranslationZ等的地方,您将这些变量作为参数传递给函数,在这些函数中,您旋转缩放平移表示对象的矩阵,然后这些矩阵与原始顶点位置数据相乘到顶点着色器上。

If you don't know how to do that, I'd recommend to go back to the beginning of the linked tutorial, or as well have a look at the great explanation on webglfundamentals.org .如果你不知道怎么做,我建议你回到链接教程的开头,或者看看webglfundamentals.org上的精彩解释。

Hope, this helped.希望,这有帮助。

There is one thing you should know.有一件事你应该知道。

There is no "pure WebGL" equivalent of rotating, scaling, and translating objects.没有旋转、缩放和平移对象的“纯 WebGL”等价物。 The only WebGL functionality that comes close to it is matrix multiplication implemented using shaders.唯一接近它的 WebGL 功能是使用着色器实现的矩阵乘法。 Instead, rotation and other transformations are usually provided by a JavaScript library such as GLMatrix.相反,旋转和其他转换通常由 JavaScript 库(例如 GLMatrix)提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM