fusenapi/server/websocket/internal/logic/ws_render_image.go

580 lines
24 KiB
Go
Raw Normal View History

2023-07-26 06:35:29 +00:00
package logic
2023-08-23 07:08:45 +00:00
//处理websocket云渲染任务数据
2023-07-26 06:35:29 +00:00
import (
2023-08-16 04:33:01 +00:00
"bytes"
2023-07-26 06:35:29 +00:00
"encoding/json"
2023-08-09 08:06:40 +00:00
"errors"
2023-08-21 08:19:30 +00:00
"fmt"
2023-07-27 09:33:50 +00:00
"fusenapi/constants"
2023-08-24 10:02:00 +00:00
"fusenapi/model/gmodel"
2023-08-16 04:16:23 +00:00
"fusenapi/service/repositories"
2023-08-16 04:33:01 +00:00
"fusenapi/utils/curl"
2023-08-07 02:48:14 +00:00
"fusenapi/utils/hash"
2023-09-06 02:48:45 +00:00
"fusenapi/utils/template_switch_info"
2023-08-24 06:24:04 +00:00
"fusenapi/utils/websocket_data"
2023-07-26 06:35:29 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-08-09 08:06:40 +00:00
"gorm.io/gorm"
2023-08-16 04:16:23 +00:00
"strconv"
2023-08-16 04:33:01 +00:00
"time"
2023-07-26 06:35:29 +00:00
)
2023-08-30 09:40:40 +00:00
// 渲染处理器
2023-08-30 09:46:47 +00:00
type renderProcessor struct {
2023-08-30 09:40:40 +00:00
}
2023-07-26 09:06:53 +00:00
// 云渲染属性
2023-08-24 04:04:58 +00:00
type extendRenderProperty struct {
2023-08-21 08:19:30 +00:00
renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId
2023-08-23 04:29:02 +00:00
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增/回调结果移除任务/更新渲染耗时属性的控制通道由于任务map无法读写并发
renderChan chan []byte //渲染消息入口的缓冲队列
2023-08-21 08:19:30 +00:00
}
2023-07-26 09:06:53 +00:00
// 渲染任务新增移除的控制通道的数据
type renderImageControlChanItem struct {
2023-08-28 10:16:19 +00:00
option int // 0删除 1添加 2修改耗时属性
taskId string //map的key(必须传)
2023-08-28 06:44:46 +00:00
renderId string // map的val(增加任务时候传)
renderNotifyImageUrl string //渲染回调数据(删除任务时候传)
taskProperty renderTask //渲染任务的属性
2023-07-27 09:41:36 +00:00
}
2023-08-23 04:29:02 +00:00
// 渲染任务属性
type renderTask struct {
2023-08-28 11:00:08 +00:00
renderId string //渲染id(新增任务传)
2023-08-28 06:44:46 +00:00
unityRenderBeginTime int64 //发送给unity时间
unityRenderEndTime int64 //unity回调结果时间
uploadUnityRenderImageTakesTime int64 //上传unity渲染结果图时间
2023-08-23 04:29:02 +00:00
}
2023-08-30 10:51:24 +00:00
// 处理分发到这里的数据
2023-08-30 10:31:20 +00:00
func (r *renderProcessor) allocationMessage(w *wsConnectItem, data []byte) {
2023-08-30 09:46:47 +00:00
logx.Info("收到渲染任务消息:", string(data))
2023-08-16 08:20:16 +00:00
select {
2023-08-30 10:31:20 +00:00
case <-w.closeChan: //已经关闭
2023-08-16 08:20:16 +00:00
return
2023-08-30 10:31:20 +00:00
case w.extendRenderProperty.renderChan <- data: //发入到缓冲队列
2023-08-16 08:20:16 +00:00
return
2023-08-28 07:50:18 +00:00
case <-time.After(time.Second * 3): //三秒没进入缓冲队列就丢弃
2023-08-16 08:20:16 +00:00
return
}
}
2023-08-30 03:13:10 +00:00
// 消费渲染缓冲队列数据
func (w *wsConnectItem) consumeRenderImageData() {
2023-08-29 02:21:11 +00:00
defer func() {
if err := recover(); err != nil {
logx.Error("func renderImage err:", err)
}
}()
2023-08-16 08:20:16 +00:00
for {
select {
case <-w.closeChan: //已关闭
return
2023-08-24 04:04:58 +00:00
case data := <-w.extendRenderProperty.renderChan:
2023-08-30 06:18:55 +00:00
w.renderImage(data)
2023-08-16 08:20:16 +00:00
}
}
}
2023-08-30 06:18:55 +00:00
// 执行渲染任务
func (w *wsConnectItem) renderImage(data []byte) {
2023-08-16 09:04:45 +00:00
logx.Info("消费渲染数据:", string(data))
2023-08-24 06:24:04 +00:00
var renderImageData websocket_data.RenderImageReqMsg
2023-07-27 09:41:36 +00:00
if err := json.Unmarshal(data, &renderImageData); err != nil {
2023-08-24 09:23:41 +00:00
w.incomeDataFormatErrResponse("invalid format of render data:" + string(data))
2023-07-27 09:41:36 +00:00
logx.Error("invalid format of websocket render image message", err)
return
}
2023-08-09 10:09:16 +00:00
//获取上传最近的logo
2023-08-16 06:09:03 +00:00
userMaterial, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindLatestOne(w.logic.ctx, w.userId, w.guestId)
2023-08-09 10:09:16 +00:00
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取用户上传logo素材失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-08-09 10:09:16 +00:00
logx.Error("failed to get user logo")
return
}
2023-08-15 07:09:53 +00:00
//使用默认logo(id=0)
2023-08-16 06:09:03 +00:00
userMaterialDefault, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindOneById(w.logic.ctx, 0)
2023-08-15 07:09:53 +00:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "默认logo不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-08-15 07:09:53 +00:00
return
}
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取默认logo失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
2023-08-23 03:24:33 +00:00
logx.Error("default logo is not exists")
2023-08-15 07:09:53 +00:00
return
}
renderImageData.RenderData.Logo = *userMaterialDefault.ResourceUrl
2023-08-09 10:09:16 +00:00
} else {
renderImageData.RenderData.Logo = *userMaterial.ResourceUrl
}
2023-08-09 04:28:06 +00:00
//用户id赋值
2023-08-08 04:22:15 +00:00
renderImageData.RenderData.UserId = w.userId
2023-08-09 04:28:06 +00:00
renderImageData.RenderData.GuestId = w.guestId
2023-09-01 07:48:47 +00:00
var (
model3dInfo *gmodel.FsProductModel3d
productTemplate *gmodel.FsProductTemplateV2
productSize *gmodel.FsProductSize
)
2023-09-06 02:09:59 +00:00
//获取信息
productSize, productTemplate, model3dInfo, err = w.getProductRelateionInfo(renderImageData)
2023-09-01 08:10:02 +00:00
if err != nil {
2023-09-06 02:09:59 +00:00
logx.Error(err)
2023-09-01 08:10:02 +00:00
return
2023-08-31 03:50:55 +00:00
}
2023-08-24 10:02:00 +00:00
//获取渲染设置信息
2023-08-29 02:31:25 +00:00
element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ElementModelId)
2023-08-24 10:02:00 +00:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "云渲染设置不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-29 02:21:11 +00:00
logx.Error("element info is not found,element_model_id = ", 0)
2023-08-24 10:02:00 +00:00
return
}
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取云渲染设置失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-28 10:37:58 +00:00
logx.Error("failed to get element ,", err)
2023-08-24 10:02:00 +00:00
return
}
2023-09-06 02:48:45 +00:00
//获取模板开关信息并且对于没有默认值的给赋值默认值
templateSwitchInfo := template_switch_info.GetTemplateSwitchInfo(productTemplate.Id, *productTemplate.MaterialImg)
if renderImageData.RenderData.Website == "" {
renderImageData.RenderData.Website = templateSwitchInfo.MaterialData.Website.DefaultValue
}
if renderImageData.RenderData.Phone == "" {
renderImageData.RenderData.Phone = templateSwitchInfo.MaterialData.Phone.DefaultValue
}
if renderImageData.RenderData.Address == "" {
renderImageData.RenderData.Address = templateSwitchInfo.MaterialData.Address.DefaultValue
}
if renderImageData.RenderData.Qrcode == "" {
renderImageData.RenderData.Qrcode = templateSwitchInfo.MaterialData.QRcode.DefaultValue
}
2023-08-28 10:16:19 +00:00
//获取刀版图
combineReq := repositories.LogoCombineReq{
UserId: renderImageData.RenderData.UserId,
GuestId: renderImageData.RenderData.GuestId,
TemplateId: productTemplate.Id,
TemplateTag: renderImageData.RenderData.TemplateTag,
Website: renderImageData.RenderData.Website,
Slogan: renderImageData.RenderData.Slogan,
Address: renderImageData.RenderData.Address,
Phone: renderImageData.RenderData.Phone,
2023-08-29 09:09:00 +00:00
Qrcode: renderImageData.RenderData.Qrcode,
2023-08-29 09:10:58 +00:00
LogoUrl: renderImageData.RenderData.Logo,
2023-08-28 10:16:19 +00:00
}
res, err := w.logic.svcCtx.Repositories.ImageHandle.LogoCombine(w.logic.ctx, &combineReq)
if err != nil {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "合成刀版图失败:"+err.Error(), renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-28 10:16:19 +00:00
logx.Error("合成刀版图失败,合成请求数据:", combineReq, "错误信息:", err)
return
}
combineImage := "" //刀版图
if res != nil && res.ResourceUrl != nil {
combineImage = *res.ResourceUrl
} else {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "合成的刀版图是空的地址", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-28 10:16:19 +00:00
logx.Error("合成刀版图失败,合成的刀版图是空指针:", err)
return
}
2023-08-31 02:35:13 +00:00
//发送合图完毕阶段消息
2023-09-06 02:14:45 +00:00
w.sendCombineImageStepResponseMessage(renderImageData.RenderId, combineImage, productSize.Id, res.DiffTimeLogoCombine, res.DiffTimeUploadFile)
2023-08-29 09:40:09 +00:00
//如果指定指定只返回刀版图
if renderImageData.OnlyReturnCombineImage {
2023-08-29 07:18:50 +00:00
logx.Info("云渲染传入size id则不走unity云渲染,只返回刀版图render_id:", renderImageData.RenderId)
2023-08-29 07:04:11 +00:00
return
}
2023-08-24 10:02:00 +00:00
//获取唯一id
2023-09-05 07:08:03 +00:00
taskId := w.genRenderTaskId(combineImage, renderImageData, model3dInfo, productTemplate, element)
2023-08-09 08:06:40 +00:00
//查询有没有缓存的资源,有就返回######################
2023-08-16 06:09:03 +00:00
resource, err := w.logic.svcCtx.AllModels.FsResource.FindOneById(w.logic.ctx, taskId)
2023-08-09 08:06:40 +00:00
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, taskId, "获取unity云渲染缓存失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-09 08:06:40 +00:00
logx.Error("failed to find render resource:", err)
return
}
} else {
//返回给客户端
2023-08-31 06:38:34 +00:00
w.sendRenderResultData(websocket_data.RenderImageRspMsg{
2023-08-29 07:13:25 +00:00
RenderId: renderImageData.RenderId,
Image: *resource.ResourceUrl,
2023-08-25 04:02:07 +00:00
RenderProcessTime: websocket_data.RenderProcessTime{
UnityRenderTakesTime: "cache",
UploadUnityRenderImageTakesTime: "cache",
},
2023-08-09 08:06:40 +00:00
})
return
}
//###########################################
//把需要渲染的图片任务加进去
2023-09-05 02:20:39 +00:00
w.createRenderTask(taskId, renderImageData.RenderId)
2023-08-16 04:16:23 +00:00
//组装数据
2023-08-30 10:51:24 +00:00
if err = w.assembleRenderDataToUnity(taskId, combineImage, renderImageData, productTemplate, model3dInfo, element, productSize); err != nil {
2023-08-16 08:20:16 +00:00
logx.Error("组装数据失败:", err)
2023-08-07 02:48:14 +00:00
return
}
2023-08-16 04:16:23 +00:00
}
2023-09-01 08:10:02 +00:00
// 获取模板相关信息(指定尺寸)
2023-09-06 02:09:59 +00:00
func (w *wsConnectItem) getProductRelateionInfo(renderImageData websocket_data.RenderImageReqMsg) (productSize *gmodel.FsProductSize, productTemplate *gmodel.FsProductTemplateV2, model3d *gmodel.FsProductModel3d, err error) {
//指定尺寸
if renderImageData.RenderData.ProductSizeId > 0 {
productSize, err = w.logic.svcCtx.AllModels.FsProductSize.FindOneByIdProductId(w.logic.ctx, renderImageData.RenderData.ProductSizeId, renderImageData.RenderData.ProductId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到产品的指定尺寸", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, renderImageData.RenderData.ProductSizeId, 0)
logx.Error("product size is not found")
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取产品的指定尺寸失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, renderImageData.RenderData.ProductSizeId, 0)
logx.Error("failed to get product size:", err)
2023-09-06 04:04:03 +00:00
return nil, nil, nil, err
2023-09-06 02:09:59 +00:00
}
} else {
//没有指定尺寸则获取产品第一个尺寸
productSize, err = w.logic.svcCtx.AllModels.FsProductSize.GetProductFirstSize(w.logic.ctx, renderImageData.RenderData.ProductId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到产品的第一个尺寸", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
logx.Error("product size is not found")
return
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取产品的第一个尺寸失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, 0, 0)
logx.Error("failed to get product size:", err)
2023-09-06 04:04:03 +00:00
return nil, nil, nil, err
2023-09-01 08:10:02 +00:00
}
}
2023-09-06 02:09:59 +00:00
2023-09-01 08:10:02 +00:00
//获取模型
model3d, err = w.logic.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(w.logic.ctx, productSize.Id, constants.TAG_MODEL)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-09-06 03:49:56 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "产品尺寸对应的模型不存在", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, productSize.Id, 0)
2023-09-01 08:10:02 +00:00
logx.Error("product model is not found")
2023-09-06 04:04:03 +00:00
return nil, nil, nil, err
2023-09-01 08:10:02 +00:00
}
2023-09-06 03:49:56 +00:00
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取产品尺寸对应的模型失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, 0, productSize.Id, 0)
2023-09-01 08:10:02 +00:00
logx.Error("failed to get product model:", err)
2023-09-06 04:04:03 +00:00
return nil, nil, nil, err
2023-09-01 08:10:02 +00:00
}
//获取模板
productTemplate, err = w.logic.svcCtx.AllModels.FsProductTemplateV2.FindOneCloudRenderByProductIdModelIdTemplateTag(w.logic.ctx, renderImageData.RenderData.ProductId, model3d.Id, renderImageData.RenderData.TemplateTag)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "找不到对应开启云渲染的模板", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3d.Id, productSize.Id, 0)
logx.Error("template info is not found")
2023-09-06 04:04:03 +00:00
return nil, nil, nil, err
2023-09-01 08:10:02 +00:00
}
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "获取对应开启云渲染模板失败", renderImageData.RenderData.ProductId, w.userId, w.guestId, 0, model3d.Id, productSize.Id, 0)
logx.Error("failed to get template info:", err)
2023-09-06 04:04:03 +00:00
return nil, nil, nil, err
2023-09-01 08:10:02 +00:00
}
2023-09-06 04:00:36 +00:00
if productTemplate.TemplateInfo == nil || *productTemplate.TemplateInfo == "" {
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "渲染模板的json设计信息是空的", renderImageData.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3d.Id, productSize.Id, 0)
2023-09-06 04:04:03 +00:00
return nil, nil, nil, errors.New("渲染模板的json设计信息是空的")
2023-09-06 04:00:36 +00:00
}
2023-09-01 08:10:02 +00:00
return
}
2023-08-24 10:02:00 +00:00
// 组装数据发送给unity
2023-08-30 10:51:24 +00:00
func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage string, info websocket_data.RenderImageReqMsg, productTemplate *gmodel.FsProductTemplateV2, model3dInfo *gmodel.FsProductModel3d, element *gmodel.FsProductTemplateElement, productSize *gmodel.FsProductSize) (err error) {
2023-08-16 04:16:23 +00:00
//组装数据
refletion := -1
if element.Refletion != nil && *element.Refletion != "" {
refletion, err = strconv.Atoi(*element.Refletion)
2023-08-17 06:48:27 +00:00
if err != nil {
logx.Error("err refletion:set default -1")
2023-08-31 03:50:55 +00:00
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "解析云渲染设置把Refletion字段值从字符串转数字失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-24 09:23:41 +00:00
return err
2023-08-17 06:48:27 +00:00
}
2023-08-16 04:16:23 +00:00
}
//组装data数据
var mode map[string]interface{}
if element.Mode != nil && *element.Mode != "" {
if err = json.Unmarshal([]byte(*element.Mode), &mode); err != nil {
logx.Error("faile to parse element mode json:", err)
2023-08-31 03:50:55 +00:00
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "解析云渲染设置字段 mode 为map对象失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-16 08:20:16 +00:00
return err
2023-08-16 04:16:23 +00:00
}
}
tempData := make([]map[string]interface{}, 0, 3)
if element.Base != nil && *element.Base != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model",
"data": "0," + combineImage + "," + *element.Base,
"type": "other",
"layer": "0",
"is_update": 1,
"mode": mode["model"],
})
}
if element.Shadow != nil && *element.Shadow != "" {
tempData = append(tempData, map[string]interface{}{
"name": "shadow",
"data": *element.Shadow,
"type": "other",
"layer": "0",
"is_update": 0,
"mode": mode["shadow"],
})
}
if element.ModelP != nil && *element.ModelP != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model_P",
"data": "0," + *element.ModelP,
"type": "other",
"layer": "0",
"is_update": 0,
"mode": mode["model_P"],
})
}
result := []interface{}{
map[string]interface{}{
"light": *element.Light,
"refletion": refletion,
"scale": *element.Scale,
"sku_id": info.RenderData.ProductId,
"tid": *element.Title,
"rotation": *element.Rotation,
"filePath": "", //todo 文件路径,针对千人千面
"data": tempData,
},
}
2023-08-31 02:04:03 +00:00
//发送运行阶段消息(组装数据)
2023-08-31 02:35:13 +00:00
w.sendAssembleRenderDataStepResponseMessage(info.RenderId)
2023-08-16 04:16:23 +00:00
sendData := map[string]interface{}{
2023-09-04 08:54:55 +00:00
"id": taskId + " " + w.uniqueId, //空格分开
2023-08-16 04:16:23 +00:00
"order_id": 0,
"user_id": info.RenderData.UserId,
"guest_id": info.RenderData.GuestId,
"sku_ids": []int64{info.RenderData.ProductId},
"tids": []string{*element.Title},
"data": result,
"is_thousand_face": 0,
"folder": "", //todo 千人千面需要使用
}
2023-08-16 06:09:03 +00:00
//请求unity接口
2023-08-17 08:07:27 +00:00
url := w.logic.svcCtx.Config.Unity.Host + "/api/render/queue/push"
2023-08-16 04:33:01 +00:00
header := make(map[string]string)
header["content-type"] = "application/json"
postData := map[string]interface{}{
"group": "unity3d",
"source": "home page",
"priority": 1,
2023-08-25 03:06:32 +00:00
"create_at": time.Now().UTC(),
2023-08-16 04:33:01 +00:00
"render_data": sendData,
}
2023-08-21 06:46:39 +00:00
postDataBytes, _ := json.Marshal(postData)
2023-08-25 03:06:32 +00:00
unityRenderBeginTime := time.Now().UTC().UnixMilli()
2023-08-21 06:46:39 +00:00
_, err = curl.ApiCall(url, "POST", header, bytes.NewReader(postDataBytes), time.Second*10)
2023-08-16 04:33:01 +00:00
if err != nil {
2023-08-31 03:50:55 +00:00
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "请求unity接口失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
2023-08-16 04:33:01 +00:00
logx.Error("failed to send data to unity")
2023-08-16 08:20:16 +00:00
return err
2023-08-16 04:33:01 +00:00
}
2023-08-21 08:19:30 +00:00
//记录发送到unity时间
2023-09-05 02:20:39 +00:00
w.modifyRenderTaskProperty(taskId, renderTask{unityRenderBeginTime: unityRenderBeginTime})
2023-08-31 02:04:03 +00:00
//发送运行阶段消息
2023-08-31 02:35:13 +00:00
w.sendRenderDataToUnityStepResponseMessage(info.RenderId)
2023-09-01 07:05:22 +00:00
logx.Info("发送到unity成功,刀版图:", combineImage, " 请求unity的数据:", string(postDataBytes))
2023-08-16 08:20:16 +00:00
return nil
2023-08-07 02:48:14 +00:00
}
2023-08-31 02:35:13 +00:00
// 发送合图完毕阶段通知消息
2023-09-06 02:14:45 +00:00
func (w *wsConnectItem) sendCombineImageStepResponseMessage(renderId, combineImage string, sizeId, combineTime, uploadTime int64) {
2023-09-05 06:58:37 +00:00
combineTakesTime := "cache"
uploadCombineImageTakesTime := "cache"
if combineTime > 0 {
combineTakesTime = fmt.Sprintf("%dms", combineTime)
}
if uploadTime > 0 {
uploadCombineImageTakesTime = fmt.Sprintf("%dms", uploadTime)
}
2023-08-31 02:35:13 +00:00
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_COMBINE_IMAGE, websocket_data.CombineImageRspMsg{
RenderId: renderId,
CombineImage: combineImage,
2023-09-06 02:14:45 +00:00
SizeId: sizeId,
2023-08-31 02:35:13 +00:00
CombineProcessTime: websocket_data.CombineProcessTime{
2023-09-05 06:58:37 +00:00
CombineTakesTime: combineTakesTime,
UploadCombineImageTakesTime: uploadCombineImageTakesTime,
2023-08-31 02:35:13 +00:00
},
}))
}
// 发送组装unity渲染数据完毕阶段通知消息
func (w *wsConnectItem) sendAssembleRenderDataStepResponseMessage(renderId string) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ASSEMBLE_RENDER_DATA, websocket_data.ToUnityRspMsg{RenderId: renderId}))
}
// 发送组装unity渲染数据完毕阶段通知消息
func (w *wsConnectItem) sendRenderDataToUnityStepResponseMessage(renderId string) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_SEND_DATA_TO_UNITY, websocket_data.AssembleRenderDataRspMsg{RenderId: renderId}))
}
2023-08-31 06:38:34 +00:00
// 发送渲染最终结果数据到前端
func (w *wsConnectItem) sendRenderResultData(data websocket_data.RenderImageRspMsg) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, data))
}
2023-08-21 05:19:12 +00:00
// 增加渲染任务
2023-09-05 02:20:39 +00:00
func (w *wsConnectItem) createRenderTask(taskId, renderId string) {
if taskId == "" {
2023-08-30 06:24:54 +00:00
logx.Error("task_id不能为空")
return
}
2023-09-05 02:20:39 +00:00
if renderId == "" {
2023-08-30 06:24:54 +00:00
logx.Error("render_id不能为空")
return
}
2023-09-05 02:20:39 +00:00
data := renderImageControlChanItem{
option: 1,
taskId: taskId,
renderId: renderId,
}
2023-08-21 05:19:12 +00:00
select {
case <-w.closeChan: //关闭
return
2023-08-24 04:04:58 +00:00
case w.extendRenderProperty.renderImageTaskCtlChan <- data:
2023-08-21 05:19:12 +00:00
return
case <-time.After(time.Second * 3):
return
}
}
// 渲染回调处理并删除渲染任务
2023-09-05 02:20:39 +00:00
func (w *wsConnectItem) deleteRenderTask(taskId, renderNotifyImageUrl string) {
if taskId == "" {
2023-08-30 06:24:54 +00:00
logx.Error("task_id不能为空")
return
}
2023-09-05 02:20:39 +00:00
data := renderImageControlChanItem{
option: 0,
taskId: taskId,
renderNotifyImageUrl: renderNotifyImageUrl,
}
2023-08-21 05:19:12 +00:00
select {
case <-w.closeChan: //关闭
return
2023-08-24 04:04:58 +00:00
case w.extendRenderProperty.renderImageTaskCtlChan <- data:
2023-08-21 05:19:12 +00:00
return
case <-time.After(time.Second * 3):
return
}
}
2023-08-29 02:56:06 +00:00
// 修改任务属性(只有耗时属性可以更新)
2023-09-05 02:20:39 +00:00
func (w *wsConnectItem) modifyRenderTaskProperty(taskId string, property renderTask) {
if taskId == "" {
2023-08-30 06:24:54 +00:00
logx.Error("task_id不能为空")
2023-08-22 10:22:27 +00:00
return
}
2023-08-30 06:24:54 +00:00
//强制设为修改任务属性
2023-09-05 02:20:39 +00:00
data := renderImageControlChanItem{
option: 2,
taskId: taskId,
taskProperty: property,
}
2023-08-21 08:19:30 +00:00
select {
case <-w.closeChan: //关闭
return
2023-08-24 04:04:58 +00:00
case w.extendRenderProperty.renderImageTaskCtlChan <- data:
2023-08-21 08:19:30 +00:00
return
case <-time.After(time.Second * 3):
return
}
}
2023-08-29 10:22:15 +00:00
// 组装渲染任务id
2023-09-05 07:08:03 +00:00
func (w *wsConnectItem) genRenderTaskId(combineImage string, renderImageData websocket_data.RenderImageReqMsg, model3dInfo *gmodel.FsProductModel3d, productTemplate *gmodel.FsProductTemplateV2, element *gmodel.FsProductTemplateElement) string {
2023-08-29 10:22:15 +00:00
//生成任务id(需要把user_id,guest_id设为0)
incomeHashParam := renderImageData.RenderData
incomeHashParam.UserId = 0 //设为0(渲染跟用户id无关)
incomeHashParam.GuestId = 0 //设为0(渲染跟用户id无关)
incomeHashBytes, _ := json.Marshal(incomeHashParam)
modelHashStr := ""
templateHashStr := ""
if model3dInfo.ModelInfo != nil {
modelHashStr = *model3dInfo.ModelInfo
}
if productTemplate.TemplateInfo != nil {
templateHashStr = *productTemplate.TemplateInfo
}
elementHashBytes, _ := json.Marshal(element)
hashMap := map[string]interface{}{
"income_param": incomeHashBytes,
"model_info": modelHashStr,
"template_info": templateHashStr,
"material_image": *productTemplate.MaterialImg,
"render_element": elementHashBytes,
2023-09-05 07:08:03 +00:00
"combine_image": combineImage,
2023-08-29 10:22:15 +00:00
}
return hash.JsonHashKey(hashMap)
}
2023-08-22 10:24:24 +00:00
// 处理渲染任务的增加/删除/修改耗时属性任务map不能读写并发所以放在chan里面串行执行
2023-08-07 02:48:14 +00:00
func (w *wsConnectItem) operationRenderTask() {
2023-08-22 10:37:49 +00:00
defer func() {
if err := recover(); err != nil {
logx.Error("operation render task panic:", err)
}
}()
2023-08-07 02:48:14 +00:00
for {
2023-07-27 07:46:07 +00:00
select {
2023-08-07 02:48:14 +00:00
case <-w.closeChan:
2023-07-27 07:46:07 +00:00
return
2023-08-24 04:04:58 +00:00
case data := <-w.extendRenderProperty.renderImageTaskCtlChan:
2023-08-28 06:44:46 +00:00
switch data.option {
2023-08-21 05:19:12 +00:00
case 0: //渲染结果回调,删除任务
2023-08-31 04:19:02 +00:00
taskData, ok := w.extendRenderProperty.renderImageTask[data.taskId]
if !ok {
continue
}
2023-08-21 05:19:12 +00:00
//存在任务,则发送渲染结果给前端
2023-09-04 03:43:21 +00:00
UnityRenderTakesTime := "cache"
uploadUnityRenderImageTakesTime := "cache"
2023-08-31 04:19:02 +00:00
//unity渲染时间
if taskData.unityRenderBeginTime > 0 && taskData.unityRenderEndTime > 0 {
UnityRenderTakesTime = fmt.Sprintf("%dms", taskData.unityRenderEndTime-taskData.unityRenderBeginTime)
2023-08-21 05:19:12 +00:00
}
2023-08-31 04:19:02 +00:00
//上传unity渲染图耗时
if taskData.uploadUnityRenderImageTakesTime > 0 {
uploadUnityRenderImageTakesTime = fmt.Sprintf("%dms", taskData.uploadUnityRenderImageTakesTime)
}
//发送到出口
2023-08-31 06:38:34 +00:00
w.sendRenderResultData(websocket_data.RenderImageRspMsg{
2023-08-31 04:19:02 +00:00
RenderId: taskData.renderId,
Image: data.renderNotifyImageUrl,
RenderProcessTime: websocket_data.RenderProcessTime{
UnityRenderTakesTime: UnityRenderTakesTime,
UploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime,
},
2023-08-31 06:38:34 +00:00
})
2023-08-22 08:07:05 +00:00
//删除任务
2023-08-28 06:44:46 +00:00
delete(w.extendRenderProperty.renderImageTask, data.taskId)
2023-08-07 02:48:14 +00:00
case 1: //新增任务
2023-08-28 06:44:46 +00:00
w.extendRenderProperty.renderImageTask[data.taskId] = &renderTask{
2023-08-29 02:56:06 +00:00
renderId: data.renderId,
2023-08-21 08:19:30 +00:00
}
2023-08-29 03:34:02 +00:00
case 2: //修改任务属性
2023-08-31 04:19:02 +00:00
taskData, ok := w.extendRenderProperty.renderImageTask[data.taskId]
if !ok {
continue
}
//上传渲染结果图耗时
if data.taskProperty.uploadUnityRenderImageTakesTime != 0 {
taskData.uploadUnityRenderImageTakesTime = data.taskProperty.uploadUnityRenderImageTakesTime
}
//发送unity时间
if data.taskProperty.unityRenderBeginTime != 0 {
taskData.unityRenderBeginTime = data.taskProperty.unityRenderBeginTime
}
//收到unity返回的时间
if data.taskProperty.unityRenderEndTime != 0 {
taskData.unityRenderEndTime = data.taskProperty.unityRenderEndTime
2023-08-21 08:19:30 +00:00
}
2023-07-28 09:15:37 +00:00
}
2023-07-26 07:01:59 +00:00
}
2023-07-26 06:35:29 +00:00
}
}