コード例 #1
0
        private void DrawNextPaint()
        {
            if (P3dPaintMaterial.CachedInstances.Count > 0)
            {
                if (paintIndex >= P3dPaintMaterial.CachedInstances.Count)
                {
                    paintIndex = 0;
                }

                var paint = P3dPaintMaterial.CachedInstances[paintIndex++];

                DestroyImmediate(paint.Thumbnail);
                paint.Thumbnail = null;

                if (paint != null && paint.Thumbnail == null && paint.Material != null)
                {
                    var tempTextures = new List <RenderTexture>();

                    if (paintProperties == null)
                    {
                        paintProperties = new MaterialPropertyBlock();
                    }
                    else
                    {
                        paintProperties.Clear();
                    }

                    if (paint.Template != null)
                    {
                        foreach (var slot in paint.Template.Slots)
                        {
                            var slotR = paint.GetSlot(slot.WriteR.SourceGroup);
                            var slotG = paint.GetSlot(slot.WriteG.SourceGroup);
                            var slotB = paint.GetSlot(slot.WriteB.SourceGroup);
                            var slotA = paint.GetSlot(slot.WriteA.SourceGroup);

                            if (slotR != null || slotG != null || slotB != null || slotA != null)
                            {
                                var tempTexture = P3dHelper.GetRenderTexture(new RenderTextureDescriptor(1024, 1024, RenderTextureFormat.ARGB32, 0));

                                tempTextures.Add(tempTexture);

                                var textureR = P3dHelper.GetRenderTexture(tempTexture.descriptor);
                                var textureG = P3dHelper.GetRenderTexture(tempTexture.descriptor);
                                var textureB = P3dHelper.GetRenderTexture(tempTexture.descriptor);
                                var textureA = P3dHelper.GetRenderTexture(tempTexture.descriptor);

                                WriteThumbnailTex(textureR, slotR, slot.WriteR);
                                WriteThumbnailTex(textureG, slotG, slot.WriteG);
                                WriteThumbnailTex(textureB, slotB, slot.WriteB);
                                WriteThumbnailTex(textureA, slotA, slot.WriteA);

                                var channelR = P3dHelper.IndexToVector((int)slot.WriteR.SourceChannel);
                                var channelG = P3dHelper.IndexToVector((int)slot.WriteG.SourceChannel);
                                var channelB = P3dHelper.IndexToVector((int)slot.WriteB.SourceChannel);
                                var channelA = P3dHelper.IndexToVector((int)slot.WriteA.SourceChannel);

                                P3dPaintReplaceChannels.Blit(tempTexture, textureR, textureG, textureB, textureA, channelR, channelG, channelB, channelA);

                                P3dHelper.ReleaseRenderTexture(textureR);
                                P3dHelper.ReleaseRenderTexture(textureG);
                                P3dHelper.ReleaseRenderTexture(textureB);
                                P3dHelper.ReleaseRenderTexture(textureA);

                                paintProperties.SetTexture(slot.Name, tempTexture);
                            }
                        }
                    }

                    BeginPreview(thumbnailUtil, new Rect(0, 0, settings.ThumbnailSize, settings.ThumbnailSize));

                    var probeAnchor = default(Transform);
                    var probeExists = false;

                    if (settings.Environment != null)
                    {
                        probeAnchor = settings.Environment.transform;
                        probeExists = true;
                    }

                    switch (paint.Style)
                    {
                    case P3dPaintMaterial.StyleType.Seamless:
                    {
                        thumbnailUtil.DrawMesh(P3dHelper.GetSphereMesh(), Matrix4x4.identity, paint.Material, 0, paintProperties, probeAnchor, probeExists);
                    }
                    break;

                    case P3dPaintMaterial.StyleType.Decal:
                    {
                        thumbnailUtil.DrawMesh(P3dHelper.GetQuadMesh(), Matrix4x4.identity, paint.Material, 0, paintProperties, probeAnchor, probeExists);
                    }
                    break;
                    }

                    paint.Thumbnail = P3dHelper.GetReadableCopy(EndPreview(thumbnailUtil));

                    foreach (var tempTexture in tempTextures)
                    {
                        P3dHelper.ReleaseRenderTexture(tempTexture);
                    }
                }
            }
        }