示例#1
0
 RPRImage GetImage(string fileName)
 {
     if (images.TryGetValue(fileName, out var image))
     {
         return(image);
     }
     else
     {
         image = new RPRImage(context, fileName);
         images.Add(fileName, image);
         return(image);
     }
 }
示例#2
0
        public override void Render()
        {
            string skyBoxFile1 = renderWrap.GetTex2DPath(nameof(skyboxTexture));

            if (skyBoxFile != skyBoxFile1)
            {
                skyBoxFile = skyBoxFile1;
                skyBoxImage?.Dispose();
                skyBoxImage = new RPRImage(context, skyBoxFile);
            }

            if (size != new Vector2(output.width, output.height))
            {
                size = new Vector2(output.width, output.height);
                var desc = new Rpr.FrameBufferDesc()
                {
                    FbWidth  = (uint)size.X,
                    FbHeight = (uint)size.Y
                };
                var format = new Rpr.FramebufferFormat()
                {
                    NumComponents = 4,
                    Type          = (uint)Rpr.ComponentType.FLOAT32
                };
                frameBuffer?.Dispose();
                frameBufferResolved?.Dispose();
                frameBuffer         = new RPRFrameBuffer(context, format, desc);
                frameBufferResolved = new RPRFrameBuffer(context, format, desc);
            }

            var camera = this.Camera;

            scene = new RPRScene(context);
            context.SetScene(scene);
            Vector3 angle = camera.Angle;

            rprCamera.SetNearPlane(camera.near);
            rprCamera.SetFarPlane(camera.far);
            rprCamera.LookAt(camera.Position, camera.LookAtPoint, Vector3.Transform(Vector3.UnitY, Matrix4x4.CreateFromYawPitchRoll(-angle.Y, -angle.X, -angle.Z)));
            scene.SetCamera(rprCamera);

            List <RPRShape>        shapes        = new List <RPRShape>();
            List <RPRLight>        lights        = new List <RPRLight>();
            List <RPRMaterialNode> materialNodes = new List <RPRMaterialNode>();
            var envLight = RPRLight.EnvLight(context);

            envLight.EnvironmentLightSetImage(skyBoxImage);
            envLight.EnvironmentLightSetIntensityScale(SkyLightMultiple / (float)Math.PI);
            envLight.SetTransform(Matrix4x4.CreateScale(-1, 1, 1) * Matrix4x4.CreateRotationY((float)Math.PI));
            scene.AttachLight(envLight);
            lights.Add(envLight);

            foreach (var visualObject in Visuals)
            {
                var visual   = visualObject.GetComponent <VisualComponent>();
                var material = visual.material;
                if (visual.UIShowType == Caprice.Display.UIShowType.Light)
                {
                    var lightType = (LightType)renderWrap.GetIndexableValue("LightType", material);
                    if (lightType == LightType.Directional)
                    {
                        var Color     = (Vector3)renderWrap.GetIndexableValue("LightColor", material);
                        var Direction = Vector3.Transform(-Vector3.UnitZ, visual.transform.rotation);
                        var Rotation  = visual.transform.rotation;

                        var light1 = RPRLight.DirectionalLight(context);
                        light1.SetTransform(Matrix4x4.CreateLookAt(Direction, new Vector3(0, 0, 0), new Vector3(0, 1, 0)));
                        light1.DirectionalLightSetRadiantPower3f(Color.X, Color.Y, Color.Z);
                        light1.DirectionalLightSetShadowSoftnessAngle(DirectionalLightShadowSoftnessAngle / 180.0f * (float)Math.PI);
                        scene.AttachLight(light1);
                        lights.Add(light1);
                    }
                    else if (lightType == LightType.Point)
                    {
                        var Color    = (Vector3)renderWrap.GetIndexableValue("LightColor", material);
                        var Position = visual.transform.position;
                        var Range    = (float)renderWrap.GetIndexableValue("LightRange", material);

                        var light1 = RPRLight.PointLight(context);
                        light1.SetTransform(Matrix4x4.CreateTranslation(Position));
                        light1.PointLightSetRadiantPower3f(Color.X, Color.Y, Color.Z);
                        scene.AttachLight(light1);
                        lights.Add(light1);
                    }
                }
            }

            foreach (var renderable in renderWrap.MeshRenderables(false))
            {
                renderable.mesh.TryGetBuffer(0, out byte[] vertexBuf);
                renderable.mesh.TryGetBuffer(1, out byte[] normalBuf);
                renderable.mesh.TryGetBuffer(2, out byte[] uvBuf);
                if (!renderable.gpuSkinning && renderable.meshOverride != null)
                {
                    renderable.meshOverride.TryGetBuffer(0, out vertexBuf);
                    renderable.meshOverride.TryGetBuffer(1, out normalBuf);
                }
                var indiceBuf  = renderable.mesh.m_indexData;
                var vertStart  = renderable.vertexStart;
                var vertCount  = renderable.vertexCount;
                var indexStart = renderable.indexStart;
                var indexCount = renderable.indexCount;

                RPRShape shape = new RPRShape(context,
                                              new Span <byte>(vertexBuf, vertStart * 12, vertCount * 12),
                                              new Span <byte>(normalBuf, vertStart * 12, vertCount * 12),
                                              new Span <byte>(uvBuf, vertStart * 8, vertCount * 8),
                                              new Span <byte>(indiceBuf, indexStart * 4, indexCount * 4), indexCount, true);
                shape.SetTransform(renderable.transform);
                scene.AttachShape(shape);
                shapes.Add(shape);
                RPRMaterialNode materialNode = new RPRMaterialNode(materialSystem, Rpr.MaterialNodeType.UBERV2);
                RPRMaterialNode colorTexNode = GetImageNode(renderWrap.GetTex2DPath(nameof(_Albedo), renderable.material));

                RPRMaterialNode roughnessTexNode = GetImageNode(renderWrap.GetTex2DPath(nameof(_Roughness), renderable.material));
                RPRMaterialNode metallicTexNode  = GetImageNode(renderWrap.GetTex2DPath(nameof(_Metallic), renderable.material));
                RPRMaterialNode emissiveTexNode  = GetImageNode(renderWrap.GetTex2DPath(nameof(_Emissive), renderable.material));

                materialNodes.Add(colorTexNode);
                materialNodes.Add(materialNode);

                float Metallic   = (float)renderWrap.GetIndexableValue(nameof(this.Metallic), renderable.material);
                float Roughness  = (float)renderWrap.GetIndexableValue(nameof(this.Roughness), renderable.material);
                float Emissive   = (float)renderWrap.GetIndexableValue(nameof(this.Emissive), renderable.material);
                bool  Refraction = (bool)renderWrap.GetIndexableValue(nameof(this.Refraction), renderable.material);
                float IOR        = (float)renderWrap.GetIndexableValue(nameof(this.IOR), renderable.material);

                materialNode.SetInputNByKey(Rpr.MaterialInput.UBER_DIFFUSE_COLOR, colorTexNode);
                materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_DIFFUSE_ROUGHNESS, Roughness, Roughness, Roughness, Roughness);
                materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_DIFFUSE_WEIGHT, 1.0f, 1.0f, 1.0f, 1.0f);
                materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_REFLECTION_IOR, IOR, IOR, IOR, IOR);
                materialNode.SetInputNByKey(Rpr.MaterialInput.UBER_REFLECTION_COLOR, colorTexNode);
                materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_REFLECTION_METALNESS, Metallic, Metallic, Metallic, Metallic);
                materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_REFLECTION_ROUGHNESS, Roughness, Roughness, Roughness, Roughness);
                materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_REFLECTION_WEIGHT, 1.0f, 1.0f, 1.0f, 1.0f);
                if (!Refraction)
                {
                    materialNode.SetInputUByKey(Rpr.MaterialInput.UBER_REFLECTION_MODE, (uint)Rpr.UberMaterialMode.METALNESS);
                }
                if (roughnessTexNode != null)
                {
                    materialNodes.Add(roughnessTexNode);
                    RPRMaterialNode selectY = new RPRMaterialNode(materialSystem, Rpr.MaterialNodeType.ARITHMETIC);
                    selectY.SetInputUByKey(Rpr.MaterialInput.OP, (uint)Rpr.MaterialNodeOp.SELECT_Y);
                    selectY.SetInputNByKey(Rpr.MaterialInput.COLOR0, roughnessTexNode);
                    materialNodes.Add(selectY);

                    materialNode.SetInputNByKey(Rpr.MaterialInput.UBER_DIFFUSE_ROUGHNESS, selectY);
                    materialNode.SetInputNByKey(Rpr.MaterialInput.UBER_REFLECTION_ROUGHNESS, selectY);
                }
                if (metallicTexNode != null)
                {
                    materialNodes.Add(metallicTexNode);
                    RPRMaterialNode selectZ = new RPRMaterialNode(materialSystem, Rpr.MaterialNodeType.ARITHMETIC);
                    selectZ.SetInputUByKey(Rpr.MaterialInput.OP, (uint)Rpr.MaterialNodeOp.SELECT_Z);
                    selectZ.SetInputNByKey(Rpr.MaterialInput.COLOR0, metallicTexNode);
                    materialNodes.Add(selectZ);
                    if (!Refraction)
                    {
                        materialNode.SetInputNByKey(Rpr.MaterialInput.UBER_REFLECTION_METALNESS, selectZ);
                    }
                }
                if (emissiveTexNode != null)
                {
                    materialNodes.Add(emissiveTexNode);
                    materialNode.SetInputNByKey(Rpr.MaterialInput.UBER_EMISSION_COLOR, emissiveTexNode);
                    //materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_EMISSION_WEIGHT, Emissive, Emissive, Emissive, Emissive);
                    //materialNode.SetInputUByKey(Rpr.MaterialInput.UBER_EMISSION_MODE,Rpr.UberMaterialEmissionMode.);
                }
                if (Refraction)
                {
                    materialNode.SetInputNByKey(Rpr.MaterialInput.UBER_REFRACTION_COLOR, colorTexNode);
                    //materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_REFRACTION_COLOR, refractionColor.X, refractionColor.Y, refractionColor.Z, 1);
                    materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_REFRACTION_IOR, IOR, IOR, IOR, IOR);
                    materialNode.SetInputFByKey(Rpr.MaterialInput.UBER_REFRACTION_WEIGHT, 1, 1, 1, 1);
                }
                shape.SetMaterial(materialNode);
            }


            context.SetParameterByKey1u(Rpr.ContextInfo.TONE_MAPPING_TYPE, (uint)Rpr.ToneMappingOperator.NONE);
            context.SetParameterByKey1u(Rpr.ContextInfo.ITERATIONS, Recording ? (uint)RecordSampleCount : (uint)ViewportSampleCount);
            frameBuffer.Clear();
            frameBufferResolved.Clear();
            context.SetAOV(Rpr.Aov.COLOR, frameBuffer);
            context.Render();
            context.ResolveFrameBuffer(frameBuffer, frameBufferResolved, false);
            frameBufferResolved.GetInfo(Rpr.FrameBuffer.DATA, frameBuffer1, out int dataSize);

            scene.Clear();
            foreach (var shape in shapes)
            {
                scene.DetachShape(shape);
                shape.Dispose();
            }
            foreach (var light in lights)
            {
                scene.DetachLight(light);
                light.Dispose();
            }
            foreach (var materialNode in materialNodes)
            {
                materialNode.Dispose();
            }
            scene.Dispose();
            renderWrap.graphicsContext.UploadTexture(noPostProcess, frameBuffer1);

            postProcess.Execute(renderWrap);
        }