Ejemplo n.º 1
0
        private void Update(Scene scene, Camera camera)
        {
            // set GL state for depth map
		    GL.ClearColor( 1, 1, 1, 1 );
		    GL.Disable( EnableCap.Blend);
            renderer.DepthTest = true;

		// update scene
		if ( scene.AutoUpdate) scene.UpdateMatrixWorld();

		// update camera matrices and frustum
		camera.matrixWorldInverse = Matrix4.GetInverse(camera.matrixWorld);
            projectionScreenMatrix.MultiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
		frustum = Frustum.FromMatrix( projectionScreenMatrix );

		// render depth map
            renderer.SetRenderTarget(renderTarget );
		renderer.Clear();

		// set object matrices & frustum culling
		renderList.Clear();
		ProjectObject(scene,scene,camera);

		// render regular objects
            foreach(var glObject in renderList) 
            {
			var o = glObject.Object;
			var buffer = glObject.Buffer;

			// TODO: create proper depth material for particles
			if(o is PointCloud /*&& !o.customDepthMaterial*/) continue;

           // var useMorphing = o.geometry.MorphTargets != null && o.geometry.MorphTargets.Count > 0; /* TODO IAN, need this for skinning? && object.Material.morphTargets */
           //     var useSkinning = o is SkinnedMesh; /* TODO IAN, need this for skinning? && object.Material.skinning */

                Material material;
			if (o.customDepthMaterial != null) material = o.customDepthMaterial;
			//else if ( useSkinning ) material = useMorphing ? depthMaterialMorphSkin : depthMaterialSkin;
            //else if ( useMorphing ) material = depthMaterialMorph;
            else material = depthMaterial;
                
			renderer.RenderBuffer( camera, scene.lights, null, material, buffer, o );
		}

		// restore GL state
		var clearColor = renderer.ClearColor;
            GL.ClearColor(clearColor.R, clearColor.G, clearColor.B, 1 );
            GL.Enable( EnableCap.Blend);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var mediaPath = Path.GetFullPath("../../../../../js/r68/examples/");
            var texturesPath = Path.Combine(mediaPath, "textures");

            var renderer = new Renderer();

            var scene = new Scene()
            {
                //Fog = new FogExp2(Color.Blue, 0.24f)
            };

            var camera = new OrthographicCamera(renderer, -1000, 1000)
            {
                Position = new Vector3(0, 0, 2)
            };

            //// create a point light
            scene.Add(new DirectionalLight(Color.White)
            {
                Target = Vector3.UnitX
            });


            var geometry = new BoxGeometry(20, 20, 20);

            for (var i = 0; i < 2000; i++)
            {
                var o = new Mesh(geometry, new MeshLambertMaterial(renderer) { Diffuse = Color.Random() });
                o.Position = new Vector3(Mathf.RandomF(-400, 400), Mathf.RandomF(-400, 400), Mathf.RandomF(-400, 400));
                o.Rotation = new Euler(Mathf.Tau * Mathf.RandomF(), Mathf.Tau * Mathf.RandomF(), Mathf.Tau * Mathf.RandomF());
                o.Scale = new Vector3(Mathf.RandomF(0.5f, 1.5f), Mathf.RandomF(0.5f, 1.5f), Mathf.RandomF(0.5f, 1.5f));
                scene.Add(o);
            }

            var raycaster = new Raycaster();
            Object3D INTERSECTED = null;
            Color previousColor = Color.White;

            var radius = 100;
            var previousTime = 0f;
            var stopwatch = Stopwatch.StartNew();
            while (!renderer.Done)
            {
                var now = (float)stopwatch.Elapsed.TotalSeconds;
                var deltaTime = now - previousTime;
                previousTime = now;

                var offset = now / 4;
                var sin = Mathf.Sin(offset) * radius;
                var cos = Mathf.Cos(offset) * radius;
                camera.Position = new Vector3(sin, sin, cos);
                camera.LookAt(Vector3.Zero);

                #region FindIntersections
                var vector = Projector.UnprojectVector(new Vector3(renderer.MousePositionNormalized,-1),camera.projectionMatrix, camera.matrixWorld);
                var direction = new Vector3(0, 0, -1);
                direction.TransformDirection(camera.matrixWorld);

                raycaster.Set(vector, direction);

                var intersects = raycaster.IntersectObjects(scene.Children);

                if (intersects != null &&  intersects.Count > 0)
                {
                    var first = intersects[0];

                    if (INTERSECTED != first.Object)
                    {
                        if (INTERSECTED != null)
                        {
                            var basic = INTERSECTED.Material as MeshLambertMaterial;
                            basic.Emissive = previousColor;
                        }

                        var firstMat = first.Object.Material as MeshLambertMaterial;

                        INTERSECTED = first.Object;
                        previousColor = firstMat.Emissive;
                        firstMat.Emissive = Color.Red;
                    }
                }
                else
                {
                    if (INTERSECTED != null)
                    {
                        (INTERSECTED.Material as MeshLambertMaterial).Emissive = previousColor;
                    }

                    INTERSECTED = null;

                }
                #endregion

                renderer.RenderFrame(scene, camera);
            }
        }
Ejemplo n.º 3
0
        private void Update(Scene scene, Camera camera)
        {
            // set GL state for depth map
            GL.ClearColor(1, 1, 1, 1);
            GL.Disable(EnableCap.Blend);
            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Ccw);

            GL.CullFace(renderer.shadowMapCullFace);

            renderer.DepthTest = true;
            var lights = new List<Light>();

            // preprocess lights
            // 	- skip lights that are not casting shadows
            //	- create virtual lights for cascaded shadow maps
            foreach (var light in scene.lights)
            {
                if (!light.DoesCastShadow) continue;

                var shadowLight = light as HasShadow;
                if (shadowLight != null && shadowLight.ShadowCascade)
                {
                    for (var n = 0; n < shadowLight.ShadowCascadeCount; n++)
                    {
                        VirtualLight virtualLight;

                        if (shadowLight.ShadowCascadeArray[n] == null)
                        {
                            virtualLight = CreateVirtualLight(light, n);
                            virtualLight.OriginalCamera = camera;

                            var gyro = new Gyroscope();
                            gyro.Position = shadowLight.ShadowCascadeOffset;

                            gyro.Add(virtualLight);
                            //gyro.Add(virtualLight.Target);
                            camera.Add(gyro);

                            shadowLight.ShadowCascadeArray[n] = virtualLight;
                            Debug.WriteLine("Created virtualLight {0}", virtualLight);
                        }
                        else
                        {
                            virtualLight = shadowLight.ShadowCascadeArray[n];
                        }

                        UpdateVirtualLight(light, n);
                        lights.Add(virtualLight);
                    }
                }
                else
                {
                    lights.Add(light);
                }

            }

            // render depth map
            foreach (var light in lights)
            {
                var hasShadow = light as HasShadow;
                if (hasShadow.shadowMap == null)
                {
                    var isSoftShadow = renderer.shadowMapType == ShadowType.PCFSoftShadowMap;
                    hasShadow.shadowMap = new RenderTarget(hasShadow.ShadowMapWidth, hasShadow.ShadowMapHeight);
                    hasShadow.shadowMap.MinFilter = isSoftShadow ? TextureMinFilter.Nearest : TextureMinFilter.Linear;
                    hasShadow.shadowMap.MagFilter = isSoftShadow ? TextureMagFilter.Nearest : TextureMagFilter.Linear; ;
                    hasShadow.shadowMap.Format = Three.Net.Renderers.PixelFormat.RGBA;

                    hasShadow.ShadowMapSize = new Vector2(hasShadow.ShadowMapWidth, hasShadow.ShadowMapHeight);
                    hasShadow.ShadowMatrix = Matrix4.Identity;
                }

                if (hasShadow.ShadowCamera == null)
                {

                    if (hasShadow is SpotLight) hasShadow.ShadowCamera = new PerspectiveCamera(renderer, hasShadow.ShadowCameraFov, hasShadow.ShadowCameraNear, hasShadow.ShadowCameraFar);
                    else if (hasShadow is DirectionalLight) hasShadow.ShadowCamera = new OrthographicCamera(hasShadow.ShadowCameraLeft, hasShadow.ShadowCameraRight, hasShadow.ShadowCameraTop, hasShadow.ShadowCameraBottom, hasShadow.ShadowCameraNear, hasShadow.ShadowCameraFar);
                    else throw new Exception("Unsupported light type for shadow");

                    scene.Add(hasShadow.ShadowCamera);
                    if (scene.AutoUpdate) scene.UpdateMatrixWorld();

                }

                if (hasShadow.ShadowCameraVisible /* && light.CameraHelper == null*/)
                {
                    throw new NotImplementedException();
                    //light.CameraHelper = new CameraHelper( hasShadow.ShadowCamera );
                    //hasShadow.ShadowCamera.Add( light.CameraHelper );
                }

                var virtualLight = light as VirtualLight;
                if (virtualLight != null && virtualLight.OriginalCamera == camera)
                {
                    UpdateShadowCamera(camera, light);
                }

                var shadowMap = hasShadow.shadowMap;
                var shadowMatrix = hasShadow.ShadowMatrix;
                var shadowCamera = hasShadow.ShadowCamera;

                shadowCamera.Position = Vector3.FromPosition(light.matrixWorld);
                matrixPosition = (light as HasTarget).Target;
                shadowCamera.LookAt(matrixPosition);
                shadowCamera.UpdateMatrixWorld();

                shadowCamera.matrixWorldInverse = Matrix4.GetInverse(shadowCamera.matrixWorld);

                //TODO : Creating helpers if ( light.cameraHelper ) light.cameraHelper.visible = light.shadowCameraVisible;
                //if (hasShadow.ShadowCameraVisible) light.cameraHelper.update();

                // compute shadow matrix

                shadowMatrix = new Matrix4(0.5f, 0.0f, 0.0f, 0.5f,
                                            0.0f, 0.5f, 0.0f, 0.5f,
                                            0.0f, 0.0f, 0.5f, 0.5f,
                                            0.0f, 0.0f, 0.0f, 1.0f);

                shadowMatrix.Multiply(shadowCamera.projectionMatrix);
                shadowMatrix.Multiply(shadowCamera.matrixWorldInverse);

                // update camera matrices and frustum
                projectionScreenMatrix.MultiplyMatrices(shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse);
                frustum = Frustum.FromMatrix(projectionScreenMatrix);

                // render shadow map
                renderer.SetRenderTarget(shadowMap);
                renderer.Clear();

                // set object matrices & frustum culling

                renderList.Clear();
                ProjectObject(scene, scene, shadowCamera);

                // render regular objects
                foreach (var glObject in renderList)
                {
                    var o = glObject.Object;
                    var buffer = glObject.Buffer;

                    throw new NotImplementedException();
                }
            }

            // restore GL state
            var clearColor = renderer.ClearColor;
            GL.ClearColor(clearColor.R, clearColor.G, clearColor.B, 1);
            GL.Enable(EnableCap.Blend);

            if (renderer.shadowMapCullFace == CullFaceMode.Front) GL.CullFace(CullFaceMode.Back);

        }
Ejemplo n.º 4
0
        protected internal override void Render(Scene scene, Camera camera, int viewportWidth, int viewportHeight)
        {
            if (!(renderer.shadowMapEnabled && renderer.shadowMapAutoUpdate)) return;

            Update(scene, camera);
        }
Ejemplo n.º 5
0
        private void ProjectObject(Scene scene, Object3D o, Camera shadowCamera)
        {
            if (o.IsVisible)
            {
                List<Scene.BufferInfo> glObjects;
                if (scene.glObjects.TryGetValue(o.Id, out glObjects))
                {
                    if (o.DoesCastShadow && (!o.frustumCulled || frustum.IntersectsObject(o)))
                    {
                        foreach(var glObject in glObjects)
                        { 
                            o.modelViewMatrix.MultiplyMatrices(shadowCamera.matrixWorldInverse, o.matrixWorld);
                            renderList.Add(glObject);
                        }
                    }

                    foreach (var c in o.Children) ProjectObject(scene, c, shadowCamera);
                }
            }
        }
Ejemplo n.º 6
0
 internal protected abstract void Render(Scene scene, Camera camera, int viewportWidth, int viewportHeight);
Ejemplo n.º 7
0
        private static void Init()
        {
            mediaPath = Path.GetFullPath("../../../../../js/r68/examples/");
            texturesPath = Path.Combine(mediaPath, "textures");
            renderer = new Renderer();

            scene = new Scene()
            {
                Fog = new FogLinear(Color.Black, 1500,2100)
            };

            camera = new PerspectiveCamera(renderer, 60, 1, 2100)
            {
                Position = new Vector3(0, 0, 1500)
            };


            cameraOrtho = new OrthographicCamera(renderer, 1, 10000)
        {
            Position = new Vector3(0, 0, 150)
        };
            sceneOrtho = new Scene();

            //var amount = 200;
            //var radius = 500;


            sceneOrtho.Add(new Mesh(new SphereGeometry(100, 50, 50), new MeshBasicMaterial(renderer) { Diffuse = Color.Red}));
            

            var group = new Object3D();

            var materialA = new SpriteMaterial(renderer)
            {
                DiffuseMap = new Texture(Path.Combine(texturesPath, "sprite0.png")),
                Diffuse = Color.White,
                UseFog = true,
            };

            spriteTL = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width,materialA.DiffuseMap.Resolution.Height,1),                
            };
            sceneOrtho.Add(spriteTL);

            spriteTR = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteTR);

            spriteBL = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteBL);

            spriteBR = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteBR);

            spriteC = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteC);

            UpdateHUDSprites();

            var materialB = new SpriteMaterial(renderer)
            {
                DiffuseMap = new Texture(Path.Combine(texturesPath, "sprite1.png")),
                Diffuse = Color.White,
                UseFog = true,
            };

            var materialC = new SpriteMaterial(renderer)
            {
                DiffuseMap = new Texture(Path.Combine(texturesPath, "sprite2.png")),
                Diffuse = Color.White,
                UseFog = true,
            };

            mediaPath = Path.Combine(mediaPath, "../../tests/");

        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            var mediaPath = Path.GetFullPath("../../../../../js/r68/examples/");
            var texturesPath = Path.Combine(mediaPath, "textures");

            var renderer = new Renderer();

            var scene = new Scene()
            {
                //Fog = new FogExp2(Color.Blue, 0.24f)
            };

            var camera = new PerspectiveCamera(renderer, 75)
            {
                Position = new Vector3(0, 0, 2)
            };

            //// create a point light
            scene.Add(new PointLight(Color.Gold)
            {
                Position = new Vector3(3, 2, 2),
                Intensity = 1
            });
            
            scene.Add(new DirectionalLight(Color.Blue)
            {
                Target = new Vector3(1,0,0),
               Intensity = 2
            });

            SpotLight s = new SpotLight(Color.Red)
            {
                Intensity = 5,
                Distance = 10,
                Angle = 106.7f,
                Exponent = 200,
                Position = new Vector3(-2, 0, 4),
                Target = new Vector3(0,0,0),
            };

            scene.Add(s);
            
            //var loader = new JsonLoader();
            mediaPath = Path.Combine(mediaPath, "../../tests/");
            //var path = Path.Combine(mediaPath, "frankie.obj");
            //var fullpath = Path.GetFullPath(path);
            //Debug.Assert(File.Exists(fullpath));
            //var frankie = ObjMtlLoader.Parse(renderer, fullpath);
            //frankie.Scale.Multiply(0.4f);
            //scene.Add(frankie);
            //var json = JObject.Parse(File.ReadAllText(fullpath));
            //var info = loader.Parse(renderer, json, mediaPath);
            //var monkey = new Mesh(info.Geometry, new MeshFaceMaterial(renderer, info.Materials));
            //scene.Add(monkey);

                var materialMap = new MeshPhongMaterial(renderer)
                //var materialMap = new MeshBasicMaterial(renderer)
                //var materialMap = new MeshLambertMaterial(renderer)
                {
                    Hardness = 1,

                    Shininess = 10,

                    Specular = new Color(.001f, .05f, .1f),

                    //DiffuseColor = Color.Red,
                    //Ambient = Color.Red,
                    ShouldWrapAround = true,

                    NormalMap = new Texture(Path.Combine(texturesPath, "planets\\earth_normal_2048.jpg")),
                    SpecularMap = new Texture(Path.Combine(texturesPath, "planets\\earth_specular_2048.jpg")),
                    DiffuseMap = new Texture(Path.Combine(texturesPath, "planets\\earth_atmos_2048.jpg")),

                    //DiffuseColor = Color.Red,

                };

                var materialNoMap = new MeshPhongMaterial(renderer)
                //var materialNoMap = new MeshBasicMaterial(renderer)
                //var materialNoMap = new MeshLambertMaterial(renderer)
                {
                    Hardness = 1,

                    Shininess = 10,

                    Specular = new Color(.001f, .05f, .1f),

                    ShouldWrapAround = true,

                    NormalMap = new Texture(Path.Combine(texturesPath, "planets\\earth_normal_2048.jpg")),
                    SpecularMap = new Texture(Path.Combine(texturesPath, "planets\\earth_specular_2048.jpg")),
                    DiffuseMap = new Texture(Path.Combine(texturesPath, "planets\\earth_atmos_2048.jpg")),

                    //DiffuseColor = Color.Blue,
                };

            var cube = CreateCube(renderer, scene, texturesPath, materialMap);
            var cube2
                = CreateCube(renderer, scene, texturesPath, materialNoMap);
            var cube3 = CreateCube(renderer, scene, texturesPath, materialMap);

            var p = cube.Position;

            //p.x -= 2;

            cube.Position = p;

            p = cube2.Position;
            p.x += 2;

            cube2.Position = p;

            scene.Add(cube);
            //scene.Add(cube2);
            //scene.Add(cube3);
            //frankie.Add(cube);
            //cube.Position = new Vector3(0, 1, 1);

            var lines = CreateHilbertCube(renderer, scene);
            lines.Scale = new Vector3(4, 4, 4);
            //scene.Add(lines);
            //lines.Position = new Vector3(0, 1, -1);

            var arrowHelper = ArrowHelper.Create(renderer, new Vector3(1,1,0), Vector3.Zero, 0.35f, Color.Red);
            arrowHelper.Position = new Vector3(0.25f, 0.25f, 0.25f);
            //scene.Add(arrowHelper);

            var axisHelper = AxisHelper.Create(renderer, 0.25f);
            axisHelper.Position = new Vector3(-0.25f, 0.25f, 0.25f);
            //scene.Add(axisHelper);

            //var boundingBoxHelper = BoundingBoxHelper.Create(renderer, frankie);
            //scene.Add(boundingBoxHelper);

            var previousTime = 0f;
            var stopwatch = Stopwatch.StartNew();
            while (!renderer.Done)
            {
                var now = (float)stopwatch.Elapsed.TotalSeconds;
                var deltaTime = now - previousTime;
                previousTime = now;

                //var offset = material.DiffuseMap.Offset;
                //offset.x += deltaTime * 0.75f;
                //offset.y -= deltaTime * 0.5f;
                //material.DiffuseMap.Offset = offset;

                //var r = frankie.Rotation;
                //r.y += deltaTime;
                //frankie.Rotation = r;

                //r = axisHelper.Rotation;
                //r.x = Mathf.Pi / 4;
                //r.y -= deltaTime;
                //axisHelper.Rotation = r;
                
                var r = cube.Rotation;
                r.y -= deltaTime / 2;
                r.x = Mathf.Sin(now / 4);
                cube.Rotation = r;

                r = cube2.Rotation;
                r.y -= deltaTime / 2;
                cube2.Rotation = r;
                //boundingBoxHelper.Update();

                renderer.RenderFrame(scene, camera);
            }
        }
Ejemplo n.º 9
0
        private static Line CreateHilbertCube(Renderer renderer, Scene scene)
        {
            var points = Mathf.Hilbert3D(Vector3.Zero, 0.25f, 2);
            var spline = new SplineCurve3(points);
            var geometrySpline = new Geometry();
            geometrySpline.vertexColors = new List<Color>();
            var subdivisions = 6;
            var count = points.Count * subdivisions;
            for (var i = 0; i < count; i++)
            {
                var index = i / (float)(points.Count * subdivisions);
                var position = spline.InterpolatedPoint(index);
                geometrySpline.vertices.Add(position);

                var hsl = Color.FromHSV(index, 1, 1);
                geometrySpline.vertexColors.Add(hsl);
            }

            var lines = new Line(geometrySpline, new LineDashedMaterial(renderer)
            {
                //DiffuseColor = Color.Cornflowerblue,
                VertexColors = VertexColorMode.Vertex,
                LineWidth = 0.01f,
                DashSize = 0.006f,
                GapSize = 0.002f,
            });
            return lines;
        }
Ejemplo n.º 10
0
 private static Mesh CreateCube(Renderer renderer, Scene scene, string texturesPath, MeshBasicMaterial material)
 {
     var cube = new Mesh(new SphereGeometry(1, 50, 50), material);
     return cube;
 }
Ejemplo n.º 11
0
 protected internal override void Render(Scene scene, Camera camera, int viewportWidth, int viewportHeight)
 {
     if (!IsEnabled) return;
     Update(scene, camera);
 }
Ejemplo n.º 12
0
        protected internal override void Render(Scene scene, Camera camera, int viewportWidth, int viewportHeight)
        {
            sprites.Clear();

            scene.TraverseVisible(child =>
            {
                if (child is Sprite)
                {
                    sprites.Add(child as Sprite);
                }
            });

            if (sprites.Count == 0) return;

            // setup gl
            GL.UseProgram(program);

            GL.EnableVertexAttribArray(attributes["position"]);
            GL.EnableVertexAttribArray(attributes["uv"]);

            GL.Disable(EnableCap.CullFace);
            GL.Enable(EnableCap.Blend);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
            GL.VertexAttribPointer(attributes["position"], 2, VertexAttribPointerType.Float, false, 2 * 8, 0);
            GL.VertexAttribPointer(attributes["uv"], 2, VertexAttribPointerType.Float, false, 2 * 8, 8);

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, elementBuffer);

            GL.UniformMatrix4(uniforms["projectionMatrix"], 1, false, camera.projectionMatrix.elements);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.Uniform1(uniforms["map"], 0);

            var oldFogType = 0;
            var sceneFogType = 0;
            var fog = scene.Fog;

            if (fog != null)
            {
                GL.Uniform3(uniforms["fogColor"], fog.Color.R, fog.Color.G, fog.Color.B);
                var linear = fog as FogLinear;

                if (linear != null)
                {
                    GL.Uniform1(uniforms["fogNear"], linear.Near);
                    GL.Uniform1(uniforms["fogFar"], linear.Far);

                    GL.Uniform1(uniforms["fogType"], 1);
                    oldFogType = 1;
                    sceneFogType = 1;
                }
                else
                {
                    var exponential = fog as FogExp2;
                    if (exponential != null)
                    {
                        GL.Uniform1(uniforms["fogDensity"], exponential.Density);
                        GL.Uniform1(uniforms["fogType"], 2);
                        oldFogType = 2;
                        sceneFogType = 2;
                    }
                    else
                    {
                        GL.Uniform1(uniforms["fogType"], 0);
                        oldFogType = 0;
                        sceneFogType = 0;
                    }
                }


                // update positions and sort
                foreach (var sprite in sprites)
                {
                    var material = sprite.Material;
                    sprite.modelViewMatrix.MultiplyMatrices(camera.matrixWorldInverse, sprite.matrixWorld);
                    sprite.Zdepth = -sprite.modelViewMatrix.elements[14];
                }
                sprites.Sort(painterSortStable);

                // render all sprites
                foreach (var sprite in sprites)
                {
                    var material = sprite.Material as SpriteMaterial;

                    GL.Uniform1(uniforms["alphaTest"], material.ShouldAlphaTest);
                    GL.UniformMatrix4(uniforms["modelViewMatrix"], 1, false, sprite.modelViewMatrix.elements);

                    var fogType = 0;

                    if (scene.Fog != null && material.UseFog)
                    {
                        fogType = sceneFogType;
                    }

                    if (oldFogType != fogType)
                    {
                        GL.Uniform1(uniforms["fogType"], fogType);
                        oldFogType = fogType;
                    }

                    if (material.DiffuseMap != null)
                    {
                        GL.Uniform2(uniforms["uvOffset"], material.DiffuseMap.Offset.x, material.DiffuseMap.Offset.y);
                        GL.Uniform2(uniforms["uvScale"], material.DiffuseMap.Repeat.x, material.DiffuseMap.Repeat.y);
                    }
                    else
                    {
                        GL.Uniform2(uniforms["uvOffset"], 0, 0);
                        GL.Uniform2(uniforms["uvScale"], 1, 1);
                    }

                    GL.Uniform1(uniforms["opacity"], material.Opacity);
                    GL.Uniform3(uniforms["color"], material.Diffuse.R, material.Diffuse.G, material.Diffuse.B);

                    GL.Uniform1(uniforms["rotation"], material.Rotation);
                    GL.Uniform2(uniforms["scale"], sprite.Scale.x, sprite.Scale.y);

                    renderer.SetBlending(material.Blending, material.BlendEquation, material.BlendSource, material.BlendDestination);
                    renderer.DepthTest = material.ShouldDepthTest;
                    renderer.DepthWrite = material.ShouldDepthWrite;

                    if (material.DiffuseMap != null && material.DiffuseMap.Resolution.Width > 0) renderer.SetTexture(material.DiffuseMap, 0);
                    else renderer.SetTexture(texture, 0);

                    GL.DrawElements(BeginMode.Triangles, 6, DrawElementsType.UnsignedShort, 0);

                }

                // restore gl
                GL.Enable(EnableCap.CullFace);
            }
        }
Ejemplo n.º 13
0
        private static void Init()
        {
            camera = new PerspectiveCamera(renderer, 30, 1, 10000 )
            {
                Position = new Vector3(0,0,100)
            };

				cameraRTT = new OrthographicCamera(renderer, -10000, 10000 )
                {
                    Position= new Vector3(0,0,100)
                };

            scene = new Scene();
            sceneRTT = new Scene();
		    sceneScreen = new Scene();

				var light = new DirectionalLight( Color.White )
                {
                    Position = Vector3.UnitZ.Normalized()
                };
            sceneRTT.Add( light );

				light = new DirectionalLight(new Color(0xffaaaa))
                {
                    Position = Vector3.UnitNegativeZ.Normalized(),
                    Intensity = 1.5f
                };
				sceneRTT.Add( light );

				rtTexture = new RenderTarget(renderer.Width, renderer.Height)
            {
                MinFilter = TextureMinFilter.Linear,
                MagFilter = TextureMagFilter.Nearest,
                Format = Three.Net.Renderers.PixelFormat.RGB
            };

            var vertexShaderSource = @"
varying vec2 vUv;
void main() 
{
	vUv = uv;
	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}";

            var fragment_shader_screenSource = @"
varying vec2 vUv;
uniform sampler2D tDiffuse;
void main() 
{
	gl_FragColor = texture2D( tDiffuse, vUv );
}";
            var fragment_shader_pass_1Source = @"
varying vec2 vUv;
uniform float time;
void main() 
{
	float r = vUv.x;
	if( vUv.y < 0.5 ) r = 0.0;
	float g = vUv.y;
	if( vUv.x < 0.5 ) g = 0.0;

	gl_FragColor = vec4( r, g, time, 1.0 );
}";
				material = new CustomShaderMaterial(renderer,vertexShaderSource,fragment_shader_pass_1Source, m => { });

				var materialScreen = new CustomShaderMaterial(renderer, vertexShaderSource, fragment_shader_screenSource, m => {})
                {
                    ShouldDepthWrite = false
                };

				var plane = new PlaneGeometry( renderer.Width, renderer.Height);
				var quad = new Mesh( plane, material )
                {
                    Position = new Vector3(0,0,-100)
                };
            sceneRTT.Add( quad );

				var geometry = new TorusGeometry( 100, 25, 15, 30 );

				var mat1 = new MeshPhongMaterial(renderer)
                {
                    Diffuse = new Color(0x555555),
                    Specular = new Color(0xffaa00),
                    Shininess = 5 
                };
				var mat2 = new MeshPhongMaterial(renderer)
                {
                    Diffuse = new Color(0x550000),
                    Specular = new Color(0xff2200),
                    Shininess = 5 
                };

				zmesh1 = new Mesh( geometry, mat1 )
                {
                    Position = new Vector3( 0, 0, 100 ),
                    Scale = new Vector3( 1.5f, 1.5f, 1.5f )
                };
				sceneRTT.Add( zmesh1 );

				zmesh2 = new Mesh( geometry, mat2 )
                {
                    Position = new Vector3( 0, 150, 100 ),
                    Scale = new Vector3( 0.75f, 0.75f, 0.75f)
                };
				sceneRTT.Add( zmesh2 );

				quad = new Mesh( plane, materialScreen ){
                    Position = new Vector3(0,0,-100)
                };
				sceneScreen.Add( quad );

				var n = 5;
            var sphereGeometry = new SphereGeometry( 10, 64, 32 );
            var material2 = new MeshBasicMaterial(renderer)
            {
                Diffuse = Color.White,
                DiffuseMap = rtTexture
            };

				for( var j = 0; j < n; j ++ ) {

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

                        var mesh = new Mesh(sphereGeometry, material2)
                        {
                            Position = new Vector3(
                                ( i - ( n - 1 ) / 2 ) * 20, 
                                ( j - ( n - 1 ) / 2 ) * 20,
                                0),
                                Rotation = new Euler(0,-Mathf.Pi / 2, 0)
                        };
                        scene.Add( mesh );
					}
				}

                renderer.ShouldAutoClear = false;
        }