Exemplo n.º 1
0
        private void Scene15()
        {
            var scene  = new BABYLON.Scene(engine);
            var light  = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -0.5, -1.0), scene);
            var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, new BABYLON.Vector3(0, 30, 0), scene);

            camera.setPosition(new BABYLON.Vector3(20, 70, 120));
            light.position = new BABYLON.Vector3(20, 150, 70);
            camera.minZ    = 10.0;

            scene.ambientColor = new BABYLON.Color3(0.3, 0.3, 0.3);

            // Ground
            var ground         = BABYLON.Mesh.CreateGround("ground", 1000, 1000, 1, scene, false);
            var groundMaterial = new BABYLON.StandardMaterial("ground", scene);

            groundMaterial.diffuseColor  = new BABYLON.Color3(0.2, 0.2, 0.2);
            groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
            ground.material       = groundMaterial;
            ground.receiveShadows = true;

            // Shadows
            var shadowGenerator = new BABYLON.ShadowGenerator(new BABYLON.Size()
            {
                width = 1024, height = 1024
            }, light);

            // Meshes
            // Dude
            BABYLON.SceneLoader.ImportMesh(new Array <string>("him"), "", "Dude.babylon", scene, (newMeshes2, particleSystems2, skeletons2) =>
            {
                this.scene = scene;
                this.scene.activeCamera.attachControl(this.canvas);

                var dude = newMeshes2[0];

                for (var index = 0; index < newMeshes2.Length; index++)
                {
                    shadowGenerator.getShadowMap().renderList.Add(newMeshes2[index]);
                }

                dude.rotation.y = Math.PI;
                dude.position   = new BABYLON.Vector3(0, 0, -80);

                scene.beginAnimation(skeletons2[0], 0, 100, true, 1.0);
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// </summary>
        /// <param name="mapSize">
        /// </param>
        /// <param name="light">
        /// </param>
        public ShadowGenerator(Size mapSize, DirectionalLight light)
        {
            this._light                     = light;
            this._scene                     = light.getScene();
            light._shadowGenerator          = this;
            this._shadowMap                 = new RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
            this._shadowMap.wrapU           = Texture.CLAMP_ADDRESSMODE;
            this._shadowMap.wrapV           = Texture.CLAMP_ADDRESSMODE;
            this._shadowMap.renderParticles = false;
            Action <SubMesh> renderSubMesh = (SubMesh subMesh) =>
            {
                var mesh   = subMesh.getRenderingMesh();
                var scene  = this._scene;
                var engine = scene.getEngine();
                engine.setState(subMesh.getMaterial().backFaceCulling);
                var batch = mesh._getInstancesRenderList(subMesh._id);
                if (batch.mustReturn)
                {
                    return;
                }

                var hardwareInstancedRendering = (engine.getCaps().instancedArrays != null) && (batch.visibleInstances != null);
                if (this.isReady(subMesh, hardwareInstancedRendering))
                {
                    engine.enableEffect(this._effect);
                    mesh._bind(subMesh, this._effect, false);
                    var material = subMesh.getMaterial();
                    this._effect.setMatrix("viewProjection", this.getTransformMatrix());
                    if (material != null && material.needAlphaTesting())
                    {
                        var alphaTexture = material.getAlphaTestTexture();
                        this._effect.setTexture("diffuseSampler", alphaTexture);
                        this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
                    }

                    var useBones = mesh.skeleton != null && mesh.isVerticesDataPresent(VertexBufferKind.MatricesIndicesKind) &&
                                   mesh.isVerticesDataPresent(VertexBufferKind.MatricesWeightsKind);
                    if (useBones)
                    {
                        this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
                    }

                    if (hardwareInstancedRendering)
                    {
                        mesh._renderWithInstances(subMesh, false, batch, this._effect, engine);
                    }
                    else
                    {
                        if (batch.renderSelf[subMesh._id])
                        {
                            this._effect.setMatrix("world", mesh.getWorldMatrix());
                            mesh._draw(subMesh, true);
                        }

                        if (batch.visibleInstances[subMesh._id] != null)
                        {
                            for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].Length; instanceIndex++)
                            {
                                var instance = batch.visibleInstances[subMesh._id][instanceIndex];
                                this._effect.setMatrix("world", instance.getWorldMatrix());
                                mesh._draw(subMesh, true);
                            }
                        }
                    }
                }
                else
                {
                    this._shadowMap.resetRefreshCounter();
                }
            };

            this._shadowMap.customRenderFunction =
                (SmartArray <SubMesh> opaqueSubMeshes,
                 SmartArray <SubMesh> alphaTestSubMeshes,
                 SmartArray <SubMesh> transparentSubMeshes,
                 System.Action beforeTransform) =>
            {
                for (var index = 0; index < opaqueSubMeshes.Length; index++)
                {
                    renderSubMesh(opaqueSubMeshes[index]);
                }

                for (var index = 0; index < alphaTestSubMeshes.Length; index++)
                {
                    renderSubMesh(alphaTestSubMeshes[index]);
                }

                if (this._transparencyShadow)
                {
                    for (var index = 0; index < transparentSubMeshes.Length; index++)
                    {
                        renderSubMesh(transparentSubMeshes[index]);
                    }
                }
            };
        }
Exemplo n.º 3
0
        private void Scene16()
        {
            this.scene = new BABYLON.Scene(engine);
            var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
            var light  = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -1, -0.2), scene);
            var light2 = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(-1, -2, -1), scene);

            light.position  = new BABYLON.Vector3(0, 30, 0);
            light2.position = new BABYLON.Vector3(10, 20, 10);

            light.intensity  = 0.6;
            light2.intensity = 0.6;

            camera.setPosition(new BABYLON.Vector3(-40, 40, 0));
            camera.lowerBetaLimit = (Math.PI / 2) * 0.9;

            // Skybox
            var skybox         = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
            var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);

            skyboxMaterial.backFaceCulling   = false;
            skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("skybox", scene);
            skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
            skyboxMaterial.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
            skybox.material = skyboxMaterial;

            // Spheres
            var sphere0 = BABYLON.Mesh.CreateSphere("Sphere0", 16, 10, scene);
            var sphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 16, 10, scene);
            var sphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 16, 10, scene);
            var cube    = BABYLON.Mesh.CreateBox("Cube", 10.0, scene);

            var material0 = new BABYLON.StandardMaterial("white", scene);

            material0.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            material0.specularColor = new BABYLON.Color3(0, 0, 0);
            material0.emissiveColor = new BABYLON.Color3(1.0, 1.0, 1.0);
            sphere0.material        = material0;

            sphere1.material = sphere0.material;
            sphere2.material = sphere0.material;

            var material1 = new BABYLON.StandardMaterial("red", scene);

            material1.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            material1.specularColor = new BABYLON.Color3(0, 0, 0);
            material1.emissiveColor = new BABYLON.Color3(1.0, 0, 0);
            cube.material           = material1;

            // Post-process
            var blurWidth = 1.0;

            var postProcess0 = new BABYLON.PassPostProcess("Scene copy", 1.0, camera);
            var postProcess1 = new BABYLON.PostProcess("Down sample", "./postprocesses/downsample", new Array <string>("screenSize", "highlightThreshold"), null, 0.25, camera, BABYLON.Texture.BILINEAR_SAMPLINGMODE);

            postProcess1.onApply = (effect) =>
            {
                effect.setFloat2("screenSize", postProcess1.width, postProcess1.height);
                effect.setFloat("highlightThreshold", 0.90);
            };
            var postProcess2 = new BABYLON.BlurPostProcess("Horizontal blur", new BABYLON.Vector2(1.0, 0), blurWidth, 0.25, camera);
            var postProcess3 = new BABYLON.BlurPostProcess("Vertical blur", new BABYLON.Vector2(0, 1.0), blurWidth, 0.25, camera);
            var postProcess4 = new BABYLON.PostProcess("Final compose", "./postprocesses/compose", new Array <string>("sceneIntensity", "glowIntensity", "highlightIntensity"), new Array <string>("sceneSampler"), 1, camera, BABYLON.Texture.BILINEAR_SAMPLINGMODE);

            postProcess4.onApply = (effect) =>
            {
                effect.setTextureFromPostProcess("sceneSampler", postProcess0);
                effect.setFloat("sceneIntensity", 0.5);
                effect.setFloat("glowIntensity", 0.4);
                effect.setFloat("highlightIntensity", 1.0);
            };

            // Animations
            var alpha = 0.0;

            scene.registerBeforeRender(() =>
            {
                sphere0.position = new BABYLON.Vector3(20 * Math.Sin(alpha), 0, 20 * Math.Cos(alpha));
                sphere1.position = new BABYLON.Vector3(20 * Math.Sin(alpha), 0, -20 * Math.Cos(alpha));
                sphere2.position = new BABYLON.Vector3(20 * Math.Cos(alpha), 0, 20 * Math.Sin(alpha));

                cube.rotation.y += 0.01;
                cube.rotation.z += 0.01;

                alpha += 0.01;
            });

            this.scene.activeCamera.attachControl(this.canvas);
        }
Exemplo n.º 4
0
        private void Scene12()
        {
            this.scene = new BABYLON.Scene(engine);
            var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
            var light  = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -1, -0.2), scene);
            var light2 = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(-1, -2, -1), scene);

            light.position  = new BABYLON.Vector3(0, 30, 0);
            light2.position = new BABYLON.Vector3(10, 20, 10);

            light.intensity  = 0.6;
            light2.intensity = 0.6;

            camera.setPosition(new BABYLON.Vector3(-20, 20, 0));

            // Skybox
            var skybox         = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
            var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);

            skyboxMaterial.backFaceCulling   = false;
            skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("skybox", scene);
            skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
            skyboxMaterial.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
            skybox.material = skyboxMaterial;

            // Ground
            var ground         = BABYLON.Mesh.CreateGround("ground", 1000, 1000, 1, scene, false);
            var groundMaterial = new BABYLON.StandardMaterial("ground", scene);

            BABYLON.Texture diffuseTexture = null;
            if (engine.getCaps().s3tc != null)
            {
                diffuseTexture = new BABYLON.Texture("grass.dds", scene);
            }
            else
            {
                diffuseTexture = new BABYLON.Texture("grass.jpg", scene);
            }
            diffuseTexture.uScale         = 60;
            diffuseTexture.vScale         = 60;
            groundMaterial.diffuseTexture = diffuseTexture;
            groundMaterial.specularColor  = new BABYLON.Color3(0, 0, 0);
            ground.position.y             = -2.05;
            ground.material = groundMaterial;

            // Torus
            var torus = BABYLON.Mesh.CreateTorus("torus", 8, 2, 32, scene, false);

            torus.position.y = 6.0;
            var torus2 = BABYLON.Mesh.CreateTorus("torus2", 4, 1, 32, scene, false);

            torus2.position.y = 6.0;

            var torusMaterial = new BABYLON.StandardMaterial("torus", scene);

            torusMaterial.diffuseColor  = new BABYLON.Color3(0.5, 0.5, 0.5);
            torusMaterial.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
            torus.material  = torusMaterial;
            torus2.material = torusMaterial;

            // Shadows
            var shadowGenerator = new BABYLON.ShadowGenerator(new BABYLON.Size()
            {
                width = 512, height = 512
            }, light);

            shadowGenerator.getShadowMap().renderList.Add(torus);
            shadowGenerator.getShadowMap().renderList.Add(torus2);
            shadowGenerator.usePoissonSampling = true;

            var shadowGenerator2 = new BABYLON.ShadowGenerator(new BABYLON.Size()
            {
                width = 512, height = 512
            }, light2);

            shadowGenerator2.getShadowMap().renderList.Add(torus);
            shadowGenerator2.getShadowMap().renderList.Add(torus2);
            shadowGenerator2.useVarianceShadowMap = true;

            ground.receiveShadows = true;

            System.Action beforeRenderFunction = () =>
            {
                // Camera
                if (camera.beta < 0.1)
                {
                    camera.beta = 0.1;
                }
                else if (camera.beta > (Math.PI / 2) * 0.99)
                {
                    camera.beta = (Math.PI / 2) * 0.99;
                }

                if (camera.radius > 150)
                {
                    camera.radius = 150;
                }

                if (camera.radius < 5)
                {
                    camera.radius = 5;
                }
            };

            scene.registerBeforeRender(beforeRenderFunction);

            // Animations
            scene.registerBeforeRender(() =>
            {
                torus.rotation.x  += 0.01;
                torus.rotation.z  += 0.02;
                torus2.rotation.x += 0.02;
                torus2.rotation.y += 0.01;
            });

            this.scene.activeCamera.attachControl(this.canvas);
        }
Exemplo n.º 5
0
        private void Scene9()
        {
            this.scene = new BABYLON.Scene(engine);
            var camera   = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
            var light0   = new BABYLON.PointLight("Omni0", new BABYLON.Vector3(0, 10, 0), scene);
            var light1   = new BABYLON.PointLight("Omni1", new BABYLON.Vector3(0, -10, 0), scene);
            var light2   = new BABYLON.PointLight("Omni2", new BABYLON.Vector3(10, 0, 0), scene);
            var light3   = new BABYLON.DirectionalLight("Dir0", new BABYLON.Vector3(1, -1, 0), scene);
            var material = new BABYLON.StandardMaterial("kosh", scene);
            var sphere   = BABYLON.Mesh.CreateSphere("Sphere", 16, 3, scene);

            camera.setPosition(new BABYLON.Vector3(-10, 10, 0));

            light3.parent = camera;

            // Creating light sphere
            var lightSphere0 = BABYLON.Mesh.CreateSphere("Sphere0", 16, 0.5, scene);
            var lightSphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 16, 0.5, scene);
            var lightSphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 16, 0.5, scene);

            var material1 = new BABYLON.StandardMaterial("red", scene);

            material1.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            material1.specularColor = new BABYLON.Color3(0, 0, 0);
            material1.emissiveColor = new BABYLON.Color3(1, 0, 0);
            lightSphere0.material   = material1;

            var material2 = new BABYLON.StandardMaterial("green", scene);

            material2.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            material2.specularColor = new BABYLON.Color3(0, 0, 0);
            material2.emissiveColor = new BABYLON.Color3(0, 1, 0);
            lightSphere1.material   = material2;

            var material3 = new BABYLON.StandardMaterial("blue", scene);

            material3.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            material3.specularColor = new BABYLON.Color3(0, 0, 0);
            material3.emissiveColor = new BABYLON.Color3(0, 0, 1);
            lightSphere2.material   = material3;

            // Sphere material
            material.diffuseColor = new BABYLON.Color3(1, 1, 1);
            sphere.material       = material;

            // Lights colors
            light0.diffuse  = new BABYLON.Color3(1, 0, 0);
            light0.specular = new BABYLON.Color3(1, 0, 0);

            light1.diffuse  = new BABYLON.Color3(0, 1, 0);
            light1.specular = new BABYLON.Color3(0, 1, 0);

            light2.diffuse  = new BABYLON.Color3(0, 0, 1);
            light2.specular = new BABYLON.Color3(0, 0, 1);

            light3.diffuse  = new BABYLON.Color3(1, 1, 1);
            light3.specular = new BABYLON.Color3(1, 1, 1);

            // Skybox
            var skybox         = BABYLON.Mesh.CreateBox("skyBox", 100.0, scene);
            var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);

            skyboxMaterial.backFaceCulling   = false;
            skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("skybox", scene);
            skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
            skyboxMaterial.diffuseColor  = new BABYLON.Color3(0, 0, 0);
            skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
            skybox.material         = skyboxMaterial;
            skybox.infiniteDistance = true;

            // Animations
            var alpha = 0.0;

            scene.registerBeforeRender(() =>
            {
                light0.position = new BABYLON.Vector3(10 * Math.Sin(alpha), 0, 10 * Math.Cos(alpha));
                light1.position = new BABYLON.Vector3(10 * Math.Sin(alpha), 0, -10 * Math.Cos(alpha));
                light2.position = new BABYLON.Vector3(10 * Math.Cos(alpha), 0, 10 * Math.Sin(alpha));

                lightSphere0.position = light0.position;
                lightSphere1.position = light1.position;
                lightSphere2.position = light2.position;

                alpha += 0.01;
            });

            this.scene.activeCamera.attachControl(this.canvas);
        }