Ejemplo n.º 1
0
 public ptObject(ptMaterial a_mat, Matrix4x4 a_worldMatrix, Matrix4x4 a_invWorldMatrix, ptShapeType a_shapeType)
 {
     material       = a_mat;
     worldMatrix    = a_worldMatrix;
     invWorldMatrix = a_invWorldMatrix;
     shapeType      = (uint)a_shapeType;
 }
Ejemplo n.º 2
0
 public ptObjectHandler(ptMaterial a_mat, Transform a_transform, ptShapeType a_shapeType)
 {
     transform     = a_transform;
     handledObject = new ptObject(a_mat, transform.localToWorldMatrix, transform.worldToLocalMatrix, a_shapeType);
 }
Ejemplo n.º 3
0
    // -- Setup Renderer
    public void SetupRenderer(ptRenderSettings a_renderSettings)
    {
        ActiveCamera = Camera.main;
        if (ActiveCamera == null)
        {
            Debug.Log("Could not setup renderer; No camera!");
            return;
        }

        ptRenderSettings options = a_renderSettings;

        if (options.outputWidth == 0)
        {
            options.outputWidth = 1280;
        }
        if (options.outputHeight == 0)
        {
            options.outputHeight = 720;
        }
        if (options.samples == 0)
        {
            options.samples = 3;
        }
        if (options.bounces == 0)
        {
            options.bounces = 2;
        }

        Width  = options.outputWidth;
        Height = options.outputHeight;

        // -- Create required resources

        computeShader = Resources.Load <ComputeShader>("Shaders/CorePathTracer");

        outputTex = new RenderTexture(Width, Height, (int)RenderTextureFormat.ARGB32);
        outputTex.enableRandomWrite = true;

        outputTex.Create();

        saveTex = new Texture2D(Width, Height, TextureFormat.ARGB32, false);

        // -- Setup Lights
        Light[] allLights = GameObject.FindObjectsOfType <Light>();
        int     numLights = allLights.Length;

        lights = new ptLightHandler[numLights];

        for (int j = 0; j < numLights; j++)
        {
            Light l = allLights[j];

            ptLightHandler newLightObject = new ptLightHandler(l);
            lights[j] = newLightObject;
        }

        if (lightBuffer != null)
        {
            lightBuffer.Dispose();
            lightBuffer.Release();
            lightBuffer = null;
            Debug.Log("Destroyed old buffer");
        }

        if (numLights > 0)
        {
            lightBuffer = new ComputeBuffer(numLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ptLight)));

            UploadLights();
        }

        // -- Setup objects
        MeshFilter[]           allMeshes     = GameObject.FindObjectsOfType <MeshFilter>();
        int                    numObjects    = allMeshes.Length;
        List <ptObjectHandler> outputObjects = new List <ptObjectHandler>();

        for (int i = 0; i < numObjects; i++)
        {
            MeshFilter mf          = allMeshes[i];
            GameObject obj         = mf.gameObject;
            Renderer   objRenderer = obj.GetComponent <Renderer>();
            Material   objMaterial = objRenderer.sharedMaterial;

            Vector3 pos = objRenderer.bounds.center;
            //http://answers.unity3d.com/questions/914923/standard-shader-emission-control-via-script.html

            Color diffuse  = objMaterial.GetColor("_Color");
            Color emission = objMaterial.GetColor("_EmissionColor");
            if (!objMaterial.IsKeywordEnabled("_EMISSION"))
            {
                emission = Color.black;
            }

            float      metallic   = objMaterial.GetFloat("_Metallic");
            float      glossiness = objMaterial.GetFloat("_Glossiness");
            ptMaterial mat        = new ptMaterial();

            mat.albedo     = diffuse;
            mat.emission   = emission;
            mat.metallic   = metallic;
            mat.smoothness = glossiness;

            ptShapeType st = ptShapeType.SPHERE;

            switch (mf.sharedMesh.name)
            {
            case "Sphere":
                st = ptShapeType.SPHERE;
                outputObjects.Add(new ptObjectHandler(mat, obj.transform, st));
                break;

            case "Plane":
                st = ptShapeType.PLANE;
                outputObjects.Add(new ptObjectHandler(mat, obj.transform, st));
                break;

            case "Cube":
                st = ptShapeType.BOX;
                break;
            }
        }

        objects = new ptObjectHandler[outputObjects.Count];
        for (int z = 0; z < outputObjects.Count; z++)
        {
            objects[z] = outputObjects[z];
        }

        if (objectsBuffer != null)
        {
            objectsBuffer.Dispose();
            objectsBuffer.Release();
            objectsBuffer = null;
            Debug.Log("Destroyed old buffer");
        }

        if (numObjects > 0)
        {
            objectsBuffer = new ComputeBuffer(numObjects, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ptObject)));
            UploadObjects();
        }

        int kernelHandle = computeShader.FindKernel("CSMain");

        settingsBuffer = new ComputeBuffer(1, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ptRenderSettings)));

        ptRenderSettings[] ptrs = new ptRenderSettings[1];
        ptrs[0] = options;
        settingsBuffer.SetData(ptrs);

        computeShader.SetBuffer(kernelHandle, "SETTINGS", settingsBuffer);
    }