美文网首页
2018-08-29

2018-08-29

作者: 浪巅 | 来源:发表于2018-08-29 17:59 被阅读0次

   

   

        html, body {

            width: 100%;

            height: 100%;

            margin:0;

            padding:0;

            overflow: hidden;

        }

   

   

   

       

   

   

   

        var world;

        var fixedTimeStep = 1 / 60;

        var maxSubSteps = 3;

        var massBall = 5, massCan = 10;

        var lastTime;

        var camera, scene, renderer, controls;

        var geometry, material, materialPlane, mesh;

        var container, camera, scene, renderer;

        const ballRad = 2/2

        const canRad = 2/2

        const canHeight = 2*2

        var balls = []

        var ballControls;

        // To be synced

        var physics = new CannonHelper(0,-10,0);  physics.world.allowSleep = true;

        initThree();

        var cannonDebugRenderer = new THREE.CannonDebugRenderer(scene, world);

        initScene();

        initEvent();

        controls = new THREE.OrbitControls( camera, renderer.domElement );

        animate();

        function pileUp(xCnt, yCnt, zPos){

            var interval = 2*canRad/3;

            for (var level = 0; level < yCnt; ++level){

                var xc = xCnt - level;

                var startX = -((xc - 1)*interval + (xc*2*canRad))/2 + canRad;

                for (var i = 0; i < xc; ++i){

                    var can = new THREE.Mesh(new THREE.CylinderGeometry(canRad,canRad,canHeight,20,20,false), material);

                    can.body = physics.addBody( can, massCan );  scene.add(can);

                    can.body.position.set(startX + i*(interval + 2*canRad) , canHeight/2 + level*canHeight, zPos);   

                    ballControls.arrTarget.push(can);

                }

            }

        }

        function initScene(){

            ballControls = new BallControls(camera, renderer.domElement);

            //far plane

            var wall = new THREE.Mesh( new THREE.PlaneGeometry( 12, 12, 1, 1 ),

                                          new THREE.MeshLambertMaterial( { color: 0xf8f8f8 } ) );

            wall.position.set(0, 6, -15)

            scene.add(wall);

            ballControls.arrTarget.push(wall);

            //ground

            var ground = new THREE.Mesh( new THREE.PlaneGeometry( 12, 16, 1, 1 ), materialPlane );

            ground.quaternion.setFromAxisAngle(new THREE.Vector3(1,0,0), -Math.PI / 2);

            ground.body = physics.addBody( ground, 0 );  scene.add(ground);

            ground.body.position.set(0,0,0,)

            pileUp(3,3,-5)

            pileUp(4,4,-7)

            var ball = new THREE.Mesh(new THREE.SphereGeometry(ballRad, 20, 20), material);

            ball.body = physics.addBody( ball, massBall );  scene.add(ball);

            ball.body.position.set(0, ballRad ,5);

            ballControls.arrBall.push(ball);

        }

        function initEvent(){

            ballControls.addEventListener('selectBall', function(e){

                controls.enabled = false;

            })

            ballControls.addEventListener('throwBall', function(e){

                controls.enabled = true;

                console.log(e);

                var ball_body = e.ball.body;

                var to = e.to.sub(e.ball.position).normalize()

                to.divideScalar(e.delta).multiplyScalar (100)

                ball_body.applyLocalImpulse(to, new CANNON.Vec3(0,0,0));

                ball_body.wakeUp();

            })

        }

        function initThree() {

            container = document.createElement( 'div' );

            document.body.appendChild( container );

            // scene

            scene = new THREE.Scene();

            scene.fog = new THREE.Fog( 0xffffff, 500, 10000 );

            // camera

            camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );

            camera.position.set(0, 0.72*10, 6.36*10);

            camera.up.set(0,1,0);

            camera.lookAt(new THREE.Vector3(0,0,0));

            scene.add(camera);

            material = new THREE.MeshLambertMaterial( { color: 0x777777 } );

            materialPlane = new THREE.MeshLambertMaterial( { color: 0xf0f0f0 } );

            // lights

            scene.add( new THREE.AmbientLight( 0x111111 ) );

            var light = new THREE.DirectionalLight( 0xffffff, 1.75 );

            var d = 20;

            light.position.set( 40, 20, 30 );

            scene.add( light );

            renderer = new THREE.WebGLRenderer( { antialias: true } );

            renderer.setSize( window.innerWidth, window.innerHeight );

            renderer.setClearColor( scene.fog.color );

            var axesHelper = new THREE.AxesHelper( 5 );  scene.add( axesHelper );

            container.appendChild( renderer.domElement );

        }

        function animate(time) {

            requestAnimationFrame( animate );

            if(time && lastTime){

                var dt = time - lastTime;

                physics.update(dt / 1000, maxSubSteps);

            }

            // cannonDebugRenderer.update();

            controls.update();

            renderer.render(scene, camera);

            lastTime = time;

        }

相关文章

  • 2018-08-30

    2018-08-29 张shilian 2018-08-29 22:22 · 字数 333 · 阅读 1 · 日记...

  • 2018-08-30

    2018-08-29 c6_李晓红Dorothy 2018-08-29 07:12 · 字数 265 · 阅读 1...

  • 2018-08-29

    戴师傅 2018-08-29 2018-08-29 20:32 打开App (稻盛哲学学习会)打卡第125天 姓名...

  • (ACM)Bitcoin's Academic Pedi

    2018-08-29 Bitcoin's Academic Pedigree | ACM杂志 2017 Vol-1...

  • 【置顶】技术研发视频分享汇总

    Android 2018-08-29更新 Android架构技术 2018-08-28 更新 Android核心技...

  • 2018-08-29

    2018-08-28 张shilian 2018-08-29 22:28 · 字数 388 · 阅读 1 · 日记...

  • Hexo博客添加在线联系功能

    title: Hexo博客添加在线联系功能date: 2018-08-29 21:03:11tags:- 博客- ...

  • 非典型生意指标

    发表于2018-08-29[http://ibochina.com/archives/1542] 非典型生意指标[...

  • 2018-08-29

    2018-08-29 万千工品金秀 2018-08-28 22:22 · 字数 376 · 阅读 4 · 日记本 ...

  • 2018-08-30

    2018-08-30 万千工品金秀 2018-08-29 21:16 · 字数 464 · 阅读 2 · 日记本 ...

网友评论

      本文标题:2018-08-29

      本文链接:https://www.haomeiwen.com/subject/cqvkwftx.html