예제 #1
0
        private static void ParseMaterials(string fileName)
        {
            MeshBasicMaterial currentMaterial = null;
            var path = Path.GetDirectoryName(fileName);
            var lines = File.ReadAllLines(fileName);
            foreach (var line in lines)
            {
                if (line.Length == 0 || line.StartsWith("#")) continue;

                var parts = line.Trim().Split(' ');

                if (parts[0] == "newmtl")
                {
                    currentMaterial = new MeshPhongMaterial(renderer)
                    {
                        Name = parts[1].Trim()
                    };
                    materials.Add(currentMaterial.Name, currentMaterial);
                }
                else if (parts[0] == "Ns")
                {
                    var phong = currentMaterial as MeshPhongMaterial;
                    if (phong != null)
                    {
                        var hardness = Convert.ToSingle(parts[1]);
                        phong.Hardness = hardness;
                    }
                }
                else if (parts[0] == "Ni")
                {

                }
                else if (parts[0] == "Ka")
                {
                    var lambert = currentMaterial as MeshLambertMaterial;
                    if (lambert != null)
                    {
                        var r = Convert.ToSingle(parts[1]);
                        var g = Convert.ToSingle(parts[2]);
                        var b = Convert.ToSingle(parts[3]);
                        lambert.Ambient = new Color(r, g, b);
                    }
                }
                else if (parts[0] == "Kd")
                {
                    var r = Convert.ToSingle(parts[1]);
                    var g = Convert.ToSingle(parts[2]);
                    var b = Convert.ToSingle(parts[3]);
                    currentMaterial.Diffuse = new Color(r, g, b);
                }
                else if (parts[0] == "map_Kd")
                {
                    var fullPath = Path.Combine(path, parts[1].Trim());
                    currentMaterial.DiffuseMap = new Texture(fullPath);
                }
                else if (parts[0] == "Ks")
                {
                    var phong = currentMaterial as MeshPhongMaterial;
                    if (phong != null)
                    {
                        var r = Convert.ToSingle(parts[1]);
                        var g = Convert.ToSingle(parts[2]);
                        var b = Convert.ToSingle(parts[3]);
                        phong.Specular = new Color(r, g, b);
                    }
                }
                else if (parts[0] == "d")
                {

                }
                else if (parts[0] == "illum")
                {

                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }
예제 #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 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);
            }
        }
예제 #3
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;
        }