THREE.JS:如何在顶点着色器中围绕对象的中心旋转对象?

时间:2019-06-02 21:55:36

标签: matrix three.js rotation glsl

我有一个简单的THREE.Points对象。必须通过着色器(绿线)将其绕其Y中心轴以及一般对象位置旋转。我已经对此进行了评论,因此它不会影响轮换问题。

enter image description here

现在它具有错误的“轨道”旋转。非常确定我需要玩tPos。尝试过没有成功。

       

		var stats, scene, renderer;
		var camera, cameraControl, mesh;

		if( !init() )	animate();

		function init(){

            renderer = new THREE.WebGLRenderer({
					antialias		: true,
					preserveDrawingBuffer	: true
				});
            renderer.setClearColor( 0xBBBBBB, 1 );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.getElementById('container').appendChild(renderer.domElement);

			scene = new THREE.Scene();

			camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 10000 );
			camera.position.set(0, 0, 5);
			scene.add(camera);

            var geometry = new THREE.BufferGeometry();

            var vertices = new Float32Array( [
                
                -1.0, -1.0,  1.0,
                1.0, -1.0,  1.0,
                1.0,  1.0,  1.0,

                1.0,  1.0,  1.0,
                -1.0,  1.0,  1.0,
                -1.0, -1.0,  1.0
                
            ] );

            geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
            
            var material = new THREE.ShaderMaterial( {

            side : THREE.DoubleSide,
                
            uniforms: {

            parentPosition: { value: new THREE.Vector3(0.0, 0.0, 0.0) },
            rotation: { type: "f", value: 0.0 },
            texture: { value: new THREE.TextureLoader().load( "assets/blob.png" ) }

            },

             vertexShader: document.getElementById( "vertexshader" ).textContent,
             fragmentShader: document.getElementById( "fragmentshader" ).textContent,
             alphaTest: 0.9

            } );
            
            mesh = new THREE.Mesh( geometry, material );
            scene.add(mesh);

		}

		function animate() {
            
                     //mesh.material.uniforms.parentPosition.value.x += 0.01;
      mesh.material.uniforms.rotation.value += 0.01;
            
			requestAnimationFrame( animate );
            
			render();

		}


		function render() {
            
			renderer.render( scene, camera );
            
		}
body{ margin: 0px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/105/three.js"></script>

<!doctype html>
<html>
	<head>
		<title>three.js template</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

        
        <script type="x-shader/x-vertex" id="vertexshader">

    uniform vec3 parentPosition;
    uniform float rotation;
    varying vec4 vColor;
    varying mat4 vPosition;
    
    void main() {
    
        mat4 tPos = mat4(vec4(1.0,0.0,0.0,0.0),
                       vec4(0.0,1.0,0.0,0.0),
                       vec4(0.0,0.0,1.0,0.0),
                       vec4(0,0,0,1.0));
                        
        mat4 rYPos = mat4(vec4(cos(rotation),0.0,sin(rotation),0.0),
                        vec4(0.0,1.0,0.0,0.0),
                        vec4(-sin(rotation),0.0,cos(rotation),0.0),
                        vec4(0.0,0.0,0.0,1.0));

        vPosition = tPos * rYPos;
        
        vColor = vec4(1.0, 0.0, 1.0, 1.0);
        vec3 newPosition = position + parentPosition;
        vec4 mvPosition = modelViewMatrix * vPosition * vec4( newPosition, 1.0 );
        gl_PointSize = 32.0 * ( 300.0 / -mvPosition.z );
        gl_Position = projectionMatrix * mvPosition;
        
    }
    
</script>

<script type="x-shader/x-fragment" id="fragmentshader">

    uniform sampler2D texture;
    varying vec4 vColor;
    varying mat4 vPosition;
    
    void main() {

        gl_FragColor = vColor;
        gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
    
    }
    
</script>
        
        
	</head>
<body>

    <div id="container"></div>

</body>
</html>

mat4 tPos = mat4(vec4(1.0,0.0,0.0,0.0),
                       vec4(0.0,1.0,0.0,0.0),
                       vec4(0.0,0.0,1.0,0.0),
                       vec4(0,0,0,1.0));

mat4 rYPos = mat4(vec4(cos(rotation),0.0,sin(rotation),0.0),
                        vec4(0.0,1.0,0.0,0.0),
                        vec4(-sin(rotation),0.0,cos(rotation),0.0),
                        vec4(0.0,0.0,0.0,1.0));

vPosition = tPos * rYPos;

vColor = vec4(1.0, 0.0, 1.0, 1.0);
vec3 newPosition = position + parentPosition;
vec4 mvPosition = modelViewMatrix * vPosition * vec4( newPosition, 1.0 );

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

这是我的错。该代码工作正常。必须将所有Z值设置为0.0。

var vertices = new Float32Array( [

-1.0, -1.0,  0.0,
1.0, -1.0,  0.0,
1.0,  1.0,  0.0,

1.0,  1.0,  0.0,
-1.0,  1.0,  0.0,
-1.0, -1.0,  0.0

] );